def lookup_series_info(self, name): self.find_mirror() full_url = self.active_mirror + SERIES_LOOKUP_URL % (name.strip().replace(' ', '%20'),) if self.debug: print "Getting data for url: %s" % full_url series_xml = parse(urllib2.urlopen(full_url)) series = [Series for Series in series_xml.findall('Series')] # now that we have the id's, get the full series info full_series = [] for s in series: id = self.get_int(s, 'id', 0) full_url = self.active_mirror + SERIES_URL % (API_KEY, id) full_series_xml = parse(urllib2.urlopen(full_url)) for full_s in full_series_xml.findall('Series'): full_series.append(full_s) if self.debug: print " found %i series:" % len(full_series) to_return = [] for s in full_series: s_obj = self.parse_series_xml(s) to_return.append(s_obj) if self.debug: print " found series '%s'" % s_obj.title return to_return
def read_robofab_glyphlist(libfname, contentsfname) : etree = parse(libfname) glist = None for e in etree.getroot().iterfind('dict') : d = read_any(e) if "org.robofab.glyphOrder" in d : glist = d["org.robofab.glyphOrder"] if not glist : return etree = parse(contentsfname) files = {} for e in etree.getroot().iterfind("dict") : d = read_any(e) f = file(contentsfname, "w") f.write("""<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> """) for g in glist : f.write(""" <key>{0}</key> <string>{1}</string> """.format(g, d[g])) f.write("""</dict> </plist> """)
def _xml_parse_gz_zip_file(self, zfile): """Recursively check for xml file in zip / gzipped files, and parse it """ # Check for zipfile self.log(logging.DEBUG, "Check ZIP/gz/xml : checking %s " % (repr(zfile),)) zfile.seek(0) if zipfile.is_zipfile(zfile): with ZipFile(zfile) as zc: nl = zc.namelist() if len(nl) == 1 and nl[0].endswith(".xml"): return parse(zc.open(nl[0], 'r')) else: # Check for gzip file with gzip.GzipFile(None, 'r', 9, zfile) as zc: zc.rewind() with os.tmpfile() as tf: try: tf.write(zc.read()) return self._xml_parse_gz_zip_file(tf) except IOError: zfile.seek(0) try: return parse(zfile) except ParseError: return None
def __init__(self, xmlfile=None, root=None, adimentionalized=True): if xmlfile is not None: if isinstance(xmlfile, file) or isinstance(xmlfile, StringIO.StringIO): self.tree = parse(xmlfile) elif isinstance(xmlfile, str): source = file(xmlfile, "r") self.tree = parse(source) source.close() self.root = self.tree.getroot() elif root is not None: self.root = root else: print 'A etree root or a xmlfile should be provided' list = [] a = self.root.findall("param") for i in a: n = i.get("name") v = i.get("value") if '.' in v or 'e' in v: v = float(v) else: v = int(v) list.append((n, v)) self.absolute_dic = dict(list) self.relative_dic = dict(list) if adimentionalized: self.adimentionalize()
def merge_databases(self, first_xml_db_filename, second_xml_db_filename, destination_filename): first_xml_db = parse(first_xml_db_filename).getroot() second_xml_db = parse(second_xml_db_filename).getroot() self.add_missing_groups(first_xml_db, second_xml_db) self.merge_entries(first_xml_db, second_xml_db) ElementTree(second_xml_db).write(destination_filename)
def getValue(key): if lang == 'en': data = parse(getScriptLoc() + '/../../src_data/full_text.en.xml').getroot() elif lang == 'es': data = parse(getScriptLoc() + '/../../src_data/full_text.es.xml').getroot() else: raise Exception('invalid language') return data.find(key).text
def assert_xml_equal(a, b): """ Compare two XML artifacts for equality. :param a, b: Paths to XML files, or a file-like object containing the XML contents. """ tools.assert_equal(tostring(parse(a).getroot()), tostring(parse(b).getroot()))
def getCraftPricing(rinfo, item): wowheadxml = None # Look up mats for item number - check db first reagents = db.getReagents(item) if len(reagents) < 1: # Not there, so check wowhead wowheadxml = parse(urllib.urlopen(WOWHEAD_BASE % (item))) ingreds = wowheadxml.findall('item/createdBy/spell/reagent') reagents = [(x.get('id'), x.get('count')) for x in ingreds] item_map = [(x.get('id'), x.get('name')) for x in ingreds] # Store in databases db.saveReagents(item, reagents) db.saveItems(item_map) # Grab item name item_name = db.getItemName(item) if not item_name: # Look it up if not wowheadxml: wowheadxml = parse(urllib.urlopen(WOWHEAD_BASE % (item))) item_name = wowheadxml.find('item/name').text db.saveItems([(item, item_name)]) # Compare prices, etc. # Query spreads for item and its reagents # TODO lookup reagent, but we ignore count for now spreads = db.getMultiCurrentSpread(rinfo, [item] + [x[0] for x in reagents]) ts, prices = spreads[item] if ts == -1 or 'buy' not in prices: return False crafted = prices['buy'][0] rprices = [] rnames = [] for id,count in reagents: rnames.append(db.getItemName(id)) ts, prices = spreads[id] if ts == None or 'buy' not in prices: return False rprices.append(prices['buy'][0]) return { 'item': item_name, 'reagents': rnames, 'profit': crafted - sum(rprices), 'sell': crafted, 'buy': rprices, }
def __init__(self, builder): if not os.path.isfile(setupXml): self.initSetup(setupXml) if not os.path.isfile(currentRegisterXml): self.initRegister(currentRegisterXml) self.currentXmlDom = parse(currentRegisterXml) if not os.path.isfile(previousRegisterXml): self._fromXmlDom2File(self.currentXmlDom, previousRegisterXml) self.previousXmlDom = parse(previousRegisterXml) self.builder = builder
def _assertETXMLWellFormed(self, data, parse, msg=None, context=2): """internal function used by /assertXML(String)?WellFormed/ functions :param data: xml_data :param parse: appropriate parser function for this data :param msg: error message :param context: number of context lines in standard message (show all data if negative). Only available with element tree """ from xml.parsers.expat import ExpatError try: from xml.etree.ElementTree import ParseError except ImportError: # compatibility for <python2.7 ParseError = ExpatError try: parse(data) except (ExpatError, ParseError) as ex: if msg is None: if hasattr(data, "readlines"): # file like object data.seek(0) lines = data.readlines() else: lines = data.splitlines(True) nb_lines = len(lines) context_lines = [] # catch when ParseError doesn't set valid lineno if ex.lineno is not None: if context < 0: start = 1 end = nb_lines else: start = max(ex.lineno - context, 1) end = min(ex.lineno + context, nb_lines) line_number_length = len("%i" % end) line_pattern = " %%%ii: %%s" % line_number_length for line_no in range(start, ex.lineno): context_lines.append(line_pattern % (line_no, lines[line_no - 1])) context_lines.append(line_pattern % (ex.lineno, lines[ex.lineno - 1])) context_lines.append("%s^\n" % (" " * (1 + line_number_length + 2 + ex.offset))) for line_no in range(ex.lineno + 1, end + 1): context_lines.append(line_pattern % (line_no, lines[line_no - 1])) rich_context = "".join(context_lines) msg = "XML stream not well formed: %s\n%s" % (ex, rich_context) self.fail(msg)
def assert_xml_equal(a, b): """ Compare two XML artifacts for equality. :param a, b: Paths to XML files, or a file-like object containing the XML contents. """ path_a = get_path(a) path_b = get_path(b) content_a = tostring(parse(a).getroot()) content_b = tostring(parse(b).getroot()) if content_a != content_b: raise AssertionError('The files %s and %s are different!' % (path_a, path_b))
def parse(self, filename, base_dir=''): self.wprops.set_base_dir(base_dir) # The order in which the different branches are handled is important. # The widget tree handler relies on all custom widgets being known, and # in order to create the connections, all widgets have to be populated. branchHandlers = ( ("layoutdefault", self.readDefaults), ("class", self.classname), ("customwidgets", self.customWidgets), ("widget", self.createUserInterface), ("connections", self.createConnections), ("tabstops", self.setTaborder), ("resources", self.readResources), ) document = parse(filename) version = document.getroot().attrib["version"] DEBUG("UI version is %s" % (version,)) # Right now, only version 4.0 is supported. assert version in ("4.0",) for tagname, actor in branchHandlers: elem = document.find(tagname) if elem is not None: actor(elem) self.finalize() w = self.toplevelWidget self.reset() return w
def step_back(io_manager): ''' deletes the output files from the most recent runs :type io_manager: IOManger ''' current_iteration = None target_iteration = None # Figure out the current iteration and modify the scan specs file appropriately parameter_scan_specs_xml_file_path = io_manager.parameter_scan_specs_xml_file_path xml_file = parse(parameter_scan_specs_xml_file_path) xml_root = xml_file.getroot() for parameter_element in xml_root.iter('Parameter'): current_iteration = int(parameter_element.attrib['CurrentIteration']) target_iteration = current_iteration - 1 print('Stepping up files to redo batch run {}'.format(target_iteration)) parameter_element.set('CurrentIteration', str(target_iteration)) ElementTree(xml_root).write(parameter_scan_specs_xml_file_path) # Remove screenshots for root, dirs, files in os.walk(io_manager.screenshot_output_path): for dir in dirs: if dir == str(target_iteration): shutil.rmtree(os.path.join(root, dir)) # Remove most recent .csv and .txt in output folder for root, dirs, files in os.walk(io_manager.output_folder): for file in files: if fnmatch(file, '*output{}.txt'.format(target_iteration)) or fnmatch(file, '*output{}.csv'.format(target_iteration)): os.remove(os.path.join(root, file))
def main(self): """TBD""" pkginfo_file = open(self.env["packageinfo_path"], "r") pkginfo_parsed = parse(pkginfo_file) version = pkginfo_parsed.getroot().attrib["version"] self.env["version"] = version
def extract_connections_credentials(self): """ Extract all connection's credentials. :return: List of dict in which one dict contains all information for a connection. """ repos_creds = [] connection_file_location = os.path.join( constant.profile["USERPROFILE"], u'.ApacheDirectoryStudio\\.metadata\\.plugins\\org.apache.directory.studio.connection.core\\connections.xml' ) if os.path.isfile(connection_file_location): try: connections = parse(connection_file_location).getroot() connection_nodes = connections.findall(".//connection") for connection_node in connection_nodes: creds = {} for connection_attr_name in connection_node.attrib: if connection_attr_name in self.attr_to_extract: creds[connection_attr_name] = connection_node.attrib[connection_attr_name].strip() if creds: repos_creds.append(creds) except Exception as e: self.error(u"Cannot retrieve connections credentials '%s'" % e) return repos_creds
def _load_from_file(self, filename): """Builds a CTDModel from a CTD XML file. """ root = parse(filename).getroot() assert root.tag == 'tool', "Invalid CTD file, root is not <tool>" # TODO: own exception self.opt_attribs = {} for tool_required_attrib in ['name', 'version']: assert tool_required_attrib in root.attrib, "CTD tool is missing a %s attribute" % tool_required_attrib setattr(self, tool_required_attrib, root.attrib[tool_required_attrib]) for tool_opt_attrib in ['docurl', 'category']: if tool_opt_attrib in root.attrib: self.opt_attribs[tool_opt_attrib] = root.attrib[tool_opt_attrib] for tool_element in root: if tool_element.tag in ['manual', 'description', 'executableName', 'executablePath']: # ignoring: cli, logs, relocators. cli and relocators might be useful later. self.opt_attribs[tool_element.tag] = tool_element.text if tool_element.tag == 'PARAMETERS': # tool_element.attrib['version'] == '1.6.2' # check whether the schema matches the one CTDOpts uses? params_container_node = tool_element.find('NODE') # we have to check the case in which the parent node contains # item/itemlist elements AND node element children params_container_node_contains_items = params_container_node.find('ITEM') is not None or params_container_node.find('ITEMLIST') # assert params_container_node.attrib['name'] == self.name # check params_container_node's first ITEM child's tool version information again? (OpenMS legacy?) params = params_container_node.find('NODE') # OpenMS legacy again, NODE with name="1" on top # check for the case when we have PARAMETERS/NODE/ITEM if params is None or params_container_node_contains_items: self.parameters = self._build_param_model(params_container_node, base=None) else: # OpenMS legacy again, PARAMETERS/NODE/NODE/ITEM self.parameters = self._build_param_model(params, base=None)
def modify_vrt(vrt, scale): """ Makes modifications to the vrt file to fix the values. :param vrt: VRT file to be processed :param scale: Scale value from get_metadata_item function :return: None """ doc = parse(vrt) root = doc.getroot() # Fix the datatype if it is wrong raster_band = root.find('VRTRasterBand') raster_band.set('dataType', 'Float32') # Add the scale to the vrt file source = root.find('VRTRasterBand').find('ComplexSource') scale_ratio = SubElement(source, 'ScaleRatio') scale_ratio.text = scale # Write the scale input # vrt files are overwritten with the same name doc.write(vrt, xml_declaration=True)
def adr2geo(adr): api = "http://www.geocoding.jp/api/?v=1.1&q=%s" % (urllib.quote(adr.encode('utf-8'))) xml = parse(urllib.urlopen(api)).getroot() lat = xml.find('coordinate/lat').text lng = xml.find('coordinate/lng').text return (float(lat), float(lng))
def getReportparameters(filename): parms = {} csidParms = True fileFound = False try: reportXML = parse(JRXMLDIRPATTERN % filename) fileFound = True parameters = reportXML.findall('{http://jasperreports.sourceforge.net/jasperreports}parameter') #print 'parameters',parameters for p in parameters: name = p.attrib['name'] isForPrompting = p.get('isForPrompting') if name == 'csid': csidParms = False try: default = p.find('{http://jasperreports.sourceforge.net/jasperreports}defaultValueExpression').text except: default = '' try: description = p.find('{http://jasperreports.sourceforge.net/jasperreports}parameterDescription').text except: description = '' parms[name] = [default.strip('"'), isForPrompting, description.strip('"')] except: #raise # indicate that .jrxml file was not found... print 'jrxml file not found, no parms extracted.' return parms,csidParms,fileFound
def get_project_files(filename, filetype=None, minfiles=0): """Parse Xilinx .xise file and extract source files having type <filetype>. No path normalization is done here.""" tree = parse(filename) root = tree.getroot() files = root.find('{http://www.xilinx.com/XMLSchema}files') properties = root.find('{http://www.xilinx.com/XMLSchema}properties') # Find chipscope files if filetype is None: matchFun = lambda ft: True else: matchFun = lambda ft: (ft == filetype) matchingFiles = [f.get('{http://www.xilinx.com/XMLSchema}name') for f in files.findall('{http://www.xilinx.com/XMLSchema}file') if matchFun(f.attrib['{http://www.xilinx.com/XMLSchema}type'])] if len(matchingFiles) < minfiles: msg = "Required at least {0:d} files of type {1}, found only {2:d}: {3} ".format(minfiles, filetype, len(matchingFiles), matchingFiles) raise XiseMissingFilesError(msg) return matchingFiles
def read_xml_config_old(self, configfile): """ Reads the XML configuration file used in data taking and reads the configuration object. Reads the configuration tag and reads the text in there and assigns actual receiver elements to rows and columns from binary file This one is for the old 3 column configuration we used to have """ if not os.path.exists(configfile): raise Exception("Config file %s does not exist" % configfile) tree = parse(configfile) elem = tree.getroot() cfg = elem.find("configuration") txt = cfg.text rows = txt.split('\n\t') self.pixeldic = {} # for a given frontend pixel has # tuple of row, col of bf for row in rows[2:-1]: args = row.split('\t') adc = int(args[1]) cable = int(args[2]) pixel = args[3].strip() print "ADC: %d, cable: %d, pixel: %s" % (adc, cable, pixel) if pixel != 'NC': self.pixeldic[pixel] = get_rowcol_for_cable(cable) self.pixel_label = dict((v, k) for k, v in self.pixeldic.iteritems())
def update_mongo_cfg(xmlFile, cfgs, brokerURL='tcp://localhost:61616?wireFormat.maxInactivityDuration=0'): doc = parse(xmlFile) root = doc.getroot() se = root.find('mongo-config').find('servers') servers = se.getchildren() for sne in se.findall('server'): se.remove(sne) for cfg in cfgs: e = Element('server') e.set('host',cfg['host']) e.set('port',str(cfg['port'])) servers.append(e) if brokerURL is not None: ace = root.find('activemq-config') if ace is not None: ace.remove(ace.find('brokerURL')) e = Element('brokerURL') e.text = brokerURL ace.append(e) pde = root.find('pluginDir') if pde is not None: pde.text = '/var/data/foundry/foundry_plugins/plugins' lde = root.find('libDir') if lde is not None: lde.text = '/var/data/foundry/foundry_plugins/lib' newFile = re.sub(r"\.xml$","_new.xml",xmlFile) doc.write(newFile) backupFile = xmlFile + '.bak' shutil.move(xmlFile,backupFile) shutil.move(newFile,xmlFile) print "wrote %s" % xmlFile
def load_training_xml(file_name): # xml_tree = etree.parse(file_name) xml_tree = parse(file_name) xml_root = xml_tree.getroot() (category_list, exercise_type_dict) = load_exercises_from_xml(xml_root) program_dict = load_programs_from_xml(xml_root, exercise_type_dict) return (program_dict, category_list)
def addToWorkingSet(newProjectPath): workingSetFilePath = os.path.expanduser("~") + os.sep + ".colt" + os.sep + "workingset.xml" projectsList = [] # Populate projects list if os.path.exists(workingSetFilePath) : workingSetElement = parse(workingSetFilePath).getroot() for projectElement in workingSetElement : projectPath = projectElement.attrib["path"] if projectPath : projectsList.append(projectPath) # Remove project path from the list projectsList = filter(lambda projectPath : projectPath != newProjectPath, projectsList) # Push new project projectsList.insert(0, newProjectPath) # Save the list workingSetElement = Element("workingset") workingSetElement.set("openRecent", "true") for projectPath in projectsList : projectElement = SubElement(workingSetElement, "project") projectElement.set("path", projectPath) workingSetFile = open(workingSetFilePath, "w") workingSetFile.write(tostring(workingSetElement)) workingSetFile.close()
def parse_manifest(self, url): """ given a url (http: or file:) get the manifest file and parse it """ file = urllib.urlopen(url) #file.get_code() not added until python 2.6 rss = parse(file).getroot() if rss.tag == None or rss.tag != 'SmoothStreamingMedia': raise MissingRootTagException("root tag is " + rss.tag + " expecting SmoothStreamingMedia") major = rss.get('MajorVersion') minor = rss.get('MinorVersion') if int(major) != 2 or int(minor) != 1: print >> self.logfile, "warning: unexpected manifest version: major=", major, "minor=", minor self.streams = [] elements = rss.findall('StreamIndex') for element in elements: qualities = [] durations = [] manifest_output = element.get('ManifestOutput') if manifest_output != None: # the spec says this value is case-insensitive manifest_output = manifest_output.lower() url = element.get('Url') if url is not None: url = self.generate_absolute_url(None, url) data = { 'Type': element.get('Type'), 'Url': url, 'ManifestOutput' : manifest_output, 'Qualities': qualities, 'Durations': durations } for quality in element.findall('QualityLevel'): cattrs = [] for custom in quality.findall('CustomAttributes'): for attr in custom.findall('Attribute'): cattr = (attr.get("Name"), attr.get("Value")) cattrs.append(cattr) qdata = { 'Bitrate': quality.get('Bitrate'), 'Cattrs': cattrs } qualities.append(qdata) #note: duration attr might not be there for non-audio/video # streams (i.e. text streams, name=GSIS) for duration in element.findall('c'): dur_value = '0' dur_attr = duration.get('d') if dur_attr != None: dur_value = dur_attr durations.append({ 'Duration': dur_value, 'start': duration.get('t') }) self.streams.append(data) return
def __init__(self, file_name, padding, trim_flanking): self.file_name = file_name self.trim_flanking = trim_flanking # Read the specified input file into a variable try: self.tree = parse(self.file_name) self.transcriptdict = {'transcripts': {}, 'root': self.tree.getroot(), 'pad': int(padding), 'pad_offset': int(padding) % 5} self.transcriptdict['fixannot'] = self.transcriptdict['root'].find( 'fixed_annotation') # ensures only exons from the fixed annotation will be taken self.transcriptdict['updatable'] = self.transcriptdict['root'].find( 'updatable_annotation') self.transcriptdict['genename'] = self.transcriptdict['root'].find( 'updatable_annotation/annotation_set/lrg_locus').text self.transcriptdict['refseqname'] = self.transcriptdict['root'].find( 'fixed_annotation/sequence_source').text if self.transcriptdict['root'].attrib['schema_version'] != '1.9': print 'This LRG file is not the correct version for this script' print 'This is designed for v.1.8' print 'This file is v.' + self.transcriptdict['root'].attrib['schema_version'] self.is_matt_awesome = True except IOError as fileNotPresent: print "The specified file cannot be located: " + fileNotPresent.filename exit() # This assertion is an artifact from LRG parsing, will need to be updated assert self.transcriptdict['pad'] <= 2000, "Padding too large, please use a value below 2000 bases" assert self.transcriptdict['pad'] >= 0, "Padding must be 0 or a positive value" if self.transcriptdict['pad'] < 0: exit()
def disfluency(self): """extract disfluency info; nite_type. TBD is flattening the info would reflect the data? what if not flattening?""" if self.traceDisf == 0: # set path disfluency_path = self.supfolder+"disfluency/"+self.filename+"disfluency"+self.suffix tree = parse(disfluency_path) self.disfluency_root = tree.getroot() if self.word_id == "s2_21": print "hs" flag = 0 for (i,child) in enumerate(self.disfluency_root[self.traceDisf:self.traceDisf+50]): for child2 in child: d_disfluency = child2.attrib # match the word id with child id for child3 in child2: d_disfluency0 = child3.attrib if d_disfluency0["href"].rstrip(')').split('(')[1] == self.word_id: flag = 1 self.current_word.append(("dialAct:niteType",d_disfluency0["niteType"])) break if flag: break if flag: break if i is not 49: self.traceDisf+=i if i==49: print "not found"
def load(self, filename=None): if filename is not None: self.filename = filename self.tree = parse(self.filename) else: self.tree = None self.filename = None
def getFeed(url,numitems=3): "returns a html list with links from the given RSS feed url" xml = parse(urllib.urlopen(url)).getroot() items = [] channel = xml.find('channel') for element in channel.findall('item'): items.append({'title': element.find('title').text, 'description': element.find('description').text, 'link': element.find('link').text}) if len(items) > numitems: items = items[:numitems] resp = '<ul>' for item in items: descr = re.compile("style=\".*?\"").sub('',item['description']) descr = re.compile("alt=\".*?\"").sub('',descr) descr = re.compile("\"").sub('',descr) d1 = re.findall("<img.*?>",descr)[0] d2 = re.findall("<span>.*?</span>",descr)[0] descr = "<h3>" + item['title'] + "</h3>" descr += d1 + "<br/>" descr += d2 resp += '<li><a onMouseover="show(\'' resp += descr resp += '\')" onMouseout="show(\'\')" href="' resp += item['link'] resp += '">' resp += item['title'] resp += '</a></li>' resp += '</ul>' print resp return resp
def handle(self, *args, **options): # Download the RSS feed and parse it u = urlopen('http://www.24sata.hr/feeds/aktualno.xml') doc = parse(u) # Extract and output tags of interest for item in doc.iter(): #item = item.findtext('item') title = item.findtext('title') link = item.findtext('link') description = item.findtext('description') pubDate = item.findtext('pubDate') author = item.findtext('dc:creator') category = item.findtext('category') #.iterfind('channel/item'): #title = item.findtext('title') #date = item.findtext('pubDate') #link = item.findtext('link') #print(item) #spremiti u listu print(title) print(link) print(description) print(pubDate) print(author) print(category)
if len(section) > 0: for element in section: try: subelement = next(iter(children.iter(element.tag))) subelement.text = element.text except StopIteration: children.append(element) else: children.text = section.text if __name__ == '__main__': # First argument is original XML # Second argument is an XML file with the full section to modify or_xml_name = sys.argv[1] new_xml_name = sys.argv[2] sanitized_file = sanitize_xml(or_xml_name) with open(or_xml_name, 'w') as f: f.writelines(sanitized_file) original_xml = parse(or_xml_name) new_xml_sections = parse(new_xml_name) for new_section in new_xml_sections.getroot(): parse_section(original_xml.getroot(), new_section) original_xml.write(or_xml_name)
ns = { 'svg': 'http://www.w3.org/2000/svg', 'inkscape': 'http://www.inkscape.org/namespaces/inkscape', '': 'http://www.w3.org/2000/svg' } xpath = './/svg:flowPara[@class="bingo-text"]' phrases = PHRASES random.shuffle(phrases) try: for node in document.findall(xpath, ns): node.text = phrases.pop() except TypeError: pass document.write(output) if __name__ == '__main__': register_namespace('svg', 'http://www.w3.org/2000/svg') register_namespace('dc', 'http://purl.org/dc/elements/1.1/') register_namespace('cc', 'http://creativecommons.org/ns#') register_namespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#') register_namespace('xlink', 'http://www.w3.org/1999/xlink') register_namespace('sodipodi', 'http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd') register_namespace('inkscape', 'http://www.inkscape.org/namespaces/inkscape') register_namespace('', 'http://www.w3.org/2000/svg') document = parse('bingo.svg') replace_text(document)
# xmlEx03.py from xml.etree.ElementTree import parse # parse : xml 문서를 파싱해주는 함수 tree = parse('xmlEx_03.xml') myroot = tree.getroot() print(type(myroot)) print('-' * 30) # 해당 속성들의 키를 list 형식으로 반환 print(myroot.keys()) print('-' * 30) # 해당 속성들의 키와 값을 튜플로 가지는 list를 반환 print(myroot.items()) print('-' * 30) print(myroot.get('설명')) print('-' * 30) print(myroot.get('qwert', '없을 경우 기본 값')) print('-' * 30) print(myroot.findall('가족')) print('-' * 30) family = myroot.find('가족') print(family) print('-' * 30) # childs = family.getchildren()
#coding=utf-8 '6-3 Parsing Simple XML Data' from urllib.request import urlopen from xml.etree.ElementTree import parse # Download the RSS feed and parse it u = urlopen('http://planet.python.org/rss20.xml') u = urlopen('http://qdaily.com/feed.xml') doc = parse(u) # Extract and output tags of interest for item in doc.iterfind('channel/item'): title = item.findtext('title') date = item.findtext('pubDate') link = item.findtext('link') print(title) # .decode('utf-8') print(date) print(link) # print()
from sys import argv, stdout, exit from os.path import dirname from xml.etree.ElementTree import parse from skimage.io import imread, imsave, use_plugin use_plugin('freeimage') if len(argv) != 3: stdout.write('Usage: %s labelling_path output_dir\n' % argv[0]) exit(1) labelling_path = argv[1] dir_path = dirname(labelling_path) output_dir = argv[2] + '/' batch_tag = parse(labelling_path).getroot() physical_signs = {} for photo_tag in batch_tag.findall('Photo'): bboxes = photo_tag.find('BBoxes') if bboxes is None: bboxes = [] else: bboxes = bboxes.findall('BBox') for bbox_tag in bboxes: bbox_id = int(bbox_tag.attrib['ID']) x = int(bbox_tag.find('X').text) y = int(bbox_tag.find('Y').text) w = int(bbox_tag.find('W').text) h = int(bbox_tag.find('H').text)
#!/usr/bin/env python3 import base64 import io import json import pathlib import sys import zlib from jinja2 import Template, Environment, FileSystemLoader from urllib.parse import unquote from xml.etree.ElementTree import parse, fromstring for x in parse(sys.argv[1]).findall("diagram"): name = x.attrib["name"] # decode with help from https://crashlaker.github.io/programming/2020/05/17/draw.io_decompress_xml_python.html #obj = zlib.decompressobj(-15) #xml = unquote(obj.decompress(base64.b64decode(x.text)) + obj.flush()) #print(xml) #doc = fromstring(xml) doc = x script_path = str(pathlib.Path(__file__).parent.absolute()) environment = Environment(extensions=["jinja2.ext.do"], loader=FileSystemLoader(searchpath=script_path)) template = environment.from_string( open(script_path + "/generator.j2").read()) doc.attrib["name"] = name print(template.render(doc=doc))
from urllib.request import urlopen from xml.etree.ElementTree import parse from lxml import etree import pdb #xml = urlopen("https://feeds.capitalbikeshare.com/stations/stations.xml").read() #root = etree.El #xml = eval(open("station.xml").read()) #print(xml) #pdb.set_trace() tree = None try: tree = parse("station.xml") except Exception as e: print(str(e)[:100]) print(type(e)) print(e.args) if tree: print("HAHA") else: print("Oh NO") station = open("station_data_1.csv", "w") attribute_list = [ "id", "name", "lat", "long", "nbBikes", "nbEmptyDocks", "installed", "locked", "temporary", "public" ] station.write(','.join(attribute_list) + '\n') for item in tree.iterfind("station"):
def parse(self, filename): filename = os.path.abspath(filename) self._filename_stack.append(filename) tree = parse(filename) self.parse_tree(tree) self._filename_stack.pop()
def from_xml(self, filename): tree = parse(filename) root = tree.getroot() # memquotebank for u in root: key = u.attrib['name'] self.memories[key] = u.text
def _parse_xml(xml_file): """Parse XML file.""" xml = parse(xml_file) root = xml.getroot() return _xml2list(root)
articleList.append(article_joined) # Solicit user input input_term = input("Enter your search terms: ") input_filename = input("Enter desired file name: ") mydict = {'term': input_term} filename = input_filename + '.xlsx' # Construct esearch url to retrieve Pubmed IDs url_esearch = base + "esearch.fcgi?db=pubmed&retmax=100000&" + urlencode( mydict) # Parse esearch XML var_url_esearch = urlopen(url_esearch) xmldoc_esearch = parse(var_url_esearch) root_esearch = xmldoc_esearch.getroot() xmlList_esearch = list(root_esearch.find('IdList')) # For every Id in xmlList, retrieve .text field then put it in a list idList = list(map(lambda x: x.text, xmlList_esearch)) print('fetching ' + str(len(idList)) + ' results... please be patient.') idListChunks = divide_chunks(idList, 400) idListJoinedChunks = list(map(lambda x: ','.join(x), idListChunks)) # Add relevant details to a dataframe for export articleList = [] for chunk in idListJoinedChunks:
def get_xpath(self): tree = parse(self.spider_path + 'xpath.xml') string = tostring(tree.getroot()) edt = xmltodict.parse(string) return edt['shop_item']
def write_minorating_config(self, preset): config_file = os.path.join(settings.MINORATING_CONFIG_DIR, 'MinoRatingPlugin.exe.config') # if server_settings doesn't have a record of the trust_token, attempt to fetch it from the config xml as this # gets initialised when minorating is activated for the first time trust_token = '' trust_token_from_file = None if not preset.server_setting.minorating_server_trust_token: try: et = parse(config_file) root = et.getroot() for add_entry in root.find('appSettings').findall('add'): add_entry_dict = add_entry.attrib if 'key' in add_entry_dict and 'value' in add_entry_dict: if add_entry_dict['key'] == 'server_trust_token': trust_token_from_file = add_entry_dict['value'] except: pass # if we don't have a recorded trust token but found one in the file - commit it's value to the db and use # its value for config writes later in this method if trust_token_from_file: server_setting = preset.server_setting server_setting.minorating_server_trust_token = trust_token_from_file server_setting.save() trust_token = trust_token_from_file else: trust_token = preset.server_setting.minorating_server_trust_token settings_dict = { 'load_server_cfg': '1', 'ac_server_directory': settings.ACSERVER_BIN_DIR, 'plugin_port': str(preset.server_setting.proxy_plugin_port), 'ac_server_port': str(preset.server_setting.proxy_plugin_local_port), 'ac_cfg_directory': self.acserver_config_dir, 'start_new_log_on_new_session': '0', 'log_server_requests': '0', 'server_trust_token': trust_token, } root = Element('configuration') startup = SubElement(root, 'startup') SubElement(startup, 'supportedRuntime', attrib={ 'version': 'v4.0', 'sku': '.NETFramework,Version=v4.5', }) app_settings = SubElement(root, 'appSettings') for k in settings_dict: SubElement(app_settings, 'add', attrib={ 'key': k, 'value': settings_dict[k] }) fh = open(config_file, 'w') fh.write(prettify_xml(root)) fh.close()
import xml.sax from xml.etree.ElementTree import parse xml_doc = parse("note.xml") for ele in xml_doc.findall("pro"): print(ele.text)
class XMLNamespaces: """将namespace映射为简单的key""" def __init__(self, **kwargs): self.namespaces = {} for name, uri in kwargs.items(): self.register(name, uri) def register(self, name, uri): self.namespaces[name] = '{' + uri + '}' def __call__(self, path): """format-string处理xpath""" return path.format_map(self.namespaces) if __name__ == "__main__": ns = XMLNamespaces(default="urn:schemas-microsoft-com:office:spreadsheet", ss='urn:schemas-microsoft-com:office:spreadsheet') print(ns.namespaces, ns('{default}Worksheet/{ss}Table')) # {'default': '{urn:schemas-microsoft-com:office:spreadsheet}', 'ss': '{urn:schemas-microsoft-com:office:spreadsheet}'} # {urn:schemas-microsoft-com:office:spreadsheet}Worksheet/{urn:schemas-microsoft-com:office:spreadsheet}Table doc = parse('test.xml') root = doc.getroot() print(root.find('{urn:schemas-microsoft-com:office:spreadsheet}Styles')) print(root.find(ns('{default}Worksheet/{ss}Table')))
from xml.etree.ElementTree import parse #import requests from urllib.request import urlopen url = 'http://ihongss.com/xml/exam1.xml' # str = requests.get(url) # doc = parse(str.text) # print(doc) str1 = urlopen(url) doc1 = parse(str1) print(doc1) for item in doc1.iterfind('items/item'): print(item.attrib) #<item id="a" print(item.findtext("name")) #<name>a</name>
import os # import requests from xml.etree.ElementTree import parse # u = requests.get('http://planet.python.org/rss20.xml') # doc = parse(u.content) # print('doc', doc) dir_path = os.path.abspath(os.path.join(os.path.abspath(__file__), '../../../')) file_name = os.path.join(dir_path, 'tmp/somefile.xml') doc = parse(file_name) print('doc\n', doc) print('-' * 70) for item in doc.iterfind('channel/item'): title = item.findtext('title') date = item.findtext('pubDate') link = item.findtext('link') print('title: ', title) print('date: ', date) print('link: ', link)
elem.tail = i for elem in elem: indent(elem, level + 1) if not elem.tail or not elem.tail.strip(): elem.tail = i else: if level and (not elem.tail or not elem.tail.strip()): elem.tail = i indent(note) # note.append(to) dump(note) # ElementTree(note).write("note.xml") tree = parse("note.xml") note = tree.getroot() print(note.get("date")) print(note.get("foo", "default")) print(note.keys()) print(note.items()) from_tag = note.find("from") from_tags = note.findall("from") from_text = note.findtext("from") print(from_tag) print(from_tags) print(from_text)
def generate_sdf_world(self, options): """ Return an etree of this Building in SDF starting from a template""" print(f'generator options: {options}') if 'gazebo' in options: template_name = 'gz_world.sdf' elif 'ignition' in options: template_name = 'ign_world.sdf' else: raise RuntimeError("expected either gazebo or ignition in options") template_path = os.path.join( get_package_share_directory('rmf_building_map_tools'), f'templates/{template_name}') tree = parse(template_path) sdf = tree.getroot() world = sdf.find('world') for level_name, level in self.levels.items(): level.generate_sdf_models(world) # todo: a better name level.generate_doors(world, options) level_include_ele = SubElement(world, 'include') level_model_name = f'{self.name}_{level_name}' name_ele = SubElement(level_include_ele, 'name') name_ele.text = level_model_name uri_ele = SubElement(level_include_ele, 'uri') uri_ele.text = f'model://{level_model_name}' pose_ele = SubElement(level_include_ele, 'pose') pose_ele.text = f'0 0 {level.elevation} 0 0 0' for lift_name, lift in self.lifts.items(): if not lift.level_doors: print(f'[{lift_name}] is not serving any floor, ignoring.') continue lift.generate_shaft_doors(world) lift.generate_cabin(world, options) charger_waypoints_ele = SubElement(world, 'rmf_charger_waypoints', {'name': 'charger_waypoints'}) for level_name, level in self.levels.items(): for vertex in level.transformed_vertices: if 'is_charger' in vertex.params: SubElement( charger_waypoints_ele, 'rmf_vertex', { 'name': vertex.name, 'x': str(vertex.x), 'y': str(vertex.y), 'level': level_name }) gui_ele = world.find('gui') c = self.center() camera_pose = f'{c[0]} {c[1]-20} 10 0 0.6 1.57' # add floor-toggle GUI plugin parameters if 'gazebo' in options: camera_pose_ele = gui_ele.find('camera').find('pose') camera_pose_ele.text = camera_pose toggle_charge_ele = SubElement(gui_ele, 'plugin', { 'name': 'toggle_charging', 'filename': 'libtoggle_charging.so' }) toggle_ele = SubElement(gui_ele, 'plugin', { 'name': 'toggle_floors', 'filename': 'libtoggle_floors.so' }) for level_name, level in self.levels.items(): floor_ele = SubElement( toggle_ele, 'floor', { 'name': level_name, 'model_name': f'{self.name}_{level_name}' }) for model in level.models: if model.static: model_ele = SubElement(floor_ele, 'model', {'name': model.name}) for door in level.doors: model_ele = SubElement(floor_ele, 'model', {'name': door.params['name'].value}) for lift_name, lift in self.lifts.items(): if level_name in lift.level_doors: for door in lift.doors: if door.name in lift.level_doors[level_name]: model_ele = SubElement( floor_ele, 'model', { 'name': (f'ShaftDoor_{lift_name}_' + f'{level_name}_{door.name}') }) elif 'ignition' in options: plugin_ele = gui_ele.find('.//plugin[@filename="GzScene3D"]') camera_pose_ele = plugin_ele.find('camera_pose') camera_pose_ele.text = camera_pose return sdf
if __name__ == "__main__": Allxml = [ y for x in os.walk(DIR_TO_ParentSave) for y in glob.glob(os.path.join(x[0], '*.xml')) ] matchers = set() for xml in Allxml: matchers.add(xml.split('/')[-1]) # get xml file names duplicatedXML = getduplicatedXMLPath(Allxml, matchers) for PATH_TO_XMLS in duplicatedXML: xmlTrees = [] for PATH_TO_XML in PATH_TO_XMLS: xml = open(PATH_TO_XML, "r") xmlTrees.append(parse(xml)) info = [[], []] for xmlTree in xmlTrees: root = xmlTree.getroot() for object in root.iter('object'): info[1].append(object.findtext("name")) for bndbox in object.iter('bndbox'): info[0].append([ bndbox.findtext("xmin"), bndbox.findtext("ymin"), bndbox.findtext("xmax"), bndbox.findtext("ymax") ]) # remove duplicated bbox for bbox in info[0]: index = [i for i, v in enumerate(info[0]) if v == bbox]
# 3. 메타파일에서 조건에 따라 검색 후 있으면 해당 디렉토리에 이동, 없으면 디렉토리를 생성 후 이동 # - 파일 : 영상파일(avi, wmv, mp4, mpg, mpeg, mkv)과 자막파일(smi, srt)만 검색 # - 조건 : 제목, 년도 # - 자막 : 파일명과 동일하면 이동, 제목만 동일하면 이름변경 후 이동 # 4. 모니터링 # # 추가기능 # - 스케쥴 기능추가 # - 포스터 및 부가정보등을 다운로드 하여 메타파일 및 DB화 # - 음악, 만화등 다른컨텐츠도 자동화(각각 별도 프로세스 필요) # - 앱에서 직접 확인 및 재생 # 환경설정 파일 읽어오기 configfile = open('config.xml', 'rb') tree = parse(configfile) config = tree.getroot() downloaddir = config.find("DownloadDir").text moviezdir = config.find("MoviezDir").text tvshowdir = config.find("TVShowzDir").text etcdir = config.find("EtcDir").text is_category = config.find("SubCategory").text.lower() == 'true' is_copy = config.find("CopyType").text.lower() == 'true' # 로거 인스턴스를 만든다 logger = logging.getLogger('moverLog') # 포매터를 만든다 fomatter = logging.Formatter('%(asctime)s %(levelname)s\t: %(message)s')
from xml.etree.ElementTree import parse tree = parse("students_info.xml") raw_info = tree.getroot() stu_male_total = 0 stu_fem_total = 0 while True: print("학생정보 데이터 분석 시작") print("1. 요약정보") print("2. 전체 데이터 조회") print("3. 종료") menu_choice = input("메뉴 입력 : ") if menu_choice == "1": stu_total = str(len(raw_info.findall("student"))) print("*전체학생수: " + stu_total) print("성별") stu_list = raw_info.getiterator("student") for stu in stu_list: if stu.get("sex") == "남": stu_male_total += 1 elif stu.get("sex") == "여": stu_fem_total += 1 stu_male_per = stu_male_total / (stu_male_total + stu_fem_total) * 100 stu_fem_per = stu_fem_total / (stu_male_total + stu_fem_total) * 100 print("남학생: " + str(stu_male_total) + "명 (" + str(stu_male_per) + "%)") print("여학생: " + str(stu_fem_total) + "명 (" + str(stu_fem_per) + "%)") stu_list = raw_info.getiterator("student") stu_major_total = 0 stu_prac_com_languages = 0 stu_prac_com_languages_upper = 0 stu_python_exper = 0
from urllib2 import urlopen from xml.etree.ElementTree import parse url = urlopen('http://feeds.feedburner.com/TechCrunch/Google') xmldoc = parse(url) xmldoc.write('output.xml') for item in xmldoc.iterfind('channel/item'): title = item.findtext('title') desc = item.findtext('description') date = item.findtext('pubDate') link = item.findtext('link') print title print desc print date print link print '---------'
# 以增量方式解析大型XML文件 from xml.etree.ElementTree import parse, iterparse from collections import Counter #from guppy import hpy # 方法一:浪费内存 potholes_by_zip = Counter() doc = parse('./res/stocks.xml') for pothole in doc.iterfind('channel/item'): potholes_by_zip[pothole.findtext('zip')] += 1 for zipcode, num in potholes_by_zip.most_common(): print(zipcode, num) #print profile.heap() # 方法二:节省内存 def parse_and_remove(filename, path): path_parts = path.split('/') doc = iterparse(filename, ('start', 'end')) # Skip the root element next(doc) tag_stack = [] elem_stack = [] for event, elem in doc: if event == 'start': tag_stack.append(elem.tag) elem_stack.append(elem)
def readEnvelope(self, path, env): # http://cdr.eionet.europa.eu/pl/eu/eprtrdat/envutc3xg/xml # _envPath = os.path.dirname(path) +'/xml' req = urllib2.Request(path + '/xml') # base64string = base64.encodestring( # '%s:%s' % (self.conf.cdr_user, self.conf.cdr_pass))[:-1] # authheader = "Basic %s" % base64string # req.add_header("Authorization", authheader) try: _url = urllib2.urlopen(req) except IOError, e: # here we shouldn't fail if the username/password is right print "It looks like the username or password is wrong." sys.exit(1) try: tree = parse(StringIO.StringIO(_url.read())) elem = tree.getroot() for child in elem: if "date" in child.tag: env.releasedate = str(child.text).replace('Z', '') if "file" in child.tag: if child.get('type') == "text/xml": env.xmlfilepath = child.get('link') env.xmlfilename = child.get('name') env.restricted = True if (str( child.get('restricted')).lower() == 'yes') else False if path == 'http://cdr.eionet.europa.eu/ie/eu/eprtrdat/envuwwvcg': print env.xmlfilename, ' - ', child.get( 'restricted'), ' - ', str(env.restricted)
if os.path.isdir(folder_directory): files = os.listdir(directory + '/' + folder) for file in files: if re.match(r'\S+\.jpg', file): print(file) img = Image.open(folder_directory + '/' + file) w = img.width h = img.height print("width: " + str(w) + ", height: " + str(h)) temp = file.split('.') annotation_path = '/Users/casuy/workplace/data/annotation/' + folder + '/' + temp[ 0] + '.xml' print(annotation_path) annotation = parse(annotation_path) root = annotation.getroot() size = root.find("size") width = size.find("width") print("original width: " + width.text) width.text = str(w) print("updated width: " + width.text) height = size.find("height") print("original height: " + height.text) height.text = str(h) print("updated height: " + height.text) annotation.write(annotation_path) print(i) i += 1
from xml.etree.ElementTree import parse, Element, SubElement, dump, ElementTree tree = parse('students_info_2.xml') student_list = tree.getroot() choice_menu = 0 tag_students = student_list.findall('student') count_students = len(student_list) class Count: count_male = 0 count_female = 0 count_major = 0 count_programming = 0 count_level = 0 count_python = 0 age_20 = 0 age_20_list = [] age_30 = 0 age_30_list = [] age_40 = 0 age_40_list = [] id_plus = 0 def count_information(self): self.count_male = 0 self.count_female = 0 self.count_major = 0 self.count_programming = 0 self.count_level = 0 self.count_python = 0
#!/usr/bin/env python3 # -*- coding=utf-8 -*- # 本脚本由黄猛编写,用于Python学习! from xml.etree.ElementTree import parse tree = parse('xml1.xml') # 打开分析的XML文件 root = tree.getroot() # 找到根位置 QYT_Teachers = {} for elem in tree.iter(tag='师资'): # 找到师资标签(tag) Teachers_list = [] for elem_in in elem.iter(tag='老师'): # 找到师资标签(tag)下的老师标签 Teachers_list.append(elem_in.attrib['name']) # 找到每一个老师的名字,添加进入列表 QYT_Teachers[elem.attrib['name'][:-2]] = Teachers_list # 把XX团队的“团队”两字去掉 # 把老师名字的列表,添加到以部门为键的字典中,作为部门这个键所映射的值! print(QYT_Teachers) # 课程与方向映射的字典 kecheng_dict = {'Security CCNP': '安全', 'Wireless CCNP': '无线', 'DataCenter CCNP': '数据中心', 'RS CCNP': '路由交换'} QYT_Courses = {} for elem in tree.iter(tag='课程'): # 找到课程标签(tag) Courses_list = [] for elem_in in elem.iter(tag='课程名'): # 找到课程标签(tag)下的课程名标签 Courses_list.append(elem_in.attrib['name']) # 找到每一个课程的名字,添加进入列表 QYT_Courses[kecheng_dict[elem.attrib['name']]] = Courses_list # 把课程名字的列表,添加到以课程方向为键的字典中,作为课程方向这个键所映射的值!
def parseXML(XMLPath): """ Parses an XML file with the appropriat format for the exercice and creates its data structure, returning a network object """ tree = parse(XMLPath) root = tree.getroot() for child in root: # Parse the network first if child.tag == "network": printIfVerbose("Building a " + child.tag + " called " + child.attrib["name"]) name = "" overhead = 0 transmission_capacity = 0 x_type = "" for attributeName in child.attrib: if attributeName == "name": name = child.attrib[attributeName] elif attributeName == "overhead": overhead = 8 * interpretQuantity( child.attrib[attributeName]) elif attributeName == "transmission-capacity": transmission_capacity = interpretQuantity( child.attrib[attributeName]) elif attributeName == "x-type": x_type = child.attrib[attributeName] net = Classes.Network(name, overhead, transmission_capacity, x_type) break else: print("ERROR: Could not find a network node!") # All attribute assignation referring to network nodes are first done as strings, and then # the corresponding nodes are assigned AFTER we've ensured all the nodes have been created. for child in root: printIfVerbose("Building a " + child.tag + " called " + child.attrib["name"]) if child.tag == "station": name = "" transmission_capacity = 0 x = 0 y = 0 for attributeName in child.attrib: if attributeName == "name": name = child.attrib[attributeName] elif attributeName == "transmission-capacity": transmission_capacity = interpretQuantity( child.attrib[attributeName]) elif attributeName == "x": x = child.attrib[attributeName] elif attributeName == "y": y = child.attrib[attributeName] newStation = Classes.Station(name, transmission_capacity, x, y) newStation.setNetwork(net) net.stations[name] = newStation elif child.tag == "switch": name = "" transmission_capacity = 0 x = 0 y = 0 redundancy = "Unspecified" for attributeName in child.attrib: if attributeName == "name": name = child.attrib[attributeName] elif attributeName == "transmission-capacity": transmission_capacity = interpretQuantity( child.attrib[attributeName]) elif attributeName == "x": x = child.attrib[attributeName] elif attributeName == "y": y = child.attrib[attributeName] elif attributeName == "redundancy": redundancy = child.attrib[attributeName] newSwitch = Classes.Switch(name, transmission_capacity, x, y) if redundancy != "Unspecified": newSwitch.setRedundancy(redundancy) newSwitch.setNetwork(net) net.switches[name] = newSwitch elif child.tag == "link": name = "" start = "" startPort = "" end = "" endPort = "" transmission_capacity = 0 for attributeName in child.attrib: if attributeName == "name": name = child.attrib[attributeName] elif attributeName == "from": start = child.attrib[attributeName] elif attributeName == "fromPort": startPort = child.attrib[attributeName] elif attributeName == "to": end = child.attrib[attributeName] elif attributeName == "toPort": endPort = child.attrib[attributeName] elif attributeName == "transmission-capacity": transmission_capacity = interpretQuantity( child.attrib[attributeName]) newLink = Classes.Link(name, start, startPort, end, endPort, transmission_capacity) newLink.setNetwork(net) net.links[name] = newLink elif child.tag == "flow": deadline = 0 jitter = 0 max_payload = 0 name = "" period = 0 priority = 0 source = "" redundancy = "MAIN" for attributeName in child.attrib: if attributeName == "deadline": deadline = interpretQuantity(child.attrib[attributeName]) elif attributeName == "jitter": jitter = interpretQuantity(child.attrib[attributeName]) elif attributeName == "max-payload": max_payload = 8 * interpretQuantity( child.attrib[attributeName]) elif attributeName == "name": name = child.attrib[attributeName] elif attributeName == "period": period = 1e-3 * interpretQuantity( child.attrib[attributeName]) elif attributeName == "priority": priorityString = child.attrib[attributeName] if priorityString == "Low": priority = 0 elif priorityString == "High": priority = 1 else: print( "ERROR: The priority string for {0} is not admitted!" .format(name)) elif attributeName == "transmission-capacity": transmission_capacity = interpretQuantity( child.attrib[attributeName]) elif attributeName == "source": source = child.attrib[attributeName] #Find the end system and add it to the targets list newFlow = Classes.Flow(deadline, jitter, max_payload, name, period, priority, source) newFlow.setNetwork(net) net.flows[name] = newFlow for targetElement in child: # Gather attributes targetStationName = targetElement.attrib["name"] sourceStationName = child.attrib["source"] try: redundancy = child.attrib["redundancy"] except: redundancy = "Unspecified" # Create target target = Classes.Target(targetStationName, sourceStationName, newFlow) # Assign redundancy if redundancy != "Unspecified": target.setRedundancy(redundancy) # Add target to flow targets newFlow.targets[targetStationName] = target # Build target's path for pathComponent in targetElement: pathNodeName = pathComponent.attrib["node"] target.path.append(pathNodeName) target.setNetwork(net) # Nodes have been asigned as names, correct to objects for link in net.links.values(): link.start = net.getNode(link.start) link.end = net.getNode(link.end) for flow in net.flows.values(): source = net.getNode(flow.source) flow.source = source for target in flow.targets.values(): target.target = net.getNode(target.target) target.source = net.getNode(target.source) for i in range(len(target.path)): target.path[i] = net.getNode(target.path[i]) printIfVerbose("Path for target " + target.name + " is " + str([str(s) for s in target.path])) if not target.hasPath(): print("ERROR: " + str(target) + " path was not built correctly!") # Initializes some variables net.initializeNodes() printIfVerbose("The network has been fully built!") return net
from urllib.request import urlopen from xml.etree.ElementTree import parse # Download the RSS feed and parse it # u = urlopen('http://planet.python.org/rss20.xml') # doc = parse(u) # load local xml doc = parse('data.xml') # Extract and output tags of interest for item in doc.iterfind('channel/item'): (title, date, link) = item.findtext('title'),\ item.findtext('pubDate'),\ item.findtext('link') print(title) print(date) print(link) print()