@@ -124,14 +124,11 @@ def init_signal_handlers():
124124 log_deprecation ("Import patching will be deprecated. Disable in "
125125 "configuration by setting `signal`.`patch_imports` "
126126 "to `False`" , "2.0.0" )
127- import ovos_utils .signal
128- ovos_utils .signal .check_for_signal = _check_for_signal
129- ovos_utils .signal .create_signal = _create_signal
130127 try :
131128 import mycroft .util .signal
132129 mycroft .util .signal .create_signal = _create_signal
133130 mycroft .util .signal .check_for_signal = _check_for_signal
134- LOG .info (f "Overrode mycroft.util.signal methods" )
131+ LOG .info ("Overrode mycroft.util.signal methods" )
135132 except (ImportError , AttributeError ) as e :
136133 LOG .debug (e )
137134 except TypeError as e :
@@ -141,12 +138,100 @@ def init_signal_handlers():
141138 else :
142139 LOG .warning ("FS signals are deprecated. Signal methods will have no effect." )
143140 if patch_imports :
144- log_deprecation ("Import patching will be deprecated. Disable in "
145- "configuration by setting `signal`.`patch_imports` "
146- "to `False`" , "2.0.0" )
147- import ovos_utils .signal
148- _create_signal = ovos_utils .signal .create_signal
149- _check_for_signal = ovos_utils .signal .check_for_signal
141+ import os
142+ import tempfile
143+ from ovos_utils .file_utils import ensure_directory_exists
144+
145+ def get_ipc_directory (domain = None , config = None ):
146+ """Get the directory used for Inter Process Communication
147+
148+ Files in this folder can be accessed by different processes on the
149+ machine. Useful for communication. This is often a small RAM disk.
150+
151+ Args:
152+ domain (str): The IPC domain. Basically a subdirectory to prevent
153+ overlapping signal filenames.
154+ config (dict): mycroft.conf, to read ipc directory from
155+
156+ Returns:
157+ str: a path to the IPC directory
158+ """
159+ if config is None :
160+ try :
161+ from ovos_config .config import Configuration
162+ config = Configuration ()
163+ except ImportError :
164+ LOG .warning ("Config not provided and ovos_config not available" )
165+ config = dict ()
166+ path = config .get ("ipc_path" )
167+ if not path :
168+ # If not defined, use /tmp/mycroft/ipc
169+ path = os .path .join (tempfile .gettempdir (), "mycroft" , "ipc" )
170+ return ensure_directory_exists (path , domain )
171+
172+ def create_file (filename ):
173+ """ Create the file filename and create any directories needed
174+
175+ Args:
176+ filename: Path to the file to be created
177+ """
178+ try :
179+ os .makedirs (os .path .dirname (filename ))
180+ except OSError :
181+ pass
182+ with open (filename , 'w' ) as f :
183+ f .write ('' )
184+
185+ def create_signal (signal_name , config = None ):
186+ """Create a named signal
187+
188+ Args:
189+ signal_name (str): The signal's name. Must only contain characters
190+ valid in filenames.
191+ config (dict): mycroft.conf, to read ipc directory from
192+ """
193+ try :
194+ path = os .path .join (get_ipc_directory (config = config ),
195+ "signal" , signal_name )
196+ create_file (path )
197+ return os .path .isfile (path )
198+ except IOError :
199+ return False
200+
201+
202+ def check_for_signal (signal_name , sec_lifetime = 0 , config = None ):
203+ """See if a named signal exists
204+
205+ Args:
206+ signal_name (str): The signal's name. Must only contain characters
207+ valid in filenames.
208+ sec_lifetime (int, optional): How many seconds the signal should
209+ remain valid. If 0 or not specified, it is a single-use signal.
210+ If -1, it never expires.
211+ config (dict): mycroft.conf, to read ipc directory from
212+
213+ Returns:
214+ bool: True if the signal is defined, False otherwise
215+ """
216+ path = os .path .join (get_ipc_directory (config = config ),
217+ "signal" , signal_name )
218+ if os .path .isfile (path ):
219+ if sec_lifetime == 0 :
220+ # consume this single-use signal
221+ os .remove (path )
222+ elif sec_lifetime == - 1 :
223+ return True
224+ elif int (os .path .getctime (path ) + sec_lifetime ) < int (time .time ()):
225+ # remove once expired
226+ os .remove (path )
227+ return False
228+ return True
229+
230+ # No such signal exists
231+ return False
232+
233+ _create_signal = create_signal
234+ _check_for_signal = check_for_signal
150235 _wait_for_signal_clear = _fs_wait_for_signal_clear
151236 _wait_for_signal_create = _fs_wait_for_signal_create
152237 else :
0 commit comments