def extractElement(self, elementpath, respdata): try: tree = ElementTree() tree.parse(StringIO(respdata)) except: return None # Strip off the top-level item if elementpath[0] == '/': elementpath = elementpath[1:] splits = elementpath.split('/', 1) root = splits[0] if tree.getroot().tag != root: return None elif len(splits) == 1: return tree.getroot().text else: elementpath = splits[1] e = tree.find(elementpath) if e is not None: return e.text else: return None
def extractElement(self, elementpath, respdata): try: tree = ElementTree() tree.parse(StringIO(respdata)) except: return None # Strip off the top-level item if elementpath[0] == '/': elementpath = elementpath[1:] splits = elementpath.split('/', 1) root = splits[0] if tree.getroot().tag != root: return None elif len(splits) == 1: return tree.getroot().text else: elementpath = splits[1] e = tree.find(elementpath) if e is not None: return e.text else: return None
def _split_configuration(self, projectfile, temp_dir): num_pieces = multiprocessing.cpu_count() tree = ET(file=projectfile) num_files = len(tree.findall('./files/file')) splitfiles = [] files_per_job = int(math.ceil(float(num_files)/num_pieces)) for idx in xrange(num_pieces): tree = ET(file=projectfile) root = tree.getroot() start = idx*files_per_job end = start + files_per_job if end > num_files: end = None for elem in ('files', 'images', 'pages', 'file-name-disambiguation'): elem_root = root.find(elem) to_keep = elem_root.getchildren()[start:end] to_remove = [x for x in elem_root.getchildren() if not x in to_keep] for node in to_remove: elem_root.remove(node) out_file = os.path.join(temp_dir, "{0}-{1}.ScanTailor".format( os.path.splitext(os.path.basename( projectfile))[0], idx)) tree.write(out_file) splitfiles.append(out_file) return splitfiles
def create_db(file_path): tree = ElementTree(file=file_path) root = tree.getroot() db_name = root.attrib['name'] conn = sqlite3.connect(db_name) c = conn.cursor() for table in tree.iter(tag='table'): create_sql_arr = [ 'CREATE TABLE IF NOT EXISTS %s' % table.attrib['name'], ' (' ] index = 0 for column in table: if index != 0: create_sql_arr.append(', ') index += 1 create_sql_arr.append( '%s %s' % (column.attrib['name'], column.attrib['type'])) if 'pk' in column.attrib and column.attrib['pk'] == '1': create_sql_arr.append(' PRIMARY KEY') create_sql_arr.append(')') create_sql = ''.join(create_sql_arr) c.execute(create_sql) print 'Execute sql:%s' % create_sql conn.commit() conn.close()
def parse_size(path): """Parses the size attribute from .gexf file viz tag and returns the dict with the node sizes. Default nx.read_gexf() does not read attributes from the visualization namespace of .gexf files so we use this function to get the node sizes. To learn more see : https://gephi.org/gexf/format/viz.html Parameters ---------- path : str File or filename of the graph in .gexf format. Returns ------- dict Returns dict containing the node sizes. Example : {'1':52.0} """ tree = ElementTree(file=path) root = tree.getroot() nodes = [child for child in root.iter() if "nodes" in child.tag][0] sizes = {} for node in nodes: id = node.attrib['id'] size = [child for child in node if "size" in child.tag][0].attrib sizes[id] = float(size['value']) return sizes
def _split_configuration(self, projectfile, temp_dir): num_pieces = multiprocessing.cpu_count() tree = ET(file=unicode(projectfile)) num_files = len(tree.findall('./files/file')) splitfiles = [] files_per_job = int(math.ceil(float(num_files)/num_pieces)) for idx in xrange(num_pieces): tree = ET(file=unicode(projectfile)) root = tree.getroot() start = idx*files_per_job end = start + files_per_job if end > num_files: end = None for elem in ('files', 'images', 'pages', 'file-name-disambiguation'): elem_root = root.find(elem) to_keep = elem_root.getchildren()[start:end] to_remove = [x for x in elem_root.getchildren() if not x in to_keep] for node in to_remove: elem_root.remove(node) out_file = temp_dir / "{0}-{1}.ScanTailor".format(projectfile.stem, idx) tree.write(unicode(out_file)) splitfiles.append(out_file) return splitfiles
def parse_operator_xml(f): et = ElementTree(file=f) r = et.getroot() def _get_value_from_first_element(r_, e_name): return r_.findall(e_name)[0].text operator_id = r.attrib['id'] task_id = _get_value_from_first_element(r, 'task-id') s = r.findall('scatter')[0] scatter_task_id = _get_value_from_first_element(s, 'scatter-task-id') sgs = s.findall('chunks')[0] schunks = [ScatterChunk(x.attrib['out'], x.attrib['in']) for x in sgs.findall('chunk')] scatter = Scatter(task_id, scatter_task_id, schunks) gs = r.findall('gather')[0].findall('chunks')[0].findall('chunk') def _to_c(x): return _get_value_from_first_element(x, 'gather-task-id'), _get_value_from_first_element(x, 'chunk-key'), _get_value_from_first_element(x, 'task-output') gchunks = [GatherChunk(*_to_c(x)) for x in gs] gather = Gather(gchunks) return ChunkOperator(operator_id, scatter, gather)
def run(self): # Run the module only once if not constant.wifi_password: interfaces_dir = os.path.join( constant.profile['ALLUSERSPROFILE'], u'Microsoft\\Wlansvc\\Profiles\\Interfaces') # for windows Vista or higher if os.path.exists(interfaces_dir): pwd_found = [] for wifi_dir in os.listdir(interfaces_dir): if os.path.isdir(os.path.join(interfaces_dir, wifi_dir)): repository = os.path.join(interfaces_dir, wifi_dir) for file in os.listdir(repository): values = {} if os.path.isfile(os.path.join(repository, file)): f = os.path.join(repository, file) tree = ElementTree(file=f) root = tree.getroot() xmlns = root.tag.split("}")[0] + '}' for elem in tree.iter(): if elem.tag.endswith('SSID'): for w in elem: if w.tag == xmlns + 'name': values['SSID'] = w.text if elem.tag.endswith('authentication'): values['Authentication'] = elem.text if elem.tag.endswith('protected'): values['Protected'] = elem.text if elem.tag.endswith('keyMaterial'): key = elem.text try: password = self.decrypt_using_lsa_secret( key=key) if not password: password = self.decrypt_using_netsh( ssid=values['SSID']) if password: values['Password'] = password else: values[ 'INFO'] = '[!] Password not found.' except Exception: self.error(traceback.format_exc()) values[ 'INFO'] = '[!] Password not found.' if values and values.get( 'Authentication') != 'open': pwd_found.append(values) return pwd_found
def _get_crumb(self): """Gets crumb by making the appropriate API call""" response = urllib2.urlopen("".join([self.base_url, "getCrumb"])) tree = ElementTree() tree.parse(response) for elem in tree.getroot().findall("crumb"): self.crumb = elem.text
def tweak_build_xml(self): runjdwp_args = [ 'transport=dt_socket', 'server=y', 'address=8765', 'suspend=n', ] runjdwp_args = ','.join(runjdwp_args) jvm_debug_args = [ '-Xdebug', '-Xrunjdwp:%s' % (runjdwp_args,), ] jvm_debug_args = ' '.join(jvm_debug_args) build_xml = self.get_build_xml() tree = ElementTree() tree.parse(build_xml) root = tree.getroot() targets = root.findall('target') for node in targets: if node.get('name') == 'run': java_node = node.find('java') SubElement(java_node, 'jvmarg', { 'line': jvm_debug_args, }) tree.write(build_xml)
def extract_positions(url): tree = ElementTree() parser = XMLParser(encoding="iso-8859-1") data = urllib.urlopen(url) tree.parse(data, parser=parser) positions = tree.getroot().findall("team") allpos = [] for pos in positions: realpos = pos.find("pos") latitude = float(realpos.attrib['a']) longitude = float(realpos.attrib['o']) speed = float(realpos.attrib['s']) course = float(realpos.attrib['c']) last_update = datetime.utcfromtimestamp(int(realpos.attrib["w"])) dtf = float(realpos.attrib['d']) _id = pos.attrib["id"] # pos = geo.xyz(latitude, latitude) # final object result = {} result["str_latitude"] = format_deg(latitude, "N", "S") result["str_longitude"] = format_deg(longitude, "E", "W") result["speed"] = speed result["course"] = course result["_id"] = _id result["dtf"] = dtf result["last_update"] = last_update allpos.append(result) return allpos
def run(self, profile): path = os.path.join(profile['APPDATA'], 'FileZilla Server') if not os.path.exists(path): return xml_file = os.path.join(path, 'FileZilla Server Interface.xml') if os.path.exists(xml_file): tree = ElementTree(file=xml_file) root = tree.getroot() host = port = password = None for item in root.iter("Item"): if item.attrib['name'] == 'Last Server Address': host = item.text elif item.attrib['name'] == 'Last Server Port': port = item.text elif item.attrib['name'] == 'Last Server Password': password = item.text # if all((host, port, login)) does not work if host is not None and port is not None and password is not None: return [{ 'Host': host, 'Port': port, 'Password': password, }]
def parse_operator_xml(f): et = ElementTree(file=f) r = et.getroot() def _get_value_from_first_element(r_, e_name): return r_.findall(e_name)[0].text operator_id = r.attrib["id"] task_id = _get_value_from_first_element(r, "task-id") s = r.findall("scatter")[0] scatter_task_id = _get_value_from_first_element(s, "scatter-task-id") sgs = s.findall("chunks")[0] schunks = [ScatterChunk(x.attrib["out"], x.attrib["in"]) for x in sgs.findall("chunk")] scatter = Scatter(task_id, scatter_task_id, schunks) gs = r.findall("gather")[0].findall("chunks")[0].findall("chunk") def _to_c(x): return ( _get_value_from_first_element(x, "gather-task-id"), _get_value_from_first_element(x, "chunk-key"), _get_value_from_first_element(x, "task-output"), ) gchunks = [GatherChunk(*_to_c(x)) for x in gs] gather = Gather(gchunks) return ChunkOperator(operator_id, scatter, gather)
def parse_position(path): """Parses the position attribute from .gexf file viz tag and returns the dict with the node sizes. Default nx.read_gexf() does not read attributes from the visualization namespace of .gexf files so we use this function to get the node positions, possibly after using layout algorithms in Gephi. To learn more see : https://gephi.org/gexf/format/viz.html Parameters ---------- path : str File or filename of the graph in .gexf format. Returns ------- dict Returns dict containing the node positions. Example : {'1':(2.0,3.0)} """ tree = ElementTree(file=path) root = tree.getroot() nodes = [child for child in root.iter() if "nodes" in child.tag][0] positions = {} for node in nodes: id = node.attrib['id'] position = [child for child in node if "position" in child.tag][0].attrib positions[id] = (float(position['x']), float(position['y'])) return positions
def __get_runtime_status(self, runtime_status_files): ''' Helper funcion for montioring the status of the task. It reads the status files of all modules and reports which ones are still running. ''' task_status = 'finished' running_module_slots = list() for (slot_name, status_file) in runtime_status_files.items(): if not os.path.isfile(status_file): time.sleep(0.1) assert os.path.isfile(status_file), 'runtime status file %r not found.' \ % status_file tree = ElementTree() tree.parse(status_file) status_file_xml_root_node = tree.getroot() node = status_file_xml_root_node.find('status') status = node.text.strip() if status == 'running': task_status = 'running' running_module_slots.append(slot_name) return (task_status, running_module_slots)
def run(self): database_path = self.get_default_database() if not database_path or not os.path.exists(database_path): return xml_file = os.path.expanduser(database_path) tree = ElementTree(file=xml_file) root = tree.getroot() pwd_found = [] for connection in root.iter('connection'): children = connection.getchildren() values = {} for child in children: for c in child: if str(c.tag) in [ 'name', 'protocol', 'host', 'port', 'description', 'login', 'password' ]: values[str(c.tag).capitalize()] = str(c.text) if values: pwd_found.append(values) return pwd_found
def run(self): settings = [ os.path.join(constant.profile['LOCALAPPDATA'], u'Microsoft Corporation\\Remote Desktop Connection Manager\\RDCMan.settings'), os.path.join(constant.profile['LOCALAPPDATA'], u'Microsoft\\Remote Desktop Connection Manager\\RDCMan.settings') ] for setting in settings: if os.path.exists(setting): self.debug(u'Setting file found: {setting}'.format(setting=setting)) tree = ElementTree(file=setting) root = tree.getroot() pwd_found = [] elements = [ 'CredentialsProfiles/credentialsProfiles/credentialsProfile', 'DefaultGroupSettings/defaultSettings/logonCredentials', 'file/server', ] for element in elements: pwd_found += self.parse_element(root, element) try: for r in root.find('FilesToOpen'): if os.path.exists(r.text): self.debug(u'New setting file found: %s' % r.text) pwd_found += self.parse_xml(r.text) except Exception: pass return pwd_found
class _DXML(object): def __init__(self, path, tagmap={}, tagdefault=None, **options): self._path, self._options = path, options self._tree = ElementTree() self._tree.parse(self._path) self.verbosity, self.traceback, self.graceful = 1, False, False self._tagmap = tagmap self._tagdefault = self._trivial if tagdefault is None else tagdefault def __iter__(self): self._preiter_hook() # Stage 1: namespaces for o in self._xml_namespaces(): # IGNORE:E1101 yield o # Stage 2: resources r = self._tree.getroot() for e in [r] + r.getchildren(): # IGNORE:E1101 try: for o in self._tagmap.get(e.tag, self._tagdefault)(e): yield o except Exception, x: self._except(Exception, x) # Stage 3: inheritance etc. self._postiter_hook()
def parse_operator_xml(f): et = ElementTree(file=f) r = et.getroot() def _get_value_from_first_element(r_, e_name): return r_.findall(e_name)[0].text operator_id = r.attrib['id'] task_id = _get_value_from_first_element(r, 'task-id') s = r.findall('scatter')[0] scatter_task_id = _get_value_from_first_element(s, 'scatter-task-id') sgs = s.findall('chunks')[0] schunks = [ ScatterChunk(x.attrib['out'], x.attrib['in']) for x in sgs.findall('chunk') ] scatter = Scatter(task_id, scatter_task_id, schunks) gs = r.findall('gather')[0].findall('chunks')[0].findall('chunk') def _to_c(x): return _get_value_from_first_element( x, 'gather-task-id'), _get_value_from_first_element( x, 'chunk-key'), _get_value_from_first_element(x, 'task-output') gchunks = [GatherChunk(*_to_c(x)) for x in gs] gather = Gather(gchunks) return ChunkOperator(operator_id, scatter, gather)
def run(self): creds = [] directory = constant.profile['USERPROFILE'] + u'\\Documents\\Rogue\'s Tale\\users' # The actual user details are stored in *.userdata files if os.path.exists(directory): files = os.listdir(directory) for f in files: if re.match('.*\.userdata', f): # We've found a user file, now extract the hash and username xmlfile = directory + '\\' + f tree = ElementTree(file=xmlfile) root = tree.getroot() # Double check to make sure that the file is valid if root.tag != 'user': self.warning(u'Profile %s does not appear to be valid' % f) continue # Now save it to credentials creds.append({ 'Login': root.attrib['username'], 'Hash': root.attrib['password'] }) return creds
def run(self, profile): creds = [] directory = profile['USERPROFILE'] + '\\Documents\\Rogue\'s Tale\\users' # The actual user details are stored in *.userdata files if not os.path.exists(directory): return files = os.listdir(directory) for f in files: if re.match('.*\.userdata', f): # We've found a user file, now extract the hash and username xmlfile = directory + '\\' + f tree = ElementTree(file=xmlfile) root = tree.getroot() # Double check to make sure that the file is valid if root.tag != 'user': log.warning('Profile %s does not appear to be valid' % f) continue # Now save it to credentials creds.append({'Login': root.attrib['username'], 'Hash': root.attrib['password']}) return creds
def tweak_build_xml(self): runjdwp_args = [ 'transport=dt_socket', 'server=y', 'address=8765', 'suspend=n', ] runjdwp_args = ','.join(runjdwp_args) jvm_debug_args = [ '-Xdebug', '-Xrunjdwp:%s' % (runjdwp_args, ), ] jvm_debug_args = ' '.join(jvm_debug_args) build_xml = self.get_build_xml() tree = ElementTree() tree.parse(build_xml) root = tree.getroot() targets = root.findall('target') for node in targets: if node.get('name') == 'run': java_node = node.find('java') SubElement(java_node, 'jvmarg', { 'line': jvm_debug_args, }) tree.write(build_xml)
def run(self): settings = [ os.path.join(constant.profile['LOCALAPPDATA'], u'Microsoft Corporation\\Remote Desktop Connection Manager\\RDCMan.settings'), os.path.join(constant.profile['LOCALAPPDATA'], u'Microsoft\\Remote Desktop Connection Manager\\RDCMan.settings') ] for setting in settings: if os.path.exists(setting): self.debug(u'Setting file found: {setting}'.format(setting=setting)) tree = ElementTree(file=setting) root = tree.getroot() pwd_found = [] elements = [ 'CredentialsProfiles/credentialsProfiles/credentialsProfile', 'DefaultGroupSettings/defaultSettings/logonCredentials', 'file/server', ] for element in elements: pwd_found += self.parse_element(root, element) try: for r in root.find('FilesToOpen'): if os.path.exists(r.text): self.debug(u'New setting file found: %s' % r.text) pwd_found += self.parse_xml(r.text) except Exception: pass return pwd_found
def writeXMLTVConfig(self): if self.epgimport is None and self.xmltvimport is None and self.crossepg is None: return if int(self.epgimportversion[0]) >= 5 and int( self.xmltvimportversion[0]) >= 5 and int( self.crossepgversion[0]) >= 5: return if config.plugins.seriesplugin.epgimport.value == False and config.plugins.seriesplugin.xmltvimport.value == False and config.plugins.seriesplugin.crossepg.value == False: return # Build Header from plugin import NAME, VERSION root = Element("sources") root.set('version', VERSION) root.set('created_by', NAME) root.append( Comment( _("Don't edit this manually unless you really know what you are doing" ))) element = SubElement(root, "source", type="gen_xmltv", channels="wunschliste.channels.xml") SubElement(element, "description").text = "Wunschliste XMLTV" SubElement(element, "url").text = config.plugins.seriesplugin.xmltv_url.value etree = ElementTree(root) indent(etree.getroot()) if config.plugins.seriesplugin.epgimport.value: log.debug("Write: xml channels for epgimport") if self.epgimport: try: self.epgimport.writeXML(etree) except Exception as e: log.exception("Exception in write XML: " + str(e)) if config.plugins.seriesplugin.xmltvimport.value: log.debug("Write: xml channels for xmltvimport") if self.xmltvimport: try: self.xmltvimport.writeXML(etree) except Exception as e: log.exception("Exception in write XML: " + str(e)) if config.plugins.seriesplugin.crossepg.value: log.debug("Write: xml channels for crossepg") if self.crossepg: try: self.crossepg.writeXML(etree) except Exception as e: log.exception("Exception in write XML: " + str(e))
def mimi_fetch_interactions(gene_id, taxid=None): gene_id = str(gene_id) url = MIMI_INT_URL % gene_id try: if _SNIPPER_DEBUG: print "DEBUG: executing MiMI URL %s" % url except: pass xml = urllib2.urlopen(url, timeout=CON_TIMEOUT) tree = ElementTree() tree.parse(xml) go_pattern = re.compile("(.+) \[GO:(\d+)\]") def extract(pattern, string): match = pattern.search(string) if match: return match.groups() else: return (None, None) results = [] for int_gene in tree.getroot().findall( "MiMI/Response/ResultSet/Result/InteractingGene"): other_gene = int_gene.find("GeneID").text interaction = GeneInteraction(gene_id, other_gene) for element in int_gene.getchildren(): if element.tag == "TaxonomyID": interaction.set_tax(element.text) elif element.tag == "InteractionAttribute": type = element.get('type') if type == "Component": tup = extract(go_pattern, element.text) interaction.add_component(*tup) elif type == "Function": tup = extract(go_pattern, element.text) interaction.add_function(*tup) elif type == "Process": tup = extract(go_pattern, element.text) interaction.add_process(*tup) elif type == "Provenance": interaction.add_provenance(element.text) elif type == "PubMed": interaction.add_pubmed(element.text) elif type == "InteractionType": interaction.add_interaction_type(element.text) # Taxonomy ID filter. if taxid != None: if interaction.taxon_id != taxid: continue results.append(interaction) return results
def run(self): windir = os.path.join(constant.profile['HOMEDRIVE'], string_to_unicode(os.sep), u'Windows') files = [ 'Panther\\Unattend.xml', 'Panther\\Unattended.xml', 'Panther\\Unattend\\Unattended.xml', 'Panther\\Unattend\\Unattend.xml', 'System32\\Sysprep\\unattend.xml', 'System32\\Sysprep\\Panther\\unattend.xml' ] pwd_found = [] xmlns = '{urn:schemas-microsoft-com:unattend}' for file in files: path = os.path.join(windir, string_to_unicode(file)) if os.path.exists(path): self.debug(u'Unattended file found: %s' % path) tree = ElementTree(file=path) root = tree.getroot() for setting in root.findall('%ssettings' % xmlns): component = setting.find('%scomponent' % xmlns) auto_logon = component.find('%sauto_logon' % xmlns) if auto_logon: username = auto_logon.find('%sUsername' % xmlns) password = auto_logon.find('%sPassword' % xmlns) if all((username, password)): # Remove false positive (with following message on password => *SENSITIVE*DATA*DELETED*) if 'deleted' not in password.text.lower(): pwd_found.append({ 'Login': username.text, 'Password': self.try_b64_decode(password.text) }) user_accounts = component.find('%suser_accounts' % xmlns) if user_accounts: local_accounts = user_accounts.find( '%slocal_accounts' % xmlns) if local_accounts: for local_account in local_accounts.findall( '%slocal_account' % xmlns): username = local_account.find('%sName' % xmlns) password = local_account.find('%sPassword' % xmlns) if all((username, password)): if 'deleted' not in password.text.lower(): pwd_found.append({ 'Login': username.text, 'Password': self.try_b64_decode(password.text) }) return pwd_found
def create_zip(file_path): tree = ElementTree(file=file_path) root = tree.getroot() db_name = root.attrib['name'] if 'zip_name' in root.attrib: zip_name = root.attrib['zip_name'] subprocess.call(['zip', '-q', '-r', zip_name, db_name]) print('The MD5 checksum of %s is %s' % (zip_name, md5_checksum(zip_name)))
def mimi_fetch_interactions(gene_id,taxid=None): gene_id = str(gene_id); url = MIMI_INT_URL % gene_id; try: if _SNIPPER_DEBUG: print "DEBUG: executing MiMI URL %s" % url; except: pass xml = urllib2.urlopen(url,timeout=CON_TIMEOUT); tree = ElementTree(); tree.parse(xml); go_pattern = re.compile("(.+) \[GO:(\d+)\]"); def extract(pattern,string): match = pattern.search(string); if match: return match.groups(); else: return (None,None); results = []; for int_gene in tree.getroot().findall("MiMI/Response/ResultSet/Result/InteractingGene"): other_gene = int_gene.find("GeneID").text; interaction = GeneInteraction(gene_id,other_gene); for element in int_gene.getchildren(): if element.tag == "TaxonomyID": interaction.set_tax(element.text); elif element.tag == "InteractionAttribute": type = element.get('type'); if type == "Component": tup = extract(go_pattern,element.text); interaction.add_component(*tup); elif type == "Function": tup = extract(go_pattern,element.text); interaction.add_function(*tup); elif type == "Process": tup = extract(go_pattern,element.text); interaction.add_process(*tup); elif type == "Provenance": interaction.add_provenance(element.text); elif type == "PubMed": interaction.add_pubmed(element.text); elif type == "InteractionType": interaction.add_interaction_type(element.text); # Taxonomy ID filter. if taxid != None: if interaction.taxon_id != taxid: continue; results.append(interaction); return results;
def normalizeXML(value): if value[0] == '<': try: tree = ElementTree(file=StringIO(value)) except Exception: return False, " Could not parse XML value: %s\n" % (value,) value = tostring(tree.getroot()) return value
def normalizeXML(value): if value[0] == '<': try: tree = ElementTree(file=StringIO(value)) except Exception: return False, " Could not parse XML value: %s\n" % (value,) value = tostring(tree.getroot()) return value
def extractElements(self, elementpath, parent, respdata): try: tree = ElementTree() tree.parse(StringIO(respdata)) except: return None if parent: tree_root = nodeForPath(tree.getroot(), parent) if not tree_root: return None tree_root = tree_root[0] # Handle absolute root element if elementpath[0] == '/': elementpath = elementpath[1:] root_path, child_path = xmlPathSplit(elementpath) if child_path: if tree_root.tag != root_path: return None e = tree_root.findall(child_path) else: e = (tree_root,) else: # Strip off the top-level item if elementpath[0] == '/': elementpath = elementpath[1:] splits = elementpath.split('/', 1) root = splits[0] if tree.getroot().tag != root: return None elif len(splits) == 1: return tree.getroot().text else: elementpath = splits[1] e = tree.findall(elementpath) if e is not None: return [item.text for item in e] else: return None
def extractElements(self, elementpath, parent, respdata): try: tree = ElementTree() tree.parse(StringIO(respdata)) except: return None if parent: tree_root = nodeForPath(tree.getroot(), parent) if not tree_root: return None tree_root = tree_root[0] # Handle absolute root element if elementpath[0] == '/': elementpath = elementpath[1:] root_path, child_path = xmlPathSplit(elementpath) if child_path: if tree_root.tag != root_path: return None e = tree_root.findall(child_path) else: e = (tree_root,) else: # Strip off the top-level item if elementpath[0] == '/': elementpath = elementpath[1:] splits = elementpath.split('/', 1) root = splits[0] if tree.getroot().tag != root: return None elif len(splits) == 1: return tree.getroot().text else: elementpath = splits[1] e = tree.findall(elementpath) if e is not None: return [item.text for item in e] else: return None
def create_zip(file_path): tree = ElementTree(file=file_path) root = tree.getroot() db_name = root.attrib['name'] if 'zip_name' in root.attrib: zip_name = root.attrib['zip_name'] subprocess.call(['zip', '-q', '-r', zip_name, db_name]) print('The MD5 checksum of %s is %s' % (zip_name, md5_checksum(zip_name)))
def get_links(self, pkg_name): """ Return the simple page links """ tree = ElementTree() try: tree.parse(urllib2.urlopen('%s/simple/%s' % (self.uri, pkg_name))) except (urllib2.HTTPError, exceptions.SyntaxError): return None ns = self._get_namespace(tree) return tree.getroot().findall(ns + 'body/ul/li/a')
def parse_pipeline_preset_xml(file_name): if not os.path.exists(file_name): raise IOError("Unable to find preset in {f}".format(f=file_name)) t = ElementTree(file=file_name) r = t.getroot() task_options = parse_task_options(r) wopts_tlist = parse_workflow_options(r) wopts = dict(wopts_tlist) workflow_options = validate_workflow_options(wopts) return PresetRecord(task_options, workflow_options)
def get_links(self, pkg_name): """ Return the simple page links. """ tree = ElementTree() try: tree.parse(urllib2.urlopen('%s/simple/%s' % (self.uri, pkg_name))) except (urllib2.HTTPError, exceptions.SyntaxError): return None return tree.getroot().findall('body/a')
def decorate_item(cls, leaf): # FIXME: Very simplified .savedSearch parsing, so far we only support # the query, without additional filtering. The simplest form of # .savedSearch file is saved by nautilus as following: # <query version="1.0"> # <text>QUERY GOES HERE</text> # </query> if not leaf.object.endswith(".savedSearch"): return None try: et = ElementTree(file=leaf.object) query = et.getroot().find("text").text if not query: return None location_tag = et.getroot().find("location") location = location_tag.text if location_tag is not None else None return cls(query, location=location_uri(location)) except Exception: pretty.print_exc(__name__) return None
def __parse_pipeline_template_xml(binding_func, file_name, registered_pipelines): t = ElementTree(file=file_name) r = t.getroot() bindings = binding_func(r, registered_pipelines) task_options = parse_task_options(r) wopts_tlist = parse_workflow_options(r) wopts = dict(wopts_tlist) workflow_options = validate_workflow_options(wopts) return BuilderRecord(bindings, task_options, workflow_options)
def decorate_item(cls, leaf): # FIXME: Very simplified .savedSearch parsing, so far we only support # the query, without additional filtering. The simplest form of # .savedSearch file is saved by nautilus as following: # <query version="1.0"> # <text>QUERY GOES HERE</text> # </query> if not leaf.object.endswith(".savedSearch"): return None try: et = ElementTree(file=leaf.object) query = et.getroot().find("text").text if not query: return None location_tag = et.getroot().find("location") location = location_tag.text if location_tag is not None else None return cls(query, location=location_uri(location)) except Exception: pretty.print_exc(__name__) return None
def normalizeXMLData(data): # Read in XML try: tree = ElementTree(file=StringIO.StringIO(data)) except Exception: raise ValueError("Could not parse XML data") # Apply filters for filter in filters: for node in tree.getiterator(filter): node.clear() return tostring(tree.getroot())
def xml(self, cmd, **kwargs): # NOTE: darcs is currently encoding agnostic and will print # patch metadata byte-for-byte, even in the XML changelog. etree = ElementTree() # While we are decoding the XML as latin-1 to be as liberal as # possible, etree will still raise an exception if any # non-printable characters are in the XML changelog. parser = XMLParser(encoding="latin-1") fp = self._run(cmd, **kwargs) etree.parse(fp, parser=parser) self.checkexit(fp.close()) return etree.getroot()
def normalizeXMLData(data): # Read in XML try: tree = ElementTree(file=StringIO.StringIO(data)) except Exception: raise ValueError("Could not parse XML data") # Apply filters for filter in filters: for node in tree.getiterator(filter): node.clear() return tostring(tree.getroot())
def run(self, profile): interfaces_dir = os.path.join('C:\\ProgramData\\Microsoft\\Wlansvc\\Profiles\\Interfaces') # # for windows Vista or higher if not os.path.isdir(interfaces_dir): return pwd_found = [] for wifi_dir in os.listdir(interfaces_dir): repository = os.path.join(interfaces_dir, wifi_dir) if not os.path.isdir(repository): continue for file in os.listdir(repository): f = os.path.join(repository, file) if not os.path.isfile(f): continue values = {} tree = ElementTree(file=f) root = tree.getroot() xmlns = root.tag.split("}")[0] + '}' for elem in tree.iter(): if elem.tag.endswith('SSID'): for w in elem: if w.tag == xmlns + 'name': values['SSID'] = w.text if elem.tag.endswith('authentication'): values['Authentication'] = elem.text if elem.tag.endswith('protected'): values['Protected'] = elem.text if elem.tag.endswith('keyMaterial'): try: password = self.decrypt_using_lsa_secret(elem.text, profile) if not password: password = self.decrypt_using_netsh(ssid=values['SSID']) if password: values['Password'] = password else: values['INFO'] = '[!] Password not found.' except Exception: log.error(traceback.format_exc()) values['INFO'] = '[!] Password not found.' if values: pwd_found.append(values) return pwd_found
def xml(self, cmd, **kwargs): # NOTE: darcs is currently encoding agnostic and will print # patch metadata byte-for-byte, even in the XML changelog. etree = ElementTree() # While we are decoding the XML as latin-1 to be as liberal as # possible, etree will still raise an exception if any # non-printable characters are in the XML changelog. parser = XMLParser(encoding='latin-1') fp = self._run(cmd, **kwargs) etree.parse(fp, parser=parser) self.checkexit(fp.close()) return etree.getroot()
def test_read(self): r = StringIO.StringIO(easy_string) tree = xml_read(r) b = '<XML_root>\n' + easy_string + '</XML_root>' b = ElementTree(fromstring(b)) bt = b.getroot() b0 = bt[0].tag b00 = bt[0][0].tag treet = tree.getroot() tree0 = treet[0].tag tree00 = treet[0][0].tag self.assert_(b0 == tree0) self.assert_(b00 == tree00)
def extract_teams(url): tree = ElementTree() parser = XMLParser(encoding="iso-8859-1") data = urllib.urlopen(url) tree.parse(data, parser=parser) teams = tree.getroot().find("teams").findall("team") allteams = {} for team in teams: name = team.attrib["name"] _id = team.attrib["id"] sail = team.attrib["sail"] allteams[_id] = "{0} ({1})".format(name, sail) return allteams
def run(self, profile): path = '{APPDATA}\\.purple\\accounts.xml'.format(**profile) if os.path.exists(path): tree = ElementTree(file=path) root = tree.getroot() pwd_found = set() for account in root.findall('account'): name = account.find('name') password = account.find('password') if None not in (name, password): pwd_found.add([name.text, password.text]) return list(pwd_found)
def run(self): windir = os.path.join(constant.profile['HOMEDRIVE'], string_to_unicode(os.sep), u'Windows') files = [ 'Panther\\Unattend.xml', 'Panther\\Unattended.xml', 'Panther\\Unattend\\Unattended.xml', 'Panther\\Unattend\\Unattend.xml', 'System32\\Sysprep\\unattend.xml', 'System32\\Sysprep\\Panther\\unattend.xml' ] pwd_found = [] xmlns = '{urn:schemas-microsoft-com:unattend}' for file in files: path = os.path.join(windir, string_to_unicode(file)) if os.path.exists(path): self.debug(u'Unattended file found: %s' % path) tree = ElementTree(file=path) root = tree.getroot() for setting in root.findall('%ssettings' % xmlns): component = setting.find('%scomponent' % xmlns) auto_logon = component.find('%sauto_logon' % xmlns) if auto_logon: username = auto_logon.find('%sUsername' % xmlns) password = auto_logon.find('%sPassword' % xmlns) if all((username, password)): # Remove false positive (with following message on password => *SENSITIVE*DATA*DELETED*) if 'deleted' not in password.text.lower(): pwd_found.append({ 'Login': username.text, 'Password': self.try_b64_decode(password.text) }) user_accounts = component.find('%suser_accounts' % xmlns) if user_accounts: local_accounts = user_accounts.find('%slocal_accounts' % xmlns) if local_accounts: for local_account in local_accounts.findall('%slocal_account' % xmlns): username = local_account.find('%sName' % xmlns) password = local_account.find('%sPassword' % xmlns) if all((username, password)): if 'deleted' not in password.text.lower(): pwd_found.append({ 'Login': username.text, 'Password': self.try_b64_decode(password.text) }) return pwd_found
def writeXMLTVConfig(self): if self.epgimport is None and self.xmltvimport is None and self.crossepg is None: return if int(self.epgimportversion[0]) >= 5 and int(self.xmltvimportversion[0]) >= 5 and int(self.crossepgversion[0]) >= 5: return; if config.plugins.seriesplugin.epgimport.value == False and config.plugins.seriesplugin.xmltvimport.value == False and config.plugins.seriesplugin.crossepg.value == False: return # Build Header from plugin import NAME, VERSION root = Element("sources") root.set('version', VERSION) root.set('created_by', NAME) root.append(Comment(_("Don't edit this manually unless you really know what you are doing"))) element = SubElement( root, "source", type = "gen_xmltv", channels = "wunschliste.channels.xml" ) SubElement( element, "description" ).text = "Wunschliste XMLTV" SubElement( element, "url" ).text = config.plugins.seriesplugin.xmltv_url.value etree = ElementTree( root ) indent(etree.getroot()) if config.plugins.seriesplugin.epgimport.value: log.debug("Write: xml channels for epgimport") if self.epgimport: try: self.epgimport.writeXML( etree ) except Exception as e: log.exception("Exception in write XML: " + str(e)) if config.plugins.seriesplugin.xmltvimport.value: log.debug("Write: xml channels for xmltvimport") if self.xmltvimport: try: self.xmltvimport.writeXML( etree ) except Exception as e: log.exception("Exception in write XML: " + str(e)) if config.plugins.seriesplugin.crossepg.value: log.debug("Write: xml channels for crossepg") if self.crossepg: try: self.crossepg.writeXML( etree ) except Exception as e: log.exception("Exception in write XML: " + str(e))
class etree(ebase): def __init__( self, fname ): ebase.__init__( self ) self.et = ElementTree( file=fname ) def write( self, fname ): self.et.write(fname) def ensure_child( self, tag ): retval = self.et.find("./"+tag) if not retval is None: return elem( retval ) else: return elem( SubElement( self.et.getroot(), tag ) )
def add_agents_to_xml(config_xml, asl_list): tree = ElementTree() tree.parse(config_xml) root = tree.getroot() nodes = root.findall('asl-list') for node in nodes: root.remove(node) node = SubElement(root, 'asl-list') for entry in asl_list: agentNode = SubElement(node, 'asl') for key, value in entry.iteritems(): agentNode.attrib[key] = value return tree
def parse_pipeline_preset_xml(file_name, validate=True): if not os.path.exists(file_name): raise IOError("Unable to find preset in {f}".format(f=file_name)) t = ElementTree(file=file_name) r = t.getroot() task_options = parse_task_options(r) wopts_tlist = parse_workflow_options(r) wopts = dict(wopts_tlist) # XXX if we have multiple preset XMLs, we need to postpone this step # until all of them have been collected and merged if validate: wopts_tlist = validate_workflow_options(wopts) # this API is a bit funky. [(k, v), ..] is the format return PresetRecord(task_options, wopts_tlist)
def parse_pipeline_preset_xml(file_name, validate=True): if not os.path.exists(file_name): raise IOError("Unable to find preset in {f}".format(f=file_name)) t = ElementTree(file=file_name) r = t.getroot() task_options = parse_task_options(r) wopts_tlist = parse_workflow_options(r) wopts = dict(wopts_tlist) # XXX if we have multiple preset XMLs, we need to postpone this step # until all of them have been collected and merged if validate: wopts_tlist = validate_workflow_options(wopts) # this API is a bit funky. [(k, v), ..] is the format return PresetRecord(task_options, wopts_tlist)
def extract_pos(link): root_namespace_reg = re.compile("\{.*\}", re.IGNORECASE) root_namespace = None try: data = urllib.urlopen(link) tree = ElementTree() parser = XMLParser(encoding="iso-8859-1") tree.parse(data, parser=parser) root_namespace = root_namespace_reg.findall(tree.getroot().tag)[0] name = tree.find(".//{0}Document//{0}name".format(root_namespace)).text # parse important datas unparsed = tree.find(".//{0}Document//{0}Placemark//{0}description".format(root_namespace)).text except Exception, e: return
def __parse_pipeline_template_xml(binding_func, file_name, registered_pipelines): t = ElementTree(file=file_name) r = t.getroot() bindings, task_opts = binding_func(r, registered_pipelines) # Values from XML file. Returned as a [(k, v), ] similar to the bindings task_options = dict(parse_task_options(r)) # Override the pipeline templated defined task option defaults with # the values in the XML task_options.update(task_opts) wopts_tlist = parse_workflow_options(r) wopts = dict(wopts_tlist) workflow_options = validate_workflow_options(wopts) return BuilderRecord(bindings, task_options, workflow_options)
def run(self): path = os.path.join(constant.profile['APPDATA'], u'.purple', u'accounts.xml') if os.path.exists(path): tree = ElementTree(file=path) root = tree.getroot() pwd_found = [] for account in root.findall('account'): name = account.find('name') password = account.find('password') if all((name, password)): pwd_found.append({ 'Login': name.text, 'Password': password.text }) return pwd_found
def decorate_item(cls, leaf): # FIXME: Very simplified .savedSearch parsing, so far we only support # the query, without additional filtering. The simplest form of # .savedSearch file is saved by nautilus as following: # <query version="1.0"> # <text>QUERY GOES HERE</text> # </query> if not leaf.object.endswith(".savedSearch"): return None try: et = ElementTree(file=leaf.object) query = et.getroot().find("text").text return cls(query) except Exception: return None
def _parse(f): tree = ElementTree() tree.parse(f) root = tree.getroot() if root.tag == 'html': # weird api, returns unstructured html on error try: err = root.findall('p')[2].text except: try: err = root.find('h1').text except: err = 'Unknown error' raise TranslateError(err) # if DEBUG: # print tostring(root) return root
def __init__(self, dump_dir, feed_dir, source_root, site_metadata_file): self.dump_dir = dump_dir self.feed_dir = feed_dir self.source_root = source_root self.dumps = read_dumps(self.dump_dir) self.transitive_dependencies = to_mutable_graph(self.dumps) inplace_transitive_closure(self.transitive_dependencies) # eliminate self-loops for v0,v1s in self.transitive_dependencies.items(): v1s.discard(v0) # Make sure there are no modularity violations self._find_dependency_cycles() self.version = '1.49-post-' + datetime.utcnow().strftime("%Y%m%d%H%M") print '### new version =', self.version print '### reading Boost library metadata...' t = ElementTree() t.parse(site_metadata_file) self.boost_metadata = t.getroot().findall('library') self._delete_old_feeds() use_threads = True if use_threads: self.tasks = threadpool.ThreadPool(8) else: class Tasks(object): def add_task(self, f, *args): return f(*args) self.tasks = Tasks() for cluster in self.clusters: self.tasks.add_task(self._write_cluster_feed, cluster) for cmake_name in self.dumps: self.GenerateRepo(self, cmake_name) print '### Awaiting completion...' self.tasks.wait_completion() print '### Done.'