def _get_article_contrib_authors(tree): from sys import stderr """ Given an ElementTree, returns article authors in a format suitable for citation. """ authors = [] front = ElementTree(tree).find('front') for contrib in front.iter('contrib'): contribTree = ElementTree(contrib) try: surname = contribTree.find('name/surname').text except AttributeError: # author is not a natural person try: citation_name = contribTree.find('collab').text if citation_name is not None: authors.append(citation_name) continue except AttributeError: # name has no immediate text node continue try: given_names = contribTree.find('name/given-names').text citation_name = ' '.join([surname, given_names[0]]) except AttributeError: # no given names citation_name = surname except TypeError: # also no given names citation_name = surname if citation_name is not None: authors.append(citation_name) return ', '.join(authors)
def _get_pmcid(tree): """ Given an ElementTree, returns PubMed Central ID. """ front = ElementTree(tree).find('front') for article_id in front.iter('article-id'): if article_id.attrib['pub-id-type'] == 'pmc': return article_id.text
def _get_journal_title(tree): """ Given an ElementTree, returns journal title. """ front = ElementTree(tree).find('front') for journal_meta in front.iter('journal-meta'): for journal_title in journal_meta.iter('journal-title'): return journal_title.text
def _get_article_url(tree): """ Given an ElementTree, returns article URL. """ article_meta = ElementTree(tree).find('front/article-meta') for article_id in article_meta.iter('article-id'): if article_id.attrib['pub-id-type'] == 'doi': return 'http://dx.doi.org/' + article_id.text return '' # FIXME: this should never, ever happen
def parse_xml(self, path): pwd_found = [] if os.path.exists(path): tree = ElementTree(file=path) elements = {'name': 'Name', 'url': 'URL', 'userName': '******', 'password': '******'} for elem in tree.iter('Bean'): values = {} for e in elem: if e.tag in elements: values[elements[e.tag]] = e.text if values: pwd_found.append(values) return pwd_found
def run(self): path = os.path.join(constant.profile['USERPROFILE'], u'.squirrel-sql', u'SQLAliases23.xml') if os.path.exists(path): tree = ElementTree(file=path) pwd_found = [] elements = {'name': 'Name', 'url': 'URL', 'userName': '******', 'password': '******'} for elem in tree.iter('Bean'): values = {} for e in elem: if e.tag in elements: values[elements[e.tag]] = e.text if values: pwd_found.append(values) return pwd_found
def _get_article_date(tree): """ Given an ElementTree, returns article date. """ article_meta = ElementTree(tree).find('front/article-meta') for pub_date in article_meta.iter('pub-date'): if pub_date.attrib['pub-type'] == 'epub': pub_date_tree = ElementTree(pub_date) year = int(pub_date_tree.find('year').text) try: month = int(pub_date_tree.find('month').text) except AttributeError: month = 1 # TODO: is this correct? try: day = int(pub_date_tree.find('day').text) except AttributeError: day = 1 # TODO: is this correct? return str(date(year, month, day)) return ''
def run(self): xml_file = self.get_application_path() if xml_file and os.path.exists(xml_file): tree = ElementTree(file=xml_file) pwd_found = [] for elem in tree.iter(): try: if elem.attrib['name'].startswith('ftp') or elem.attrib['name'].startswith('ftps') \ or elem.attrib['name'].startswith('sftp') or elem.attrib['name'].startswith('http') \ or elem.attrib['name'].startswith('https'): encrypted_password = base64.b64decode(elem.attrib['value']) password = Win32CryptUnprotectData(encrypted_password, is_current_user=constant.is_current_user, user_dpapi=constant.user_dpapi) pwd_found.append({ 'URL': elem.attrib['name'], 'Password': password, }) except Exception as e: self.debug(str(e)) return pwd_found
def get_passphrase(self, path): xml_name = u'product-preferences.xml' xml_file = None if os.path.exists(os.path.join(path, xml_name)): xml_file = os.path.join(path, xml_name) else: for p in os.listdir(path): if p.startswith('system'): new_directory = os.path.join(path, p) for pp in os.listdir(new_directory): if pp.startswith(u'o.sqldeveloper'): if os.path.exists(os.path.join(new_directory, pp, xml_name)): xml_file = os.path.join(new_directory, pp, xml_name) break if xml_file: tree = ElementTree(file=xml_file) for elem in tree.iter(): if 'n' in elem.attrib.keys(): if elem.attrib['n'] == 'db.system.id': return elem.attrib['v']
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()
__copyright__ = 'Copyright (C) 2015 Karl Wette' import sys from xml.etree.cElementTree import ElementTree # print error message and exit def fail(msg): sys.stderr.write('%s: %s\n' % (sys.argv[0], msg)) sys.exit(1) # get input arguments tagfile, = sys.argv[1:] # parse tag file tree = ElementTree() try: tree.parse(tagfile) except: fail("could not parse XML input from '%s'" % tagfile) # warn on duplicate documentation anchors anchors = dict() for elem in tree.iter('docanchor'): if elem.text in anchors: anchors[elem.text] = anchors[elem.text] + 1 else: anchors[elem.text] = 1 for anchor in anchors: if anchors[anchor] > 1: print('%s: warning: duplicate anchor %s' % (tagfile, anchor))
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[ 'Authentication'] != 'open': pwd_found.append(values) constant.wifi_password = True return pwd_found
sys.exit(1) # get input arguments tagfile, = sys.argv[1:] # parse tag file tree = ElementTree() try: tree.parse(tagfile) except: fail("could not parse XML input from '%s'" % tagfile) # find duplicate documentation anchors anchors = dict() for elem in tree.iter('docanchor'): if not elem.text in anchors: anchors[elem.text] = [] anchors[elem.text].append(elem) dup_anchors = dict( (anchor, elems) for anchor, elems in anchors.items() if len(elems) > 1) # remove duplicate documentation anchors, preferring namespace and group anchors to other kinds parent_map = dict( (elem, parent_elem) for parent_elem in tree.iter() for elem in parent_elem) for anchor in dup_anchors: elem_rank = dict() for elem in dup_anchors[anchor]: parent_elem = elem while parent_elem in parent_map: parent_elem = parent_map[parent_elem]
def parse_xml(xml_file): tree = ElementTree(file=xml_file) print("Iterating using a tree iterator") for elem in tree.iter(): print(f'{elem.tag=}, {elem.text=}')