mycroft-run-script/__init__.py

44 lines
1.5 KiB
Python

import os
from threading import Thread
from mycroft import MycroftSkill, intent_handler
class RunScriptSkill(MycroftSkill):
def __init__(self):
super().__init__()
self.script_dir = None
self.scripts = []
def initialize(self):
self.settings_change_callback = self.on_settings_changed
self.on_settings_changed()
def on_settings_changed(self):
self.script_dir = self.settings.get('script_dir', os.path.expanduser("~/scripts"))
self.read_scripts_dir()
def read_scripts_dir(self):
if self.script_dir and os.path.isdir(self.script_dir):
self.log.info("Reading scripts from % s" % self.script_dir)
self.scripts = os.listdir(self.script_dir)
self.log.debug("Scripts: % s" % ','.join(self.scripts))
else:
self.log.critical("The script_dir directory % s does not exist or can't be read" % self.script_dir)
self.scripts = []
@intent_handler('RunScript.intent')
def handle_run_script_intent(self, msg=None):
script = msg.data.get('script', None).replace(" ", "-")
script_path = os.path.join(self.script_dir, script)
if script in self.scripts and os.path.isfile(script_path):
self.speak_dialog('Running', {'script': script})
thread = Thread(target = os.system, args = (script_path, ))
thread.start()
else:
self.speak_dialog('NotFound', {'script': script})
def stop(self):
pass
def create_skill():
return RunScriptSkill()