Merge pull request #1835 from forslund/refactor/skills-core

Cleanup of core.py
pull/1845/head
Michael Nguyen 2018-10-10 11:08:37 -05:00 committed by GitHub
commit 6289bb5b50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 38 deletions

View File

@ -48,6 +48,15 @@ from mycroft.util.log import LOG
MainModule = '__init__'
def simple_trace(stack_trace):
stack_trace = stack_trace[:-1]
tb = "Traceback:\n"
for line in stack_trace:
if line.strip():
tb += line
return tb
def dig_for_message():
"""
Dig Through the stack for message.
@ -105,19 +114,16 @@ def load_skill(skill_descriptor, bus, skill_id, BLACKLISTED_SKILLS=None):
BLACKLISTED_SKILLS = BLACKLISTED_SKILLS or []
path = skill_descriptor["path"]
name = basename(path)
LOG.info("ATTEMPTING TO LOAD SKILL: {} with ID {}".format(
name, skill_id
))
LOG.info("ATTEMPTING TO LOAD SKILL: {} with ID {}".format(name, skill_id))
if name in BLACKLISTED_SKILLS:
LOG.info("SKILL IS BLACKLISTED " + name)
return None
main_file = join(path, MainModule + '.py')
try:
with open(main_file, 'rb') as fp:
skill_module = imp.load_module(
name.replace('.', '_'), fp, main_file,
('.py', 'rb', imp.PY_SOURCE)
)
skill_module = imp.load_module(name.replace('.', '_'), fp,
main_file, ('.py', 'rb',
imp.PY_SOURCE))
if (hasattr(skill_module, 'create_skill') and
callable(skill_module.create_skill)):
# v2 skills framework
@ -167,11 +173,10 @@ def get_handler_name(handler):
Returns:
string: handler name as string
"""
name = ''
if '__self__' in dir(handler) and 'name' in dir(handler.__self__):
name += handler.__self__.name + '.'
name += handler.__name__
return name
return handler.__self__.name + '.' + handler.__name__
else:
return handler.__name__
def intent_handler(intent_parser):
@ -238,13 +243,9 @@ class MycroftSkill(object):
if self._enclosure:
return self._enclosure
else:
LOG.error("ERROR: Skill not fully initialized. Move code from " +
" __init__() to initialize() to correct this.")
tb = "Traceback:\n"
for line in traceback.format_stack()[:-1]:
if line.strip():
tb += line
LOG.error(tb)
LOG.error("Skill not fully initialized. Move code " +
"from __init__() to initialize() to correct this.")
LOG.error(simple_trace(traceback.format_stack()))
raise Exception("Accessed MycroftSkill.enclosure in __init__")
@property
@ -252,13 +253,9 @@ class MycroftSkill(object):
if self._bus:
return self._bus
else:
LOG.error("ERROR: Skill not fully initialized. Move code from " +
" __init__() to initialize() to correct this.")
tb = "Traceback:\n"
for line in traceback.format_stack()[:-1]:
if line.strip():
tb += line
LOG.error(tb)
LOG.error("Skill not fully initialized. Move code " +
"from __init__() to initialize() to correct this.")
LOG.error(simple_trace(traceback.format_stack()))
raise Exception("Accessed MycroftSkill.bus in __init__")
@property
@ -436,8 +433,7 @@ class MycroftSkill(object):
validator = validator or validator_default
on_fail_fn = on_fail if callable(on_fail) else on_fail_default
self.speak(get_announcement(), expect_response=True)
wait_while_speaking()
self.speak(get_announcement(), expect_response=True, wait=True)
num_fails = 0
while True:
response = self.__get_response()
@ -478,11 +474,10 @@ class MycroftSkill(object):
if self.voc_match(resp, 'yes'):
return 'yes'
if self.voc_match(resp, 'no'):
elif self.voc_match(resp, 'no'):
return 'no'
return resp
else:
return resp
def voc_match(self, utt, voc_filename, lang=None):
""" Determine if the given utterance contains the vocabulary provided
@ -1176,7 +1171,7 @@ class MycroftSkill(object):
Message("detach_skill", {"skill_id": str(self.skill_id) + ":"}))
try:
self.stop()
except: # noqa
except Exception:
LOG.error("Failed to stop skill: {}".format(self.name),
exc_info=True)
@ -1307,27 +1302,29 @@ class MycroftSkill(object):
data = {'name': event_name}
# making event_status an object so it's refrence can be changed
event_status = [None]
finished_callback = [False]
event_status = None
finished_callback = False
def callback(message):
nonlocal event_status
nonlocal finished_callback
if message.data is not None:
event_time = int(message.data[0][0])
current_time = int(time.time())
time_left_in_seconds = event_time - current_time
event_status[0] = time_left_in_seconds
finished_callback[0] = True
event_status = time_left_in_seconds
finished_callback = True
emitter_name = 'mycroft.event_status.callback.{}'.format(event_name)
self.bus.once(emitter_name, callback)
self.bus.emit(Message('mycroft.scheduler.get_event', data=data))
start_wait = time.time()
while finished_callback[0] is False and time.time() - start_wait < 3.0:
while finished_callback is False and time.time() - start_wait < 3.0:
time.sleep(0.1)
if time.time() - start_wait > 3.0:
raise Exception("Event Status Messagebus Timeout")
return event_status[0]
return event_status
def cancel_all_repeating_events(self):
""" Cancel any repeating events started by the skill. """