Skip to content
This repository has been archived by the owner on Apr 13, 2018. It is now read-only.

Commit

Permalink
增加插件的enable开关,支持动态开关插件
Browse files Browse the repository at this point in the history
  • Loading branch information
wzpan committed Jul 7, 2017
1 parent 8a7376d commit affdcd8
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 27 deletions.
16 changes: 15 additions & 1 deletion client/brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
41 changes: 21 additions & 20 deletions client/plugins/Camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion client/plugins/Echo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8-*-

WORDS = [u"ECHO", u"CHUANHUA"]

SLUG = "echo"
PRIORITY = 0


Expand Down
9 changes: 5 additions & 4 deletions client/plugins/Email.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from dateutil import parser

WORDS = ["EMAIL", "INBOX"]
SLUG = "email"


# 字符编码转换方法
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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 = []
Expand Down
1 change: 1 addition & 0 deletions client/plugins/SendQR.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys

WORDS = [u"ERWEIMA"]
SLUG = "sendqr"


def handle(text, mic, profile, wxbot=None):
Expand Down
1 change: 1 addition & 0 deletions client/plugins/Time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 0 additions & 1 deletion client/plugins/Unclear.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from client.robot import get_robot_by_slug

WORDS = []

PRIORITY = -(maxint + 1)


Expand Down

0 comments on commit affdcd8

Please sign in to comment.