Filesystem access
Skills have access to both persistent and temporary namespaced filesystems independent of the Skill's root directory.
Many Skills may want access to parts of the filesystem. To account for the many different platforms that can run Mycroft there are three locations that a Skill can utilize.
    Persistent filesystem
    Temporary cache
    Skill's own root directory

Persistent Files

When your Skill needs to store some data that will persist over time and cannot easily be rebuilt, there is a persistent filesystem namespaced to your Skill.

Reading and writing to files

This uses the standard Python open() method to read and write files. It takes two parameters:
    file_name (str) - a path relative to the namespace. subdirs not currently supported.
    mode (str) – a file handle mode [r, r+, w, w+, rb, rb+, wb+, a, ab, a+, ab+, x]
Example:
1
def write_line_to_file(self, file_name, line):
2
"""Write a single line to a file in the Skills persistent filesystem."""
3
with self.file_system.open(file_name, "w") as my_file:
4
my_file.write(line)
5
6
def read_file(self, file_name):
7
"""Read the contents of a file in the Skills persistent filesystem."""
8
with self.file_system.open(file_name, "r") as my_file:
9
return my_file.read()
Copied!

Check if a file exists

Quick method to see if some file exists in the namespaced directory.
Example:
1
file_name = "example.txt"
2
with self.file_system.open(file_name, "w") as my_file:
3
my_file.write("Hello world")
4
self.log.info(self.file_system.exists(file_name))
5
# True
6
self.log.info(self.file_system.exists("new.txt"))
7
# False
Copied!

Get the path of the namespaced directory.

self.file_system.path is a member value containing the root path of the namespace. However it is recommended that you use the self.file_system.open() method to read and write files.
Example:
1
from mycroft import MycroftSkill
2
3
class FileSystemSkill(MycroftSkill):
4
5
def initialize(self):
6
"""Log the path of this Skills persistent namespace."""
7
self.log.info(self.file_system.path)
8
9
def create_skill():
10
return FileSystemSkill()
Copied!

Create subdirectories

Now that we have the path of our namespaced filesystem, we can organize our files however we like within that directory.
In this example, we create a subdirectory called "cache", then write to a text file inside of it.
1
from os import mkdir
2
from os.path import join
3
4
from mycroft import MycroftSkill
5
6
class FileSystemSkill(MycroftSkill):
7
8
def initialize(self):
9
"""Create a cache subdirectory and write to a file inside it"""
10
cache_dir = "cache"
11
file_name = "example.txt"
12
if not self.file_system.exists(cache_dir):
13
mkdir(join(self.file_system.path, cache_dir))
14
with self.file_system.open(join(cache_dir, file_name), "w") as my_file:
15
my_file.write('hello')
16
17
18
def create_skill():
19
return FileSystemSkill()
Copied!

Example Skill

1
from mycroft import MycroftSkill, intent_handler
2
3
class FileSystemSkill(MycroftSkill):
4
5
def initialize(self):
6
"""Perform initial setup for the Skill.
7
8
For this example we do four things:
9
1. Log the path of this directory.
10
2. Write to a file in the directory.
11
3. Check that our file exists.
12
4. Read the contents of our file from disk.
13
"""
14
file_name = "example.txt"
15
self.log.info(self.file_system.path)
16
self.write_line_to_file(file_name, "hello world")
17
self.log.info(self.file_system.exists(file_name))
18
self.log.info(self.read_file(file_name))
19
20
def write_line_to_file(self, file_name, line):
21
"""Write a single line to a file in the Skills persistent filesystem."""
22
with self.file_system.open(file_name, "w") as my_file:
23
my_file.write(line)
24
25
def read_file(self, file_name):
26
"""Read the contents of a file in the Skills persistent filesystem."""
27
with self.file_system.open(file_name, "r") as my_file:
28
return my_file.read()
29
30
def create_skill():
31
return FileSystemSkill()
Copied!

Temporary Cache

Skills can create a directory for caching temporary data to speed up performance.
This directory will likely be part of a small RAM disk and may be cleared at any time. So code that uses these cached files must be able to fallback and regenerate the file.

Example Skill

1
from os.path import join
2
from mycroft import MycroftSkill, intent_handler
3
from mycroft.util import get_cache_directory
4
5
class CachingSkill(MycroftSkill):
6
7
def initialize(self):
8
"""Perform initial setup for the Skill.
9
10
For this example we do four things:
11
1. Get a cache directory namespaced for our Skill.
12
2. Define a file path for the cache_file.
13
3. Write some data to the cache_file
14
4. Log the path of the cache_file
15
4. Log the contents of the cache_file.
16
"""
17
cache_dir = get_cache_directory('CachingSkill')
18
self.cache_file = join(cache_dir, "myfile.txt")
19
self.cache_data()
20
self.log.info(self.cache_file)
21
self.log.info(self.read_cached_data())
22
23
def cache_data(self):
24
with open(self.cache_file, "w") as cache_file:
25
cache_file.write("Some cached data")
26
27
def read_cached_data(self):
28
with open(self.cache_file, "r") as cache_file:
29
return cache_file.read()
30
31
def create_skill():
32
return CachingSkill()
Copied!

Skill Root Directory

1
self.root_dir
Copied!
This member variable contains the absolute path of a Skill’s root directory e.g. ~.local/share/mycroft/skills/my-skill.me/.
Generally Skills should not modify anything within this directory. Modifying anything in the Skill directory will reload the Skill. This will also prevent the Skill from updating as we do not want to overwrite changes made during development. It is also not guaranteed that the Skill will have permission to write to this directory.
Last modified 7mo ago