def test_tree(self): #Node types use string rather than numerical constants now #The root node type is called entity doc = amara.parse(self.MONTY_XML) self.assertEqual(doc.xml_type, tree.entity.xml_type) m = doc.xml_children[0] #xml_children is a sequence of child nodes self.assertEqual(m.xml_local, u'monty') #local name, i.e. without any prefix self.assertEqual (m.xml_qname, u'monty') #qualified name, e.g. includes prefix self.assertEqual(m.xml_prefix, None) self.assertEqual(m.xml_qname, u'monty') #qualified name, e.g. includes prefix self.assertEqual(m.xml_namespace, None) self.assertEqual(m.xml_name, (None, u'monty')) #The "universal name" or "expanded name" self.assertEqual(m.xml_parent, doc) p1 = m.xml_children[1] XML_output = '<python spam="eggs">What do you mean "bleh"</python>' output = cStringIO.StringIO() xml_print(p1, stream=output) self.assertEqual(output.getvalue(), XML_output) self.assertEqual(p1.xml_attributes[(None, u'spam')], u"eggs") #Some manipulation p1.xml_attributes[(None, u'spam')] = u"greeneggs" self.assertEqual(p1.xml_attributes[(None, u'spam')], u"greeneggs") p1.xml_children[0].xml_value = u"Close to the edit" output = cStringIO.StringIO() xml_print(p1, stream=output) self.assertEqual(output.getvalue(), '<python spam="greeneggs">Close to the edit</python>')
def compare_output(self, doc, expected): """ Auxiliar method for testing output puposes """ output = cStringIO.StringIO() xml_print(doc, stream=output) return self.assertEqual(output.getvalue(), expected)
def test_dom(self): doc = dom.parse(self.MONTY_XML) for p, line in zip(doc.getElementsByTagNameNS(None, u"python"), self.lines_py): #A generator output = cStringIO.StringIO() xml_print(p, stream=output) self.assertEqual(output.getvalue(), line) p1 = doc.getElementsByTagNameNS(None, u"python").next() self.assertEqual(p1.getAttributeNS(None, u'spam'), u'eggs')
def test_xpath(self): doc = bindery.parse(self.MONTY_XML) m = doc.monty p1 = doc.monty.python self.assertEqual(p1.xml_select(u'string(@spam)'), u'eggs') for p, line in zip(doc.xml_select(u'//python'), self.lines_py): output = cStringIO.StringIO() xml_print(p, stream=output) self.assertEqual(output.getvalue(), line)
def test_namespace_free_xhtml2(self): 'namespace-free XHTML' + '...as HTML with Print' doc = self._build_namespace_free_xhtml() s = cStringIO.StringIO() xml_print(doc, stream=s, is_html=True) out = s.getvalue() #self.assertEqual(out, ATOMENTRY1) diff = treecompare.document_diff(out, XHTML_EXPECTED_2) diff = '\n'.join(diff) self.assertFalse(diff, msg=(None, diff))
def test_bindery(self): doc = bindery.parse(self.MONTY_XML) m = doc.monty p1 = doc.monty.python #or m.python; p1 is just the first python element self.assertEqual(p1.xml_attributes[(None, u'spam')], u'eggs') self.assertEqual(p1.spam, u'eggs') for p, line in zip(doc.monty.python, self.lines_py): #The loop will pick up both python elements output = cStringIO.StringIO() xml_print(p, stream=output) self.assertEqual(output.getvalue(), line)
def test_simple_atom_entry(): '''Basic ns fixup upon mutation''' doc = bindery.parse(ATOMENTRY1) s = cStringIO.StringIO() xml_print(doc, stream=s) out = s.getvalue() #self.assertEqual(out, ATOMENTRY1) diff = treecompare.xml_diff(out, ATOMENTRY1) diff = '\n'.join(diff) assert not diff, "Expected=%r, returned=%r diff=%r" % (ATOMENTRY1, out, diff) #Make sure we can parse the result doc2 = bindery.parse(out)
def test_namespace_free_xhtml3(self): 'namespace-free XHTML' + '...as XML with pretty print' doc = self._build_namespace_free_xhtml() s = cStringIO.StringIO() xml_print(doc, stream=s, indent=True) out = s.getvalue() #self.assertEqual(out, ATOMENTRY1) diff = treecompare.xml_diff(out, XHTML_EXPECTED_3, whitespace=False) diff = '\n'.join(diff) self.assertFalse(diff, msg=(None, diff)) #Make sure we can parse the result doc2 = bindery.parse(out)
def test_minimal_document(self): 'minimal document with DOCTYPE' doc = amara.tree.entity() doc.xml_append(amara.tree.element(None, u'foo')) doc.xml_public_id = u'myPub' doc.xml_system_id = u'mySys' s = cStringIO.StringIO() xml_print(doc, stream=s) out = s.getvalue() #self.assertEqual(out, ATOMENTRY1) diff = treecompare.xml_diff(out, DOCTYPE_EXPECTED_1) diff = '\n'.join(diff) self.assertFalse(diff, msg=(None, diff))
def test_bindery(self): doc = bindery.parse(self.MONTY_XML) m = doc.monty p1 = doc.monty.python #or m.python; p1 is just the first python element self.assertEqual(p1.xml_attributes[(None, u'spam')], u'eggs') self.assertEqual(p1.spam, u'eggs') for p, line in zip( doc.monty.python, self.lines_py): #The loop will pick up both python elements output = cStringIO.StringIO() xml_print(p, stream=output) self.assertEqual(output.getvalue(), line)
def test_reading_building(self): doc = tree.entity() doc.xml_append(tree.processing_instruction(u'xml-stylesheet', u'href="classic.xsl" type="text/xml"')) A = tree.element(None, u'A') A.xml_attributes[u'a'] = u'b' A.xml_append(tree.text(u'One')) doc.xml_append(A) doc.xml_append(tree.comment(u"This is easy")) doc2 = amara.parse('docs/whatsnew_doc1.xml') output1 = cStringIO.StringIO() output2 = cStringIO.StringIO() xml_print(doc, stream=output1) xml_print(doc2, stream=output2) self.assertEqual(output1.getvalue(), output2.getvalue()) return
def run(source, xpattern, xpath, limit, sentinel, display, prefixes): prefixes = prefixes or {} try: prefixes = dict([p.split('=') for p in prefixes]) except ValueError: raise ValueError("Invalid prefix declaration") #if hasattr(source, 'read'): # if hasattr(source, 'rewind'): # nss = saxtools.sniff_namespace(source) # source.rewind() # else: # source = source.read() # nss = saxtools.sniff_namespace(source) #else: # nss = saxtools.sniff_namespace(source) #nss.update(prefixes) nss = prefixes doc = amara.parse(source) #nodes = amara.pushbind(source, xpattern, prefixes=nss) count = 0 search_space = doc.xml_select(u'//' + xpattern.lstrip(u'//')) #FIXME: Until we have something pushbind-like trim all nodes not in the search space for node in search_space: if not xpath or node.xml_select(xpath): count += 1 if display: #Print specified subset result = node.xml_select(display) if hasattr(result, 'next'): #print '\n'.join([ n.xml_type == tree.attribute.xml_type and n.xml_value or amara.xml_print(n) for n in result ]) print '\n'.join( (n.xml_type == tree.attribute.xml_type and n.xml_value or amara.xml_print(n) for n in result)) else: print result else: #Print the whole thing try: amara.xml_print(node) except AttributeError: print unicode(node).encode('utf-8') if limit != -1 and count >= limit: break if sentinel and node.xml_select(sentinel): break print return
def test_reading_building(self): doc = tree.entity() doc.xml_append( tree.processing_instruction(u'xml-stylesheet', u'href="classic.xsl" type="text/xml"')) A = tree.element(None, u'A') A.xml_attributes[u'a'] = u'b' A.xml_append(tree.text(u'One')) doc.xml_append(A) doc.xml_append(tree.comment(u"This is easy")) doc2 = amara.parse('docs/whatsnew_doc1.xml') output1 = cStringIO.StringIO() output2 = cStringIO.StringIO() xml_print(doc, stream=output1) xml_print(doc2, stream=output2) self.assertEqual(output1.getvalue(), output2.getvalue()) return
def run(source, xpattern, xpath, limit, sentinel, display, prefixes): prefixes = prefixes or {} try: prefixes = dict([ p.split('=') for p in prefixes ]) except ValueError: raise ValueError("Invalid prefix declaration") #if hasattr(source, 'read'): # if hasattr(source, 'rewind'): # nss = saxtools.sniff_namespace(source) # source.rewind() # else: # source = source.read() # nss = saxtools.sniff_namespace(source) #else: # nss = saxtools.sniff_namespace(source) #nss.update(prefixes) nss = prefixes doc = amara.parse(source) #nodes = amara.pushbind(source, xpattern, prefixes=nss) count = 0 search_space = doc.xml_select(u'//' + xpattern.lstrip(u'//')) #FIXME: Until we have something pushbind-like trim all nodes not in the search space for node in search_space: if not xpath or node.xml_select(xpath): count += 1 if display: #Print specified subset result = node.xml_select(display) if hasattr(result, 'next'): #print '\n'.join([ n.xml_type == tree.attribute.xml_type and n.xml_value or amara.xml_print(n) for n in result ]) print '\n'.join( (n.xml_type == tree.attribute.xml_type and n.xml_value or amara.xml_print(n) for n in result) ) else: print result else: #Print the whole thing try: amara.xml_print(node) except AttributeError: print unicode(node).encode('utf-8') if limit != -1 and count >= limit: break if sentinel and node.xml_select(sentinel): break print return
def test_tree(self): #Node types use string rather than numerical constants now #The root node type is called entity doc = amara.parse(self.MONTY_XML) self.assertEqual(doc.xml_type, tree.entity.xml_type) m = doc.xml_children[0] #xml_children is a sequence of child nodes self.assertEqual(m.xml_local, u'monty') #local name, i.e. without any prefix self.assertEqual(m.xml_qname, u'monty') #qualified name, e.g. includes prefix self.assertEqual(m.xml_prefix, None) self.assertEqual(m.xml_qname, u'monty') #qualified name, e.g. includes prefix self.assertEqual(m.xml_namespace, None) self.assertEqual( m.xml_name, (None, u'monty')) #The "universal name" or "expanded name" self.assertEqual(m.xml_parent, doc) p1 = m.xml_children[1] XML_output = '<python spam="eggs">What do you mean "bleh"</python>' output = cStringIO.StringIO() xml_print(p1, stream=output) self.assertEqual(output.getvalue(), XML_output) self.assertEqual(p1.xml_attributes[(None, u'spam')], u"eggs") #Some manipulation p1.xml_attributes[(None, u'spam')] = u"greeneggs" self.assertEqual(p1.xml_attributes[(None, u'spam')], u"greeneggs") p1.xml_children[0].xml_value = u"Close to the edit" output = cStringIO.StringIO() xml_print(p1, stream=output) self.assertEqual( output.getvalue(), '<python spam="greeneggs">Close to the edit</python>')
assert doc.xml_type == tree.entity.xml_type m = doc.xml_children[0] # xml_children is a sequence of child nodes assert m.xml_local == u"monty" # local name, i.e. without any prefix assert m.xml_qname == u"monty" # qualified name, e.g. includes prefix assert m.xml_prefix == None assert m.xml_qname == u"monty" # qualified name, e.g. includes prefix assert m.xml_namespace == None assert m.xml_name == (None, u"monty") # The "universal name" or "expanded name" assert m.xml_parent == doc p1 = m.xml_children[0] from amara import xml_print # <python spam="eggs">What do you mean "bleh"</python> xml_print(p1) print print p1.xml_attributes[(None, u"spam")] # Some manipulation p1.xml_attributes[(None, u"spam")] = u"greeneggs" p1.xml_children[0].xml_value = u"Close to the edit" xml_print(p1) print print print "---------" "Bindery" from amara import bindery from amara import xml_print
# print dir(doc) # print dir(doc.monty.python) # print doc.monty.python.xml_namespaces[None] # print doc.xml_namespaces print print "---------" "Attribute constraint class" doc = bindery.parse(MONTY_XML) c = attribute_constraint(None, u"ministry", u"nonesuch") doc.monty.python.xml_model.add_constraint(c, validate=True) xml_print(doc) print print "---------" "Child element constraint class" SVG = """<?xml version="1.0" encoding="utf-8"?> <svg version="1.1" baseProfile="full" xmlns="http://www.w3.org/2000/svg"> <title>A pair of lines and a pair of ellipses</title> <g> <ellipse cx="150" cy="100" rx="100" ry="50"/> <line x1="450" y1="50" x2="550" y2="150"/> </g> <g> <title>Rotated shapes</title> <ellipse cx="150" cy="300" rx="100" ry="50"
def test_tnb_feed(): doc = bindery.parse(TNBFEED) s = cStringIO.StringIO() xml_print(doc.feed.entry, stream=s) out = s.getvalue() doc2 = bindery.parse(out)
#The root node type is called entity assert doc.xml_type == tree.entity.xml_type m = doc.xml_children[0] #xml_children is a sequence of child nodes assert m.xml_local == u'monty' #local name, i.e. without any prefix assert m.xml_qname == u'monty' #qualified name, e.g. includes prefix assert m.xml_prefix == None assert m.xml_qname == u'monty' #qualified name, e.g. includes prefix assert m.xml_namespace == None assert m.xml_name == (None, u'monty') #The "universal name" or "expanded name" assert m.xml_parent == doc p1 = m.xml_children[0] from amara import xml_print #<python spam="eggs">What do you mean "bleh"</python> xml_print(p1) print print p1.xml_attributes[(None, u'spam')] #Some manipulation p1.xml_attributes[(None, u'spam')] = u"greeneggs" p1.xml_children[0].xml_value = u"Close to the edit" xml_print(p1) print print print '---------' 'Bindery' from amara import bindery from amara import xml_print
if child.xml_qname == u'folder': for a_folder in xbel1.folder: if unicode(child.title) == unicode(a_folder.title): merge_folders(a_folder, child) break else: xbel1.xml_append(child) elif child.xml_qname == u'bookmark': xbel1.xml_append(child) return doc1 = bindery.parse(BM1) doc2 = bindery.parse(BM2) xbel_merge(doc1.xbel, doc2.xbel) xml_print(doc1, indent=True) print print '---------' 'Merge XBEL by grouping iterators' BM1 = 'bm1.xbel' BM2 = 'bm2.xbel' import itertools import functools from amara import bindery, xml_print from amara.bindery.util import property_str_getter title_getter = functools.partial(property_str_getter, 'title') def merge(f1, f2):