This repository has been archived by the owner on Dec 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHandlerChain.py
84 lines (74 loc) · 2.52 KB
/
HandlerChain.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
# coding: utf-8
'''
Created on 2013-7-3
handler chain
@author: xuechong
'''
from moehandlers.FlowerHandler import FlowerHandler
from moehandlers.SellMoeHandler import SellMoeHandler
from moehandlers.HelpHandler import HelpHandler
from moehandlers.AnimeListHandler import AnimeListHandler
from Weixin import textReply
import logging
from moehandlers.SearchHandler import SearchHandler
__default_chain__ = (SellMoeHandler,SearchHandler,FlowerHandler,HelpHandler,AnimeListHandler)
__text_chain__=(SellMoeHandler,SearchHandler,FlowerHandler,HelpHandler,AnimeListHandler)
def textHandlerChain(userMsg):
"""
return a new instance of a default text msg handler chain
"""
return HandlerChain(userMsg,list(__text_chain__))
class HandlerChain(object):
"""
the handler chain
call doChain to get a reply xml str
"""
handlers = list()
userMsg = None
def __init__(self,userMsg,handlerList=None):
self.handlers = handlerList
if self.handlers==None:
self.handlers=list(__default_chain__)
logging.debug("new handlerChain" + str(self.handlers))
self.userMsg = userMsg
def doChain(self):
"""
invoke handlers and return reply xmlstr
"""
try :
result = self.invokeNext()
return result==None and textReply(self.userMsg) or result
except Exception as e:
logging.exception(str(e))
return textReply(self.userMsg,"555更新姬被玩坏了啦><")
def invokeNext(self):
"""
invoke next handler until there is no handler left or have result
returns None if no handler can hand the income msg
"""
result = None
if len(self.handlers)>0:
handler = self.handlers.pop()()
result = handler.handle(self)
if result==None and len(self.handlers)>0:
result = self.invokeNext()
return result
def getMsgType(self):
"""
get the msgtype of the income msg
"""
return self.userMsg.get("MsgType")
def getMsgContent(self):
"""
get the content of the income msg
"""
return self.userMsg.get("Content").encode("utf-8").strip()
def forceStop(self):
"""
stop the handler chain
after call this ,if you can return a None to reply user a default msg
and the rest of the handlers will not be invoked
*return a None*
"""
self.handlers = list()
return None