Procházet zdrojové kódy

rename base service

Martin před 10 roky
rodič
revize
ccd7b83c21

+ 6 - 4
voice_control/services/base.py

@@ -1,7 +1,9 @@
-class BaseFunction(object):
+class BaseService(object):
     initialized = True
     
     def init(self):
-        pass
-    def preferred_phrases(self):
-        return []
+        if not self.initialized:
+            raise NotImplementedError()
+
+    def call(self):
+        raise NotImplementedError()

+ 3 - 5
voice_control/services/email_mailgun.py

@@ -3,6 +3,7 @@ import requests.exceptions
 import base
 import input_sources
 import output
+import base
 
 _RECIPIENTS = {
     'martin': 'martin@gerner.co',
@@ -28,10 +29,7 @@ def _send_by_sendgrid(recipient, subject, email_body):
     if resp.status_code != 200:
         resp.raise_for_status()
 
-class Email(base.BaseFunction):
-    def preferred_phrases(self):
-        return _RECIPIENTS.keys()
-
+class Email(base.BaseService):
     def _send(self, recipient):
         output.debug('email _send: %s' % recipient)
         tokens, terminator = input_sources.get_until_terminated()
@@ -49,7 +47,7 @@ class Email(base.BaseFunction):
 
     def call(self):
         output.text('Specify recipient')
-        recipient = input_sources.get_token(preferred_phrases=self.preferred_phrases())
+        recipient = input_sources.get_token(preferred_phrases=_RECIPIENTS.keys())
         if recipient in _RECIPIENTS:
             self._send(recipient)
         else:

+ 5 - 10
voice_control/services/music.py

@@ -5,7 +5,7 @@ import output
 
 _FOLDER = '/mnt/nas/music/albums'
 
-class Music(base.BaseFunction):
+class Music(base.BaseService):
     folders = None
     current_process = None
 
@@ -27,7 +27,7 @@ class Music(base.BaseFunction):
             t = t.lower()
             eligible = {f: v for f, v in eligible.iteritems() if t in f}
         
-        logging.info('tokens: %s; eligible: %s', tokens, eligible.keys())
+        output.debug('tokens: %s; eligible: %s', tokens, eligible.keys())
         if eligible:
             return eligible[eligible.keys()[0]]
 
@@ -36,14 +36,9 @@ class Music(base.BaseFunction):
             self.current_process.terminate()
             self.current_process = None
 
-    def call(self, additional_tokens=None, **_):
-        f = None
-        if additional_tokens:
-            f = self._search(additional_tokens)
-        if not f:
-            output.debug('ready')
-            tokens = ears.listen(pause_threshold=3, preferred_phrases=self.folders.keys()[:300])
-            f = self._search(tokens)
+    def call(self):
+        tokens = ears.listen(pause_threshold=3, preferred_phrases=self.folders.keys()[:300])
+        f = self._search(tokens)
 
         if f:
             output.debug('playing %s' % f)

+ 2 - 4
voice_control/services/todo_wunderlist.py

@@ -1,8 +1,7 @@
-import logging
-import base
 import requests
 import input_sources
 import output
+import base
 
 _LISTEN_TIMEOUT = 600
 _GROCERIES = ['milk', 'bread']
@@ -39,8 +38,7 @@ class _WunderlistClient(object):
         lists_res = self.call('GET', 'https://a.wunderlist.com/api/v1/lists')
         self.lists = {x['title'].lower(): x['id'] for x in lists_res}
     
-class Wunderlist(base.BaseFunction):
-    client = None
+class Wunderlist(base.BaseService):
 
     def __init__(self, multiple_tasks, list_name=None):
         self.client = _WunderlistClient(_WUNDERLIST_CLIENT_ID, _WUNDERLIST_ACCESS_TOKEN)