Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup of core.py #1835

Merged
merged 5 commits into from
Oct 10, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 35 additions & 38 deletions mycroft/skills/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@
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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -238,27 +243,19 @@ def enclosure(self):
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
def bus(self):
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
Expand Down Expand Up @@ -433,8 +430,7 @@ def validator_default(utterance):
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()
Expand Down Expand Up @@ -475,11 +471,10 @@ def ask_yesno(self, prompt, data=None):

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
Expand Down Expand Up @@ -1129,7 +1124,7 @@ def default_shutdown(self):
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)

Expand Down Expand Up @@ -1260,27 +1255,29 @@ def get_scheduled_event_status(self, name):
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. """
Expand Down