示例#1
0
    def __init__(self, conf):
        root = logging.getLogger()
        root.setLevel(logging.DEBUG)
        sh = logging.StreamHandler()
        sh.setFormatter(logging.Formatter(logging.BASIC_FORMAT, None))
        root.addHandler(sh)
        try:
            self.contextPath = conf
            source = SourceResolver().resolveUri(conf, "")
            self.xconf = etree.fromstring(source.read())

            logging.disable(
                logging.getLevelName(
                    self.xconf.find("logging").get("disable", "NOTSET")))
            for l in self.xconf.findall("logging/loggers/logger"):
                logging.getLogger(l.get("name")).setLevel(
                    logging.getLevelName(l.get("level", "DEBUG")))

            loader = ClassLoader()
            for h in self.xconf.findall("logging/handlers/handler"):
                args = [eval(arg.text) for arg in h.findall("args/arg")]
                if h.find("kwargs"):
                    kwargs = dict([(arg.get("name"), eval(arg.text))
                                   for arg in h.findall("kwargs/arg")])
                else:
                    kwargs = {}
                handler = loader.getClass(h.get("class"))(*args, **kwargs)
                handler.setLevel(logging.getLevelName(h.get("level", "DEBUG")))
                handler.setFormatter(
                    logging.Formatter(logging.BASIC_FORMAT, None))
                for l in h.findall("logger-ref"):
                    logging.getLogger(l.get("name")).addHandler(handler)

            # Pseudo-servlet parameters (and their defaults)
            #self.params = {
            #"form-encoding": "iso-8859-1",
            #}
            self.params = dict([
                (p.find("param-name").text, p.find("param-value").text)
                for p in self.xconf.findall("web-app/servlet/init-param")
            ])

            os.environ.update(
                dict([(p.get("name"), p.get("value"))
                      for p in self.xconf.findall("properties/property")]))

            mimetypes.init()
            for m in self.xconf.findall("web-app/mime-mapping"):
                mimetypes.add_type(
                    m.find("mime-type").text, ".%s" % m.find("extension").text)

            self.processorIsShared = os.getenv(
                "threading.treeprocessor.shared", "no") == "yes"

            if self.processorIsShared:
                self.processor = self.createTreeProcessor()
        finally:
            root.removeHandler(sh)
示例#2
0
 def __init__(self, conf):
     root = logging.getLogger()
     root.setLevel(logging.DEBUG)
     sh = logging.StreamHandler()
     sh.setFormatter(logging.Formatter(logging.BASIC_FORMAT, None))
     root.addHandler(sh)
     try:
         self.contextPath = conf 
         source = SourceResolver().resolveUri(conf, "")
         self.xconf = etree.fromstring(source.read())
         
         logging.disable(logging.getLevelName(self.xconf.find("logging").get("disable", "NOTSET")))
         for l in self.xconf.findall("logging/loggers/logger"):
             logging.getLogger(l.get("name")).setLevel(logging.getLevelName(l.get("level", "DEBUG")))
             
         loader = ClassLoader()
         for h in self.xconf.findall("logging/handlers/handler"):
             args = [eval(arg.text) for arg in h.findall("args/arg")]
             if h.find("kwargs"):
                 kwargs = dict([(arg.get("name"), eval(arg.text)) for arg in h.findall("kwargs/arg")])
             else:
                 kwargs = {}
             handler = loader.getClass(h.get("class"))(*args, **kwargs)
             handler.setLevel(logging.getLevelName(h.get("level", "DEBUG")))
             handler.setFormatter(logging.Formatter(logging.BASIC_FORMAT, None))
             for l in h.findall("logger-ref"):
                 logging.getLogger(l.get("name")).addHandler(handler)
             
         # Pseudo-servlet parameters (and their defaults)
         #self.params = {
             #"form-encoding": "iso-8859-1",
         #}
         self.params = dict([(p.find("param-name").text,  p.find("param-value").text)
             for p in self.xconf.findall("web-app/servlet/init-param")])
             
         os.environ.update(dict([(p.get("name"), p.get("value"))
             for p in self.xconf.findall("properties/property")]))
             
         mimetypes.init()
         for m in self.xconf.findall("web-app/mime-mapping"):
             mimetypes.add_type(m.find("mime-type").text, ".%s" % m.find("extension").text)
 
         self.processorIsShared = os.getenv("threading.treeprocessor.shared", "no") == "yes"
         
         if self.processorIsShared:
             self.processor = self.createTreeProcessor()
     finally:
         root.removeHandler(sh)
