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
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
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)
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
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)
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)
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())
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())
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)
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
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
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
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')
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')
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')
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')