def readDistributionTable(self, table): """ returns a list conataining dictionaries with the distribution data reads from the current file """ self._dist_data = {} self._dist_list = [] class XmlReader(ContentHandler): def __init__(XmlReader, scrwid=79, *args): ContentHandler.__init__(XmlReader, *args) def startElement(XmlReader, name, attrs): # print "start element",name if name == "rule": self._dist_data["ftp_source"] = attrs.get("ftp_source") self._dist_data["remote_dir"] = attrs.get("remote_dir") self._dist_data["target"] = attrs.get("target") self._dist_data["ftp_mode"] = attrs.get("ftp_mode") def endElement(XmlReader, name): if name == "rule": self._dist_list.append(dict(self._dist_data)) self._dist_data.clear() def characters(XmlReader, chars): pass parse(os.path.join(self.project, "setup", table), XmlReader()) return self._dist_list
def headersParser(headers): """ This function calls a class that parses the input HTTP headers to fingerprint the back-end database management system operating system and the web application technology """ # It is enough to parse the headers on first four HTTP responses if kb.headersCount > 3: return kb.headersCount += 1 topHeaders = { "cookie": "%s/cookie.xml" % paths.SQLMAP_XML_BANNER_PATH, "microsoftsharepointteamservices": "%s/sharepoint.xml" % paths.SQLMAP_XML_BANNER_PATH, "server": "%s/server.xml" % paths.SQLMAP_XML_BANNER_PATH, "servlet-engine": "%s/servlet.xml" % paths.SQLMAP_XML_BANNER_PATH, "set-cookie": "%s/cookie.xml" % paths.SQLMAP_XML_BANNER_PATH, "x-aspnet-version": "%s/x-aspnet-version.xml" % paths.SQLMAP_XML_BANNER_PATH, "x-powered-by": "%s/x-powered-by.xml" % paths.SQLMAP_XML_BANNER_PATH, } for header in headers: if header in topHeaders.keys(): value = headers[header] xmlfile = topHeaders[header] checkFile(xmlfile) handler = FingerprintHandler(value, kb.headersFp) parse(xmlfile, handler) parse(paths.GENERIC_XML, handler)
def __call__(self, play=False, fav = 1): flag = self.plugin.Execute('screamer.exe',self.plugin.ScreamerPath) if self.plugin.path2: self.plugin.Execute('Start_SR_Events.exe',self.plugin.path2) if flag: if self.plugin.path2: self.plugin.Execute('Start_SR_Events.exe',self.plugin.path2) if play: for n in range(50): sleep(.2) hwnds = Handle() if len(hwnds) > 0: flag = False break if not flag: sleep(2) ScreamerPath = self.plugin.ScreamerPath xmltoparse = ScreamerPath+'\\favorites.xml' self.dh2 = my_xml_handler2() sax.parse(xmltoparse.encode(eg.systemEncoding), self.dh2) if fav <= len(self.plugin.favList): self.plugin.fav_num=fav-1 PostMessage(hwnds[0], WM_COMMAND, 9216+fav, 0) return str(fav)+": "+self.plugin.favList[self.plugin.fav_num] else: return self.text.over % (str(fav),\ str(len(self.plugin.favList))) else: return self.plugin.text.text1 else: return self.text.alt_ret
def main(filename): f = open(filename) wp = WikiParser() sax.parse(f, wp) f.close() wp.out.close()
def get_cycles(recipe_xml_filepath): """ returns the number of cycles found in Recipe*.xml """ handler = CycleXmlHandler() sax.parse(recipe_xml_filepath, handler) return handler.cycle_count
def __init__(self, xmlfile, drawing): # fill drawing's geometry lists self.vertices = drawing.vertices # sorted list of vertices self.edges = drawing.edges # sorted list of edges self.facets = drawing.facets # sorted list of facets self.loops = drawing.loops # chains of vertices in order connected self.polygons = drawing.polygons # parse path vertices into loops sax.parse(xmlfile, self) # discard open loops self.loops = [loop for loop in self.loops if loop.closed] # make sure loops run forwards (counterclockwise) for loop in self.loops: if not loop.is_forward(): loop._vertices.reverse() # find loops that are holes in other loops and build polygons self._polygons_from_loops() # generate facets for each polygon for polygon in self.polygons: self._facets_from_loops(polygon)
def _read_xml(self): '''Read input as Excel XML. This creates the entire message file collection''' excelHandler = ExcelHandler() sax.parse(self.input, excelHandler) self.apps = excelHandler.tables.keys() for app in self.apps: table = excelHandler.tables[app] langs = table[0][2:] # first row is the languages numlang = len(langs) for i in xrange(numlang): lang = langs[i] # Extract language from header line if ':' in lang: lang = lang[lang.index(':')+1:].strip() # if language not specified in the XML and only one # language requested then use requested language # instead if (lang == '' or lang == 'new language') and len(self.languages) == 1: lang = self.languages[0] if lang in self.languages: mf = MessageFile() self.files.append(mf) mf.app = app mf.lang = lang say(DEBUG, 'i %d app %s lang %s' % (i, app, mf.lang)) for row in table[1:]: if len(row) >= 2+numlang: mf.tags[row[1]] = row[2+i]
def get_word_paragraphs(xmlfile): """Return a list of paragraphs from the docx-formatted xml file at xmlfile""" class tagHandler(ContentHandler): def __init__(self): self.paragraphMarker = "w:p" self.textMarker = "w:t" self.paragraphs = [] self.string = "" self.inText = False self.inParagraph = False def startElement(self, name, attr): if name == self.textMarker: self.inText = True elif name == self.paragraphMarker: self.inParagraph = True def endElement(self, name): if name == self.textMarker: self.inText = False elif name == self.paragraphMarker: self.inParagraph == False self.paragraphs.append(self.string) self.string = "" def characters(self, ch): if self.inText: self.string+=ch handler = tagHandler() parse(xmlfile, handler) return handler.paragraphs
def main(): parser = argparse.ArgumentParser(description='Migrate XML to YAML') parser.add_argument('idspace', type=str, help='the project IDSPACE, e.g. FOO') parser.add_argument('xml_file', type=argparse.FileType('r'), default=sys.stdin, nargs='?', help='read from the XML file (or STDIN)') parser.add_argument('yaml_file', type=argparse.FileType('w'), default=sys.stdout, nargs='?', help='write to the YAML file (or STDOUT)') args = parser.parse_args() args.upper_idspace = args.idspace.upper() args.lower_idspace = args.idspace.lower() args.base_url = '/obo/' + args.lower_idspace sax = xml.sax.make_parser() sax.setContentHandler(OCLCHandler(args)) sax.parse(args.xml_file) entries = exact + sorted(prefix, key=lambda k: len(k['id']), reverse=True) if len(entries) == 0: raise ValueError('No entries to migrate') args.yaml_file.write(header_template % (args.base_url, args.upper_idspace, args.base_url, args.lower_idspace, args.lower_idspace)) for entry in entries: args.yaml_file.write(entry_template % (entry['rule'], entry['id'], entry['url']))
def readNet(filename, **others): """ load a .net.xml file The following named options are supported: 'net' : initialize data structurs with an existing net object (default Net()) 'withPrograms' : import all traffic light programs (default False) 'withLatestPrograms' : import only the last program for each traffic light. This is the program that would be active in sumo by default. (default False) 'withConnections' : import all connections (default True) 'withFoes' : import right-of-way information (default True) 'withInternal' : import internal edges and lanes (default False) """ netreader = NetReader(**others) try: if not os.path.isfile(filename): print("Network file '%s' not found" % filename, file=sys.stderr) sys.exit(1) parse(filename, netreader) except None: print( "Please mind that the network format has changed in 0.13.0, you may need to update your network!", file=sys.stderr) sys.exit(1) return netreader.getNet()
def main(argv=None): opts, args = getopt.getopt(sys.argv[1:], "i:o:h", ["in=", "out=", "help"]) input_file = sys.stdin output_file = sys.stdout for o, a in opts: if o in ("-i", "--in"): input_file = open(a, 'r') if o in ("-o", "--out"): output_file = open(a, 'w') if o in ("-h", "--help"): usage() sys.exit(0) # Using the SAX parser as it is at least 4X faster and far, far # smaller on this dataset than the DOM-based interface in xml.dom.minidom. # With SAX and a 5.4MB xml file, this requires about seven seconds of # wall-clock time and 32MB VSZ. With the DOM interface, about 22 seconds # and over 270MB VSZ. handler = SimPerfHostXMLParser() sax.parse(input_file, handler) if input_file != sys.stdin: input_file.close() # Various format fixups: string-to-num, gauge-to-counts, add # a time stamp, etc. simperf_host_xml_fixup(handler) # Create JSONable dict with interesting data and format/print it print >>output_file, simplejson.dumps({ "step" : handler.rrd_step, "lastupdate": handler.rrd_last_update * 1000, "ds" : handler.rrd_ds, "database" : handler.rrd_records }) return 0
def do_check(self): # check the http code code = self.response.getcode() if code != 200: logging.warning('%s returned %d' % (self.url, code)) return False # check the mime type mime = self.response.info().type if mime != self.conf['mime']: logging.warning('Expected mime type: %s, but got %s' % ( self.conf['mime'], mime)) return False # do some extended checks for XML if mime.endswith('xml') and self.conf['root']: class Handler(sax.handler.ContentHandler): def startElement(self, name, attrs): if not hasattr(self, 'root'): self.root = name h = Handler() sax.parse(self.response, h) if h.root != self.conf['root']: logging.warning('Expected root element: %s, but got %s' % ( self.conf['root'], h.root)) return False return True
def main(): handler = BioProjectXMLHandler() sax.parse(open('/Users/admin/Documents/rnammer_errors_bioprojects.xml', 'rb'), handler) bioprojects = handler.results; print 'Bioproject_Id\tBioproject\tINSDC/Chromosomes\tINSDC/Plasmids' for bioproject in bioprojects: print str(bioproject.bioproject_id) + '\t' + str(bioproject.bioproject_accession) + '\t' + str(bioproject.insdc_chromosomes) + '\t' + str(bioproject.insdc_plasmids)
def main(): parser = argparse.ArgumentParser(description='Migrate XML to YAML') parser.add_argument('base_url', type=str, help='the base URL, e.g. /obo/foo') parser.add_argument('xml_file', type=argparse.FileType('r'), default=sys.stdin, nargs='?', help='read from the XML file (or STDIN)') parser.add_argument('yaml_file', type=argparse.FileType('w'), default=sys.stdout, nargs='?', help='write to the YAML file (or STDOUT)') args = parser.parse_args() sax = xml.sax.make_parser() sax.setContentHandler(OCLCHandler(args)) sax.parse(args.xml_file) args.yaml_file.write(header_template % (args.base_url, args.base_url)) entries = exact + sorted(prefix, key=lambda k: len(k['id']), reverse=True) for entry in entries: args.yaml_file.write(entry_template % (entry['rule'], entry['id'], entry['url']))
def loadMap(filename, map=Map()): # Parse the file with our custom handler loader = MapLoader() loader.map = map parse(filename, loader) return loader.map
def parse_sax(self, xmlfile, **kwargs): if PY3: result = io.StringIO() else: result = io.BytesIO() handler = XMLGenerator(result) sax.parse(xmlfile, handler, **kwargs) return result.getvalue()
def parse(self, languages, filename): """ Parse XML "languages" must be a list """ self._languages = languages parse(filename, self)
def parse_file(file): global graph graph = G.Graph() structure_handler = StructureHandler() XS.parse(file, structure_handler) return graph
def readNet(filename, **others): netreader = NetReader(**others) try: parse(filename, netreader) except KeyError: print >> sys.stderr, "Please mind that the network format has changed in 0.13.0, you may need to update your network!" raise return netreader.getNet()
def __init__(self, filename): logging.debug("Trying to parse langs.xml") self.lh = LangHandler() try: sax.parse(filename, self.lh) logging.info("XML file succesfully parsed") except: logging.info("Error parsing XML file")
def handle(self, input, config): handlers = self.multiple_handlers(config) parse(input, handlers) result = [] for defined_plugin in config.plugins(): plugin = self.find_plugin(defined_plugin) plugin.append_csv_cell_to(result) return result
def check_xml(): for directory in ("xml",): for file in ls('../%s/*.xml' % directory): try: print file parse(file, ContentHandler()) except: raise
def main(argv, outfile=None, vias={}, calledBy=""): routefile = argv[0] attrList = argv[1:] if outfile is None: parse(routefile, RouteReader(attrList, sys.stdout, vias, calledBy)) else: with open(outfile, 'w') as outf: parse(routefile, RouteReader(attrList, outf, vias, calledBy))
def __start__(self, ScreamerPath, path2 = None): self.ScreamerPath = ScreamerPath self.path2 = path2 xmltoparse = ScreamerPath+'\\screamer.xml' self.dh = my_xml_handler1() sax.parse(xmltoparse.encode(eg.systemEncoding), self.dh) xmltoparse = self.dh.document['LanguageFile'] sax.parse(xmltoparse.encode(eg.systemEncoding), self.dh)
def main(argv, outfile=None): routefile = argv[0] attrList = argv[1:] if outfile is None: parse(routefile, RouteReader(attrList, sys.stdout)) else: with open(outfile, 'w') as outf: parse(routefile, RouteReader(attrList, outf))
def load(): remote = urllib.urlopen(TITLES) handler = AnidbHandler() try: sax.parse(remote, handler) except ValueError: pass
def parseXmlFile(XMLFileData, handler): """ Parses XML file by a given handler """ try: with contextlib.closing(StringIO(XMLFileData)) as stream: parse(stream, handler) except Exception,e: print str(e)
def parse(self, fileobj): channels = defaultdict(str) programme = defaultdict(list) sax = xml.sax.make_parser() sax.setContentHandler(SAXParser.XMLTVHandler(channels, programme)) sax.parse(fileobj) return self.join(channels, programme)
def parse(self, filename, language_model): self.__language_model = language_model self.__command = None self.__argument = None LOG.debug("parsing %s" % filename) sax.parse(filename, self)
def read_gml(filename): CurrentHandler=ITNHandler() sax.parse(filename, CurrentHandler) CurrentData = ITNData(CurrentHandler.roads, CurrentHandler.roadNodes, CurrentHandler.roadLinks, CurrentHandler.roadLinkInformations, CurrentHandler.roadRouteInformations) return CurrentData
def read_interval2(filepath, element, attrsconfigs): # print 'read_interval2' attrsconfigs_cumulative = [] attrsconfigs_average = [] for attrsconfig in attrsconfigs: # print ' check',attrsconfig.attrname if hasattr(attrsconfig, 'is_average'): if attrsconfig.is_average: attrsconfigs_average.append(attrsconfig) else: attrsconfigs_cumulative.append(attrsconfig) else: attrsconfigs_cumulative.append(attrsconfig) reader = IntervalAvReader2(element, attrsconfigs_cumulative, attrsconfigs_average) #parser = make_parser() # parser.setContentHandler(reader) #fn = '"'+filepath+'"' # print 'read_interval >'+fn+'<' # print ' >'+filepath+'<' # parser.parse(filepath) parse(filepath, reader) return reader.get_ids(), reader.get_data(), reader.get_interval()
def parseAndWrite(xmlfile, ovfile): log("parsing xml file %s" % (xmlfile, )) layerlist = [] parser = sax.parse(xmlfile, ListHandler(layerlist)) if len(layerlist) > 0: log("created %d layers from %s" % (len(layerlist), xmlfile)) layerlist.sort(key=lambda x: x.maxzoom, reverse=True) try: writeOverview(ovfile, layerlist) return True except: log("error while creating %s:%s" % (ovfile, traceback.format_exc())) else: log("xml file %s did not contain any layers" % (xmlfile, )) return False
def readConfigAndCreateHandlers(self, filename): AVNLog.info("reading config %s", filename) if not os.path.exists(filename): AVNLog.error("unable to read config file %s", filename) return False try: self.currentHandlerData = None self.currentHandlerClass = None parser = sax.parse(filename, self) except: AVNLog.error("error parsing cfg file %s : %s", filename, traceback.format_exc()) return False for handler in avnav_handlerList.getAllHandlerClasses(): ai = handler.autoInstantiate() if ai: existing = AVNWorker.findHandlerByName(handler.getConfigName(), True) if existing is None: AVNLog.info("auto instantiate for %s", handler.getConfigName()) if isinstance(ai, str): try: self.restrictedHandler = handler parser = sax.parseString(ai, self) self.restrictedHandler = None except Exception: AVNLog.error( "error parsing default config %s for %s:%s", ai, handler.getConfigName(), sys.exc_info()[1]) return False else: cfg = handler.parseConfig({}, handler.getConfigParam(None)) cfg.update(self.baseParam) handler.createInstance(cfg) return len(AVNWorker.getAllHandlers()) > 0
def Configure(self, fore=(0, 0, 0), back=(255, 255, 255), fontInfo=None): ScreamerPath = self.plugin.ScreamerPath xmltoparse = ScreamerPath + '\\favorites.xml' self.dh2 = my_xml_handler2() sax.parse(xmltoparse.encode(eg.systemEncoding), self.dh2) choices = self.plugin.favList self.fore = fore self.back = back self.oldSel = 0 global panel panel = eg.ConfigPanel(self) mainSizer = wx.BoxSizer(wx.VERTICAL) previewLbl = wx.StaticText(panel, -1, self.text.menuPreview) mainSizer.Add(previewLbl) panel.sizer.Add(mainSizer) listBoxCtrl = wx.ListBox(panel, -1, size=wx.Size(420, 120), choices=choices, style=wx.LB_SINGLE | wx.LB_NEEDED_SB) listBoxCtrl.SetBackgroundColour(self.back) listBoxCtrl.SetForegroundColour(self.fore) if fontInfo is None: font = listBoxCtrl.GetFont() font.SetPointSize(36) fontInfo = font.GetNativeFontInfoDesc() else: font = wx.FontFromNativeInfoString(fontInfo) for n in range(10, 20): font.SetPointSize(n) listBoxCtrl.SetFont(font) if listBoxCtrl.GetTextExtent('X')[1] > 20: break mainSizer.Add(listBoxCtrl, 0, wx.TOP, 5) #Font button fontLbl = wx.StaticText(panel, -1, self.text.menuFont) fontButton = self.MenuFontButton(fontInfo) #Button Text Colour foreLbl = wx.StaticText(panel, -1, self.text.txtColour + ':') foreColourButton = self.MenuColourSelectButton(0, fore, self.text.txtColour) #Button Background Colour backLbl = wx.StaticText(panel, -1, self.text.background + ':') backColourButton = self.MenuColourSelectButton(1, back, self.text.background) bottomSizer = wx.FlexGridSizer(3, 3, hgap=0, vgap=3) bottomSizer.Add((140, 10)) bottomSizer.Add((140, 10)) bottomSizer.Add((140, 10)) bottomSizer.Add(fontLbl) bottomSizer.Add(foreLbl) bottomSizer.Add(backLbl) bottomSizer.Add(fontButton) bottomSizer.Add(foreColourButton) bottomSizer.Add(backColourButton) mainSizer.Add(bottomSizer) def OnClick(evt): listBoxCtrl.SetSelection(-1) evt.StopPropagation() listBoxCtrl.Bind(wx.EVT_LISTBOX, OnClick) # re-assign the test button def OnButton(event): self.Show_OSM(foreColourButton.GetValue(), backColourButton.GetValue(), fontButton.GetValue(), True) panel.dialog.buttonRow.testButton.Bind(wx.EVT_BUTTON, OnButton) while panel.Affirmed(): panel.SetResult( foreColourButton.GetValue(), backColourButton.GetValue(), fontButton.GetValue(), )
def parse(self, filename, errorHandler=ErrorHandler()): parse(filename, self, errorHandler)
cell_dst = getDir(old) if cell_dst == '': cell_dst = 'newcell/' else: cell_dst += '/newcell/' if not os.path.exists(cell_dst): os.makedirs(cell_dst) shutil.copy(new, cell_dst) oldCells = {} newCells = {} parse(old, TestHandle(oldCells)) print '\n\n\n\n\n' parse(new, TestHandle(newCells)) ret = ENTER retCells = [] totalSize = 0 defaultCell = {'md5': 'null'} for cellName in newCells: cell = newCells[cellName] name = cell['name'] md5 = cell['md5']
def startElement(self, tagname, attrs): #print "Element: " + tagname + ": ", global tmpRow if tagname == "person": tmpRow.append(attrs.getValue("ssn")) tmpRow.append(attrs.getValue("empid")) # print "Element: %s: ssn=%s empid=%s" % (tagname,attrs.getValue("ssn"),attrs.getValue("empid")), def endElement(self, tagname): global tmpRow if tagname == "person": # If xml->sql would do it here print "%s %s %s %s" % (tmpRow[0], tmpRow[1], tmpRow[2], tmpRow[3]) tmpRow = [] def characters(self, content): global tmpRow if content != '\n': tmpRow.append(content) #print content, try: if __name__ == "__main__": source = open("employee.xml") sax.parse(source, MyHandler()) except Exception, arg: print "Oops: %s" % arg finally: print "Buh-bye!"
def calFirstRouteProbs(dumpfile, sumoAltFile, addweights, ecoMeasure=None): basename = sumoAltFile.split('_')[0] outputAltFile = basename + "_001.rou.galt.xml" outputRouFile = basename + "_001.rou.alt.xml" edgesList = [] edgesMap = {} vehList = [] vehMap = {} parse(netfile, netReader(edgesList, edgesMap)) parse(addweights, addweightsReader(edgesList, edgesMap)) parse(dumpfile, dumpsReader(edgesList, edgesMap)) parse(sumoAltFile, routeReader(vehList, vehMap)) fout = open(outputAltFile, 'w') foutrout = open(outputRouFile, 'w') fout.write('<?xml version="1.0"?>\n') fout.write('<!--\n') fout.write('route choices are generated with use of %s' % os.path.join(os.getcwd(), 'routeChoices.py')) fout.write('-->\n') fout.write('<route-alternatives>\n') foutrout.write('<?xml version="1.0"?>\n') foutrout.write('<!--\n') foutrout.write('route choices are generated with use of %s' % os.path.join(os.getcwd(), 'routeChoices.py')) foutrout.write('-->\n') foutrout.write('<routes>') for v in vehMap: vehObj = vehMap[v] for r in vehObj.routesList: for e in r.edges.split(' '): eObj = edgesMap[e] if ecoMeasure != 'fuel' and eObj.traveltime == 0.: r.act_cost += eObj.freetraveltime r.ex_cost += eObj.freetraveltime elif ecoMeasure != 'fuel' and eObj.traveltime > 0.: r.act_cost += eObj.traveltime r.ex_cost += eObj.freetraveltime elif ecoMeasure == 'fuel' and eObj.fuel_perVeh == 0.: r.act_cost += eObj.fuel_perVeh_default r.ex_cost += eObj.fuel_perVeh_default elif ecoMeasure == 'fuel' and eObj.fuel_perVeh > 0.: r.act_cost += eObj.fuel_perVeh r.ex_cost += eObj.fuel_perVeh costSum = 0. for r in vehObj.routesList: costSum += r.ex_cost for r in vehObj.routesList: r.ex_probability = r.ex_cost / costSum randProb = random.random() selected = 0 if len(vehObj.routesList) > 1: cumulatedProbs = 0. for i, r in enumerate(vehObj.routesList): cumulatedProbs += r.ex_probability if cumulatedProbs >= randProb: selected = i break # generate the *.rou.xml foutrout.write( ' <vehicle id="%s" depart="%.2f" departLane="%s" departPos="%s" departSpeed="%s">\n' % (vehObj.label, vehObj.depart, vehObj.departlane, vehObj.departpos, vehObj.departspeed)) foutrout.write(' <route edges="%s"/>\n' % vehObj.routesList[selected].edges) foutrout.write(' </vehicle> \n') # generate the *.rou.alt.xml fout.write( ' <vehicle id="%s" depart="%.2f" departLane="%s" departPos="%s" departSpeed="%s">\n' % (vehObj.label, vehObj.depart, vehObj.departlane, vehObj.departpos, vehObj.departspeed)) fout.write(' <routeDistribution last="%s">\n' % selected) for route in vehObj.routesList: fout.write( ' <route cost="%.4f" probability="%s" edges="%s"/>\n' % (route.act_cost, route.ex_probability, route.edges)) fout.write(' </routeDistribution>\n') fout.write(' </vehicle> \n') fout.write('</route-alternatives>\n') fout.close() foutrout.write('</routes>\n') foutrout.close()
def read(filename, includeTaz=False): polys = PolygonReader(includeTaz) parse(filename, polys) return polys.getPolygons()
argParser = sumolib.options.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter) fillOptions(argParser) return argParser.parse_args(args), argParser if __name__ == "__main__": options, argParser = parse_args() if not options.net_file: argParser.print_help() argParser.exit("Error! Providing a network is mandatory") if options.verbose: print("Reading net '" + options.net_file + "'") nets = options.net_file.split(",") if len(nets) > 1: print( "Warning! Multiple networks specified. Parsing the first one for edges and tazs, the others for " + "taz only.") reader = DistrictEdgeComputer(sumolib.net.readNet(nets[0])) tazFiles = nets + options.taz_files.split(",") polyReader = sumolib.shapes.polygon.PolygonReader(True) for tf in tazFiles: parse(tf, polyReader) if options.verbose: print("Calculating") reader.computeWithin(polyReader.getPolygons(), options) if options.verbose: print("Writing results") reader.writeResults(options)
class PageMaker(ContentHandler): passthrough = False def startElement(self, name, attrs): if name == 'page': self.passthrough = True self.out = open(attrs['name'] + '.html', 'w') self.out.write('<html><head>\n') self.out.write('<title>%s</title>\n' % attrs['title']) self.out.write('</head><body>\n') elif self.passthrough: self.out.write('<' + name) for key, val in attrs.items(): self.out.write(' %s="%s"' % (key, val)) self.out.write('>') def endElement(self, name): if name == 'page': self.passthrough = False self.out.write('\n</body></html>\n') self.out.close() elif self.passthrough: self.out.write('</%s>' % name) def characters(self, chars): if self.passthrough: self.out.write(chars) parse('website.xml', PageMaker())
def Show_OSM(self, fore, back, fontInfo, flag): if self.plugin.menuDlg is not None: return self.fore = fore self.back = back self.flag = flag ScreamerPath = self.plugin.ScreamerPath xmltoparse = ScreamerPath + '\\favorites.xml' self.dh2 = my_xml_handler2() sax.parse(xmltoparse.encode(eg.systemEncoding), self.dh2) choices = self.plugin.favList self.plugin.menuDlg = wx.Frame(None, -1, 'OS_Menu', style=wx.STAY_ON_TOP | wx.SIMPLE_BORDER) favChoiceCtrl = wx.ListBox(self.plugin.menuDlg, choices=choices, style=wx.LB_SINGLE | wx.LB_NEEDED_SB) if fontInfo is None: font = favChoiceCtrl.GetFont() font.SetPointSize(36) fontInfo = font.GetNativeFontInfoDesc() else: font = wx.FontFromNativeInfoString(fontInfo) favChoiceCtrl.SetFont(font) # menu height calculation: h = favChoiceCtrl.GetCharHeight() height0 = len(choices) * h + 5 height1 = h * ((GetSystemMetrics(1) - 20) / h) + 5 height = min(height0, height1) # menu width calculation: width_lst = [] for item in choices: width_lst.append(favChoiceCtrl.GetTextExtent(item + ' ')[0]) width = max(width_lst) + 8 if height < height0: width += 20 #for vertical scrollbar width = min((width, GetSystemMetrics(0) - 50)) self.plugin.menuDlg.SetSize((width + 6, height + 6)) favChoiceCtrl.SetDimensions(2, 2, width, height, wx.SIZE_AUTO) mainSizer = wx.BoxSizer(wx.VERTICAL) self.plugin.menuDlg.SetSizer(mainSizer) favChoiceCtrl.SetSelection(self.plugin.fav_num) self.plugin.menuDlg.SetBackgroundColour((0, 0, 0)) favChoiceCtrl.SetBackgroundColour(self.back) favChoiceCtrl.SetForegroundColour(self.fore) mainSizer.Add(favChoiceCtrl, 0, wx.EXPAND) def OnClose(evt): self.plugin.menuDlg = None evt.Skip() self.plugin.menuDlg.Bind(wx.EVT_CLOSE, OnClose) def On2Click(evt): if self.plugin.menuDlg is not None: self.plugin.PlayFavFromMenu() evt.Skip() favChoiceCtrl.Bind(wx.EVT_LISTBOX_DCLICK, On2Click) self.plugin.menuDlg.Centre() if self.flag: cm = self.CloseMenu(self.plugin.menuDlg) cm.start() self.plugin.menuDlg.Show()
def check_parse(self, f): from xml.sax import parse result = StringIO() parse(f, XMLGenerator(result, 'utf-8')) self.assertEqual(result.getvalue(), xml_str(self.data, 'utf-8'))
class ExcelHandler(saxutils.DefaultHandler): def __init__(self): self.chars=[] self.cells=[] self.rows=[] self.tables=[] def characters(self, content): self.chars.append(content) def startElement(self, name, atts): if name=="Cell": self.chars=[] elif name=="Row": self.cells=[] elif name=="Table": self.rows=[] def endElement(self, name): if name=="Cell": self.cells.append(''.join(self.chars)) elif name=="Row": self.rows.append(self.cells) elif name=="Table": self.tables.append(self.rows) excelHandler=ExcelHandler() parse(sys.argv[1], excelHandler) print excelHandler.tables
def parseSMIFile(infofile): handler = SharedMimeInfoHandler() parse(infofile, handler) return handler.mimes
optParser.set_usage( '\n-n inputs\\pasubio\\pasubio.net.xml -p inputs\\pasubio\\') # parse options (options, args) = optParser.parse_args() if not options.netfile: print("Missing arguments") optParser.print_help() exit() netfile = options.netfile e2OutputFile = os.path.join(options.path, 'e2_output.xml') net = sumolib.net.readNet(netfile) e2Output = E2OutputReader() parse(e2OutputFile, e2Output) tlsID2NodeID = tlsIDToNodeID(net) tlsInfo = getBasicStats(net, e2Output._lanes, e2Output._maxT) if options.harmonoiseFile: harmonoiseOutput = HarmonoiseReader(net, tlsID2NodeID) parse(options.harmonoiseFile, harmonoiseOutput) mergeInfos(tlsInfo, harmonoiseOutput._tlsNoise, 'noise') if options.hbefaFile: hbefaOutput = HBEFAReader(net, tlsID2NodeID) parse(hbefaFile, hbefaOutput) mergeInfos(tlsInfo, hbefaOutput._tlsCO, 'CO') mergeInfos(tlsInfo, hbefaOutput._tlsCO2, 'CO2') mergeInfos(tlsInfo, hbefaOutput._tlsHC, 'HC')
def startElement(self, name, attrs): # 将一开始得到的元素节点名字赋值给tag self.tag=name if name=='stu': # 开始时碰见标记stu时创建对象并赋值给stud self.stud=Student() print('startElement') def characters(self, content): if self.tag=='name': self.stud.name=content if self.tag=='age': self.stud.age=content if self.tag=='sex': self.stud.sex=content if self.tag=='cj': self.stud.cj=content print('characters') def endElement(self, name): if name=='stu': lst.append(self.stud) self.stud=None self.tag=None print('endElement') def endDocument(self): print('endDocument') st=mysaxxml() parse('./domxml/xl_1.xml',st) for p in lst: print(p)
pass # logger.info(content) if self.IN_USERNAME: if self.SEEN.has_key(content): self.SEEN[content] += 1 else: self.SEEN[content] = 1 def endDocument(self): for k, v in sorted(self.SEEN.iteritems(), key=lambda x: x[1]): logger.info("%s: %d" % (k, v)) def endElement(self, name): if name == "title": logger.debug("out title") self.IN_TITLE = False if name == "username": logger.debug("out title") self.IN_USERNAME = False if __name__ == "__main__": fname = "data/enwiki-latest-pages-articles1.xml-p000000010p000010000-shortened.bz2" f = bz2.BZ2File(fname) handler = WikiHandler() sax.parse(f, handler)
def startElement(self, name, attrs): # 遇到<tag>标签时候会执行的方法,这里的name,attrs不用自己传值的(这里其实是重写) self._tag = name if name == "bookstore": print "=========BOOKSTORE=========" if self._tag == "book": print "BOOK: " + attrs["category"] print "--------------------------" def endElement(self, name): # 遇到</tag>执行的方法,name不用自己传值(重写) # print "endElement" if name == "bookstore": print "=========BOOKSTORE=========" elif name == "title": print "Title: " + self._content elif name == "author": print "Author: " + self._content elif name == "year": print "Year: " + self._content elif name == "price": print "Price: " + self._content else: pass def characters(self, content): # 获取标签内容 self._content = content if __name__ == "__main__": handler = TestHandler() # 自定义类实例化成对象 sax.parse("Test2.xml", handler) # 解析xml文件
documentTitleMapping.write( str(self.docID) + "#" + self.pageTitle + ":" + self.buffer + "\n") except: documentTitleMapping.write( str(self.docID) + "#" + self.pageTitle.encode('utf-8') + ":" + self.buffer.encode('utf-8') + "\n") self.flag = False self.buffer = "" def characters(self, content): self.buffer = self.buffer + content if len(sys.argv) != 2: print "Incorrect Number of Command Line Arguments provided." print "Run using : ./index.sh <path-to-dump>" sys.exit(1) # Parsing the dump and creating the index print "Parsing the Dump." start = timeit.default_timer() print "Input File Given: ", sys.argv[1] parse(sys.argv[1], WikiDataHandler()) stop = timeit.default_timer() print "Time for Parsing:", stop - start, " seconds." mins = float(stop - start) / float(60) print "Time for Parsing:", mins, " Minutes." hrs = float(mins) / float(60) print "Time for Parsing:", hrs, " Hours." print "Check the External File(s) Now!"
def startDirectory(self, attrs): self.directory.append(attrs['name']) self.ensureDirectory() def endDirectory(self): self.directory.pop() def startPage(self, attrs): filename = os.path.join(*self.directory+[attrs['name']+'.html']) self.out = open(filename, 'w') self.writeHeader(attrs['title']) self.passThrough = True def endPage(self): self.passThrough = False self.writeFooter() self.out.close() def writeHeader(self, title): self.out.write('<html>\n<head>\n<title>') self.out.write(title) self.out.write('</title>\n</head>\n<body>\n') def writeFooter(self): self.out.write('\n</body>\n</html>\n') parse('website.xml', WebsiteConstructor('public_html')) #if '__main__' == __name__:
packets.append(p) self.packet = None self.ignore = False self.timestamp = None elif name == "timestamp": self.inTimestamp = False from xml.sax import make_parser from xml.sax import parse import sys if __name__ == "__main__": dh = FindPackets() parse(sys.argv[1], dh) cmds = {} for p in packets: if cmds.has_key(p.four): cmds[p.four].append(p) else: cmds[p.four] = [p] if len(sys.argv) > 2: if p.four == sys.argv[2]: p.show() else: p.show() print "" print "cmd #tot"
def getRouteChoices(edgesMap, dumpfile, routeAltfile, netfile, addWeightsfile, alpha, beta, step, ecoMeasure=None): random.seed(42) # check with micha edgesList = [] vehList = [] vehMap = {} print('run getRouteChoices') print('ecoMeasure:', ecoMeasure) outputPath = os.path.abspath(routeAltfile) outputPath = os.path.dirname(outputPath) prefix = os.path.basename(routeAltfile) # prefix = prefix[:prefix.find('.')] prefix = prefix[:-12] # print('outputPath:', outputPath) print('prefix:', prefix) outputAltfile = os.path.join(outputPath, prefix + '.rou.galt.xml') outputRoufile = os.path.join(outputPath, prefix + '.grou.xml') if len(edgesMap) == 0: try: print('parse network file') parse(netfile, netReader(edgesList, edgesMap)) except AttributeError: print("could not parse netfile: " + str(netfile)) try: parse(addWeightsfile, addweightsReader(edgesList, edgesMap)) except AttributeError: print("could not parse weights file: " + str(addWeightsfile)) else: resetEdges(edgesMap) fout = open(outputAltfile, 'w') foutrout = open(outputRoufile, 'w') fout.write('<?xml version="1.0"?>\n') fout.write('<!--\n') fout.write('route choices are generated with use of %s' % os.path.join(os.getcwd(), 'routeChoices.py')) fout.write('-->\n') fout.write('<route-alternatives>\n') foutrout.write('<?xml version="1.0"?>\n') foutrout.write('<!--\n') foutrout.write('route choices are generated with use of %s' % os.path.join(os.getcwd(), 'routeChoices.py')) foutrout.write('-->\n') foutrout.write('<routes>') print('parse dumpfile') print(dumpfile) parse(dumpfile, dumpsReader(edgesList, edgesMap)) # parse routeAltfile from SUMO try: print('parse routeAltfile:', routeAltfile) parse(routeAltfile, routeReader(vehList, vehMap)) except IOError: print('could not parse routeAltfile:', routeAltfile) ex_outputAltFile = prefix[:prefix.rfind('_')] + '_%03i' % ( step - 1) + '.rou.galt.xml' try: print('parse routeAltfile from externalGawron: ', ex_outputAltFile) parse( ex_outputAltFile, vehrouteReader(vehList, vehMap, edgesMap, fout, foutrout, ecoMeasure, alpha, beta)) except IOError: print('could not parse routeAltfile from externalGawron:', ex_outputAltFile) return outputRoufile, edgesMap
def readDump(file, attrsToCollect, edgesToCollect=None): dump = DumpReader(attrsToCollect, edgesToCollect) parse(file, dump) return dump
def read(filename): pois = PoIReader() parse(filename, pois) return pois._pois
def countLaneChanges(dumpfile): dr = DumpReader() parse(dumpfile, dr) print(dr.changes, dr.changes / float(len(dr.vehicles)))
from xml.sax.handler import ContentHandler from xml.sax import parse class TestHnadler(ContentHandler): def startElement(self, name, attrs): print(name, attrs.keys()) parse('website.xml', TestHnadler())
def read(filename): ils = InductiveLoopReader() parse(filename, ils) return ils._ils
def get_leo_data(source): """Return the root node for the specificed .leo file (path or file)""" parser = LeoReader() parse(source, parser) return parser.root
"-w", "--weighted", action="store_true", default=False, help="Weights sources/sinks by lane number and length") optParser.add_option( "-f", "--assign-from", action="store_true", default=False, help= "Assign the edge always to the district where the \"from\" node is located" ) (options, args) = optParser.parse_args() if not options.netfiles: optParser.print_help() optParser.exit("Error! Providing networks is mandatory") reader = NetDistrictEdgeHandler() for netfile in options.netfiles.split(","): if options.verbose: print "Reading net '" + netfile + "'" parse(netfile, reader) if options.verbose: print "Calculating" reader.computeWithin(options.complete, options.maxspeed, options.assign_from, options.verbose) if options.verbose: print "Writing results" reader.writeResults(options.output, options.weighted)