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

refactor/cleanup_resource_files #79

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 60 additions & 13 deletions ovos_workshop/resource_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ def _read(self) -> str:


class QmlFile(ResourceFile):
def __init__(self, resource_name):
super().__init__(resource_type=SkillResourceTypes.qml,
resource_name=resource_name)

def _locate(self):
""" QML files are special because we do not want to walk the directory """
file_path = None
Expand Down Expand Up @@ -306,8 +310,8 @@ def load(self):
class DialogFile(ResourceFile):
"""Defines a dialog file, which is used instruct TTS what to speak."""

def __init__(self, resource_type, resource_name):
super().__init__(resource_type, resource_name)
def __init__(self, resource_name):
super().__init__(SkillResourceTypes.dialog, resource_name)
self.data = None

def load(self) -> List[str]:
Expand Down Expand Up @@ -342,6 +346,9 @@ def render(self, dialog_renderer):
class VocabularyFile(ResourceFile):
"""Defines a vocabulary file, which skill use to form intents."""

def __init__(self, resource_name):
super().__init__(SkillResourceTypes.vocabulary, resource_name)

def load(self) -> List[List[str]]:
"""Loads a vocabulary file.

Expand All @@ -364,8 +371,8 @@ def load(self) -> List[List[str]]:
class NamedValueFile(ResourceFile):
"""Defines a named value file, which maps a variable to a values."""

def __init__(self, resource_type, resource_name):
super().__init__(resource_type, resource_name)
def __init__(self, resource_name):
super().__init__(SkillResourceTypes.named_value, resource_name)
self.delimiter = ","

def load(self) -> dict:
Expand Down Expand Up @@ -404,14 +411,21 @@ def _load_line(self, line: str) -> Tuple[str, str]:


class ListFile(DialogFile):
pass
def __init__(self, resource_name):
ResourceFile.__init__(self, SkillResourceTypes.list, resource_name)
self.data = None


class TemplateFile(DialogFile):
pass
def __init__(self, resource_name):
ResourceFile.__init__(self, SkillResourceTypes.template, resource_name)
self.data = None


class RegexFile(ResourceFile):
def __init__(self, resource_name):
super().__init__(SkillResourceTypes.regex, resource_name)

def load(self):
regex_patterns = []
if self.file_path:
Expand All @@ -423,6 +437,9 @@ def load(self):
class WordFile(ResourceFile):
"""Defines a word file, which defines a word in the configured language."""

def __init__(self, resource_name):
super().__init__(SkillResourceTypes.word, resource_name)

def load(self) -> Optional[str]:
"""Load and lines from a file and populate the variables.

Expand All @@ -438,6 +455,36 @@ def load(self) -> Optional[str]:
return word


class IntentFile(ResourceFile):
"""Defines a .intent file, which is used to train example based intents"""

def __init__(self, resource_name):
super().__init__(resource_type=SkillResourceTypes.intent,
resource_name=resource_name)

def load(self) -> List[str]:
"""Load and lines from a file and populate the variables.

Returns:
Contents of the file with variables resolved.
"""
samples = None
if self.file_path is not None:
samples = []
for line in self._read():
line = line.replace("{{", "{").replace("}}", "}")
samples += expand_options(line.lower())

return samples


class EntityFile(VocabularyFile):
"""Defines a entity file, which skill use to form intent slots."""

def __init__(self, resource_name):
ResourceFile.__init__(self, SkillResourceTypes.entity, resource_name)


class SkillResources:
def __init__(self, skill_directory, language, dialog_renderer=None, skill_id=None):
self.skill_directory = skill_directory
Expand Down Expand Up @@ -505,12 +552,12 @@ def load_dialog_file(self, name, data=None) -> List[str]:
Returns:
A list of phrases with variables resolved
"""
dialog_file = DialogFile(self.types.dialog, name)
dialog_file = DialogFile(name)
dialog_file.data = data
return dialog_file.load()

def locate_qml_file(self, name):
qml_file = QmlFile(self.types.qml, name)
qml_file = QmlFile(name)
return qml_file.load()

def load_list_file(self, name, data=None) -> List[str]:
Expand Down Expand Up @@ -544,7 +591,7 @@ def load_named_value_file(self, name, delimiter=None) -> dict:
if name in self.static:
named_values = self.static[name]
else:
named_value_file = NamedValueFile(self.types.named_value, name)
named_value_file = NamedValueFile(name)
if delimiter is not None:
named_value_file.delimiter = delimiter
named_values = named_value_file.load()
Expand All @@ -564,7 +611,7 @@ def load_regex_file(self, name) -> List[str]:
Returns:
List representation of the regular expression file.
"""
regex_file = RegexFile(self.types.regex, name)
regex_file = RegexFile(name)
return regex_file.load()

def load_template_file(self, name, data=None) -> List[str]:
Expand Down Expand Up @@ -595,7 +642,7 @@ def load_vocabulary_file(self, name) -> List[List[str]]:
Returns:
List representation of the regular expression file.
"""
vocabulary_file = VocabularyFile(self.types.vocabulary, name)
vocabulary_file = VocabularyFile(name)
return vocabulary_file.load()

def load_word_file(self, name) -> Optional[str]:
Expand All @@ -606,7 +653,7 @@ def load_word_file(self, name) -> Optional[str]:
Returns:
List representation of the regular expression file.
"""
word_file = WordFile(self.types.word, name)
word_file = WordFile(name)
return word_file.load()

def render_dialog(self, name, data=None) -> str:
Expand All @@ -618,7 +665,7 @@ def render_dialog(self, name, data=None) -> str:
Returns:
Random record from the file with variables resolved.
"""
resource_file = DialogFile(self.types.dialog, name)
resource_file = DialogFile(name)
resource_file.data = data
return resource_file.render(self.dialog_renderer)

Expand Down
9 changes: 5 additions & 4 deletions ovos_workshop/skills/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
from ovos_workshop.decorators.killable import killable_event, \
AbortQuestion
from ovos_workshop.filesystem import FileSystemAccess
from ovos_workshop.resource_files import ResourceFile, \
CoreResources, SkillResources, find_resource
from ovos_workshop.resource_files import EntityFile, \
CoreResources, SkillResources, find_resource, IntentFile
from ovos_workshop.settings import SkillSettingsManager


Expand Down Expand Up @@ -1463,11 +1463,12 @@ def register_intent_file(self, intent_file, handler):
"""
for lang in self._native_langs:
name = f'{self.skill_id}:{intent_file}'
resource_file = ResourceFile(self._resources.types.intent, intent_file)
resource_file = IntentFile(intent_file)
if resource_file.file_path is None:
self.log.error(f'Unable to find "{intent_file}"')
continue
filename = str(resource_file.file_path)
samples = resource_file.load()
self.intent_service.register_padatious_intent(name, filename, lang)
if handler:
self.add_event(name, handler, 'mycroft.skill.handler')
Expand All @@ -1490,7 +1491,7 @@ def register_entity_file(self, entity_file):
if entity_file.endswith('.entity'):
entity_file = entity_file.replace('.entity', '')
for lang in self._native_langs:
entity = ResourceFile(self._resources.types.entity, entity_file)
entity = EntityFile(entity_file)
if entity.file_path is None:
self.log.error(f'Unable to find "{entity_file}"')
continue
Expand Down