📄 __init__.py
字号:
## Copyright (c) 2005, John Mettraux, OpenWFE.org# All rights reserved.# # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met:# # . Redistributions of source code must retain the above copyright notice, this# list of conditions and the following disclaimer. # # . Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution.# # . Neither the name of the "OpenWFE" nor the names of its contributors may be# used to endorse or promote products derived from this software without# specific prior written permission.# # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE.## $Id: __init__.py 2676 2006-05-27 11:08:21Z jmettraux $#""" All the pyya APRE implementation lies here APRE = Automatic Participant Runtime Environment $Id: __init__.py 2676 2006-05-27 11:08:21Z jmettraux $"""import re, sysfrom os.path import joinfrom threading import Threadfrom xml.dom.minidom import parsefrom openwfe.utils import printLogMethodfrom openwfe.applic import Servicefrom openwfe.applic import BasicServicefrom openwfe.applic import instantiateClassfrom openwfe.socket import WorkitemConsumerfrom openwfe.xmlutils import fetchParams, extractText, getChildrenByTagName#from openwfe.engine import definitionsfrom openwfe.engine.participant import lookupParticipantMapfrom openwfe.workitem import InFlowWorkitemAGENT_RESULT = '__agent_result__'## AGENT""" This class is an extension of openwfe.applic.BasicService which you can find at openwfe/applic/__init__.py"""class Agent (BasicService): def use (self, workitem): passdef setAgentResult (workitem, returnCode): workitem.setAttribute(AGENT_RESULT, returnCode)## ROUTERclass Entry: #router = None #agent = None #regexes = [] #def __init__ (self, router, matches, agent): def __init__ (self, router, agent): self.router = router #self.regexes = [] #self.router.ldebug("building entry for agent %s" % agent) #for sMatch in matches: # #self.router.ldebug("adding match '%s'" % sMatch) # self.regexes.append(re.compile(sMatch)) self.agent = agent self.regex = re.compile(agent.name) #for regex in self.regexes: # self.router.ldebug(" regex >> >%s<" % regex) def matches (self, participantName): #self.router.ldebug("matches() of entry for agent %s" % self.agent) #for regex in self.regexes: # #self.router.ldebug("does entry '%s' matches '%s' ? %s" % (regex, participantName, regex.match(participantName))) # if regex.match(participantName): return 1 #self.router.ldebug("no match for the entry") #return 0 return self.regex.match(participantName) def getAgent (self): if isinstance(self.agent, Agent): return self.agent (agentName, agentClassName, agentParams) = self.agent # just-in-time agent jitAgent = instantiateClass(agentClassName) jitAgent.init(agentName, self.router.applicationContext, agentParams) return jitAgent class Router (Service): def init (self, name, applicationContext, params): Service.init(self, name, applicationContext, params) self.entries = [] self._build_() def _build_ (self): pass def determineAgent (self, participantName): self.ldebug("determining agent for workitem addressed to participant '%s'" % participantName) for entry in self.entries: self.ldebug('examining entry for agent %s' % entry.agent) if entry.matches(participantName): return entry.getAgent() return None#def fetchMatches (elt):## result = []## for eMatch in getChildrenByTagName(elt, 'matches'):# result.append(extractText(eMatch))## #print "fetchMatches() returns ", result## return result""" An implementation of Router that fetches its info from an XML configuration file"""class XmlRouter (Router): NAME = 'name' REGEX = 'regex' PATH = 'path' AGENT = 'agent' CLASS = 'class' INSTANTIATE = 'instantiate' ONCE = 'once' AGENT_FILE = 'agentFile' def _build_ (self): agentFile = self.params.get(self.AGENT_FILE) if agentFile == None: agentFile = 'etc/agents.xml' if not agentFile.startswith\ (self.applicationContext.getApplicationDirectory()): agentFile = join\ (self.applicationContext.getApplicationDirectory(), agentFile) doc = parse(agentFile) rootElt = doc.documentElement for eAgent in getChildrenByTagName(rootElt, self.AGENT): agentName = eAgent.getAttribute(self.NAME) agentPath = eAgent.getAttribute(self.PATH) agentClass = eAgent.getAttribute(self.CLASS) sInstantiate = eAgent.getAttribute(self.INSTANTIATE) params = fetchParams(eAgent) #matches = fetchMatches(eAgent) if agentPath != None: sys.path.append(str(agentPath)) agent = None if sInstantiate == self.ONCE: agent = instantiateClass(agentClass) agent.init(agentName, self.applicationContext, params) else: agent = (agentName, agentClass, params) #self.entries.append(Entry(self, matches, agent)) self.entries.append(Entry(self, agent)) self.ldebug("XmlRouter _build_() added '%s' (%s)" % \ (agentName, agentClass)) self.ldebug("XmlRouter built. %i entries." % len(self.entries))## REACTOR CONSUMERdef lookupRouter (applicationContext): r = applicationContext.get('router') return r #if r: return r #return applicationContext.get('ReactorContext.router')class ApreConsumer (WorkitemConsumer): """ Handles incoming workitem. It should especially discard items that are not InFlowWorkitem """ def init (self, name, applicationContext, params): WorkitemConsumer.init(self, name, applicationContext, params) def use (self, workitem): self.ldebug('use()') if not isinstance(workitem, InFlowWorkitem): return (UsageThread(self.applicationContext, workitem, self.linfo)).start()class UsageThread (Thread): def __init__ (self, applicationContext, workitem, logMethod=printLogMethod): Thread.__init__(self) self.applicationContext = applicationContext self.workitem = workitem self.logMethod = logMethod self.logMethod("__init__() ok") def run (self): router = lookupRouter(self.applicationContext) agent = router.determineAgent(self.workitem.participantName) if agent: self.logMethod("routing to agent '%s'" % agent.name) try: agent.use(self.workitem) # ensure that __agent_result__ is set if not self.workitem.getAttribute(AGENT_RESULT): setAgentResult(self.workitem, 0) except: # # intercepting any remaining problem # setAgentResult(self.workitem, -1) else: self.logMethod("no agent found") setAgentResult(self.workitem, 1) self.logMethod("sending back workitem to engine '%s'" % self.workitem.lastExpressionId.engineId) pmap = lookupParticipantMap(self.applicationContext) engine = pmap.get(self.workitem.lastExpressionId.engineId) engine.dispatch(self.workitem)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -