def runTest(self): '''Test ParseTreeBuilder class''' # - Test \n before and after h / p / pre # - Test break line into lines input = '''\ <?xml version='1.0' encoding='utf-8'?> <zim-tree> foo<h level="1">bar</h>baz dus<pre>ja</pre>hmm <h level="2">foo </h>bar dus ja <emphasis>hmm dus ja </emphasis>grrr <strong>foo bar </strong> <strike></strike><emphasis> </emphasis>. </zim-tree>''' wanted = '''\ <?xml version='1.0' encoding='utf-8'?> <zim-tree> foo <h level="1">bar</h> baz dus <pre>ja </pre> hmm <h level="2">foo</h> bar dus ja <emphasis>hmm</emphasis> <emphasis>dus ja</emphasis> grrr <strong>foo</strong> <strong>bar</strong> . </zim-tree>''' # For some reason this does not work with cElementTree.XMLBuilder ... from xml.etree.ElementTree import XMLTreeBuilder builder = XMLTreeBuilder(target=ParseTreeBuilder()) builder.feed(input) root = builder.close() tree = ParseTree(root) self.assertEqualDiff(tree.tostring(), wanted)
class xmlmodel(object) : def __init__(self, parent = None) : self.data = {} self.axes = {} self.parent = parent def parse_file(self, fname) : if os.path.exist(fname) : self.xml = XMLTreeBuilder(target=self) self.xml.curr = self fh = file(fname) self.xml.feed(fh.read()) def set_key(self, key, value) : if len(key) > 1 : if not self.axes.has_key(key[0]) : self.axes[key[0]] = xmlmodel(self) self.axes[key[0]].set_key(key[1:], value) else : self.data[key[0]] = value def get_key(self, key) : if len(key) > 1 : if not self.axes.has_key(key[0]) : return None return self.axes[key[0]].get_key(key[1:]) else : return self.data.get(key[0]) def start(self, name, attrs) : if name == "data" : self.xml.currkey = attrs['key'] elif name == 'axis' : newaxis = xmlmodel(self.xml.curr) self.axes[attrs['key']] = newaxis self.xml.curr = newaxis def data(self, text) : if self.xml.currkey : self.xml.curr[self.xml.currkey] = text def end(self, name) : if name == "axis" and self.xml.curr.parent : self.xml.curr = self.xml.curr.parent def save(self, fname) : fh = file(fname, "w") fh.writelines(("<?xml version='1.0'?>\n", "<palaso_model>\n")) self.write_xml(fh, "") fh.write("</palaso_model>\n") def write_xml(self, fh, indent) : indent += " " for (k, v) in self.data.iteritems() : fh.write("%s<data key='%s'>%s</data>\n" % (indent, k, v)) for (k, v) in self.axes.iteritems() : fh.write("%s<axis key='%s'>\n" % (indent, k)) v.write_xml(fh, indent) fh.write("%s</axis>\n" % (indent))
def __init__(self, target=None): """Initialize @param target Target class """ from xml.etree.ElementTree import XMLTreeBuilder XMLTreeBuilder.__init__(self, html=0, target=target) self._parser.CommentHandler = self.comment
def get_tree_from_xml(xml): # For some reason this does not work with cElementTree.XMLBuilder ... from xml.etree.ElementTree import XMLTreeBuilder builder = XMLTreeBuilder() builder.feed(xml) root = builder.close() return ParseTree(root)
class xmlmodel(object): def __init__(self, parent=None): self.data = {} self.axes = {} self.parent = parent def parse_file(self, fname): if os.path.exist(fname): self.xml = XMLTreeBuilder(target=self) self.xml.curr = self fh = file(fname) self.xml.feed(fh.read()) def set_key(self, key, value): if len(key) > 1: if not self.axes.has_key(key[0]): self.axes[key[0]] = xmlmodel(self) self.axes[key[0]].set_key(key[1:], value) else: self.data[key[0]] = value def get_key(self, key): if len(key) > 1: if not self.axes.has_key(key[0]): return None return self.axes[key[0]].get_key(key[1:]) else: return self.data.get(key[0]) def start(self, name, attrs): if name == "data": self.xml.currkey = attrs['key'] elif name == 'axis': newaxis = xmlmodel(self.xml.curr) self.axes[attrs['key']] = newaxis self.xml.curr = newaxis def data(self, text): if self.xml.currkey: self.xml.curr[self.xml.currkey] = text def end(self, name): if name == "axis" and self.xml.curr.parent: self.xml.curr = self.xml.curr.parent def save(self, fname): fh = file(fname, "w") fh.writelines(("<?xml version='1.0'?>\n", "<palaso_model>\n")) self.write_xml(fh, "") fh.write("</palaso_model>\n") def write_xml(self, fh, indent): indent += " " for (k, v) in self.data.iteritems(): fh.write("%s<data key='%s'>%s</data>\n" % (indent, k, v)) for (k, v) in self.axes.iteritems(): fh.write("%s<axis key='%s'>\n" % (indent, k)) v.write_xml(fh, indent) fh.write("%s</axis>\n" % (indent))
def __init__(self, filePath, absUrl, xmlIf, treeWrapper): XMLTreeBuilder.__init__( self, target=TreeBuilder(element_factory=ElementExtension)) self._parser.StartNamespaceDeclHandler = self._start_ns self._parser.EndNamespaceDeclHandler = self._end_ns self.namespaces = [] XmlIfBuilderExtensionBase.__init__(self, filePath, absUrl, treeWrapper, xmlIf.elementWrapperClass)
def new_parsetree_from_xml(xml): # For some reason this does not work with cElementTree.XMLBuilder ... from xml.etree.ElementTree import XMLTreeBuilder from zim.formats import ParseTree builder = XMLTreeBuilder() builder.feed(xml) root = builder.close() return ParseTree(root)
def parse_xml(self, raw_xml, parser=None): if not parser: parser = XMLTreeBuilder() parser.feed(raw_xml) self._root = parser.close() return self._root
def runTest(self): '''Test OldParseTreeBuilder class''' # - Test \n before and after h / p / pre # - Test break line into lines input = '''\ <?xml version='1.0' encoding='utf-8'?> <zim-tree> foo<h level="1">bar</h>baz dus<pre>ja</pre>hmm <h level="2">foo </h>bar dus ja <emphasis>hmm dus ja </emphasis>grrr <strong>foo bar </strong> <strike></strike><emphasis> </emphasis>. </zim-tree>''' wanted = '''\ <?xml version='1.0' encoding='utf-8'?> <zim-tree> foo <h level="1">bar</h> baz dus <pre>ja </pre>hmm <h level="2">foo</h> bar dus ja <emphasis>hmm</emphasis> <emphasis>dus ja</emphasis> grrr <strong>foo</strong> <strong>bar</strong> . </zim-tree>''' # For some reason this does not work with cElementTree.XMLBuilder ... from xml.etree.ElementTree import XMLTreeBuilder builder = XMLTreeBuilder(target=OldParseTreeBuilder()) builder.feed(input) root = builder.close() tree = ParseTree(root) self.assertEqual(tree.tostring(), wanted)
def kplist_parse(plist): """Parse a kernel-style property list.""" try: builder = _KPlistBuilder() parser = XMLTreeBuilder(target=builder) parser.feed(plist) return parser.close() except: return None
def __init__(self, filePath, absUrl, xmlIf, treeWrapper): XMLTreeBuilder.__init__( self, target=TreeBuilder( element_factory=ElementExtension)) self._parser.StartNamespaceDeclHandler = self._start_ns self._parser.EndNamespaceDeclHandler = self._end_ns self.namespaces = [] XmlIfBuilderExtensionBase.__init__( self, filePath, absUrl, treeWrapper, xmlIf.elementWrapperClass)
def get_unread_count(self): url = self.urls['reader'] + self.urls['unread-count'] data = self.get_results(url) if data is None: return tree = XMLTreeBuilder() tree.feed(data) root_object = tree.close() li = root_object.getchildren()[1] counts = [] for obj in li: feed_id, count, timestamp = \ [ele.text for ele in obj.getchildren()] counts.append((feed_id, count, timestamp)) return counts
def get_subscriptions(self): url = self.urls['reader'] + self.urls['subscription-list'] data = self.get_results(url) if data is None: return tree = XMLTreeBuilder() tree.feed(data) root_object = tree.close() li = root_object.getchildren()[0] feeds = {} for obj in li: feed_id = obj.getchildren()[0].text title = obj.getchildren()[1].text feeds[feed_id] = title return feeds
def parse_xml(request, user): SoftLine.objects.filter(user=user.id).delete() # SoftLine.objects.all().delete() # print(request.POST) loader = InstalledSoftwareLoader() parser = XMLTreeBuilder(target=loader) parser.feed(request) # with open(file_name, 'rt') as f: # for line in f: # parser.feed(line) parser.close() for item in loader.items: line = SoftLine.objects.create(name=item.name, version=item.version, user=user) # print("Item => '%s'\n" % item.name) return len(loader.items)
def parseXRDS(text): """Parse the given text as an XRDS document. @return: ElementTree containing an XRDS document @raises XRDSError: When there is a parse error or the document does not contain an XRDS. """ try: parser = XMLTreeBuilder() parser.feed(text) element = parser.close() except XMLError, why: exc = XRDSError('Error parsing document as XML') exc.reason = why raise exc
def read_generated_namelist(nml_file=None): """ This will read a set of previously generated namelists from a file. """ if not nml_file: # No file specified return None else: read_dict = {} target = InputXMLToNamelist(read_dict) parser = XMLTreeBuilder(target=target) with open(nml_file,'rt') as f: for line in f: parser.feed(line) parser.close() return read_dict
def csv_treebuilder(): import csv from xml.etree.ElementTree import XMLTreeBuilder import sys class PodcastListToCSV(object): def __init__(self, outputFile): self.writer = csv.writer(outputFile, quoting=csv.QUOTE_NONNUMERIC) self.group_name = '' def start(self, tag, attrib): if tag != 'outline': # Ignore anything not part of the outline return if not attrib.get('xmlUrl'): # Remember the current group self.group_name = attrib['text'] else: # Output a podcast entry self.writer.writerow((self.group_name, attrib['text'], attrib['xmlUrl'], attrib.get('htmlUrl', ''))) def end(self, tag): # Ignore closing tags pass def data(self, data): # Ignore data inside nodes pass def close(self): return target = PodcastListToCSV(sys.stdout) parser = XMLTreeBuilder(target=target) with open('podcasts.opml', 'rt') as f: for line in f: parser.feed(line)
def build_sql_queries(filename, bbox=None): if bbox != None: target = SQLBuilder(box_node_list='box_node_list', box_way_list='box_way_list') else: target = SQLBuilder() init_queries = [] ## If bounding box is given, create a temporary table and ## store all matching nodes there if bbox != None: ## Create tables init_queries.append("CREATE TEMPORARY TABLE box_node_list " "(id bigint PRIMARY KEY) ON COMMIT DROP") init_queries.append("CREATE TEMPORARY TABLE box_way_list " "(id bigint PRIMARY KEY) ON COMMIT DROP") ## Fill them bbox_sql = "GeomFromEWKT('POLYGON((%f %f,%f %f,%f %f,%f %f,%f %f))')"%\ (bbox[0],bbox[1],bbox[2],bbox[1],bbox[2],bbox[3],bbox[0],bbox[3], bbox[0],bbox[1]) srid = "find_srid('','nodes','geom')" init_queries.append("INSERT INTO box_node_list SELECT id FROM nodes " "WHERE (geom && SetSRID(%s,%s) )" % (bbox_sql, srid)) init_queries.append("INSERT INTO box_way_list " "SELECT wn.way_id FROM way_nodes wn " "INNER JOIN box_node_list n ON wn.node_id = n.id " "GROUP BY wn.way_id") parser = XMLTreeBuilder(target=target) parser.feed(open(filename).read()) return init_queries, target.result
def body(self, xml): """ Body importer """ if isinstance(xml, (str, unicode)): parser = XMLTreeBuilder() parser.feed(xml) tree = parser.close() tree = ElementTree(tree) elem = tree.getroot() else: elem = xml if elem.tag != 'object': raise AttributeError('Invalid xml root element %s' % elem.tag) name = elem.get('name') if not name: raise AttributeError('No name provided for object') if hasattr(self.context, '__name__') and ( name != self.context.__name__): raise AttributeError(('XML root object name %s ' 'should match context name %s') % ( name, self.context.__name__)) for child in elem.getchildren(): if child.tag == 'property': self.attribute = child elif child.tag == 'object': self.child = child wftool = getToolByName(self.context, 'portal_workflow') state = wftool.getInfoFor(self.context, 'review_state', '(Unknown)') if state != 'published': try: wftool.doActionFor(self.context, 'publish', comment='Initial import') except Exception, err: logger.exception(err)
def build_sql_queries(filename, bbox=None): if bbox != None: target = SQLBuilder(box_node_list='box_node_list', box_way_list='box_way_list') else: target = SQLBuilder() init_queries = [] ## If bounding box is given, create a temporary table and ## store all matching nodes there if bbox != None: ## Create tables init_queries.append("CREATE TEMPORARY TABLE box_node_list " "(id bigint PRIMARY KEY) ON COMMIT DROP") init_queries.append("CREATE TEMPORARY TABLE box_way_list " "(id bigint PRIMARY KEY) ON COMMIT DROP") ## Fill them bbox_sql = "GeomFromEWKT('POLYGON((%f %f,%f %f,%f %f,%f %f,%f %f))')"%\ (bbox[0],bbox[1],bbox[2],bbox[1],bbox[2],bbox[3],bbox[0],bbox[3], bbox[0],bbox[1]) srid = "find_srid('','nodes','geom')" init_queries.append("INSERT INTO box_node_list SELECT id FROM nodes " "WHERE (geom && SetSRID(%s,%s) )"%(bbox_sql,srid)) init_queries.append("INSERT INTO box_way_list " "SELECT wn.way_id FROM way_nodes wn " "INNER JOIN box_node_list n ON wn.node_id = n.id " "GROUP BY wn.way_id") parser = XMLTreeBuilder(target=target) parser.feed(open(filename).read()) return init_queries, target.result
def _set_led(self, val): global glob_led_policy, glob_led_signal_list if not self.preview_list[val].display_type == 'not_checked': return new_signal = ElementTree() parser = XMLTreeBuilder() if val == 3: parser.feed(glob_signal_ALL) else: parser.feed(glob_signal_RBO) new_signal._root = parser.close() new_signal_list = new_signal.getiterator("signal") new_signal_list[0].attrib = glob_led_signal_list[ self.signal_num].attrib new_signal_list[0].tail = glob_led_signal_list[self.signal_num].tail glob_led_policy.remove(glob_led_signal_list[self.signal_num]) glob_led_policy.insert(self.signal_num, new_signal_list[0]) glob_led_signal_list[self.signal_num] = new_signal_list[0] for i in range(len(self.preview_list)): self.preview_list[i].display_type = self._get_led(i) self.preview_list[i].reset_view()
def _start_list(self, tag, attrib_in): elem = XMLTreeBuilder._start_list(self, tag, attrib_in) self.start(elem, attrib_in)
def parse(self, source, parser=None): if not parser: parser = XMLTreeBuilder() parser.feed(source) self._root = parser.close() return self._root
def parse_file(self, fname) : if os.path.exist(fname) : self.xml = XMLTreeBuilder(target=self) self.xml.curr = self fh = file(fname) self.xml.feed(fh.read())
if not attrib.get('xmlUrl'): # Remember the current group self.group_name = attrib['text'] else: # Output a podcast entry self.writer.writerow(( self.group_name, attrib['text'], attrib['xmlUrl'], attrib.get('htmlUrl', ''), )) def end(self, tag): # Ignore closing tags pass def data(self, data): # Ignore data inside nodes pass def close(self): # Nothing special to do here return target = PodcastListToCSV(sys.stdout) parser = XMLTreeBuilder(target=target) with open('podcasts.opml', 'rt') as f: for line in f: parser.feed(line) parser.close()
from xml.etree.ElementTree import XMLTreeBuilder class MaxDepth: # The target object of the parser maxDepth = 0 depth = 0 def start(self, tag, attrib): # Called for each opening tag. self.depth += 1 if self.depth > self.maxDepth: self.maxDepth = self.depth def end(self, tag): # Called for each closing tag. self.depth -= 1 def data(self, data): pass # We do not need to do anything with data. def close(self): # Called when all data has been parsed. return self.maxDepth target = MaxDepth() parser = XMLTreeBuilder(target=target) n = int(raw_input()) exampleXml = "" for _ in range(n): exampleXml += raw_input()+"\n" parser.feed(exampleXml) print parser.close()-1
def __init__(self): XMLTreeBuilder.__init__(self) parser = self._parser parser.CommentHandler = self.comment parser.ProcessingInstructionHandler = self.pi
def _end(self, tag): """Do this when we're out of XML!""" return self._endhandler(XMLTreeBuilder._end(self, tag))
def __init__(self): XMLTreeBuilder.__init__(self) self.parser.StartNamespaceDeclHandler = self._start_ns self.namespaces = {u'http://www.w3.org/XML/1998/namespace': u'xml'}
def _end(self, tag): elem = XMLTreeBuilder._end(self, tag) self.end(elem)
def start(self, tag, attrib): if tag != "outline": # Ignora qualsiasi al di fuori di outline return if not attrib.get("xmlUrl"): # Ricorda il gruppo corrente self.group_name = attrib["text"] else: # Scrive una voce di podcast self.writer.writerow((self.group_name, attrib["text"], attrib["xmlUrl"], attrib.get("htmlUrl", ""))) def end(self, tag): # Ignora i tag di chiusura pass def data(self, data): # Ignora i dati all'interno dei nodi pass def close(self): # Nulla di speciale da fare qui return target = PodcastListToCSV(sys.stdout) parser = XMLTreeBuilder(target=target) with open("podcasts.opml", "rt") as f: for line in f: parser.feed(line) parser.close()
def __init__(self): XMLTreeBuilder.__init__(self) self._parser.StartNamespaceDeclHandler = self._start_ns self.namespaces = {}
def parse_file(self, fname): if os.path.exist(fname): self.xml = XMLTreeBuilder(target=self) self.xml.curr = self fh = file(fname) self.xml.feed(fh.read())
def __init__(self, callback, *args, **kwargs): self._endhandler = callback XMLTreeBuilder.__init__(self, *args, **kwargs)
def __init__ ( self, html = 0, target = None ): XMLTreeBuilder.__init__( self, html, target ) self._parser.CommentHandler = self.handle_comment