scripts/mycroft/filesystem/__init__.py
Go to the documentation of this file.
1 # Copyright 2017 Mycroft AI Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 import os
16 from os.path import join, expanduser, isdir
17 
18 
20  """
21  A class for providing access to the mycroft FS sandbox. Intended to be
22  attached to skills at initialization time to provide a skill-specific
23  namespace.
24  """
25 
26  def __init__(self, path):
27  self.path = self.__init_path(path)
28 
29  @staticmethod
30  def __init_path(path):
31  if not isinstance(path, str) or len(path) == 0:
32  raise ValueError("path must be initialized as a non empty string")
33  path = join(expanduser('~'), '.mycroft', path)
34 
35  if not isdir(path):
36  os.makedirs(path)
37  return path
38 
39  def open(self, filename, mode):
40  """
41  Get a handle to a file (with the provided mode) within the
42  skill-specific namespace.
43 
44  :param filename: a str representing a path relative to the namespace.
45  subdirs not currently supported.
46 
47  :param mode: a file handle mode
48 
49  :return: an open file handle.
50  """
51  file_path = join(self.path, filename)
52  return open(file_path, mode)
53 
54  def exists(self, filename):
55  return os.path.exists(join(self.path, filename))


mycroft_ros
Author(s):
autogenerated on Mon Apr 26 2021 02:35:40