示例#3
0
    def configure(self, element):
        sitemap = element.find("sitemap")
        self.checkReload = (sitemap.get("check-reload", "yes") == "yes")
        self.uri = sitemap.get("file", "sitemap.xmap")
        self.source = SourceResolver().resolveUri(self.uri, self.contextPath)
        self.log = logging.getLogger(sitemap.get("logger"))

        self.parentComponentManager = ComponentManager()
        elems = element.findall("input-modules/component-instance")

        def getClassAndElement(e):
            return (self.parentComponentManager.classLoader.getClass(
                e.get("{%(py)s}class" % ns)), e)

        classes = dict([(e.get("name"), getClassAndElement(e)) for e in elems])
        self.parentComponentManager.components["input-module"] = (classes,
                                                                  None, None)

        self.log.debug("Tree processor is configured")
示例#4
0
 def createChildProcessor(self, src, env):
     if src in self.children:
         child = self.children[src]
     else:
         child = TreeProcessor()
         child.parent = self
         child.log = logging.getLogger("sitemap.processor")
         child.uri = src
         child.source = SourceResolver(env).resolveUri(child.uri)
         self.children[src] = child
     child.parentComponentManager = self.componentManager
     return child
示例#5
0
    def configure(self, element):
        sitemap = element.find("sitemap")
        self.checkReload = (sitemap.get("check-reload", "yes") == "yes")
        self.uri = sitemap.get("file", "sitemap.xmap")
        self.source = SourceResolver().resolveUri(self.uri, self.contextPath)
        self.log = logging.getLogger(sitemap.get("logger"))

        self.parentComponentManager = ComponentManager()
        elems = element.findall("input-modules/component-instance")
        def getClassAndElement(e):
            return (self.parentComponentManager.classLoader.getClass(e.get("{%(py)s}class" % ns)), e)
        classes = dict([(e.get("name"), getClassAndElement(e)) for e in elems])
        self.parentComponentManager.components["input-module"] = (classes, None, None)
        
        self.log.debug("Tree processor is configured")
示例#6
0
    def __init__(self, req, isExternal=True, environ=None):
        self.log = logging.getLogger("environment")
        self.prefix = "/"

        self.request = req
        self.response = HttpResponse()
        self.sourceResolver = SourceResolver(self)
        self.componentManager = None
        self.isExternal = isExternal
        self.environ = environ

        self.objectModel = {}
        self.objectModel["request"] = self.request
        self.objectModel["response"] = self.response

        # Possibly non-standard
        self.objectModel["processor"] = None
        self.objectModel["root-processor"] = None
        self.objectModel["source-resolver"] = self.sourceResolver

        self.log.debug('Created: %s, self.isExternal: %s' %
                       (self, self.isExternal))
示例#7
0
class TreeProcessor(Node):    
    def __init__(self, contextPath=None):
        Node.__init__(self)
        self.contextPath = contextPath
        self.rootNode = None
        self.children = {}
        self.parent = None
        self.parentComponentManager = None
        self.componentConfigurations = None
        self.lastModified = 0
        self.log = logging.getLogger("sitemap.processor")
        self.checkReload = True
    
    def configure(self, element):
        sitemap = element.find("sitemap")
        self.checkReload = (sitemap.get("check-reload", "yes") == "yes")
        self.uri = sitemap.get("file", "sitemap.xmap")
        self.source = SourceResolver().resolveUri(self.uri, self.contextPath)
        self.log = logging.getLogger(sitemap.get("logger"))

        self.parentComponentManager = ComponentManager()
        elems = element.findall("input-modules/component-instance")
        def getClassAndElement(e):
            return (self.parentComponentManager.classLoader.getClass(e.get("{%(py)s}class" % ns)), e)
        classes = dict([(e.get("name"), getClassAndElement(e)) for e in elems])
        self.parentComponentManager.components["input-module"] = (classes, None, None)
        
        self.log.debug("Tree processor is configured")

    def process(self, env):
        t0 = time.clock()
        try:
            env.objectModel["processor"] = self
            if env.objectModel.get("root-processor") is None:
                env.objectModel["root-processor"] = self.parent
            self.setupProcessor(env)
            context = InvokeContext()
            return self.rootNode.invoke(env, context)
        finally:
            t1 = time.clock()
            if self.parent is None:
                self.log.debug("'%s': rendered in %.3f s" % (env.request.uri, t1 - t0))
    
    def buildPipeline(self, env):
        env.objectModel["processor"] = self
        if env.objectModel.get("root-processor") is None:
            env.objectModel["root-processor"] = self.parent
        context = InvokeContext(True)
        if self.rootNode.invoke(env, context):
            return context.processingPipeline
        else:
            return None
        
    def setupProcessor(self, env):
        log = logging.getLogger("%s.setup" % self.log.name)
        log.debug("Setting up processor for: %s" % self.source.uri)
        if self.parent is None:
            env.changeContext("", self.source.uri)
        
        if self.checkReload and self.source.getLastModified() != self.lastModified:
            log.debug("Source: %s" % self.source.uri)
            log.debug("Source mtime: %d" % self.source.getLastModified())
            log.debug("Processor mtime: %d" % self.lastModified)
            self.buildProcessor(env)
        env.componentManager = self.componentManager

    @synchronized
    def buildProcessor(self, env):
        # We are now in the critical section, so check the conditions once again
        if self.rootNode is not None and self.source.getLastModified() == self.lastModified:
            return
        builder = TreeBuilder()
        builder.processor = self
        root = builder.build(self.source)
        self.rootNode = root
        self.componentManager = builder.componentManager
        if self.parentComponentManager is not None:
            self.componentManager.parent = self.parentComponentManager
        if self.parent is None:
            self.contextPath = env.contextPath
        self.lastModified = self.source.getLastModified()
        self.log.debug("Processor (re)built")
        
    @synchronized
    def createChildProcessor(self, src, env):
        if src in self.children:
            child = self.children[src]
        else:
            child = TreeProcessor()
            child.parent = self
            child.log = logging.getLogger("sitemap.processor")
            child.uri = src
            child.source = SourceResolver(env).resolveUri(child.uri)
            self.children[src] = child
        child.parentComponentManager = self.componentManager
        return child
