Exemplo n.º 1
0
 def generate(self, request, node):
     if self.macroTemplate:
         templ = view.View(
             self.model,
             template = self.macroTemplate).lookupTemplate(request)
     else:
         templ = view.View(
             self.model,
             templateFile=self.macroFile,
             templateDirectory=self.macroFileDirectory).lookupTemplate(request)
     macrolist = domhelpers.locateNodes(templ.childNodes, "macro", self.macroName)
     assert len(macrolist) == 1, ("No macro or more than "
         "one macro named %s found." % self.macroName)
     macro = macrolist[0]
     del macro.attributes['macro']
     slots = domhelpers.findElementsWithAttributeShallow(macro, "slot")
     for slot in slots:
         slotName = slot.attributes.get("slot")
         fillerlist = domhelpers.locateNodes(node.childNodes, "fill-slot", slotName)
         assert len(fillerlist) <= 1, "More than one fill-slot found with name %s" % slotName
         if len(fillerlist):
             filler = fillerlist[0]
             filler.tagName = filler.endTagName = slot.tagName
             del filler.attributes['fill-slot']
             del slot.attributes['slot']
             filler.attributes.update(slot.attributes)
             slot.parentNode.replaceChild(filler, slot)
     return macro
Exemplo n.º 2
0
    def generate(self, request, node):
        templ = view.View(
            self.model,
            templateFile=self.macroFile, 
            templateDirectory=self.macroFileDirectory).lookupTemplate(request)

        ## We are going to return the macro node from the metatemplate,
        ## after replacing any slot= nodes in it with fill-slot= nodes from `node'
        macrolist = domhelpers.locateNodes(templ.childNodes, "macro", self.macroName)
        assert len(macrolist) == 1, ("No macro or more than "
            "one macro named %s found." % self.macroName)

        macro = macrolist[0]
        macro.removeAttribute('macro')
        slots = domhelpers.findElementsWithAttributeShallow(macro, "slot")
        for slot in slots:
            slotName = slot.getAttribute("slot")
            fillerlist = domhelpers.locateNodes(node.childNodes, "fill-slot", slotName)
            assert len(fillerlist) <= 1, "More than one fill-slot found with name %s" % slotName
            if len(fillerlist):
                filler = fillerlist[0]
                filler.tagName = filler.endTagName = slot.tagName
                filler.removeAttribute('fill-slot')
                slot.removeAttribute('slot')
                for k, v in slot.attributes.items():
                    filler.setAttribute(k, v)
                slot.parentNode.replaceChild(filler, slot)

        return macro
Exemplo n.º 3
0
    def test_locateNodes(self):
        doc1=microdom.parseString('<a><b foo="olive"><c foo="olive"/></b><d foo="poopy"/></a>')
        node_list=domhelpers.locateNodes(doc1.childNodes, 'foo', 'olive',
                                         noNesting=1)
        actual=''.join([node.toxml() for node in node_list])
        expected='<b foo="olive"><c foo="olive"></c></b>'
        assert actual==expected, 'expected %s, got %s' % (expected, actual)

        node_list=domhelpers.locateNodes(doc1.childNodes, 'foo', 'olive',
                                         noNesting=0)
        actual=''.join([node.toxml() for node in node_list])
        expected='<b foo="olive"><c foo="olive"></c></b><c foo="olive"></c>'
        assert actual==expected, 'expected %s, got %s' % (expected, actual)
Exemplo n.º 4
0
 def getAllPatterns(self, name, default=missingPattern, clone=1, deep=1):
     """Get all nodes below this one which have a matching pattern attribute.
     """
     if self.slots.has_key(name):
         slots = self.slots[name]
     else:
         sm = self.submodel.split('/')[-1]
         slots = domhelpers.locateNodes(self.templateNode, name + 'Of', sm)
         if not slots:
             matcher = lambda n, name=name: isinstance(n, Element) and \
                         n.attributes.has_key("pattern") and n.attributes["pattern"] == name
             recurseMatcher = lambda n: isinstance(n, Element) and not n.attributes.has_key("view") and not n.attributes.has_key('model')
             slots = domhelpers.findNodesShallowOnMatch(self.templateNode, matcher, recurseMatcher)
             if not slots:
                 msg = 'WARNING: No template nodes were found '\
                           '(tagged %s="%s"'\
                           ' or pattern="%s") for node %s (full submodel path %s)' % (name + "Of",
                                         sm, name, self.templateNode, `self.submodel`)
                 if default is _RAISE:
                     raise Exception(msg)
                 if DEBUG:
                     warnings.warn(msg)
                 if default is missingPattern:
                     newNode = missingPattern.cloneNode(1)
                     newNode.appendChild(document.createTextNode(msg))
                     return [newNode]
                 if default is None:
                     return None
                 return [default]
         self.slots[name] = slots
     if clone:
         return [x.cloneNode(deep) for x in slots]
     return slots
Exemplo n.º 5
0
 def visitNode_body(self, node):
     tocs = domhelpers.locateNodes([node], 'class', 'toc')
     domhelpers.clearNode(node)
     if len(tocs):
         toc = tocs[0]
         node.appendChild(toc)
     self.visitNodeDefault(node)
Exemplo n.º 6
0
 def visitNode_body(self, node):
     tocs=domhelpers.locateNodes([node], 'class', 'toc')
     domhelpers.clearNode(node)
     if len(tocs):
         toc=tocs[0]
         node.appendChild(toc)
     self.visitNodeDefault(node)
Exemplo n.º 7
0
    def test_locateNodes(self):
        doc1 = self.dom.parseString('<a><b foo="olive"><c foo="olive"/></b><d foo="poopy"/></a>')
        node_list=domhelpers.locateNodes(
            doc1.childNodes, 'foo', 'olive', noNesting=1)
        actual=''.join([node.toxml() for node in node_list])
        expected = self.dom.Element('b')
        expected.setAttribute('foo', 'olive')
        c = self.dom.Element('c')
        c.setAttribute('foo', 'olive')
        expected.appendChild(c)

        self.assertEqual(actual, expected.toxml())

        node_list=domhelpers.locateNodes(
            doc1.childNodes, 'foo', 'olive', noNesting=0)
        actual=''.join([node.toxml() for node in node_list])
        self.assertEqual(actual, expected.toxml() + c.toxml())
Exemplo n.º 8
0
    def test_locateNodes(self):
        doc1 = self.dom.parseString('<a><b foo="olive"><c foo="olive"/></b><d foo="poopy"/></a>')
        node_list=domhelpers.locateNodes(
            doc1.childNodes, 'foo', 'olive', noNesting=1)
        actual=''.join([node.toxml() for node in node_list])
        expected = self.dom.Element('b')
        expected.setAttribute('foo', 'olive')
        c = self.dom.Element('c')
        c.setAttribute('foo', 'olive')
        expected.appendChild(c)

        self.assertEqual(actual, expected.toxml())

        node_list=domhelpers.locateNodes(
            doc1.childNodes, 'foo', 'olive', noNesting=0)
        actual=''.join([node.toxml() for node in node_list])
        self.assertEqual(actual, expected.toxml() + c.toxml())
Exemplo n.º 9
0
    def test_locateNodes(self):
        doc1 = self.dom.parseString(
            '<a><b foo="olive"><c foo="olive"/></b><d foo="poopy"/></a>'
        )
        doc = self.dom.Document()
        node_list = domhelpers.locateNodes(doc1.childNodes, "foo", "olive", noNesting=1)
        actual = "".join([node.toxml() for node in node_list])
        expected = doc.createElement("b")
        expected.setAttribute("foo", "olive")
        c = doc.createElement("c")
        c.setAttribute("foo", "olive")
        expected.appendChild(c)

        self.assertEqual(actual, expected.toxml())

        node_list = domhelpers.locateNodes(doc1.childNodes, "foo", "olive", noNesting=0)
        actual = "".join([node.toxml() for node in node_list])
        self.assertEqual(actual, expected.toxml() + c.toxml())
Exemplo n.º 10
0
    def test_locateNodes(self):
        doc1 = microdom.parseString(
            '<a><b foo="olive"><c foo="olive"/></b><d foo="poopy"/></a>')
        node_list = domhelpers.locateNodes(doc1.childNodes,
                                           'foo',
                                           'olive',
                                           noNesting=1)
        actual = ''.join([node.toxml() for node in node_list])
        expected = '<b foo="olive"><c foo="olive"></c></b>'
        assert actual == expected, 'expected %s, got %s' % (expected, actual)

        node_list = domhelpers.locateNodes(doc1.childNodes,
                                           'foo',
                                           'olive',
                                           noNesting=0)
        actual = ''.join([node.toxml() for node in node_list])
        expected = '<b foo="olive"><c foo="olive"></c></b><c foo="olive"></c>'
        assert actual == expected, 'expected %s, got %s' % (expected, actual)
Exemplo n.º 11
0
 def getPattern(self, name, default=missingPattern, clone=1, deep=1):
     """Get a named slot from the incoming template node. Returns a copy
     of the node and all its children. If there was more than one node with
     the same slot identifier, they will be returned in a round-robin fashion.
     """
     #print self.templateNode.toxml()
     if self.slots.has_key(name):
         slots = self.slots[name]
     else:
         sm = self.submodel.split('/')[-1]
         slots = domhelpers.locateNodes(self.templateNode, name + 'Of', sm)
         if not slots:
             slots = domhelpers.locateNodes(self.templateNode, "pattern", name, noNesting=1)
             if not slots:
                 msg = 'WARNING: No template nodes were found '\
                           '(tagged %s="%s"'\
                           ' or pattern="%s") for node %s (full submodel path %s)' % (name + "Of",
                                         sm, name, self.templateNode, `self.submodel`)
                 if default is _RAISE:
                     raise Exception(msg)
                 if DEBUG:
                     warnings.warn(msg)
                 if default is missingPattern:
                     newNode = missingPattern.cloneNode(1)
                     newNode.appendChild(document.createTextNode(msg))
                     return newNode
                 return default
             else:
                 for node in slots:
                     node.removeAttribute('pattern')
         else:
             for node in slots:
                 node.removeAttribute(name + 'Of')
         self.slots[name] = slots
     slot = slots.pop(0)
     slots.append(slot)
     if clone:
         parentNode = slot.parentNode
         slot.parentNode = None
         clone = slot.cloneNode(deep)
         slot.parentNode = parentNode
         return clone
     return slot
Exemplo n.º 12
0
    def generate(self, request, node):
        if self.macroTemplate:
            templ = view.View(
                self.model,
                template=self.macroTemplate).lookupTemplate(request)
        else:
            templ = view.View(
                self.model,
                templateFile=self.macroFile,
                templateDirectory=self.macroFileDirectory).lookupTemplate(
                    request)

        ## We are going to return the macro node from the metatemplate,
        ## after replacing any slot= nodes in it with fill-slot= nodes from `node'
        macrolist = domhelpers.locateNodes(templ.childNodes, "macro",
                                           self.macroName)
        assert len(macrolist) == 1, ("No macro or more than "
                                     "one macro named %s found." %
                                     self.macroName)

        macro = macrolist[0]
        del macro.attributes['macro']
        slots = domhelpers.findElementsWithAttributeShallow(macro, "slot")
        for slot in slots:
            slotName = slot.attributes.get("slot")
            fillerlist = domhelpers.locateNodes(node.childNodes, "fill-slot",
                                                slotName)
            assert len(
                fillerlist
            ) <= 1, "More than one fill-slot found with name %s" % slotName
            if len(fillerlist):
                filler = fillerlist[0]
                filler.tagName = filler.endTagName = slot.tagName
                del filler.attributes['fill-slot']
                del slot.attributes['slot']
                filler.attributes.update(slot.attributes)
                slot.parentNode.replaceChild(filler, slot)

        return macro
Exemplo n.º 13
0
 def getAllPatterns(self, name, default=missingPattern, clone=1, deep=1):
     """Get all nodes below this one which have a matching pattern attribute.
     """
     if self.slots.has_key(name):
         slots = self.slots[name]
     else:
         sm = self.submodel.split('/')[-1]
         slots = domhelpers.locateNodes(self.templateNode, name + 'Of', sm)
         if not slots:
             #                slots = domhelpers.locateNodes(self.templateNode, "pattern", name, noNesting=1)
             matcher = lambda n, name=name: isinstance(n, Element) and \
                         n.attributes.has_key("pattern") and n.attributes["pattern"] == name
             recurseMatcher = lambda n: isinstance(
                 n, Element) and not n.attributes.has_key(
                     "view") and not n.attributes.has_key('model')
             slots = domhelpers.findNodesShallowOnMatch(
                 self.templateNode, matcher, recurseMatcher)
             if not slots:
                 msg = 'WARNING: No template nodes were found '\
                           '(tagged %s="%s"'\
                           ' or pattern="%s") for node %s (full submodel path %s)' % (name + "Of",
                                         sm, name, self.templateNode, `self.submodel`)
                 if default is _RAISE:
                     raise Exception(msg)
                 if DEBUG:
                     warnings.warn(msg)
                 if default is missingPattern:
                     newNode = missingPattern.cloneNode(1)
                     newNode.appendChild(document.createTextNode(msg))
                     return [newNode]
                 if default is None:
                     return None
                 return [default]
         self.slots[name] = slots
     if clone:
         return [x.cloneNode(deep) for x in slots]
     return slots
Exemplo n.º 14
0
 def wvupdate_adjuster(self, request, widget, data):
     size = request.args.get('thumbnailSize',('200',))[0]
     domhelpers.locateNodes(widget.node.childNodes, 
                            'value', size)[0].setAttribute('selected', '1')
Exemplo n.º 15
0
 def wvupdate_adjuster(self, request, widget, data):
     size = request.args.get('thumbnailSize', ('200', ))[0]
     domhelpers.locateNodes(widget.node.childNodes, 'value',
                            size)[0].setAttribute('selected', '1')
Exemplo n.º 16
0
 def wvupdate_adjuster(self, request, widget, data):
     prefs = request.getSession(IPreferences)
     size = getattr(prefs, 'size', '200')
     domhelpers.locateNodes(widget.node.childNodes, 'value',
                            size)[0].setAttribute('selected', '1')
Exemplo n.º 17
0
 def wvupdate_adjuster(self, request, widget, data):
     prefs = request.getSession(IPreferences)
     size = getattr(prefs, 'size','200')
     domhelpers.locateNodes(widget.node.childNodes, 
                            'value', size)[0].setAttribute('selected', '1')