-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathovos.py
More file actions
2606 lines (2260 loc) · 106 KB
/
Copy pathovos.py
File metadata and controls
2606 lines (2260 loc) · 106 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import binascii
import datetime
import json
import os
import re
import shutil
import sys
import time
import traceback
from copy import copy
from hashlib import md5
from inspect import signature
from itertools import chain
from os.path import join, abspath, dirname, basename, isfile
from threading import Event, RLock
from typing import Dict, Callable, List, Optional, Union
from json_database import JsonStorage
from ovos_config.config import Configuration
from ovos_config.locations import get_xdg_cache_save_path
from ovos_config.locations import get_xdg_config_save_path
from ovos_number_parser import pronounce_number, extract_number
from ovos_yes_no_solver import YesNoSolver
from ovos_bus_client import MessageBusClient
from ovos_bus_client.apis.enclosure import EnclosureAPI
from ovos_bus_client.apis.gui import GUIInterface
from ovos_bus_client.apis.ocp import OCPInterface
from ovos_bus_client.message import Message, dig_for_message
from ovos_bus_client.session import SessionManager, Session
from ovos_bus_client.util import get_message_lang
from ovos_plugin_manager.language import OVOSLangTranslationFactory, OVOSLangDetectionFactory
from ovos_utils import camel_case_split, classproperty
from ovos_utils.dialog import MustacheDialogRenderer
from ovos_utils.events import EventContainer, EventSchedulerInterface
from ovos_utils.events import get_handler_name, create_wrapper
from ovos_utils.file_utils import FileWatcher
from ovos_utils.gui import get_ui_directories
from ovos_utils.json_helper import merge_dict
from ovos_utils.lang import standardize_lang_tag
from ovos_utils.log import LOG
from ovos_utils.parse import match_one
from ovos_utils.process_utils import ProcessStatus, StatusCallbackMap, RuntimeRequirements
from ovos_utils.skills import get_non_properties
from ovos_utils.text_utils import remove_accents_and_punct
from ovos_workshop.decorators.killable import AbortEvent, killable_event, AbortQuestion
from ovos_workshop.decorators.layers import IntentLayers
from ovos_workshop.filesystem import FileSystemAccess
from ovos_workshop.intents import IntentBuilder, Intent, munge_regex, munge_intent_parser, IntentServiceInterface
from ovos_workshop.resource_files import ResourceFile, CoreResources, find_resource, SkillResources
from ovos_workshop.settings import PrivateSettings
def simple_trace(stack_trace: List[str]) -> str:
"""
Generate a simplified traceback.
@param stack_trace: Formatted stack trace (each string ends with \n)
@return: Stack trace with any empty lines removed and last line removed
"""
stack_trace = stack_trace[:-1]
tb = 'Traceback:\n'
for line in stack_trace:
if line.strip():
tb += line
return tb
class OVOSSkill:
"""
Base class for OpenVoiceOS skills providing common behaviour and parameters
to all Skill implementations.
skill_launcher.py used to be skill_loader-py in mycroft-core
for launching skills one can use skill_launcher.py to run them standalone
(eg, docker)
KwArgs:
name (str): skill name - DEPRECATED
skill_id (str): unique skill identifier
bus (MycroftWebsocketClient): Optional bus connection
"""
def __init__(self, name: Optional[str] = None,
bus: Optional[MessageBusClient] = None,
resources_dir: Optional[str] = None,
settings: Optional[JsonStorage] = None,
gui: Optional[GUIInterface] = None,
skill_id: str = ""):
"""
Create an OVOSSkill object.
@param name: DEPRECATED skill_name
@param bus: MessageBusClient to bind to skill
@param resources_dir: optional root resource directory (else defaults to
skill `root_dir`
@param settings: Optional settings object, else defined in skill config
path
@param gui: Optional SkillGUI, else one is initialized
@param skill_id: Unique ID for this skill
"""
self.log = LOG # a dedicated namespace will be assigned in _startup
self._init_event = Event()
self.name = name or self.__class__.__name__
self.skill_id = skill_id # set by SkillLoader, guaranteed unique
self.private_settings = None
# Get directory of skill source (__init__.py)
self.root_dir = dirname(abspath(sys.modules[self.__module__].__file__))
self.res_dir = resources_dir or self.root_dir
self.gui = gui
self._bus = bus
self._enclosure = EnclosureAPI()
# optional lang translation, lazy inited on first access
self._lang_detector = None
self._translator = None # can be passed to solvers plugins
# Core configuration
self.config_core: Configuration = Configuration()
self._settings = None
self._initial_settings = settings or dict()
self._settings_watchdog = None
self._settings_lock = RLock()
# Override to register a callback method that will be called every time
# the skill's settings are updated. The referenced method should
# include any logic needed to handle the updated settings.
self.settings_change_callback = None
# fully initialized when self.skill_id is set
self._file_system = None
self.reload_skill = True # allow reloading (default True)
self.events = EventContainer(bus)
# Cached voc file contents
self._voc_cache = {}
# loaded lang file resources
self._lang_resources = {}
# Delegator classes
self.event_scheduler = EventSchedulerInterface()
self.intent_service = IntentServiceInterface()
self.audio_service = None
self.intent_layers = IntentLayers()
# Skill Public API
self.public_api: Dict[str, dict] = {}
self._cq_handler = None
self._cq_callback = None
self.__responses = {}
self.__validated_responses = {}
self._threads = [] # for killable events decorator
# yay, following python best practices again!
if self.skill_id and bus:
self._startup(bus, self.skill_id)
# skill developer abstract methods
# devs are meant to override these
def initialize(self):
"""
Legacy method overridden by skills to perform extra init after __init__.
Skills should now move any code in this method to `__init__`, after a
call to `super().__init__`.
"""
pass
def get_intro_message(self) -> str:
"""
Override to return a string to speak on first run. i.e. for post-install
setup instructions.
"""
return ""
def stop(self):
"""
Optional method implemented by subclass. Called when system or user
requests `stop` to cancel current execution.
"""
pass
def shutdown(self):
"""
Optional shutdown procedure implemented by subclass.
This method is intended to be called during the skill process
termination. The skill implementation must shut down all processes and
operations in execution.
"""
pass
# skill class properties
@classproperty
def runtime_requirements(self) -> RuntimeRequirements:
"""
Override to specify what a skill expects to be available at init and at
runtime. Default will assume network and internet are required and GUI
is not required for backwards-compat.
some examples:
IOT skill that controls skills via LAN could return:
scans_on_init = True
RuntimeRequirements(internet_before_load=False,
network_before_load=scans_on_init,
requires_internet=False,
requires_network=True,
no_internet_fallback=True,
no_network_fallback=False)
online search skill with a local cache:
has_cache = False
RuntimeRequirements(internet_before_load=not has_cache,
network_before_load=not has_cache,
requires_internet=True,
requires_network=True,
no_internet_fallback=True,
no_network_fallback=True)
a fully offline skill:
RuntimeRequirements(internet_before_load=False,
network_before_load=False,
requires_internet=False,
requires_network=False,
no_internet_fallback=True,
no_network_fallback=True)
"""
return RuntimeRequirements()
@property
def is_fully_initialized(self) -> bool:
"""
Determines if the skill has been fully loaded and setup.
When True, all data has been loaded and all internal state
and events set up.
"""
return self._init_event.is_set()
def can_stop(self, message: Message) -> bool:
"""
Determine whether the skill can be stopped at the current moment.
If this method returns True, OVOS will call self.stop() when the user
issues a command to stop the current activity.
TIP: you can use SessionManager.get(message) if the skill is session aware
Args:
message (Message): The message context triggering the check.
Returns:
bool: True if the skill is currently performing an action that can be stopped; False otherwise.
"""
if self.__class__.stop is not OVOSSkill.stop or \
self.__class__.stop_session is not OVOSSkill.stop_session:
raise NotImplementedError("All skills that implement self.stop or self.stop_session must also implement self.can_stop.")
return False # if there isnt a stop method, we can be more lenient and not require can_stop to be implemented
# safe skill_id/bus wrapper properties
@property
def alphanumeric_skill_id(self) -> str:
"""
Skill id converted to only alphanumeric characters and "_".
Non alphanumeric characters are converted to "_"
"""
return ''.join(c if c.isalnum() else '_'
for c in str(self.skill_id))
@property
def lang_detector(self):
""" language detector, lazy init on first access"""
if not self._lang_detector:
# if it's being used, there is no recovery, do not try: except:
self._lang_detector = OVOSLangDetectionFactory.create(self.config_core)
return self._lang_detector
@lang_detector.setter
def lang_detector(self, val):
self._lang_detector = val
@property
def translator(self):
""" language translator, lazy init on first access"""
if not self._translator:
# if it's being used, there is no recovery, do not try: except:
self._translator = OVOSLangTranslationFactory.create(self.config_core)
return self._translator
@translator.setter
def translator(self, val):
self._translator = val
@property
def settings_path(self) -> str:
"""
Absolute file path of this skill's `settings.json` (file may not exist)
"""
return join(get_xdg_config_save_path(), 'skills', self.skill_id,
'settings.json')
@property
def settings(self) -> JsonStorage:
"""
Get settings specific to this skill
"""
if self._settings is not None:
return self._settings
else:
self.log.warning('Skill not fully initialized. Only default values '
'can be set, no settings can be read or changed.'
f"to correct this add kwargs "
f"__init__(bus=None, skill_id='') "
f"to skill class {self.__class__.__name__} "
"You can only use self.settings after the call to 'super()'")
self.log.error(simple_trace(traceback.format_stack()))
return self._initial_settings
@settings.setter
def settings(self, val: dict):
"""
Update settings specific to this skill
"""
LOG.warning(
"Skills are not supposed to override self.settings, expect breakage! Set individual dict keys instead")
assert isinstance(val, dict)
# init method
if self._settings is None:
self._initial_settings = val
return
with self._settings_lock:
# ensure self._settings remains a JsonDatabase
self._settings.clear() # clear data
self._settings.merge(val, skip_empty=False) # merge new data
@property
def enclosure(self) -> EnclosureAPI:
"""
Get an EnclosureAPI object to interact with hardware
"""
if self._enclosure:
return self._enclosure
else:
self.log.warning('Skill not fully initialized.'
f"to correct this add kwargs "
f"__init__(bus=None, skill_id='') "
f"to skill class {self.__class__.__name__}."
"You can only use self.enclosure after the call to 'super()'")
self.log.error(simple_trace(traceback.format_stack()))
raise Exception('Accessed OVOSSkill.enclosure in __init__')
@property
def file_system(self) -> FileSystemAccess:
"""
Get an object that provides managed access to a local Filesystem.
"""
if not self._file_system and self.skill_id:
self._file_system = FileSystemAccess(join('skills', self.skill_id))
if self._file_system:
return self._file_system
else:
self.log.warning('Skill not fully initialized.'
f"to correct this add kwargs __init__(bus=None, skill_id='') "
f"to skill class {self.__class__.__name__} "
"You can only use self.file_system after the call to 'super()'")
self.log.error(simple_trace(traceback.format_stack()))
raise Exception('Accessed OVOSSkill.file_system in __init__')
@file_system.setter
def file_system(self, fs: FileSystemAccess):
"""
Provided mainly for backwards compatibility with derivative
MycroftSkill classes. Skills are advised against redefining the file
system directory.
@param fs: new FileSystemAccess object to use
"""
LOG.warning(f"Skill manually overriding file_system path to: {fs.path}")
self._file_system = fs
@property
def bus(self) -> MessageBusClient:
"""
Get the MessageBusClient bound to this skill
"""
if self._bus:
return self._bus
else:
self.log.warning('Skill not fully initialized.'
f"to correct this add kwargs "
f"__init__(bus=None, skill_id='') "
f"to skill class {self.__class__.__name__} "
"You can only use self.bus after the call to 'super()'")
self.log.error(simple_trace(traceback.format_stack()))
raise Exception('Accessed OVOSSkill.bus in __init__')
@bus.setter
def bus(self, value: MessageBusClient):
"""
Set the MessageBusClient bound to this skill. Note that setting this
after init may have unintended consequences as expected events might
not be registered. Call `bind` to connect a new MessageBusClient.
@param value: new MessageBusClient object
"""
from ovos_bus_client import MessageBusClient
from ovos_utils.fakebus import FakeBus
if isinstance(value, (MessageBusClient, FakeBus)):
self._bus = value
else:
raise TypeError(f"Expected a MessageBusClient, got: {type(value)}")
# magic properties -> depend on message.context / Session
@property
def dialog_renderer(self) -> Optional[MustacheDialogRenderer]:
"""
Get a dialog renderer for this skill. Language will be determined by
message history to match the language associated with the current
session or else from Configuration.
"""
return self.resources.dialog_renderer
@property
def system_unit(self) -> str:
"""
Get the units preference (metric vs imperial)
This info may come from Session, eg, injected by a voice satellite
"""
sess = SessionManager.get()
return sess.system_unit
@property
def date_format(self) -> str:
"""
Get the date format (DMY/MDY/YMD)
This info may come from Session, eg, injected by a voice satellite
"""
sess = SessionManager.get()
return sess.date_format
@property
def time_format(self) -> str:
"""
Get the time format (half vs full)
This info may come from Session, eg, injected by a voice satellite
"""
sess = SessionManager.get()
return sess.time_format
@property
def location(self) -> dict:
"""
Get the JSON data struction holding location information.
This info may come from Session, eg, injected by a voice satellite
"""
sess = SessionManager.get()
return sess.location_preferences
@property
def location_pretty(self) -> Optional[str]:
"""
Get a speakable city from the location config if available
This info may come from Session, eg, injected by a voice satellite
"""
loc = self.location
if type(loc) is dict and loc['city']:
return loc['city']['name']
return None
@property
def location_timezone(self) -> Optional[str]:
"""
Get the timezone code, such as 'America/Los_Angeles'
This info may come from Session, eg, injected by a voice satellite
"""
loc = self.location
if type(loc) is dict and loc['timezone']:
return loc['timezone']['code']
return None
@property
def lang(self) -> str:
"""
Get the current language as a BCP-47 language code.
This info may come from Session, eg, injected by a voice satellite
"""
lang = self.core_lang
message = dig_for_message()
if message:
lang = get_message_lang(message)
return standardize_lang_tag(lang)
@property
def core_lang(self) -> str:
"""
Get the configured default language as a BCP-47 language code.
"""
return standardize_lang_tag(self.config_core.get("lang", "en-US"))
@property
def secondary_langs(self) -> List[str]:
"""
Get the configured secondary languages; resources will be loaded for
these languages to provide support for multilingual input, in addition
to `core_lang`. A skill may override this method to specify which
languages intents are registered in.
"""
return [standardize_lang_tag(lang) for lang in self.config_core.get('secondary_langs', [])
if lang != self.core_lang]
@property
def native_langs(self) -> List[str]:
"""
Languages natively supported by this skill (ie, resource files available
and explicitly supported). This is equivalent to normalized
secondary_langs + core_lang.
"""
valid = set([standardize_lang_tag(lang) for lang in self.secondary_langs
if lang != self.core_lang] + [self.core_lang])
return list(valid)
@property
def resources(self) -> SkillResources:
"""
Get a SkillResources object for the current language. Objects are
initialized for the current language as needed.
"""
return self.load_lang(self.res_dir, self.lang)
# resource file loading
def load_lang(self, root_directory: Optional[str] = None,
lang: Optional[str] = None) -> SkillResources:
"""
Get a SkillResources object for this skill in the requested `lang` for
resource files in the requested `root_directory`.
@param root_directory: root path to find resources (default res_dir)
@param lang: language to get resources for (default self.lang)
@return: SkillResources object
"""
lang = standardize_lang_tag(lang or self.lang)
root_directory = root_directory or self.res_dir
if lang not in self._lang_resources:
self._lang_resources[lang] = SkillResources(root_directory, lang,
skill_id=self.skill_id)
return self._lang_resources[lang]
def load_dialog_files(self, root_directory: Optional[str] = None):
"""
Load dialog files for all configured languages
@param root_directory: Directory to locate resources in
(default self.res_dir)
"""
root_directory = root_directory or self.res_dir
# If "<skill>/dialog/<lang>" exists, load from there. Otherwise,
# load dialog from "<skill>/locale/<lang>"
for lang in self.native_langs:
resources = self.load_lang(root_directory, lang)
if resources.types.dialog.base_directory is None:
self.log.debug(f'No dialog loaded for {lang}')
def load_data_files(self, root_directory: Optional[str] = None):
"""
Called by the skill loader to load intents, dialogs, etc.
Args:
root_directory (str): root folder to use when loading files.
"""
root_directory = root_directory or self.res_dir
self.load_dialog_files(root_directory)
self.load_vocab_files(root_directory)
self.load_regex_files(root_directory)
def load_vocab_files(self, root_directory: Optional[str] = None):
""" Load vocab files found under skill's root directory."""
root_directory = root_directory or self.res_dir
for lang in self.native_langs:
resources = self.load_lang(root_directory, lang)
if resources.types.vocabulary.base_directory is None:
self.log.debug(f'No vocab loaded for {lang}')
else:
skill_vocabulary = resources.load_skill_vocabulary(
self.alphanumeric_skill_id
)
# For each found intent register the default along with any aliases
for vocab_type in skill_vocabulary:
for line in skill_vocabulary[vocab_type]:
entity = line[0]
aliases = line[1:]
self.intent_service.register_adapt_keyword(
vocab_type, entity, aliases, lang)
def load_regex_files(self, root_directory=None):
""" Load regex files found under the skill directory."""
root_directory = root_directory or self.res_dir
for lang in self.native_langs:
resources = self.load_lang(root_directory, lang)
if resources.types.regex.base_directory is not None:
regexes = resources.load_skill_regex(self.alphanumeric_skill_id)
for regex in regexes:
self.intent_service.register_adapt_regex(regex, lang)
def find_resource(self, res_name: str, res_dirname: Optional[str] = None,
lang: Optional[str] = None):
"""
Find a resource file.
Searches for the given filename using this scheme:
1. Search the resource lang directory:
<skill>/<res_dirname>/<lang>/<res_name>
2. Search the resource directory:
<skill>/<res_dirname>/<res_name>
3. Search the locale lang directory or other subdirectory:
<skill>/locale/<lang>/<res_name> or
<skill>/locale/<lang>/.../<res_name>
Args:
res_name (string): The resource name to be found
res_dirname (string, optional): A skill resource directory, such
'dialog', 'vocab', 'regex' or 'ui'.
Defaults to None.
lang (string, optional): language folder to be used.
Defaults to self.lang.
Returns:
string: The full path to the resource file or None if not found
"""
lang = standardize_lang_tag(lang or self.lang)
x = find_resource(res_name, self.res_dir, res_dirname, lang)
if x:
return str(x)
self.log.error(f"Skill {self.skill_id} resource '{res_name}' for lang "
f"'{lang}' not found in skill")
# skill object setup
def _handle_first_run(self):
"""
The very first time a skill is run, speak a provided intro_message.
"""
intro = self.get_intro_message()
if intro:
# supports .dialog files for easy localization
# when .dialog does not exist, the text is spoken
# it is backwards compatible
self.speak_dialog(intro)
def _check_for_first_run(self):
"""
Determine if this is the very first time a skill is run by looking for
`__mycroft_skill_firstrun` in skill settings.
"""
first_run = self.settings.get("__mycroft_skill_firstrun", True)
if first_run:
self.log.info("First run of " + self.skill_id)
self._handle_first_run()
self.settings["__mycroft_skill_firstrun"] = False
self.settings.store()
def on_ready_status(self):
LOG.info(f'{self.skill_id} is ready.')
def on_error_status(self, e='Unknown'):
LOG.exception(f'{self.skill_id} initialization failed')
def on_stopping_status(self):
LOG.info(f'{self.skill_id} is shutting down...')
def on_alive_status(self):
LOG.debug(f'{self.skill_id} is alive.')
def on_started_status(self):
LOG.debug(f'{self.skill_id} started.')
def _startup(self, bus: MessageBusClient, skill_id: str = ""):
"""
Startup the skill. Connects the skill to the messagebus, loads resources
and finally calls the skill's "intialize" method.
@param bus: MessageBusClient to bind to skill
@param skill_id: Unique skill identifier, defaults to skill path for
legacy skills and python entrypoints for modern skills
"""
if self.is_fully_initialized:
self.log.warning(f"Tried to initialize {self.skill_id} multiple "
f"times, ignoring")
return
callbacks = StatusCallbackMap(on_ready=self.on_ready_status,
on_error=self.on_error_status,
on_stopping=self.on_stopping_status,
on_alive=self.on_alive_status,
on_started=self.on_started_status)
# NOTE: this method is called by SkillLoader
# it is private to make it clear to skill devs they should not touch it
try:
# set the skill_id
self.skill_id = skill_id or basename(self.root_dir)
self.intent_service.set_id(self.skill_id)
self.event_scheduler.set_id(self.skill_id)
self.enclosure.set_id(self.skill_id)
# initialize anything that depends on skill_id
self.log = LOG.create_logger(self.skill_id)
self._init_settings()
# initialize anything that depends on the messagebus
self.bind(bus)
self.status = ProcessStatus(self.skill_id, self.bus, callback_map=callbacks)
self.status.set_alive()
if not self.gui:
self._init_skill_gui()
self.load_data_files()
self._register_skill_json()
self._register_decorated()
self._register_app_launcher()
self.register_resting_screen()
self.status.set_started()
# run skill developer initialization code
self.initialize()
self._check_for_first_run()
self._init_event.set()
self.status.set_ready()
except Exception as e:
self.status.set_error(str(e))
# If an exception occurs, attempt to clean up the skill
try:
self.default_shutdown()
except Exception as e2:
LOG.debug(e2)
raise e
def _register_skill_json(self, root_directory: Optional[str] = None):
"""Load skill.json metadata found under locale folder and register with homescreen"""
root_directory = root_directory or self.res_dir
for lang in self.native_langs:
resources = self.load_lang(root_directory, lang)
if resources.types.json.base_directory is None:
self.log.debug(f'No skill.json loaded for {lang}')
else:
skill_meta = resources.load_json_file("skill.json")
utts = skill_meta.get("examples", [])
if utts:
self.log.info(f"Registering example utterances with homescreen for lang: {lang} - {utts}")
self.bus.emit(Message("homescreen.register.examples",
{"skill_id": self.skill_id, "utterances": utts, "lang": lang}))
def _register_app_launcher(self):
# register app launcher if registered via decorator
for attr_name in get_non_properties(self):
method = getattr(self, attr_name)
if hasattr(method, 'homescreen_app_icon'):
name = getattr(method, 'homescreen_app_name')
event = f"{self.skill_id}.{name or method.__name__}.homescreen.app"
icon = getattr(method, 'homescreen_app_icon')
name = name or self.__skill_id2name
LOG.debug(f"homescreen app registered: {name} - '{event}'")
self.register_homescreen_app(icon=icon,
name=name or self.skill_id,
event=event)
self.add_event(event, method, speak_errors=False)
@property
def __skill_id2name(self) -> str:
"""helper to make a nice string out of a skill_id"""
return (self.skill_id.split(".")[0].replace("_", " ").
replace("-", " ").replace("skill", "").title().strip())
def _init_settings(self):
"""
Set up skill settings. Defines settings in the specified file path,
handles any settings passed to skill init, and starts watching the
settings file for changes.
"""
self.log.debug(f"initializing skill settings for {self.skill_id}")
# NOTE: lock is disabled due to usage of deepcopy and to allow json
# serialization
self._settings = JsonStorage(self.settings_path, disable_lock=True)
with self._settings_lock:
if self._initial_settings and not self.is_fully_initialized:
self.log.warning("Copying default settings values defined in "
"__init__ \nto correct this add kwargs "
"__init__(bus=None, skill_id='') "
f"to skill class {self.__class__.__name__}")
for k, v in self._initial_settings.items():
if k not in self._settings:
self._settings[k] = v
self._initial_settings = copy(self.settings)
# starting on ovos-core 0.0.8 a bus event is emitted
# all settings.json files are monitored for changes in ovos-core
self.add_event("ovos.skills.settings_changed", self._handle_settings_changed, speak_errors=False)
if self._monitor_own_settings:
self._start_filewatcher()
@property
def _monitor_own_settings(self):
# account for isolated setups where skills might not share a filesystem with core
return self.settings.get("monitor_own_settings", False)
def _handle_settings_changed(self, message):
"""external signal to reload skill settings"""
skill_id = message.data.get("skill_id", "")
if skill_id == self.skill_id:
self._handle_settings_file_change(self._settings.path)
def _init_skill_gui(self):
"""
Set up the SkillGUI for this skill and connect relevant bus events.
"""
self.gui = SkillGUI(self)
self.gui.setup_default_handlers()
def register_homescreen_app(self, icon: str, name: str, event: str):
"""the icon file MUST be located under 'gui' subfolder"""
# this path is hardcoded in ovos_gui.constants and follows XDG spec
# we use it to ensure resource availability between containers
# it is the only path assured to be accessible both by skills and GUI
GUI_CACHE_PATH = get_xdg_cache_save_path('ovos_gui')
full_icon_path = f"{self.res_dir}/gui/{icon}"
if not os.path.isfile(full_icon_path):
self.log.error(f"failed to register homescreen app, icon does not exist: {full_icon_path}")
return
os.makedirs(f"{GUI_CACHE_PATH}/{self.skill_id}", exist_ok=True)
shared_path = f"{GUI_CACHE_PATH}/{self.skill_id}/{icon}"
shutil.copy(full_icon_path, shared_path)
self.bus.emit(Message("homescreen.register.app",
{"skill_id": self.skill_id,
"icon": shared_path,
"name": name,
"event": event}))
def register_resting_screen(self):
"""
Registers resting screen from the resting_screen_handler decorator.
This only allows one screen and if two is registered only one
will be used.
"""
for attr_name in get_non_properties(self):
handler = getattr(self, attr_name)
if hasattr(handler, 'resting_handler'):
resting_name = handler.resting_handler
LOG.debug(f"{get_handler_name(handler)} is a resting screen, name: {resting_name}")
def register(message=None, name=resting_name):
self.log.info(f'Registering resting screen {name} for {self.skill_id}.')
self.bus.emit(Message("homescreen.manager.add",
{"name": name, "id": self.skill_id}))
register() # initial registering
self.add_event("homescreen.manager.reload.list", register, speak_errors=False)
def wrapper(message, cb=handler):
if message.data["homescreen_id"] == self.skill_id:
LOG.debug(f"triggering resting_handler: {get_handler_name(cb)}")
cb(message)
self.add_event("homescreen.manager.activate.display", wrapper, speak_errors=False)
def shutdown_handler(message):
if message.data["id"] == self.skill_id:
msg = message.forward("homescreen.manager.remove",
{"id": self.skill_id})
self.bus.emit(msg)
self.add_event("mycroft.skills.shutdown", shutdown_handler, speak_errors=False)
break # TODO - if multiple decorators are used what do? this is not deterministic
def _start_filewatcher(self):
"""
Start watching settings for file changes if settings file exists and
there isn't already a FileWatcher watching it
"""
if self._settings_watchdog is None and isfile(self._settings.path):
self._settings_watchdog = \
FileWatcher([self._settings.path],
callback=self._handle_settings_file_change,
ignore_creation=True)
def _register_decorated(self):
"""
Register all intent handlers that are decorated with an intent.
Looks for all functions that have been marked by a decorator
and read the intent data from them. The intent handlers aren't the
only decorators used. Skip properties as calling getattr on them
executes the code which may have unintended side effects
"""
for attr_name in get_non_properties(self):
method = getattr(self, attr_name)
if hasattr(method, 'intents'):
for intent in getattr(method, 'intents'):
voc_blacklist = method.voc_blacklist if hasattr(method, 'voc_blacklist') else []
self.register_intent(intent, method, voc_blacklist=voc_blacklist)
if hasattr(method, 'intent_files'):
for intent_file in getattr(method, 'intent_files'):
self.register_intent_file(intent_file, method)
if hasattr(method, 'intent_layers'):
for layer_name, intent_files in \
getattr(method, 'intent_layers').items():
self.register_intent_layer(layer_name, intent_files)
# TODO support for multiple common query handlers (?)
if hasattr(method, 'common_query'):
self._cq_handler = method
self._cq_callback = method.cq_callback
LOG.debug(f"Registering common query handler for: {self.skill_id} - callback: {self._cq_callback}")
self.__handle_common_query_ping(Message("ovos.common_query.ping"))
def bind(self, bus: MessageBusClient):
"""
Register MessageBusClient with skill.
@param bus: MessageBusClient to bind to skill and internal objects
"""
if bus:
self._bus = bus
self.events.set_bus(bus)
self.intent_service.set_bus(bus)
self.event_scheduler.set_bus(bus)
self._enclosure.set_bus(bus)
self._register_system_event_handlers()
self._register_public_api()
self.intent_layers.bind(self)
self.audio_service = OCPInterface(self.bus)
self.private_settings = PrivateSettings(self.skill_id)
def __handle_common_query_ping(self, message):
if self._cq_handler:
# announce skill to common query pipeline
self.bus.emit(message.reply("ovos.common_query.pong",
{"skill_id": self.skill_id, "is_classic_cq": False},
{"skill_id": self.skill_id}))
def __handle_query_action(self, message: Message):
"""
If this skill's response was spoken to the user, this method is called.
@param message: `question:action` message
"""
# backwards compat, for older common query pipeline versions
if not self._cq_callback or message.data["skill_id"] != self.skill_id:
# Not for this skill!
return
# call the correct handler as if cq was updated
message.msg_type += f".{self.skill_id}"
self.bus.emit(message)
def __handle_skill_query_action(self, message: Message):
LOG.debug(f"common query callback for: {self.skill_id}")
lang = get_message_lang(message)
answer = message.data.get("answer") or message.data.get("callback_data", {}).get("answer")
self.speak(answer)
if not self._cq_callback:
LOG.debug(f"no common query callback registered for: {self.skill_id}")
return # nothing to do
# Inspect the callback signature
callback_signature = signature(self._cq_callback)
params = callback_signature.parameters
# Check if the first parameter is 'self' (indicating it's an instance method)
if len(params) > 0 and list(params.keys())[0] == 'self':
# Instance method: pass 'self' as the first argument
self._cq_callback(self, message.data["phrase"], answer, lang)
else:
# Static method or function: don't pass 'self'
self._cq_callback(message.data["phrase"], answer, lang)
def __handle_question_query(self, message: Message):
"""
Handle an incoming question query.
@param message: Message with matched query 'phrase'
"""
if not self._cq_handler:
return
lang = get_message_lang(message)
search_phrase = message.data["phrase"]
message.context["skill_id"] = self.skill_id
LOG.debug(f"Common QA: {self.skill_id}")
# First, notify the requestor that we are attempting to handle
# (this extends a timeout while this skill looks for a match)
self.bus.emit(message.response({"phrase": search_phrase,
"skill_id": self.skill_id,
"searching": True}))
answer = None