signal.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 tempfile
16 import time
17 
18 import os
19 import os.path
20 
21 import mycroft
22 from mycroft.util.log import LOG
23 
24 
25 def get_ipc_directory(domain=None):
26  """Get the directory used for Inter Process Communication
27 
28  Files in this folder can be accessed by different processes on the
29  machine. Useful for communication. This is often a small RAM disk.
30 
31  Args:
32  domain (str): The IPC domain. Basically a subdirectory to prevent
33  overlapping signal filenames.
34 
35  Returns:
36  str: a path to the IPC directory
37  """
38  config = mycroft.configuration.Configuration.get()
39  dir = config.get("ipc_path")
40  if not dir:
41  # If not defined, use /tmp/mycroft/ipc
42  dir = os.path.join(tempfile.gettempdir(), "mycroft", "ipc")
43  return ensure_directory_exists(dir, domain)
44 
45 
46 def ensure_directory_exists(directory, domain=None):
47  """ Create a directory and give access rights to all
48 
49  Args:
50  domain (str): The IPC domain. Basically a subdirectory to prevent
51  overlapping signal filenames.
52 
53  Returns:
54  str: a path to the directory
55  """
56  if domain:
57  directory = os.path.join(directory, domain)
58 
59  # Expand and normalize the path
60  directory = os.path.normpath(directory)
61  directory = os.path.expanduser(directory)
62 
63  if not os.path.isdir(directory):
64  try:
65  save = os.umask(0)
66  os.makedirs(directory, 0o777) # give everyone rights to r/w here
67  except OSError:
68  LOG.warning("Failed to create: " + directory)
69  pass
70  finally:
71  os.umask(save)
72 
73  return directory
74 
75 
76 def create_file(filename):
77  """ Create the file filename and create any directories needed
78 
79  Args:
80  filename: Path to the file to be created
81  """
82  try:
83  os.makedirs(os.path.dirname(filename))
84  except OSError:
85  pass
86  with open(filename, 'w') as f:
87  f.write('')
88 
89 
90 def create_signal(signal_name):
91  """Create a named signal
92 
93  Args:
94  signal_name (str): The signal's name. Must only contain characters
95  valid in filenames.
96  """
97  try:
98  path = os.path.join(get_ipc_directory(), "signal", signal_name)
99  create_file(path)
100  return os.path.isfile(path)
101  except IOError:
102  return False
103 
104 
105 def check_for_signal(signal_name, sec_lifetime=0):
106  """See if a named signal exists
107 
108  Args:
109  signal_name (str): The signal's name. Must only contain characters
110  valid in filenames.
111  sec_lifetime (int, optional): How many seconds the signal should
112  remain valid. If 0 or not specified, it is a single-use signal.
113  If -1, it never expires.
114 
115  Returns:
116  bool: True if the signal is defined, False otherwise
117  """
118  path = os.path.join(get_ipc_directory(), "signal", signal_name)
119  if os.path.isfile(path):
120  if sec_lifetime == 0:
121  # consume this single-use signal
122  os.remove(path)
123  elif sec_lifetime == -1:
124  return True
125  elif int(os.path.getctime(path) + sec_lifetime) < int(time.time()):
126  # remove once expired
127  os.remove(path)
128  return False
129  return True
130 
131  # No such signal exists
132  return False
def check_for_signal(signal_name, sec_lifetime=0)
Definition: signal.py:105
def create_file(filename)
Definition: signal.py:76
def create_signal(signal_name)
Definition: signal.py:90
def ensure_directory_exists(directory, domain=None)
Definition: signal.py:46
def get_ipc_directory(domain=None)
Definition: signal.py:25


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