[z3-checkins] r31302 - z3/deliverance/branches/packaged/deliverance

ianb at codespeak.net ianb at codespeak.net
Mon Aug 14 19:34:13 CEST 2006


Author: ianb
Date: Mon Aug 14 19:34:11 2006
New Revision: 31302

Modified:
   z3/deliverance/branches/packaged/deliverance/main.py
Log:
lxml now uses a special parser setup to have custom element class lookups; invoke that (but allow for the old implicit registration too), and remove workarounds

Modified: z3/deliverance/branches/packaged/deliverance/main.py
==============================================================================
--- z3/deliverance/branches/packaged/deliverance/main.py	(original)
+++ z3/deliverance/branches/packaged/deliverance/main.py	Mon Aug 14 19:34:11 2006
@@ -1,8 +1,11 @@
 import os
 from lxml import etree
 from time import time
-from lxml.etree import Namespace, ElementBase
-
+from lxml.etree import Namespace, ElementBase, XMLParser
+try:
+    from lxml.etree import ElementNamespaceClassLookup
+except ImportError:
+    ElementNamespaceClassLookup = None
 
 nsmap = {
     "dv": "http://www.plone.org/deliverance",
@@ -11,6 +14,14 @@
     "at": "http://plone.org/archetypes",
     }
 
+parser = XMLParser()
+if ElementNamespaceClassLookup is not None:
+    # Earlier versions of lxml did class lookup in all
+    # cases; newer versions require this explicit parser
+    # setup
+    lookup = ElementNamespaceClassLookup()
+    parser.setElementClassLookup(lookup)
+
 class AppMap:
 
     def __init__(self, layout_dir):
@@ -19,7 +30,8 @@
         self.module_dir = os.path.dirname(os.path.abspath(__file__))
         self.layout_dir = os.path.join(self.module_dir, layout_dir)
         layoutsfn = os.path.join(self.layout_dir, "appmap.xml")
-        self.tree = etree.ElementTree(file=layoutsfn)
+        self.tree = etree.ElementTree(
+            file=layoutsfn, parser=parser)
         self.tree.xinclude()
 
         # Make a themeprocessor to style all outgoing pages.  Note that the 
@@ -27,7 +39,8 @@
         # defined via a custom Python class defined below (class LayoutElement)
         root = self.tree.getroot()
         layout = root.xpath("dv:layouts/dv:layout", nsmap)[0]
-        self.themeprocessor = make_processor(layout)
+        #self.themeprocessor = make_processor(layout)
+        self.themeprocessor = layout.processor
 
 
     def publish(self, xmlstring):
@@ -44,64 +57,55 @@
 class DVRuleBase(ElementBase):
 
     def getThemeNode(self):
-        return getThemeNode(self)
+        """Get a node in the theme doc"""
 
-def getThemeNode(el):
-    """Get a node in the theme doc"""
+        # Current node is a rule, get xpath from the @theme attr
+        themedoc = self.xpath("../../dv:theme", nsmap)[0][0]
+        xpath = self.get("theme")
+        try:
+            themenode = themedoc.xpath(xpath, nsmap)[0]
+        except IndexError:
+            msg = "Themedoc has no node at: %s" % xpath
+            print msg
+            themenode = None
 
-    # Current node is a rule, get xpath from the @theme attr
-    themedoc = el.xpath("../../dv:theme", nsmap)[0][0]
-    xpath = el.get("theme")
-    try:
-        themenode = themedoc.xpath(xpath, nsmap)[0]
-    except IndexError:
-        msg = "Themedoc has no node at: %s" % xpath
-        print msg
-        themenode = None
-
-    return themenode
+        return themenode
 
 
 class LayoutElement(ElementBase):
 
     def processor(self):
-        return make_processor(self)
-    
-    processor = property(processor)
+        """Make XSLT processor by changing theme based on rules"""
 
-def make_processor(el):
-    """Make XSLT processor by changing theme based on rules"""
+        # Apply all the rules
+        for rule in self.xpath("./dv:rules/*", nsmap):
+            rule.apply()
 
-    # Apply all the rules
-    for rule in el.xpath("./dv:rules/*", nsmap):
-        apply_rules(rule)
-
-    # Merge applied rules into compilerdoc
-    compilerroot = el.xpath("../dv:compiler/xsl:stylesheet", nsmap)[0]
-    themeroot = el.xpath("dv:theme/html:html", nsmap)[0]
-    target = compilerroot.xpath("xsl:template[@match='/']", nsmap)[0]
-    target.append(themeroot)
+        # Merge applied rules into compilerdoc
+        compilerroot = self.xpath("../dv:compiler/xsl:stylesheet", nsmap)[0]
+        themeroot = self.xpath("dv:theme/html:html", nsmap)[0]
+        target = compilerroot.xpath("xsl:template[@match='/']", nsmap)[0]
+        target.append(themeroot)
 
-    #print etree.tostring(compilerroot)
+        #print etree.tostring(compilerroot)
 
-    return etree.XSLT(compilerroot)
+        return etree.XSLT(compilerroot)
+    
+    processor = property(processor)
         
 
 class RuleReplaceElement(DVRuleBase):
 
     def apply(self):
-        return apply_rules(self)
-
-def apply_rules(el):
-    # TODO: Someething here
-    themenode = getThemeNode(el)
-    if themenode is None:
-        return
-    del(themenode[:])
-    themenode.text = None
-    xslvalueof = etree.SubElement(themenode,
-                                  "{%s}value-of" % nsmap["xsl"])
-    xslvalueof.set("select", el.get("content"))
+        # TODO: Someething here
+        themenode = self.getThemeNode()
+        if themenode is None:
+            return
+        del(themenode[:])
+        themenode.text = None
+        xslvalueof = etree.SubElement(themenode,
+                                      "{%s}value-of" % nsmap["xsl"])
+        xslvalueof.set("select", self.get("content"))
 
 
 class RuleCopyElement(DVRuleBase):


More information about the z3-checkins mailing list