Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Refactor MycroftSkill #2256

Merged
merged 20 commits into from
Aug 30, 2019
Merged

Conversation

forslund
Copy link
Collaborator

@forslund forslund commented Aug 13, 2019

Description

The intent of this PR is to reduce the size of the MycroftSkill class by separating some of the more complicated parts into separate classes and move some helper functions into separate files. The big things moved were event scheduling code, intent registration code, event handler tracking.

But there was some feature creep...

This PR intents to do the following

  • Extract the event handling code
  • Create test cases for the event handling parts
  • Extract the event scheduler interface to separate class
  • Minor cleanup such as spaces around operators
  • Cleanup skill data loading
  • Refactor translation methods
  • Unify the intent_handler and intent_file_handler decorators
  • replace the _dir member with the root_folder member
  • reduce get_response complexity
  • refactor intent handler registration

How to test

Contributor license agreement signed?

CLA [ Yes ]

@forslund forslund added Status: Work in progress PR being actively worked on, not yet ready for review. Type: Refactoring and other improvements Improvement of code and documentation that does not alter functionality. labels Aug 13, 2019
@pep8speaks
Copy link

pep8speaks commented Aug 13, 2019

Hello @forslund! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2019-08-30 11:32:31 UTC

@devs-mycroft devs-mycroft added the CLA: Yes Contributor License Agreement exists (see https://github.com/MycroftAI/contributors) label Aug 13, 2019
@forslund forslund force-pushed the refactor-split-mycroftskill branch 4 times, most recently from 98f3b6f to 57f9b10 Compare August 15, 2019 05:49
@forslund forslund force-pushed the refactor-split-mycroftskill branch 2 times, most recently from b1da14e to 4e5e20a Compare August 25, 2019 14:46
@forslund forslund removed the Status: Work in progress PR being actively worked on, not yet ready for review. label Aug 25, 2019
@chrisveilleux
Copy link
Member

Would you please put this in a Mycroft-core branch? I would like to be able to look at it in my IDE where it would be easier to navigate/compare.

@forslund
Copy link
Collaborator Author

Done, a copy of the refactor-split-mycroftskill branch is now available on the mycroftai/mycroft-core repo

Copy link
Member

@chrisveilleux chrisveilleux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, this is one hella big PR. I am not actually done with my review but I am starting to go cross-eyed looking at this code. There should be enough in here to keep you busy tomorrow ;) I will make some time to look at the rest tomorrow.

@@ -250,3 +251,163 @@ def shutdown(self):
self.clear_empty()
# Store all pending scheduled events
self.store()


class EventSchedulerInterface:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider renaming to SkillEventScheduler. The current name seems too generic. At face value, this new class looks a lot like the existing EventScheduler class. At first I found myself asking why not combine the two. Maybe the class-level doc string needs more information to indicate why both classes are needed. Maybe the method names should have the word "skill" in them for clarity? schedule_skill_event?

Copy link
Collaborator Author

@forslund forslund Aug 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit you have a point here, it's very entangled with the skills class right now. I'll do some rework to isolate it better.

mycroft/skills/event_scheduler.py Outdated Show resolved Hide resolved
mycroft/skills/event_scheduler.py Outdated Show resolved Hide resolved
self.scheduled_repeats.append(name) # store "friendly name"

data = data or {}
self.skill.add_event(unique_name, handler, once=not repeat)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not track the skill's events in this class? This is the skill's event interface. Shouldn't all event-related logic for a skill be included in this class?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handled

mycroft/skills/event_scheduler.py Outdated Show resolved Hide resolved
mycroft/skills/mycroft_skill/mycroft_skill.py Show resolved Hide resolved
mycroft/skills/mycroft_skill/mycroft_skill.py Show resolved Hide resolved
mycroft/skills/mycroft_skill/mycroft_skill.py Show resolved Hide resolved
mycroft/skills/mycroft_skill/mycroft_skill.py Show resolved Hide resolved
'data': data
}
self.bus.emit(Message('mycroft.schedule.update_event', data=data))
self.event_scheduler.update_scheduled_event(name, data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you leaving this (and the next several methods like it) in here for backwards compatibility? if so, do we need to since this is a breaking release? if we do need to keep these, can we put a deprecation warning on them?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are basically convenience methods right now. We'll need to have a chat after the release of the future of the MycroftSkill interface. There's been some discussion in the past but the current rule is the MycroftSkill class should contain all the methods a skill creator needs so it's my interpretation at the moment that these methods should remain as part of the interface.

@forslund forslund force-pushed the refactor-split-mycroftskill branch 6 times, most recently from 57c18fc to 9afcab9 Compare August 29, 2019 14:36
Copy link
Member

@chrisveilleux chrisveilleux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a few comments and answered a few questions.

mycroft/skills/event_scheduler.py Show resolved Hide resolved
mycroft/skills/intent_service_interface.py Show resolved Hide resolved
mycroft/skills/intent_service_interface.py Show resolved Hide resolved
mycroft/skills/intent_service_interface.py Show resolved Hide resolved
mycroft/skills/mycroft_skill/__init__.py Outdated Show resolved Hide resolved
mycroft/skills/mycroft_skill/mycroft_skill.py Show resolved Hide resolved
mycroft/skills/mycroft_skill/mycroft_skill.py Show resolved Hide resolved
test/unittests/skills/test_event_container.py Outdated Show resolved Hide resolved
@forslund forslund force-pushed the refactor-split-mycroftskill branch 2 times, most recently from 4490526 to 3535e24 Compare August 29, 2019 19:21
Group event scheduler interface logic into subclass
Split of decorators from the mycroft_skill file and create a submodule
for mycroft_skill
This makes the register_intent decorator work for all types of intents,
both Adapt and Padatious.
- Create separate method for regisetering mycroft system event handlers
- Fix some string concatenations
- Group acknowledge together with speak and speak_dialog
This also removes the _dir member which cointains the same information.
This container remembers registered skills and allows unregistering
all handlers connected to a skill at shutdown.
- The data loading no longer require the bus
- Add an intent service interface class for better testing
- Update test cases
Move much of the complex methods for registering intents to the
IntentServiceInterface to reduce bloat of MycroftSkill
Move file reading logic into skill_data.py
Use the more modern bus.wait_for_response()
- Remove once-logic from the default handler wrapper, no need to do it there.
- Add docstring for handle_wrapper
- add the on_start, on_end handlers and move skill logic to respective
handlers.
Copy link
Member

@chrisveilleux chrisveilleux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, further clean up can occur over time. This is a great improvement to the MycroftSkill class. Well done.

mycroft/skills/event_scheduler.py Show resolved Hide resolved
mycroft/skills/mycroft_skill/mycroft_skill.py Show resolved Hide resolved
@forslund forslund merged commit 75ad11b into MycroftAI:dev Aug 30, 2019
@forslund forslund deleted the refactor-split-mycroftskill branch November 28, 2019 09:43
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
CLA: Yes Contributor License Agreement exists (see https://github.com/MycroftAI/contributors) Type: Refactoring and other improvements Improvement of code and documentation that does not alter functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants