📄 participant.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: participant.py 2144 2005-10-02 20:35:57Z jmettraux $#""" The pyya ParticipantMap is here $Id: participant.py 2144 2005-10-02 20:35:57Z jmettraux $"""from os import stat as filestatfrom re import compile as recompile#from xml.dom.minidom import parsefrom openwfe.socket import SocketDispatcherfrom openwfe.applic import Servicefrom openwfe.xmlutils import \ fetchParamsAndAttributes, getContent, getChildrenByTagName, parseUrl## Some help methodsdef mtime (path): """ returns the 'lastModified' time of the given path """ return filestat(path)[8]## XmlParticipantMapPMAP_FILE = 'participantMapFile'DEFAULT_PMAP_FILE = 'etc/participant-map.xml'E_PARTICIPANT = 'participant'A_NAME = 'name'A_REF = 'ref'A_REGEX = 'regex'E_INCLUDE = 'include'A_URL = 'url'P_DISPATCHER = 'dispatcher'def lookupParticipantMap (applicationContext): pm = applicationContext.get('participantMap') if pm: return pm return applicationContext.get('EngineContext.participantMap')class XmlParticipantMap (Service): def __init__ (self): self.pmapFileName = None self.nameMap = dict() self.regexList = list() self.mtime = -1L def init (self, name, context, params): Service.init(self, name, context, params) self.pmapFileName = params.get(PMAP_FILE, DEFAULT_PMAP_FILE) self.linfo("participantMap file is >%s<" % self.pmapFileName) def _checkRefresh (self): if not self.pmapFileName or self.mtime < mtime(self.pmapFileName): self._build(self.pmapFileName) def _build (self, pmapFileName): self.ldebug("_build() building %s" % pmapFileName) if (pmapFileName == self.pmapFileName): # # purge # self.mtime = mtime(self.pmapFileName) self.nameMap = dict() self.regexList = list() doc = parseUrl(pmapFileName) rootElt = doc.documentElement pElts = getChildrenByTagName(rootElt, E_PARTICIPANT) for pElt in pElts: pName = pElt.getAttribute(A_NAME) pRegex = pElt.getAttribute(A_REGEX) pRef = pElt.getAttribute(A_REF) pParams = fetchParamsAndAttributes(pElt) #self.ldebug("_build() params are %s" % pParams) self._buildParticipant(pName, pRegex, pRef, pParams) iElts = getChildrenByTagName(rootElt, E_INCLUDE) for iElt in iElts: iUrl = iElt.getAttribute(A_URL) self._build(iUrl) def get (self, participantName): participantName = str(participantName) # neutralize unicode strings... self._checkRefresh() # look for a matching regex for regexParticipant in self.regexList: if regexParticipant.regex.match(participantName): return regexParticipant # look for the direct name #for k in self.nameMap.keys(): self.ldebug('k : "%s"' % k) return self.nameMap[participantName] def _buildParticipant (self, name, regex, ref, params): participant = RegularParticipant if ref: participant = RefParticipant participant(self, name, regex, ref, params)## Participant classesdef _recompile (regex): if not regex: return None return recompile(regex)class Participant: def __init__ (self, participantMap, name, regex, ref, params): self.name = name self.regex = _recompile(regex) self.ref = ref self.params = params self.participantMap = participantMap if self.name: self.participantMap.nameMap[self.name] = self self.participantMap.linfo("built participant named '%s'" % self.name) elif self.regex: self.participantMap.regexList.append(self) self.participantMap.linfo("built participant with regex '%s'" % self.regex.pattern) def dispatch (self, workitem): passclass RegularParticipant (Participant): def dispatch (self, workitem): sd = SocketDispatcher() sd.init('%s::dispatcher.to.participant:%s' % (self.__class__, self.name), self.participantMap.applicationContext, self.params) workitem.participantName = self.name sd.dispatch(workitem)class RefParticipant (Participant): def __init__ (self, participantMap, name, regex, ref, params): Participant.__init__(self, participantMap, name, regex, ref, params) self.ref = ref def dispatch (self, workitem): realParticipant = self.participantMap.get(self.ref) realParticipant.dispatch(workitem)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -