def build(self, source): t0 = time.clock() try: self.root = etree.fromstring(source.read()) self.componentManager = ComponentManager() components = self.root.find("{%(map)s}components" % ns) if components is not None: self.componentManager.configure(components) return self.buildNode(self.root.find("{%(map)s}pipelines" % ns)) finally: t1 = time.clock() self.log.debug("%s is built in %.3f s" % (source.uri, t1 - t0))
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")
class TreeBuilder: nodeClasses = { "pipelines": PipelinesNode, "pipeline": PipelineNode, "match": MatchNode, "aggregate": AggregateNode, "serialize": SerializeNode, "call": ResourceCall, "generate": GenerateNode, "transform": TransformNode, "select": SelectNode, "read": ReadNode, "handle-errors": HandleErrorsNode, "mount": MountNode, "act": ActNode, "redirect-to": RedirectNode, "component-configurations": ComponentConfigurations, } def __init__(self): self.componentManager = None self.log = logging.getLogger("sitemap.builder") def build(self, source): t0 = time.clock() try: self.root = etree.fromstring(source.read()) self.componentManager = ComponentManager() components = self.root.find("{%(map)s}components" % ns) if components is not None: self.componentManager.configure(components) return self.buildNode(self.root.find("{%(map)s}pipelines" % ns)) finally: t1 = time.clock() self.log.debug("%s is built in %.3f s" % (source.uri, t1 - t0)) def buildNode(self, element): if not isinstance(element.tag, str) or not element.tag.startswith("{%(map)s}" % ns): return None suffix = element.tag[len("{%(map)s}" % ns):] if suffix == "parameter": return None try: node = self.nodeClasses[suffix]() except KeyError: raise Exception("Unknown sitemap element tag: %s" % suffix) node.build(element) if isinstance(node, SelectNode): node.whenElements = [] for when in element.findall("{%(map)s}when" % ns): test = variables.getResolver(when.get("test")) nodes = [self.buildNode(c) for c in when] actualNodes = [n for n in nodes if n is not None] node.whenElements.append((test, actualNodes)) otherwise = element.find("{%(map)s}otherwise" % ns) if otherwise is not None: nodes = [self.buildNode(c) for c in otherwise] node.otherwiseNodes = [n for n in nodes if n is not None] return node if isinstance(node, ResourceCall): element = None for r in self.root.findall("{%(map)s}resources/{%(map)s}resource" % ns): if r.get("name") == node.name: element = r break if element is None: raise Exception('<map:resource name="%s"> not found' % node.name) if isinstance(node, ContainerNode): children = [self.buildNode(c) for c in element] def isRegularNode(c): return isinstance(c, Node) and not isinstance(c, HandleErrorsNode) node.children = [c for c in children if isRegularNode(c)] handleNodes = [c for c in children if isinstance(c, HandleErrorsNode)] if len(handleNodes) > 0: node.handleErrorsNode = handleNodes.pop() if isinstance(node, PipelinesNode): if len(node.children) > 0: node.children[-1].isLast = True if isinstance(node, ComponentConfigurations): self.processor.componentConfigurations = node.element return node
class TreeBuilder: nodeClasses = { "pipelines": PipelinesNode, "pipeline": PipelineNode, "match": MatchNode, "aggregate": AggregateNode, "serialize": SerializeNode, "call": ResourceCall, "generate": GenerateNode, "transform": TransformNode, "select": SelectNode, "read": ReadNode, "handle-errors": HandleErrorsNode, "mount": MountNode, "act": ActNode, "redirect-to": RedirectNode, "component-configurations": ComponentConfigurations, } def __init__(self): self.componentManager = None self.log = logging.getLogger("sitemap.builder") def build(self, source): t0 = time.clock() try: self.root = etree.fromstring(source.read()) self.componentManager = ComponentManager() components = self.root.find("{%(map)s}components" % ns) if components is not None: self.componentManager.configure(components) return self.buildNode(self.root.find("{%(map)s}pipelines" % ns)) finally: t1 = time.clock() self.log.debug("%s is built in %.3f s" % (source.uri, t1 - t0)) def buildNode(self, element): if not isinstance(element.tag, str) or not element.tag.startswith( "{%(map)s}" % ns): return None suffix = element.tag[len("{%(map)s}" % ns):] if suffix == "parameter": return None try: node = self.nodeClasses[suffix]() except KeyError: raise Exception("Unknown sitemap element tag: %s" % suffix) node.build(element) if isinstance(node, SelectNode): node.whenElements = [] for when in element.findall("{%(map)s}when" % ns): test = variables.getResolver(when.get("test")) nodes = [self.buildNode(c) for c in when] actualNodes = [n for n in nodes if n is not None] node.whenElements.append((test, actualNodes)) otherwise = element.find("{%(map)s}otherwise" % ns) if otherwise is not None: nodes = [self.buildNode(c) for c in otherwise] node.otherwiseNodes = [n for n in nodes if n is not None] return node if isinstance(node, ResourceCall): element = None for r in self.root.findall("{%(map)s}resources/{%(map)s}resource" % ns): if r.get("name") == node.name: element = r break if element is None: raise Exception('<map:resource name="%s"> not found' % node.name) if isinstance(node, ContainerNode): children = [self.buildNode(c) for c in element] def isRegularNode(c): return isinstance( c, Node) and not isinstance(c, HandleErrorsNode) node.children = [c for c in children if isRegularNode(c)] handleNodes = [ c for c in children if isinstance(c, HandleErrorsNode) ] if len(handleNodes) > 0: node.handleErrorsNode = handleNodes.pop() if isinstance(node, PipelinesNode): if len(node.children) > 0: node.children[-1].isLast = True if isinstance(node, ComponentConfigurations): self.processor.componentConfigurations = node.element return node