def __init__(self, emitter): FallbackSkill.__init__(self) self.config = ConfigurationManager.get()['padatious'] intent_cache = expanduser(self.config['intent_cache']) try: from padatious import IntentContainer except ImportError: LOG.error('Padatious not installed. Please re-run dev_setup.sh') try: call(['notify-send', 'Padatious not installed', 'Please run build_host_setup and dev_setup again']) except OSError: pass return ver = get_distribution('padatious').version if ver != PADATIOUS_VERSION: LOG.warning('Using Padatious v' + ver + '. Please re-run ' + 'dev_setup.sh to install ' + PADATIOUS_VERSION) self.container = IntentContainer(intent_cache) self.emitter = emitter self.emitter.on('padatious:register_intent', self.register_intent) self.emitter.on('padatious:register_entity', self.register_entity) self.register_fallback(self.handle_fallback, 5) self.finished_training_event = Event() self.train_delay = self.config['train_delay'] self.train_time = get_time() + self.train_delay self.wait_and_train()
def handler(message): # indicate fallback handling start ws.emit(Message("mycroft.skill.handler.start", data={'handler': "fallback"})) stopwatch = Stopwatch() handler_name = None with stopwatch: for _, handler in sorted(cls.fallback_handlers.items(), key=operator.itemgetter(0)): try: if handler(message): # indicate completion handler_name = get_handler_name(handler) ws.emit(Message( 'mycroft.skill.handler.complete', data={'handler': "fallback", "fallback_handler": handler_name})) break except Exception: LOG.exception('Exception in fallback.') else: # No fallback could handle the utterance ws.emit(Message('complete_intent_failure')) warning = "No fallback could handle intent." LOG.warning(warning) # indicate completion with exception ws.emit(Message('mycroft.skill.handler.complete', data={'handler': "fallback", 'exception': warning})) # Send timing metric if message.context and message.context['ident']: ident = message.context['ident'] report_timing(ident, 'fallback_handler', stopwatch, {'handler': handler_name})
def transcribe(self, audio): try: # Invoke the STT engine on the audio clip text = self.stt.execute(audio).lower().strip() LOG.debug("STT: " + text) return text except sr.RequestError as e: LOG.error("Could not request Speech Recognition {0}".format(e)) except ConnectionError as e: LOG.error("Connection Error: {0}".format(e)) self.emitter.emit("recognizer_loop:no_internet") except HTTPError as e: if e.response.status_code == 401: LOG.warning("Access Denied at mycroft.ai") return "pair my device" # phrase to start the pairing process else: LOG.error(e.__class__.__name__ + ': ' + str(e)) except RequestException as e: LOG.error(e.__class__.__name__ + ': ' + str(e)) except Exception as e: self.emitter.emit('recognizer_loop:speech.recognition.unknown') if isinstance(e, IndexError): LOG.info('no words were transcribed') else: LOG.error(e) LOG.error("Speech Recognition could not understand audio") return None if connected(): dialog_name = 'backend.down' else: dialog_name = 'not connected to the internet' self.emitter.emit('speak', {'utterance': dialog.get(dialog_name)})
def process(self, audio): SessionManager.touch() payload = { 'utterance': self.wakeword_recognizer.key_phrase, 'session': SessionManager.get().session_id, } self.emitter.emit("recognizer_loop:wakeword", payload) if self._audio_length(audio) < self.MIN_AUDIO_SIZE: LOG.warning("Audio too short to be processed") else: stopwatch = Stopwatch() with stopwatch: transcription = self.transcribe(audio) if transcription: ident = str(stopwatch.timestamp) + str(hash(transcription)) # STT succeeded, send the transcribed speech on for processing payload = { 'utterances': [transcription], 'lang': self.stt.lang, 'session': SessionManager.get().session_id, 'ident': ident } self.emitter.emit("recognizer_loop:utterance", payload) self.metrics.attr('utterances', [transcription]) else: ident = str(stopwatch.timestamp) # Report timing metrics report_timing(ident, 'stt', stopwatch, {'transcription': transcription, 'stt': self.stt.__class__.__name__})
def ensure_directory_exists(directory, domain=None): """ Create a directory and give access rights to all Args: domain (str): The IPC domain. Basically a subdirectory to prevent overlapping signal filenames. Returns: str: a path to the directory """ if domain: directory = os.path.join(directory, domain) # Expand and normalize the path directory = os.path.normpath(directory) directory = os.path.expanduser(directory) if not os.path.isdir(directory): try: save = os.umask(0) os.makedirs(directory, 0o777) # give everyone rights to r/w here except OSError: LOG.warning("Failed to create: " + directory) pass finally: os.umask(save) return directory
def read(self, size, of_exc=False): """ Read data from stream. Arguments: size (int): Number of bytes to read of_exc (bool): flag determining if the audio producer thread should throw IOError at overflows. Returns: Data read from device """ frames = collections.deque() remaining = size while remaining > 0: to_read = min(self.wrapped_stream.get_read_available(), remaining) if to_read == 0: sleep(.01) continue result = self.wrapped_stream.read(to_read, exception_on_overflow=of_exc) frames.append(result) remaining -= to_read if self.muted: return self.muted_buffer input_latency = self.wrapped_stream.get_input_latency() if input_latency > 0.2: LOG.warning("High input latency: %f" % input_latency) audio = b"".join(list(frames)) return audio
def transcribe(self, audio): text = None try: # Invoke the STT engine on the audio clip text = self.stt.execute(audio).lower().strip() LOG.debug("STT: " + text) except sr.RequestError as e: LOG.error("Could not request Speech Recognition {0}".format(e)) except ConnectionError as e: LOG.error("Connection Error: {0}".format(e)) self.emitter.emit("recognizer_loop:no_internet") except HTTPError as e: if e.response.status_code == 401: text = "pair my device" # phrase to start the pairing process LOG.warning("Access Denied at mycroft.ai") except Exception as e: LOG.error(e) LOG.error("Speech Recognition could not understand audio") if text: # STT succeeded, send the transcribed speech on for processing payload = { 'utterances': [text], 'lang': self.stt.lang, 'session': SessionManager.get().session_id } self.emitter.emit("recognizer_loop:utterance", payload) self.metrics.attr('utterances', [text])
def callback_disconnect(self, gui_id): LOG.info("Disconnecting!") # TODO: Whatever is needed to kill the websocket instance LOG.info(self.GUIs.keys()) LOG.info('deleting: {}'.format(gui_id)) if gui_id in self.GUIs: del self.GUIs[gui_id] else: LOG.warning('ID doesn\'t exist')
def on_error(self, ws, error): try: self.emitter.emit('error', error) self.client.close() except Exception as e: LOG.error(repr(e)) LOG.warning("WS Client will reconnect in %d seconds." % self.retry) time.sleep(self.retry) self.retry = min(self.retry * 2, 60) self.client = self.create_client() self.run_forever()
def remove_fallback(cls, handler_to_del): """ Remove a fallback handler Args: handler_to_del: reference to handler """ for priority, handler in cls.fallback_handlers.items(): if handler == handler_to_del: del cls.fallback_handlers[priority] return LOG.warning('Could not remove fallback!')
def load_skill(skill_descriptor, emitter, skill_id, BLACKLISTED_SKILLS=None): """ load skill from skill descriptor. Args: skill_descriptor: descriptor of skill to load emitter: messagebus emitter skill_id: id number for skill Returns: MycroftSkill: the loaded skill or None on failure """ BLACKLISTED_SKILLS = BLACKLISTED_SKILLS or [] try: LOG.info("ATTEMPTING TO LOAD SKILL: " + skill_descriptor["name"] + " with ID " + str(skill_id)) if skill_descriptor['name'] in BLACKLISTED_SKILLS: LOG.info("SKILL IS BLACKLISTED " + skill_descriptor["name"]) return None skill_module = imp.load_module( skill_descriptor["name"] + MainModule, *skill_descriptor["info"]) if (hasattr(skill_module, 'create_skill') and callable(skill_module.create_skill)): # v2 skills framework skill = skill_module.create_skill() skill.settings.allow_overwrite = True skill.settings.load_skill_settings_from_file() skill.bind(emitter) skill.skill_id = skill_id skill.load_data_files(dirname(skill_descriptor['info'][1])) # Set up intent handlers skill.initialize() skill._register_decorated() LOG.info("Loaded " + skill_descriptor["name"]) # The very first time a skill is run, speak the intro first_run = skill.settings.get("__mycroft_skill_firstrun", True) if first_run: LOG.info("First run of "+skill_descriptor["name"]) skill.settings["__mycroft_skill_firstrun"] = False skill.settings.store() intro = skill.get_intro_message() if intro: skill.speak(intro) return skill else: LOG.warning( "Module %s does not appear to be skill" % ( skill_descriptor["name"])) except: LOG.error( "Failed to load skill: " + skill_descriptor["name"], exc_info=True) return None
def process(self, audio): SessionManager.touch() payload = { 'utterance': self.wakeword_recognizer.key_phrase, 'session': SessionManager.get().session_id, } self.emitter.emit("recognizer_loop:wakeword", payload) if self._audio_length(audio) < self.MIN_AUDIO_SIZE: LOG.warning("Audio too short to be processed") else: self.transcribe(audio)
def _register_object(self, message, object_name, register_func): file_name = message.data['file_name'] name = message.data['name'] LOG.debug('Registering Padatious ' + object_name + ': ' + name) if not isfile(file_name): LOG.warning('Could not find file ' + file_name) return register_func(name, file_name) self.train_time = get_time() + self.train_delay self.wait_and_train()
def _load_or_reload_skill(self, skill_folder): """ Check if unloaded skill or changed skill needs reloading and perform loading if necessary. """ if skill_folder not in self.loaded_skills: self.loaded_skills[skill_folder] = { "id": hash(os.path.join(SKILLS_DIR, skill_folder)) } skill = self.loaded_skills.get(skill_folder) skill["path"] = os.path.join(SKILLS_DIR, skill_folder) # check if folder is a skill (must have __init__.py) if not MainModule + ".py" in os.listdir(skill["path"]): return # getting the newest modified date of skill modified = _get_last_modified_date(skill["path"]) last_mod = skill.get("last_modified", 0) # checking if skill is loaded and wasn't modified if skill.get("loaded") and modified <= last_mod: return # check if skill was modified elif skill.get("instance") and modified > last_mod: # check if skill is allowed to reloaded if not skill["instance"].reload_skill: return LOG.debug("Reloading Skill: " + skill_folder) # removing listeners and stopping threads skill["instance"].shutdown() # Remove two local references that are known refs = sys.getrefcount(skill["instance"]) - 2 if refs > 0: LOG.warning( "After shutdown of {} there are still " "{} references remaining. The skill " "won't be cleaned from memory." .format(skill['instance'].name, refs)) del skill["instance"] # (Re)load the skill from disk with self.__msm_lock: # Make sure msm isn't running skill["loaded"] = True desc = create_skill_descriptor(skill["path"]) skill["instance"] = load_skill(desc, self.ws, skill["id"], BLACKLISTED_SKILLS) skill["last_modified"] = modified
def read(self): while self.alive: try: data = self.serial.readline()[:-2] if data: try: data_str = data.decode() except UnicodeError as e: data_str = data.decode('utf-8', errors='replace') LOG.warning('Invalid characters in response from ' ' enclosure: {}'.format(repr(e))) self.process(data_str) except Exception as e: LOG.error("Reading error: {0}".format(e))
def emit(self, message): if not self.connected_event.wait(10): if not self.started_running: raise ValueError('You must execute run_forever() ' 'before emitting messages') self.connected_event.wait() try: if hasattr(message, 'serialize'): self.client.send(message.serialize()) else: self.client.send(json.dumps(message.__dict__)) except WebSocketConnectionClosedException: LOG.warning('Could not send {} message because connection ' 'has been closed'.format(message.type))
def __init__(self, key_phrase="hey mycroft", config=None, lang="en-us"): super(PocketsphinxHotWord, self).__init__(key_phrase, config, lang) # Hotword module imports from pocketsphinx import Decoder # Hotword module config module = self.config.get("module") if module != "pocketsphinx": LOG.warning( str(module) + " module does not match with " "Hotword class pocketsphinx") # Hotword module params self.phonemes = self.config.get("phonemes", "HH EY . M AY K R AO F T") self.num_phonemes = len(self.phonemes.split()) self.threshold = self.config.get("threshold", 1e-90) self.sample_rate = self.listener_config.get("sample_rate", 1600) dict_name = self.create_dict(key_phrase, self.phonemes) config = self.create_config(dict_name, Decoder.default_config()) self.decoder = Decoder(config)
def read(self, size): frames = collections.deque() remaining = size while remaining > 0: to_read = min(self.wrapped_stream.get_read_available(), remaining) if to_read == 0: sleep(.01) continue result = self.wrapped_stream.read(to_read) frames.append(result) remaining -= to_read if self.muted: return self.muted_buffer input_latency = self.wrapped_stream.get_input_latency() if input_latency > 0.2: LOG.warning("High input latency: %f" % input_latency) audio = b"".join(list(frames)) return audio
def install_or_update(skill): """Install missing defaults and update existing skills""" if get_skill_data(skill.name).get('beta'): skill.sha = None # Will update to latest head if skill.is_local: skill.update() if skill.name not in installed_skills: skill.update_deps() elif skill.name in default_names: try: msm.install(skill, origin='default') except Exception: if skill.name in default_names: LOG.warning('Failed to install default skill: ' + skill.name) nonlocal default_skill_errored default_skill_errored = True raise installed_skills.add(skill.name)
def initialize(): nonlocal instance, complete try: clazz = HotWordFactory.CLASSES[module] instance = clazz(hotword, config, lang=lang) except TriggerReload: complete.set() sleep(0.5) loop.reload() except NoModelAvailable: LOG.warning('Could not found find model for {} on {}.'.format( hotword, module )) instance = None except Exception: LOG.exception( 'Could not create hotword. Falling back to default.') instance = None complete.set()
def __init__(self, key_phrase="hey mycroft", config=None, lang="en-us"): super(SnowboyHotWord, self).__init__(key_phrase, config, lang) # Hotword module imports from snowboydecoder import HotwordDetector # Hotword module config module = self.config.get("module") if module != "snowboy": LOG.warning(module + " module does not match with Hotword class " "snowboy") # Hotword params models = self.config.get("models", {}) paths = [] for key in models: paths.append(models[key]) sensitivity = self.config.get("sensitivity", 0.5) self.snowboy = HotwordDetector(paths, sensitivity=[sensitivity] * len(paths)) self.lang = str(lang).lower() self.key_phrase = str(key_phrase).lower()
def transcribe(self, audio): text = None try: # Invoke the STT engine on the audio clip text = self.stt.execute(audio).lower().strip() LOG.debug("STT: " + text) except sr.RequestError as e: LOG.error("Could not request Speech Recognition {0}".format(e)) except ConnectionError as e: LOG.error("Connection Error: {0}".format(e)) self.emitter.emit("recognizer_loop:no_internet") except HTTPError as e: if e.response.status_code == 401: text = "pair my device" # phrase to start the pairing process LOG.warning("Access Denied at mycroft.ai") except Exception as e: self.emitter.emit('recognizer_loop:speech.recognition.unknown') LOG.error(e) LOG.error("Speech Recognition could not understand audio") return text
def load_skill(skill_descriptor, emitter, skill_id, BLACKLISTED_SKILLS=None): """ load skill from skill descriptor. Args: skill_descriptor: descriptor of skill to load emitter: messagebus emitter skill_id: id number for skill """ BLACKLISTED_SKILLS = BLACKLISTED_SKILLS or [] try: LOG.info("ATTEMPTING TO LOAD SKILL: " + skill_descriptor["name"] + " with ID " + str(skill_id)) if skill_descriptor['name'] in BLACKLISTED_SKILLS: LOG.info("SKILL IS BLACKLISTED " + skill_descriptor["name"]) return None skill_module = imp.load_module( skill_descriptor["name"] + MainModule, *skill_descriptor["info"]) if (hasattr(skill_module, 'create_skill') and callable(skill_module.create_skill)): # v2 skills framework skill = skill_module.create_skill() skill.bind(emitter) skill.skill_id = skill_id skill.load_data_files(dirname(skill_descriptor['info'][1])) # Set up intent handlers skill.initialize() skill._register_decorated() LOG.info("Loaded " + skill_descriptor["name"]) return skill else: LOG.warning( "Module %s does not appear to be skill" % ( skill_descriptor["name"])) except: LOG.error( "Failed to load skill: " + skill_descriptor["name"], exc_info=True) return None
def load(self, dialog_dir): """ Load all dialog files within the specified directory. Args: dialog_dir (str): directory that contains dialog files Returns: a loaded instance of a dialog renderer """ directory = Path(dialog_dir) if not directory.exists() or not directory.is_dir(): LOG.warning("No dialog files found: " + dialog_dir) return self.__renderer for path, _, files in os.walk(str(directory)): for f in files: if f.endswith(".dialog"): self.__renderer.load_template_file( f.replace('.dialog', ''), join(path, f)) return self.__renderer
def load(self, dialog_dir): """ Load all dialog files within the specified directory. Args: dialog_dir (str): directory that contains dialog files Returns: a loaded instance of a dialog renderer """ if not os.path.exists(dialog_dir) or not os.path.isdir(dialog_dir): LOG.warning("No dialog found: " + dialog_dir) return self.__renderer for f in sorted( filter(lambda x: os.path.isfile( os.path.join(dialog_dir, x)), os.listdir(dialog_dir))): dialog_entry_name = os.path.splitext(f)[0] self.__renderer.load_template_file( dialog_entry_name, os.path.join(dialog_dir, f)) return self.__renderer
def on_error(self, ws, error): """ On error start trying to reconnect to the websocket. """ if isinstance(error, WebSocketConnectionClosedException): LOG.warning('Could not send message because connection has closed') else: LOG.exception('=== ' + repr(error) + ' ===') try: self.emitter.emit('error', error) if self.client.keep_running: self.client.close() except Exception as e: LOG.error('Exception closing websocket: ' + repr(e)) LOG.warning("WS Client will reconnect in %d seconds." % self.retry) time.sleep(self.retry) self.retry = min(self.retry * 2, 60) try: self.emitter.emit('reconnecting') self.client = self.create_client() self.run_forever() except WebSocketException: pass
def install_or_update(skill): """Install missing defaults and update existing skills""" if skills_data.get(skill.name, {}).get('beta'): skill.sha = None # Will update to latest version if skill.is_local: skill.update() updated_skills.append(skill.name) if skill.name not in installed_skills: skill.update_deps() installed_skills.add(skill.name) elif skill.name in default_names: try: new_installs.append(skill.name) skill.install() except Exception: if skill.name in default_names: LOG.warning( 'Failed to install default skill: ' + skill.name ) nonlocal default_skill_errored default_skill_errored = True raise installed_skills.add(skill.name)
def handler(message): # indicate fallback handling start ws.emit(Message("mycroft.skill.handler.start", data={'handler': "fallback"})) for _, handler in sorted(cls.fallback_handlers.items(), key=operator.itemgetter(0)): try: if handler(message): # indicate completion ws.emit(Message( 'mycroft.skill.handler.complete', data={'handler': "fallback", "fallback_handler": get_handler_name( handler)})) return except Exception as e: LOG.info('Exception in fallback: ' + str(e)) ws.emit(Message('complete_intent_failure')) LOG.warning('No fallback could handle intent.') # indicate completion with exception ws.emit(Message('mycroft.skill.handler.complete', data={'handler': "fallback", 'exception': "No fallback could handle intent."}))
def stop(self): super(KeypadSkill, self).shutdown() LOG.warning("Keypad Skill CLOSED") self.keypad_client.cleanup()
def remove(self, event_name, func): try: if event_name in self.emitter._events: LOG.debug("Removing found '" + str(event_name) + "'") else: LOG.debug("Not able to find '" + str(event_name) + "'") self.emitter.remove_listener(event_name, func) except ValueError: LOG.warning('Failed to remove event {}: {}'.format( event_name, str(func))) for line in traceback.format_stack(): LOG.warning(line.strip()) if event_name in self.emitter._events: LOG.debug("Removing found '" + str(event_name) + "'") else: LOG.debug("Not able to find '" + str(event_name) + "'") LOG.warning("Existing events: " + str(self.emitter._events)) for evt in self.emitter._events: LOG.warning(" " + str(evt)) LOG.warning(" " + str(self.emitter._events[evt])) if event_name in self.emitter._events: LOG.debug("Removing found '" + str(event_name) + "'") else: LOG.debug("Not able to find '" + str(event_name) + "'") LOG.warning('----- End dump -----')
def remove(self, event_name, func): try: self.emitter.remove_listener(event_name, func) except ValueError as e: LOG.warning('Failed to remove event {}: {}'.format(event_name, e))
def __init__(self): super().__init__() LOG.warning( "WebsocketClient is deprecated, use MessageBusClient instead" )
import sys import traceback from adapt.intent import IntentBuilder from mycroft import MycroftSkill, intent_handler from mycroft.util.log import LOG from .code.message.object_recognition_message import ObjectRecognitionMessage from .code.misc.camera import Camera from .code.misc.receiver import Receiver from .code.misc.sender import Sender from .default_config import DefaultConfig # TODO: Make sure "." before module name is not missing LOG.warning('Running Skill Object Recognizer On Python ' + sys.version) try: import picamera import inflect except ImportError: # re-install yourself from msm import MycroftSkillsManager msm = MycroftSkillsManager() msm.install("https://github.com/ITE-5th/skill-object-recognizer") COUNT = 'count' OBJECT = 'object'
def __init__(self, renderer_factory=MustacheDialogRenderer): LOG.warning('Deprecated, use load_dialogs() instead.') self.__renderer = renderer_factory()
def remove(self, event_name, func): try: if event_name in self.emitter._events: LOG.debug("Removing found '"+str(event_name)+"'") else: LOG.debug("Not able to find '"+str(event_name)+"'") self.emitter.remove_listener(event_name, func) except ValueError as e: LOG.warning('Failed to remove event {}: {}'.format(event_name, str(func))) for line in traceback.format_stack(): LOG.warning(line.strip()) if event_name in self.emitter._events: LOG.debug("Removing found '"+str(event_name)+"'") else: LOG.debug("Not able to find '"+str(event_name)+"'") LOG.warning("Existing events: " + str(self.emitter._events)) for evt in self.emitter._events: LOG.warning(" "+str(evt)) LOG.warning(" "+str(self.emitter._events[evt])) if event_name in self.emitter._events: LOG.debug("Removing found '"+str(event_name)+"'") else: LOG.debug("Not able to find '"+str(event_name)+"'") LOG.warning('----- End dump -----')
def set_changed_callback(self, callback): LOG.warning('DEPRECATED - set the settings_change_callback attribute') self._skill.settings_change_callback = callback
def _load_or_reload_skill(self, skill_path): """ Check if unloaded skill or changed skill needs reloading and perform loading if necessary. Returns True if the skill was loaded/reloaded """ skill_path = skill_path.rstrip('/') skill = self.loaded_skills.setdefault(skill_path, {}) skill.update({"id": basename(skill_path), "path": skill_path}) # check if folder is a skill (must have __init__.py) if not MainModule + ".py" in os.listdir(skill_path): return False # getting the newest modified date of skill modified = _get_last_modified_date(skill_path) last_mod = skill.get("last_modified", 0) # checking if skill is loaded and hasn't been modified on disk if skill.get("loaded") and modified <= last_mod: return False # Nothing to do! # check if skill was modified elif skill.get("instance") and modified > last_mod: # check if skill has been blocked from reloading if (not skill["instance"].reload_skill or not skill.get('active', True)): return False LOG.debug("Reloading Skill: " + basename(skill_path)) # removing listeners and stopping threads try: skill["instance"].default_shutdown() except Exception: LOG.exception("An error occured while shutting down {}".format( skill["instance"].name)) if DEBUG: gc.collect() # Collect garbage to remove false references # Remove two local references that are known refs = sys.getrefcount(skill["instance"]) - 2 if refs > 0: msg = ("After shutdown of {} there are still " "{} references remaining. The skill " "won't be cleaned from memory.") LOG.warning(msg.format(skill['instance'].name, refs)) del skill["instance"] self.bus.emit( Message("mycroft.skills.shutdown", { "path": skill_path, "id": skill["id"] })) skill["loaded"] = True desc = create_skill_descriptor(skill_path) skill["instance"] = load_skill(desc, self.bus, skill["id"], BLACKLISTED_SKILLS) skill["last_modified"] = modified if skill['instance'] is not None: self.bus.emit( Message( 'mycroft.skills.loaded', { 'path': skill_path, 'id': skill['id'], 'name': skill['instance'].name, 'modified': modified })) return True else: self.bus.emit( Message('mycroft.skills.loading_failure', { 'path': skill_path, 'id': skill['id'] })) return False
def remove_git_locks(self): """If git gets killed from an abrupt shutdown it leaves lock files""" for i in glob(join(self.msm.skills_dir, '*/.git/index.lock')): LOG.warning('Found and removed git lock file: ' + i) os.remove(i)
def run(self): global ws, loaded_skills, last_modified_skill # Load priority skills first by order load_skill_list(PRIORITY_SKILLS) self._loaded_priority.set() # Scan the file folder that contains Skills. If a Skill is updated, # unload the existing version from memory and reload from the disk. while not self._stop_event.is_set(): if exists(SKILLS_DIR): # checking skills dir and getting all skills there list = filter( lambda x: os.path.isdir(os.path.join(SKILLS_DIR, x)), os.listdir(SKILLS_DIR)) for skill_folder in list: if skill_folder not in loaded_skills: loaded_skills[skill_folder] = { "id": hash(os.path.join(SKILLS_DIR, skill_folder)) } skill = loaded_skills.get(skill_folder) skill["path"] = os.path.join(SKILLS_DIR, skill_folder) # checking if is a skill if not MainModule + ".py" in os.listdir(skill["path"]): continue # getting the newest modified date of skill last_mod = _get_last_modified_date(skill["path"]) skill["last_modified"] = last_mod modified = skill.get("last_modified", 0) # checking if skill is loaded and wasn't modified if skill.get("loaded") and modified <= last_modified_skill: continue # checking if skill was modified elif (skill.get("instance") and modified > last_modified_skill): # checking if skill should be reloaded if not skill["instance"].reload_skill: continue LOG.debug("Reloading Skill: " + skill_folder) # removing listeners and stopping threads skill["instance"].shutdown() # -2 since two local references that are known refs = sys.getrefcount(skill["instance"]) - 2 if refs > 0: LOG.warning("After shutdown of {} there are still " "{} references remaining. The skill " "won't be cleaned from memory.".format( skill['instance'].name, refs)) del skill["instance"] skill["loaded"] = True skill["instance"] = load_skill( create_skill_descriptor(skill["path"]), ws, skill["id"], BLACKLISTED_SKILLS) # get the last modified skill modified_dates = map(lambda x: x.get("last_modified"), loaded_skills.values()) if len(modified_dates) > 0: last_modified_skill = max(modified_dates) if not self._loaded_once.is_set(): self._loaded_once.set() # Pause briefly before beginning next scan time.sleep(2)
def execute(self, audio, language=None): LOG.warning("WITSTT language should be configured at wit.ai settings.") return self.recognizer.recognize_wit(audio, self.token)
def test_logging(): LOG.debug('testing debug') LOG.info('testing info') LOG.warning('testing warning') LOG.error('testing error') LOG('testing custom').debug('test')
# File Path Manager # import os # sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) import json import os import time from mycroft import MycroftSkill from mycroft.util import play_wav from mycroft.util.log import LOG from websocket import create_connection from .file_path_manager import FilePathManager LOG.warning('Skill Keypad is Running ') try: from .code.keypad import Keypad from mycroft.messagebus.client.ws import WebsocketClient from mycroft.messagebus.message import Message except ImportError: # re-install yourself from msm import MycroftSkillsManager msm = MycroftSkillsManager() msm.install("https://github.com/ITE-5th/skill-keypad") sleep_time = 1 class KeypadSkill(MycroftSkill):
def store(self, force=False): LOG.warning('DEPRECATED - use mycroft.skills.settings.save_settings()') save_settings(self._skill.root_dir, self._settings)
def _load_or_reload_skill(self, skill_folder): """ Check if unloaded skill or changed skill needs reloading and perform loading if necessary. """ if skill_folder not in self.loaded_skills: self.loaded_skills[skill_folder] = { "id": hash(os.path.join(SKILLS_DIR, skill_folder)) } skill = self.loaded_skills.get(skill_folder) skill["path"] = os.path.join(SKILLS_DIR, skill_folder) # check if folder is a skill (must have __init__.py) if not MainModule + ".py" in os.listdir(skill["path"]): return # getting the newest modified date of skill modified = _get_last_modified_date(skill["path"]) last_mod = skill.get("last_modified", 0) # checking if skill is loaded and hasn't been modified on disk if skill.get("loaded") and modified <= last_mod: return # Nothing to do! # check if skill was modified elif skill.get("instance") and modified > last_mod: # check if skill has been blocked from reloading if not skill["instance"].reload_skill: return LOG.debug("Reloading Skill: " + skill_folder) # removing listeners and stopping threads skill["instance"].shutdown() if DEBUG: gc.collect() # Collect garbage to remove false references # Remove two local references that are known refs = sys.getrefcount(skill["instance"]) - 2 if refs > 0: LOG.warning("After shutdown of {} there are still " "{} references remaining. The skill " "won't be cleaned from memory.".format( skill['instance'].name, refs)) del skill["instance"] self.ws.emit( Message("mycroft.skills.shutdown", { "folder": skill_folder, "id": skill["id"] })) # (Re)load the skill from disk with self.__msm_lock: # Make sure msm isn't running skill["loaded"] = True desc = create_skill_descriptor(skill["path"]) skill["instance"] = load_skill(desc, self.ws, skill["id"], BLACKLISTED_SKILLS) skill["last_modified"] = modified if skill['instance'] is not None: self.ws.emit( Message( 'mycroft.skills.loaded', { 'folder': skill_folder, 'id': skill['id'], 'name': skill['instance'].name, 'modified': modified })) else: self.ws.emit( Message('mycroft.skills.loading_failure', { 'folder': skill_folder, 'id': skill['id'] }))