def test_subelement(): parent = Element("parent") child0 = SubElement(parent, "child0") assert parent[0].tag == "child0" assert parent[0].attrib == {} assert parent[0].text is None assert parent[0].tail is None child1 = SubElement(parent, "child1", dict(a=0, b=1)) assert parent[1].tag == "child1" assert parent[1].attrib == {"a": 0, "b": 1} assert parent[1].text is None assert parent[1].tail is None child2 = SubElement(parent, "child2", a=0, b=1) assert parent[2].tag == "child2" assert parent[2].attrib == {"a": 0, "b": 1} assert parent[2].text is None assert parent[2].tail is None child3 = SubElement(parent, "child3", dict(a=0, b=1), c=2) assert parent[3].tag == "child3" assert parent[3].attrib == {"a": 0, "b": 1, "c": 2} assert parent[3].text is None assert parent[3].tail is None assert parent.getchildren() == [child0, child1, child2, child3]
def test_namespaces(): element = Element("test", { "xmlns": "default ns", "xmlns:junk-ns0": "junk-ns value 0", "xmlns:junk-ns1": "junk-ns value 1", "py:pyattr": "py value", "attr": "a new attr", }) ns = namespaces(element) expected = { "": "default ns", "junk-ns0": "junk-ns value 0", "junk-ns1": "junk-ns value 1", } assert ns == expected assert element.get("xmlns") == "default ns" assert element.get("xmlns:junk-ns0") == "junk-ns value 0" assert element.get("xmlns:junk-ns1") == "junk-ns value 1" assert element.get("py:pyattr") == "py value" assert element.get("attr") == "a new attr" ns = namespaces(element, remove=True) assert element.get("xmlns") is None assert element.get("xmlns:junk-ns0") is None assert element.get("xmlns:junk-ns1") is None assert element.get("py:pyattr") == "py value" assert element.get("attr") == "a new attr"
def test_element_init(): element = Element("test0") assert element.tag == "test0" print element.attrib assert element.attrib == {} assert element.text is None assert element.tail is None element = Element("test1", dict(a=0, b=1)) assert element.tag == "test1" assert element.attrib == {"a": 0, "b": 1} assert element.text is None assert element.tail is None element = Element("test2", a=0, b=1) assert element.tag == "test2" assert element.attrib == {"a": 0, "b": 1} assert element.text is None assert element.tail is None element = Element("test3", dict(a=0, b=1), c=2) assert element.tag == "test3" assert element.attrib == {"a": 0, "b": 1, "c": 2} assert element.text is None assert element.tail is None
def _custom_status(x): if x.is_dirty: e = Element("span", {"class": "statusDirty"}) e.text = u"Updating\u2026" return e e = Element("span", {"class": "status%s" % x.status}) e.text = x.status return e
def _custom_status(x): if x.is_dirty: e = Element('span', {'class': 'statusDirty'}) e.text = u'Updating\u2026' return e e = Element('span', {'class' : 'status%s' % x.status}) e.text = x.status return e
def details_url(record): #_url = url("/trades/show/") + str(self.trade_date) + "_" + self.user_reference _url = url(request.base + request.path + "show/") + str(record.trade_date) + "_" + record.user_reference #_url = url("/trades/show/") + str(record.trade_date) + "_" + record.user_reference #link = ET.Element('a', href="http://localhost/repo/", style='text-decoration: underline' ) link = Element('a', href="javascript:printThisPage('"+_url+"')", style='text-decoration: underline') #link = Element('a', href=_url, style='text-decoration: underline') link.text = "Show Details" return link
def _pull(self, elem, tail=False): """Make a stream from an Element.""" orig = elem elem = Element(orig.tag, dict(orig.attrib)) ## XXX: find a better way if elem.tag in (Comment, ProcessingInstruction): elem.text = orig.text orig.text = None yield START, elem if orig.text: yield TEXT, orig.text for child in orig.getchildren(): for event in self._pull(child, tail=True): yield event yield END, elem if tail and orig.tail: yield TEXT, orig.tail
def format_field(record, field, element = '', alignment='', commatized='', font_face='', font_color='', font_size='', font=''): if type(field) is FunctionType: return field(record) try: output = getattr(record,field) except: output = "Unknown field: " + str(field) log.debug("Unknown field: " + str(field)) return output if commatized == 'True': if isDecimal(getattr(record,field)): output = convertNumberToCurrency(getattr(record,field)) if alignment=='right' or alignment == 'left' or alignment == 'center': e = Element('div',align=alignment) e.text = output output = e return output
def _custom_status(x): if x.is_dirty: e = Element('span', {'class': 'statusDirty'}) e.text = u'Updating\u2026' return e e = Element('span', {'class': 'status%s' % x.status}) e.text = x.status return e
def __iter__(self): clarknames = {} def clarkname(key): try: name = clarknames[key] except KeyError: if "}" in key: name = "{" + key else: name = key clarknames[key] = name return name stack = [] current = None for ev, stuff in self._expat_stream(): if ev == TEXT: yield TEXT, stuff elif ev == START: tag, attrib = stuff tag = clarkname(tag) attrib = dict([(clarkname(key), value) for key, value in attrib.items()]) parent = current current = Element(tag, attrib) stack.append(current) yield START, current elif ev == END: current = stack.pop() assert clarkname(stuff) == current.tag parent = len(stack) and stack[-1] or None yield END, current elif ev == COMMENT: current = Comment(stuff) yield START, current yield END, current elif ev == PI: current = ProcessingInstruction(*stuff) yield START, current yield END, current else: yield ev, stuff
def inject_meta_tags(self, stream, taglist): """Injects meta tags at the start of the head section. If meta tags already exist at that position, they are kept. Expects a list of meta-tag attributes with content keys. The attribute names and values must be given in lower case. """ done = False meta_tag = None for ev, item in stream: if not done: if ev in (START, END): tag = self.tagname(item.tag) if meta_tag: if item.tag == meta_tag: if ev == START: for attributes in taglist: for attrib, value in item.items(): attrib = attrib.lower() if attrib == 'content': continue if attrib not in attributes: break value = value.lower() if attributes[attrib] != value: break else: # that meta tag exists already attributes['content'] = None break else: for attributes in taglist: if attributes['content'] is None: continue meta_item = Element(meta_tag, **attributes) yield START, meta_item yield END, meta_item yield TEXT, '\n' done = True elif tag == 'head' and ev == START: meta_tag = item.tag[:-4] + 'meta' yield ev, item
def _custom_result(x): e = Element('span', {'class': 'result%s' % x.result}) e.text = x.result return e
def test_element_attributes(): element = Element("test", dict(a=0, b=1), c=2) assert element.get("d") is None assert element.get("d", "") == "" assert element.get("c") == 2 assert element.get("c", "") == 2 element.set("c", "c") assert element.get("c") == "c" keys = element.keys() assert len(keys) == 3 for k in ["a", "b", "c"]: assert k in keys items = element.items() assert len(items) == 3 for i in [("a", 0), ("b", 1), ("c", "c")]: assert i in items
def test_element_children(): parent = Element("parent") parent.append(Element("child0")) parent.append(Element("child1")) parent.append(Element("child2")) parent.append(Element("child3")) assert len(parent) == 4 assert parent[0].tag == "child0" assert parent[3].tag == "child3" parent[3] = Element("child4") assert parent[3].tag == "child4" del parent[3] assert len(parent) == 3 child0, child1 = parent[:2] assert child0.tag == "child0" assert child1.tag == "child1" parent[:2] = Element("firstone"), Element("firsttwo") assert parent[0].tag == "firstone" assert parent[1].tag == "firsttwo" del parent[:2] assert len(parent) == 1 print parent[0].tag assert parent[0].tag == "child2" parent.insert(0, Element("insert1")) parent.insert(0, Element("insert0")) assert parent[0].tag == "insert0" assert parent[1].tag == "insert1" assert len(parent) == 3 child = parent[-1] parent.remove(child) assert len(parent) == 2 children = parent.getchildren() assert len(children) == 2 assert children[0].tag == "insert0" assert children[1].tag == "insert1" parent.clear() assert parent.tag == "parent" assert len(parent) == 0
def test_element_repr(): element = Element("test0") print `element` match = re.match(r"^<Element test0 at [\-a-f\d]*>$", `element`) assert match is not None
def _custom_result(x): e = Element("span", {"class": "result%s" % x.result}) e.text = x.result return e
def _custom_result(x): e = Element('span', {'class' : 'result%s' % x.result}) e.text = x.result return e