示例#8
0
class TreeProcessor(Node):
    def __init__(self, contextPath=None):
        Node.__init__(self)
        self.contextPath = contextPath
        self.rootNode = None
        self.children = {}
        self.parent = None
        self.parentComponentManager = None
        self.componentConfigurations = None
        self.lastModified = 0
        self.log = logging.getLogger("sitemap.processor")
        self.checkReload = True

    def configure(self, element):
        sitemap = element.find("sitemap")
        self.checkReload = (sitemap.get("check-reload", "yes") == "yes")
        self.uri = sitemap.get("file", "sitemap.xmap")
        self.source = SourceResolver().resolveUri(self.uri, self.contextPath)
        self.log = logging.getLogger(sitemap.get("logger"))

        self.parentComponentManager = ComponentManager()
        elems = element.findall("input-modules/component-instance")

        def getClassAndElement(e):
            return (self.parentComponentManager.classLoader.getClass(
                e.get("{%(py)s}class" % ns)), e)

        classes = dict([(e.get("name"), getClassAndElement(e)) for e in elems])
        self.parentComponentManager.components["input-module"] = (classes,
                                                                  None, None)

        self.log.debug("Tree processor is configured")

    def process(self, env):
        t0 = time.clock()
        try:
            env.objectModel["processor"] = self
            if env.objectModel.get("root-processor") is None:
                env.objectModel["root-processor"] = self.parent
            self.setupProcessor(env)
            context = InvokeContext()
            return self.rootNode.invoke(env, context)
        finally:
            t1 = time.clock()
            if self.parent is None:
                self.log.debug("'%s': rendered in %.3f s" %
                               (env.request.uri, t1 - t0))

    def buildPipeline(self, env):
        env.objectModel["processor"] = self
        if env.objectModel.get("root-processor") is None:
            env.objectModel["root-processor"] = self.parent
        context = InvokeContext(True)
        if self.rootNode.invoke(env, context):
            return context.processingPipeline
        else:
            return None

    def setupProcessor(self, env):
        log = logging.getLogger("%s.setup" % self.log.name)
        log.debug("Setting up processor for: %s" % self.source.uri)
        if self.parent is None:
            env.changeContext("", self.source.uri)

        if self.checkReload and self.source.getLastModified(
        ) != self.lastModified:
            log.debug("Source: %s" % self.source.uri)
            log.debug("Source mtime: %d" % self.source.getLastModified())
            log.debug("Processor mtime: %d" % self.lastModified)
            self.buildProcessor(env)
        env.componentManager = self.componentManager

    @synchronized
    def buildProcessor(self, env):
        # We are now in the critical section, so check the conditions once again
        if self.rootNode is not None and self.source.getLastModified(
        ) == self.lastModified:
            return
        builder = TreeBuilder()
        builder.processor = self
        root = builder.build(self.source)
        self.rootNode = root
        self.componentManager = builder.componentManager
        if self.parentComponentManager is not None:
            self.componentManager.parent = self.parentComponentManager
        if self.parent is None:
            self.contextPath = env.contextPath
        self.lastModified = self.source.getLastModified()
        self.log.debug("Processor (re)built")

    @synchronized
    def createChildProcessor(self, src, env):
        if src in self.children:
            child = self.children[src]
        else:
            child = TreeProcessor()
            child.parent = self
            child.log = logging.getLogger("sitemap.processor")
            child.uri = src
            child.source = SourceResolver(env).resolveUri(child.uri)
            self.children[src] = child
        child.parentComponentManager = self.componentManager
        return child