From affdcd8210e69b82d848b576789d3bee2c9a1adb Mon Sep 17 00:00:00 2001 From: Joseph Pan Date: Fri, 7 Jul 2017 23:15:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8F=92=E4=BB=B6=E7=9A=84en?= =?UTF-8?q?able=E5=BC=80=E5=85=B3=EF=BC=8C=E6=94=AF=E6=8C=81=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E5=BC=80=E5=85=B3=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/brain.py | 16 ++++++++++++++- client/plugins/Camera.py | 41 ++++++++++++++++++++------------------- client/plugins/Echo.py | 2 +- client/plugins/Email.py | 9 +++++---- client/plugins/SendQR.py | 1 + client/plugins/Time.py | 1 + client/plugins/Unclear.py | 1 - 7 files changed, 44 insertions(+), 27 deletions(-) diff --git a/client/brain.py b/client/brain.py index 9a70e6d..fc4b3b8 100644 --- a/client/brain.py +++ b/client/brain.py @@ -62,6 +62,20 @@ def get_plugins(cls): else 0, reverse=True) return (plugins, exclude_plugins) + def isEnabled(self, plugin): + """ + whether a plugin is enabled. + """ + if plugin is None: + return False + if not hasattr(plugin, 'SLUG'): + return True + slug = plugin.SLUG + if slug in self.profile and 'enable' in self.profile[slug]: + return self.profile[slug]['enable'] + else: + return True + def query(self, texts, wxbot=None, thirdparty_call=False): """ Passes user input to the appropriate plugin, testing it against @@ -83,7 +97,7 @@ def query(self, texts, wxbot=None, thirdparty_call=False): for plugin in self.plugins: for text in texts: - if plugin.isValid(text): + if plugin.isValid(text) and self.isEnabled(plugin): self._logger.debug("'%s' is a valid phrase for plugin " + "'%s'", text, plugin.__name__) try: diff --git a/client/plugins/Camera.py b/client/plugins/Camera.py index 4ab0507..3a42460 100644 --- a/client/plugins/Camera.py +++ b/client/plugins/Camera.py @@ -5,7 +5,8 @@ import time import sys -WORDS = [u"ECHO", u"CHUANHUA"] +WORDS = [u"PAIZHAO", u"ZHAOPIAN"] +SLUG = "camera" def handle(text, mic, profile, wxbot=None): @@ -30,28 +31,28 @@ def handle(text, mic, profile, wxbot=None): send_to_user = True sound = True # read config - if profile['camera'] and 'enable' in profile['camera'] and \ - profile['camera']['enable']: - if 'count_down' in profile['camera'] and \ - profile['camera']['count_down'] > 0: - count_down = profile['camera']['count_down'] - if 'quality' in profile['camera'] and \ - profile['camera']['quality'] > 0: - quality = profile['camera']['quality'] - if 'dest_path' in profile['camera'] and \ - profile['camera']['dest_path'] != '': - dest_path = profile['camera']['dest_path'] - if 'vertical_flip' in profile['camera'] and \ - profile['camera']['vertical_flip']: + if profile[SLUG] and 'enable' in profile[SLUG] and \ + profile[SLUG]['enable']: + if 'count_down' in profile[SLUG] and \ + profile[SLUG]['count_down'] > 0: + count_down = profile[SLUG]['count_down'] + if 'quality' in profile[SLUG] and \ + profile[SLUG]['quality'] > 0: + quality = profile[SLUG]['quality'] + if 'dest_path' in profile[SLUG] and \ + profile[SLUG]['dest_path'] != '': + dest_path = profile[SLUG]['dest_path'] + if 'vertical_flip' in profile[SLUG] and \ + profile[SLUG]['vertical_flip']: vertical_flip = True - if 'horizontal_flip' in profile['camera'] and \ - profile['camera']['horizontal_flip']: + if 'horizontal_flip' in profile[SLUG] and \ + profile[SLUG]['horizontal_flip']: horizontal_flip = True - if 'send_to_user' in profile['camera'] and \ - not profile['camera']['send_to_user']: + if 'send_to_user' in profile[SLUG] and \ + not profile[SLUG]['send_to_user']: send_to_user = False - if 'sound' in profile['camera'] and \ - not profile['camera']['sound']: + if 'sound' in profile[SLUG] and \ + not profile[SLUG]['sound']: sound = False if any(word in text for word in [u"安静", u"偷偷", u"悄悄"]): sound = False diff --git a/client/plugins/Echo.py b/client/plugins/Echo.py index 85572ab..ef870c0 100644 --- a/client/plugins/Echo.py +++ b/client/plugins/Echo.py @@ -1,7 +1,7 @@ # -*- coding: utf-8-*- WORDS = [u"ECHO", u"CHUANHUA"] - +SLUG = "echo" PRIORITY = 0 diff --git a/client/plugins/Email.py b/client/plugins/Email.py index 20ec211..250948e 100644 --- a/client/plugins/Email.py +++ b/client/plugins/Email.py @@ -6,6 +6,7 @@ from dateutil import parser WORDS = ["EMAIL", "INBOX"] +SLUG = "email" # 字符编码转换方法 @@ -44,7 +45,7 @@ def isSelfEmail(msg, profile): """ Whether the email is sent by the user """ fromstr = msg["From"] addr = (fromstr[fromstr.find('<')+1:fromstr.find('>')]).strip('\"') - address = profile['email']['address'].strip() + address = profile[SLUG]['address'].strip() return addr == address @@ -134,10 +135,10 @@ def fetchUnreadEmails(profile, since=None, markRead=False, limit=None): A list of unread email objects. """ - conn = imaplib.IMAP4(profile['email']['imap_server'], - profile['email']['imap_port']) + conn = imaplib.IMAP4(profile[SLUG]['imap_server'], + profile[SLUG]['imap_port']) conn.debug = 0 - conn.login(profile['email']['address'], profile['email']['password']) + conn.login(profile[SLUG]['address'], profile[SLUG]['password']) conn.select(readonly=(not markRead)) msgs = [] diff --git a/client/plugins/SendQR.py b/client/plugins/SendQR.py index 295b586..e929905 100644 --- a/client/plugins/SendQR.py +++ b/client/plugins/SendQR.py @@ -4,6 +4,7 @@ import sys WORDS = [u"ERWEIMA"] +SLUG = "sendqr" def handle(text, mic, profile, wxbot=None): diff --git a/client/plugins/Time.py b/client/plugins/Time.py index ba25ab4..b89a74b 100644 --- a/client/plugins/Time.py +++ b/client/plugins/Time.py @@ -4,6 +4,7 @@ from semantic.dates import DateService WORDS = [u"TIME", u"SHIJIAN", u"JIDIAN"] +SLUG = "time" def handle(text, mic, profile, wxbot=None): diff --git a/client/plugins/Unclear.py b/client/plugins/Unclear.py index ddc4cf8..812006a 100644 --- a/client/plugins/Unclear.py +++ b/client/plugins/Unclear.py @@ -4,7 +4,6 @@ from client.robot import get_robot_by_slug WORDS = [] - PRIORITY = -(maxint + 1)