-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy path__init__.py
315 lines (265 loc) · 10.9 KB
/
__init__.py
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
# Copyright 2017, Mycroft AI Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import hashlib
import os
import random
from adapt.intent import IntentBuilder
from glob import glob
from os.path import isfile, expanduser, isdir, join
from requests import HTTPError
from shutil import rmtree
import xdg.BaseDirectory
from mycroft.api import DeviceApi
from mycroft.configuration import LocalConf
from mycroft.configuration.locations import (
DEFAULT_CONFIG,
OLD_USER_CONFIG,
SYSTEM_CONFIG,
USER_CONFIG
)
from mycroft.messagebus.message import Message
from mycroft import MycroftSkill, intent_handler
from .file_watch import FileWatcher
def on_error_speak_dialog(dialog_file):
def decorator(function):
def wrapper(self, message):
try:
try:
function(self, message)
except TypeError:
function(self)
except Exception:
self.log.exception('In safe wrapped function')
self.speak_dialog(dialog_file)
return wrapper
return decorator
def get_possible_config_files():
found_configs = set()
# XDG Locations
for conf_dir in xdg.BaseDirectory.load_config_paths('mycroft'):
config_file = join(conf_dir, 'mycroft.conf')
if isfile(config_file):
found_configs.add(config_file)
hardcoded_configs = (
DEFAULT_CONFIG,
OLD_USER_CONFIG,
SYSTEM_CONFIG,
USER_CONFIG
)
for config in hardcoded_configs:
if isfile(config):
found_configs.add(config)
return found_configs
class ConfigurationSkill(MycroftSkill):
PRECISE_DEV_DIST_URL = "https://github.com/MycroftAI/precise-data/" \
"raw/dist/{arch}/latest"
PRECISE_DEV_MODEL_URL = "https://raw.githubusercontent.com/MycroftAI/" \
"precise-data/models-dev/{wake_word}.tar.gz"
def __init__(self):
super().__init__("ConfigurationSkill")
self.api = DeviceApi()
self.config_hash = ''
self.model_file = expanduser('~/.mycroft/precise/hey-mycroft.pb')
self.settings["max_delay"] = 60
self.config_watcher = FileWatcher(
get_possible_config_files(),
self.config_changed_callback
)
def initialize(self):
self.schedule_repeating_event(self.update_remote, None, 60,
'UpdateRemote')
def config_changed_callback(self, path):
"""Handler for updated configurations on disk.
Args:
path (str): file that triggered the update
"""
self.log.info(f"{path} changed on disk, update config!")
config = LocalConf(path)
self.bus.emit(Message("configuration.updated", config))
@intent_handler(IntentBuilder('').require('Query').require('Name'))
def handle_query_name(self, message):
device = DeviceApi().get()
self.speak_dialog("my.name.is", data={"name": device["name"]})
@intent_handler("EnablePreciseDev.intent")
@on_error_speak_dialog('must.update')
def handle_use_precise_dev(self, message):
from mycroft.configuration.config import (
LocalConf, USER_CONFIG, Configuration
)
wake_word = Configuration.get()['listener']['wake_word']
new_config = {
'precise': {
"dist_url": self.PRECISE_DEV_DIST_URL,
"model_url": self.PRECISE_DEV_MODEL_URL
},
'hotwords': {wake_word: {'module': 'precise', 'sensitivity': 0.5}}
}
user_config = LocalConf(USER_CONFIG)
user_config.merge(new_config)
user_config.store()
self.bus.emit(Message('configuration.updated'))
self.speak_dialog('precise.devmode.enabled')
@intent_handler("DisablePreciseDev.intent")
@on_error_speak_dialog('must.update')
def handle_disable_precise_dev(self, message):
from mycroft.configuration.config import (
LocalConf, USER_CONFIG
)
for item in glob(expanduser('~/.mycroft/precise/precise-engine*')):
self.log.info('Removing: {}...'.format(item))
if isdir(item):
rmtree(item)
else:
os.remove(item)
local_conf = LocalConf(USER_CONFIG)
pconfig = local_conf.get('precise', {})
if pconfig.get('dist_url') == self.PRECISE_DEV_DIST_URL:
del pconfig['dist_url']
if pconfig.get('model_url') == self.PRECISE_DEV_MODEL_URL:
del pconfig['model_url']
local_conf.store()
self.bus.emit(Message('configuration.updated'))
self.speak_dialog('precise.devmode.disabled')
@intent_handler("WhereAreYou.intent")
def handle_where_are_you(self, message):
from mycroft.configuration.config import Configuration
config = Configuration.get()
data = {
"city": config["location"]["city"]["name"],
"state": config["location"]["city"]["state"]["name"],
"country": config["location"]["city"]["state"]["country"]["name"]
}
self.speak_dialog("i.am.at", data)
def get_listener(self):
"""Raises ImportError or KeyError if not supported"""
from mycroft.configuration.config import Configuration
wake_word = Configuration.get()['listener']['wake_word']
ww_config = Configuration.get()['hotwords'].get(wake_word, {})
return ww_config.get('module', 'pocketsphinx')
@intent_handler(IntentBuilder('SetListenerIntent').
require('Set').
require('Listener').
require('ListenerType'))
@on_error_speak_dialog('must.update')
def handle_set_listener(self, message):
from mycroft.configuration.config import (
LocalConf, USER_CONFIG, Configuration
)
module = message.data['ListenerType'].replace(' ', '')
module = module.replace('default', 'precise')
name = module.replace('pocketsphinx', 'pocket sphinx')
if self.get_listener() == module:
self.speak_dialog('listener.same', data={'listener': name})
return
wake_word = Configuration.get()['listener']['wake_word']
new_config = {
'hotwords': {wake_word: {'module': module}}
}
user_config = LocalConf(USER_CONFIG)
user_config.merge(new_config)
user_config.store()
self.bus.emit(Message('configuration.updated'))
if module == 'precise':
engine_folder = expanduser('~/.mycroft/precise/precise-engine')
if not isdir(engine_folder):
self.speak_dialog('download.started')
return
self.speak_dialog('set.listener', data={'listener': name})
@intent_handler(IntentBuilder('UpdatePrecise').
require('Update').
require('Precise'))
@on_error_speak_dialog('must.update')
def handle_update_precise(self):
if self.get_listener() != 'precise':
self.speak_dialog('not.precise')
if isfile(self.model_file):
os.remove(self.model_file)
new_conf = {'config': {'rand_val': random.random()}}
self.bus.emit(Message('configuration.patch', new_conf))
self.bus.emit(Message('configuration.updated'))
self.speak_dialog('models.updated')
else:
self.speak_dialog('models.not.found')
@intent_handler(IntentBuilder('WhatPreciseModel').
require('Query').
require('Precise').
require('Using'))
@on_error_speak_dialog('must.update')
def handle_what_precise_model(self):
if self.get_listener() != 'precise':
self.speak_dialog('not.precise')
if isfile(self.model_file):
with open(self.model_file, 'rb') as f:
model_hash = hashlib.md5(f.read()).hexdigest()
from humanhash import humanize
model_name = humanize(model_hash, separator=' ')
self.speak_dialog('model.is', {'name': model_name})
@intent_handler(IntentBuilder('GetListenerIntent').
require('Query').
require('Listener'))
@on_error_speak_dialog('must.update')
def handle_get_listener(self):
module = self.get_listener()
name = module.replace('pocketsphinx', 'pocket sphinx')
self.speak_dialog('get.listener', data={'listener': name})
@intent_handler(IntentBuilder('UpdateConfigurationIntent').
require('Update').
require('Config'))
def handle_update_intent(self, message):
try:
self.bus.emit(Message('mycroft.skills.settings.update'))
if self.update():
self.speak_dialog("config.updated")
else:
self.speak_dialog("config.no_change")
except HTTPError as e:
self.__api_error(e)
def update_remote(self, message):
""" Handler for scheduled remote configuration update.
Updates configuration and handles exceptions.
"""
try:
if self.update():
self.log.info('Remote configuration updated')
except Exception as e:
if isinstance(e, HTTPError) and e.response.status_code == 401:
self.log.warn("Impossible to update configuration because "
"device isn't paired")
else:
self.log.warn("Failed to update settings, will retry later")
def update(self):
""" Update remote configuration.
Reads remote configuration from the Mycroft backend and trigger
an update event if a change has occured.
"""
config = self.api.get_settings() or {}
location = self.api.get_location()
if location:
config["location"] = location
if self.config_hash != hash(str(config)):
self.bus.emit(Message("configuration.updated", config))
self.config_hash = hash(str(config))
return True
else:
return False
def __api_error(self, e):
if e.response.status_code == 401:
self.speak_dialog('config.not.paired.dialog')
def get_times(self):
return [self.get_utc_time() + self.settings["max_delay"]]
def shutdown(self):
self.cancel_scheduled_event('UpdateRemote')
self.config_watcher.shutdown()
def create_skill():
return ConfigurationSkill()