📄 __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 1931 2005-07-07 07:39:47Z jmettraux $#""" This file contains application configuration utilities $Id: __init__.py 1931 2005-07-07 07:39:47Z jmettraux $"""import sysfrom os import pathfrom time import ctimefrom xml.dom.minidom import parsefrom openwfe.xmlutils import fetchParams, getChildrenByTagName__contexts__ = dict()NAME = 'name'VALUE = 'value'CLASS = 'class'SERVICE = 'service'APPLICATION_DIRECTORY = 'applicationDirectory'DEBUG = 'DEBUG'INFO = 'INFO 'WARN = 'WARN 'VERSION = "OpenWFE pyya application v1.01 - jmettraux@openwfe.org"""" BasicService has no equivalent in basic java. Here it has two children (extensions) : Service and Agent (you can find the latter at openwfe/reactor/__init__.py)"""class BasicService: name = None applicationContext = None params = None def __init__ (self): pass def init (self, name, context, params): self.name = name self.applicationContext = context self.params = params self.linfo("BasicService init done") # logging methods def ldebug (self, message): self.log(DEBUG, message) def linfo (self, message): self.log(INFO, message) def lwarn (self, message): self.log(WARN, message) def log (self, level, message): log(level, self.name, message)""" Service is mapped from applic/openwfe.org.Service but inherits from BasicService, which is only found in pyya"""class Service (BasicService): def stop (self): self.linfo("stopping") def status (self): self.linfo("I'm ok")def splitClassName (className): l = className.split('.') pl = l[:-1] package = '' for module in pl: package = package+module+'.' package = package[:-1] #print 'package >%s<' % package classname = l[-1] #print 'classname >%s<' % classname return (package, classname)def instantiateClass (classname): #print "sys.path ::::::: ", sys.path (package, className) = splitClassName(classname) exec("from %s import %s" % (package, className)) exec("result = %s()" % className) #print 'instantiated class >%s<' % result.__class__ return resultdef buildService (applicationContext, elt): serviceName = elt.getAttribute(NAME) fullClassName = elt.getAttribute(CLASS) params = fetchParams(elt) service = instantiateClass(fullClassName) service.init(serviceName, applicationContext, params) return service## a bit of loggingdef log (level, emitter, message): print "%s %s '%s' - %s" % (ctime(), level, emitter, message) sys.stdout.flush()""" a classical"""class ServiceException (Exception): pass""" ApplicationContext is directly mapped from applic/openwfe.org.ApplicationContext It's a set of params and services"""class ApplicationContext: applicationName = None contextMap = dict() def __init__ (self, applicationHome, applicationName=None): self.applicationName = applicationName self.put(APPLICATION_DIRECTORY, applicationHome) def put (self, key, object): self.contextMap[key] = object def get (self, key): return self.contextMap.get(key) def getApplicationDirectory (self): dir = self.get(APPLICATION_DIRECTORY) if dir == None: dir = './' self.put(APPLICATION_DIRECTORY, dir) elif not dir.endswith(path.sep): dir = dir+path.sep self.put(APPLICATION_DIRECTORY, dir) return dir # # static methods def lookupContext (contextName): return __contexts__.get(contextName) lookupContext = staticmethod(lookupContext) def build (configurationFileName, applicationHome): #if logFileName: # logfile = open(logFileName, 'w') # sys.stdout = logfile # sys.stderr = logfile log(INFO, 'app', VERSION) doc = parse(configurationFileName) rootElt = doc.documentElement applicationName = rootElt.getAttribute(NAME) context = ApplicationContext(applicationHome, applicationName) # # fetch global params d = fetchParams(rootElt) context.contextMap.update(d) # # fetch and build services elts = getChildrenByTagName(rootElt, SERVICE) for elt in elts: service = buildService(context, elt) context.put(service.name, service) log(INFO, applicationName, "has been launched.\n\n") return context build = staticmethod(build)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -