def modify_meta_data(self, extra_dir, key, value): print print "=====================" print "+++Update meta_data++" print "=====================" print ">>>extra_dir: %s" % extra_dir print ">>>key: %s" % key print ">>>value: %s" % value mani_xml_path = "%s/AndroidManifest.xml" % extra_dir schemas = "http://schemas.android.com/apk/res/android" ET.register_namespace("android", schemas) tree = ET.parse(mani_xml_path) k_android_name = "{%s}name" % schemas k_android_value = "{%s}value" % schemas application = tree.find("application") items = application.findall("meta-data") meta_data = None for item in items: android_name = item.get(k_android_name) if android_name == key: meta_data = item break if meta_data != None: old_android_value = meta_data.get(k_android_value) print ">>>old meta-data: %s" % old_android_value if value: meta_data.set(k_android_value, str(value)) tree.write(mani_xml_path, "utf-8", True) new_android_value = meta_data.get(k_android_value) print ">>>new meta-data: %s" % new_android_value print "Update meta-data %s to %s => OK" % (key, value) print else: print ">>>not exists meta-data %s" % key
def ban(self, jid, reason=None): """Ban a user""" ET.register_namespace('', self.muc_namespace) root_element = ET.Element('{' + self.muc_namespace + '}query') item = ET.SubElement(root_element, 'item') item.set('affiliation', 'outcast') item.set('jid', jid) if reason is not None: reasonelement = ET.SubElement(item, 'reason') reasonelement.text = reason iq = self.connection.create_iq(id='ban', itype='set', payload=root_element, namespace=self.muc_namespace) try: response = iq.send() log.debug('Ban success') return True except IqError as iqe: log.warning('IqError happened! Error: ' + iqe.text) return False except IqTimeout as iqt: log.warning('IqTimeout happened!') return False
def modify_app_lanucher(self, decoded_file_path, ldpi, mdpi, hdpi, xhdpi, xxhdpi): print print "=====================" print "+++Update app icon+++" print "=====================" print ">>>decoded_file_path: %s" % decoded_file_path print ">>>ldpi: %s" % ldpi print ">>>mdpi: %s" % mdpi print ">>>hdpi: %s" % hdpi print ">>>xhdpi: %s" % xhdpi print ">>>xxhdpi: %s" % xxhdpi mani_xml_path = "%s/AndroidManifest.xml" % decoded_file_path schemas = "http://schemas.android.com/apk/res/android" ET.register_namespace("android", schemas) tree = ET.parse(mani_xml_path) app = tree.find("application") k_icon = "{%s}icon" % schemas s_icon = app.get(k_icon) icon_array = s_icon.split("/") new_icon_name = "%s.png" % icon_array[1] print ">>>New icon name: %s" % new_icon_name if hdpi: res_path = "%s/res" % decoded_file_path res_array = os.listdir(res_path) res_array = os.listdir(res_path) for item in res_array: if item.startswith("drawable"): self.modify_drawable_icon(decoded_file_path, item, ldpi, mdpi, hdpi, xhdpi, xxhdpi, new_icon_name) else: print ">>>You must give me hdpi image"
def delete_dest_laucher_action_category(self): dest_mani = "%s/AndroidManifest.xml" % self.decode_dir schemas = "http://schemas.android.com/apk/res/android" ET.register_namespace("android", schemas) k_android_name = "{%s}name" % schemas tree = ET.parse(dest_mani) application = tree.find("application") activitys = application.findall("activity") for activity in activitys: intent_filters = activity.findall("intent-filter") for intent_filter in intent_filters: action_main = None actions = intent_filter.findall("action") for action in actions: android_name = action.get(k_android_name) if android_name == "android.intent.action.MAIN": action_main = action category_launcher = None categories = intent_filter.findall("category") for category in categories: android_name = category.get(k_android_name) if android_name == "android.intent.category.LAUNCHER": category_launcher = category if action_main != None and category_launcher != None: intent_filter.remove(action_main) intent_filter.remove(category_launcher) tree.write(dest_mani, "utf-8", True) print "Delete dest launcher action category %s => OK" % dest_mani print
def banlist(self): """Fetch the banlist and process it""" # Register namespace to an empty prefix ET.register_namespace('', self.muc_namespace) # Create the query elements root_element = ET.Element('{' + self.muc_namespace + '}query') item = ET.SubElement(root_element, 'item') item.set('affiliation', 'outcast') # Create IQ stanza iq = self.connection.create_iq(id='banlist', itype='get', payload=root_element, namespace=self.muc_namespace) # Get response and find elements inside try: response = iq.send() items = response.findall('.//{' + self.muc_namespace + '}item') log.debug("Banlist contents: " + str(items)) res = "" for item in items: if item.get('jid') is not None: res += "\n" + item.get('jid') + ": " + str(item[0].text) return res except IqError as iqe: log.warning('IqError happened! Error: ' + iqe.text) return iqe.text except IqTimeout as iqt: log.warning('IqTimeout happened!') return iqt.text
def update_dest_mani_application(self, new_items, tag): print print "=================================" print "++++Merge mani application+++++++" print "=================================" print ">>>tag: %s" % tag dest_mani = "%s/AndroidManifest.xml" % self.decode_dir schemas = "http://schemas.android.com/apk/res/android" ET.register_namespace("android", schemas) k_android_name = "{%s}name" % schemas tree = ET.parse(dest_mani) application = tree.find("application") old_items = application.findall(tag) delete_items = [] # 检索出android:name值相同的 for old_item in old_items: old_item_android_name = old_item.get(k_android_name) for new_item in new_items: new_item_android_name = new_item.get(k_android_name) if old_item_android_name == new_item_android_name: delete_items.append(old_item) # 去除android:name值相同的 for delete_item in delete_items: application.remove(delete_item) tree.write(dest_mani, "utf-8", True) # 添加新的 for new_item in new_items: application.append(new_item) tree.write(dest_mani, "utf-8", True) print "Update mani application tag %s %s => OK" % (tag, dest_mani) print
def update_dest_mani_permission(self, new_permissions): print print "=================================" print "++++Merge mani permission+++++++" print "=================================" dest_mani = "%s/AndroidManifest.xml" % self.decode_dir schemas = "http://schemas.android.com/apk/res/android" ET.register_namespace("android", schemas) k_android_name = "{%s}name" % schemas tree = ET.parse(dest_mani) root = tree.getroot() old_permissions = root.findall("uses-permission") # 添加新的 for new_permission in new_permissions: # 判断是否存在相同的 new_permission_android_name = new_permission.get(k_android_name) exist_permission = False for old_permission in old_permissions: old_permission_android_name = old_permission.get( k_android_name) if new_permission_android_name == old_permission_android_name: exist_permission = True break # 不存在则添加 if not exist_permission: root.append(new_permission) tree.write(dest_mani, "utf-8", True) print "Update mani permissions %s => OK" % dest_mani print
def track_to_garmin_gpxx(track, pretty=False): ns = gpx_ns root = xml.Element(ns('gpx')) root.set(xsi_ns('schemaLocation'), ' '.join([ _GPX_NS, _GPX_NS_XSD, _TPX_NS, _TPX_NS_XSD])) root.set(ns('version'), '1.1') root.set(ns('creator'), 'Bryton-GPS-Linux') xml.register_namespace('', _GPX_NS) xml.register_namespace('gpxtpx', _TPX_NS) trk = xml.SubElement(root, ns('trk')) for seg in track.merged_segments(): create_tpx_trkseg(seg, trk, ns) if pretty: indent_element_tree(root, ws=' ') return "<?xml version='1.0' encoding='utf-8'?>\n" + xml.tostring(root)
def modify_mani_activity_name(self, extra_dir, old_activity, new_activity): print print "=====================" print "+Update Mani activity" print "=====================" mani_xml_path = "%s/AndroidManifest.xml" % extra_dir schemas = "http://schemas.android.com/apk/res/android" ET.register_namespace("android", schemas) tree = ET.parse(mani_xml_path) k_android_name = "{%s}name" % schemas application = tree.find("application") items = application.findall("activity") activity = None for item in items: android_name = item.get(k_android_name) if android_name == old_activity: activity = item break if activity != None: old_android_name = activity.get(k_android_name) print ">>>old activity name: %s" % old_android_name if new_activity: activity.set(k_android_name, str(new_activity)) tree.write(mani_xml_path, "utf-8", True) new_android_name = activity.get(k_android_name) print ">>>new activity name: %s" % new_android_name print "Update AndroidManifest.xml activity name %s to %s => OK" % (old_activity, new_activity) print else: print ">>>not exists activity name %s" % old_activity
def fix_mani_meta_bug(self, app_path): print print "==============================================" print "++++++Fix AndroidManifest meta-data bug+++++++" print "==============================================" try: mani_xml_path = "%s/AndroidManifest.xml" % app_path schemas = "http://schemas.android.com/apk/res/android" ET.register_namespace("android", schemas) tree = ET.parse(mani_xml_path) k_android_name = "{%s}name" % schemas application = tree.find("application") meta_datas = application.findall("meta-data") for meta_data in meta_datas: android_name = meta_data.get(k_android_name) if android_name.startswith("@string"): label_array = android_name.split("/") if len(label_array) == 2: key = label_array[1] value_in_strings = self.get_strings_item_value( app_path, key) meta_data.set(k_android_name, value_in_strings) tree.write(mani_xml_path, "utf-8", True) print "Fix mani meta-data bug in %s => OK" % app_path print except Exception, e: print "Fix mani meta-data bug in %s => Failure" % app_path logging.error(e)
def checkFile(fn, options): global current_file, warn_nbr, root current_file = fn print("Starting %s%s" % (fn, options)) tree = ET.parse(fn) root = tree.getroot() #print("root.attrib=%s, test -> %d" % (root.attrib, "xmlns" in root.attrib)) # # attrib list doesn't have includes "xmlns", even though it's there #print("root.tag=%s" % root.tag) no_ns = root.tag.find("{") < 0 #print("no_ns = %s" % no_ns) ET.register_namespace("", "http://www.w3.org/2000/svg") # Stops tree.write() from prefixing above with "ns0" check(root, 0) if trace and len(bad_namespaces) != 0: print("bad_namespaces = %s" % bad_namespaces) if new_file: sp = fn.rfind('.svg') if sp+3 != len(fn)-1: # Indeces of last chars print("filename doesn't end in '.svg' (%d, %d)" % (sp, len(fn))) else: if no_ns: root.attrib["xmlns"] = "http://www.w3.org/2000/svg" for ns in bad_namespaces: remove_namespace(root, ns) new_fn = fn.replace(".svg", ".new.svg") print("writing to %s" % (new_fn)) tree.write(new_fn) return warn_nbr
def xml(self): ET.register_namespace('', SCHEME_PREFIX[1:-1]) root = ET.Element("USERJOURNEY", {'APPNAME': self.app_name, 'NAME': self.name, 'VALID': 'true', 'VERSION': self.version}) root.set('xmlns', "http://www.reflective.com") root.set('xmlns:xsi', "http://www.w3.org/2001/XMLSchema-instance") root.set('xsi:schemaLocation', "http://www.reflective.com stschema.xsd") description = ET.SubElement(root, 'DESCRIPTION') plugin = ET.SubElement(root, 'PLUGIN', {'ID': '1'}) responseprocessor = ET.SubElement(root, 'RESPONSEPROCESSOR') created = ET.SubElement(root, 'CREATED') created.text = self.created for nvp in self.uj_global_settings: text = nvp['text'] attributes = {key: value for key, value in nvp.items() if key is not 'text'} new_nvp = ET.SubElement(root, 'NVP', attributes) new_nvp.text = text dynamic_data_element = ET.SubElement(root, 'DYNAMICDATA') for ddi in self.dditems: dynamic_data_element.append(ddi.xml()) steps_element = ET.SubElement(root, 'STEPS') for step in [step for sg in self.stepgroups for step in sg.steps]: st = step.xml() steps_element.append(step.xml()) rough_string = ET.tostring(root, 'utf-8') reparsed = minidom.parseString(rough_string) return reparsed.toprettyxml(indent='\t').replace('<?xml version="1.0" ?>\n', self.first_import_line)
def __init__(self, ns, prefix=None): """Args: ns (string) - namespace prefix (string) - optionnal prefix to register""" self.ns = ns if prefix: ElementTree.register_namespace(prefix, ns)
def __init__(self, root, req): """ <phyloxml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.phyloxml.org http://www.phyloxml.org/1.10/phyloxml.xsd" xmlns="http://www.phyloxml.org"> 002 <phylogeny rooted="false"> """ et.register_namespace('', self.namespace) self.req = req self.e = self.element('phyloxml') phylogeny = self.element('phylogeny', rooted="true") #rect = self.element('rectangular') #rect.append(self.element('alignRight', 'true')) #params = self.element('parameters') #params.append(rect) #render = self.element('render') #render.append(params) #phylogeny.append(render) """ <render> <parameters> <rectangular> <alignRight>true</alignRight> </rectangular> </parameters> </render> """ phylogeny.append(self.element('name', root.name)) phylogeny.append(self.element('description', root.name)) clade = self.clade(root) self.append_children(clade, root) phylogeny.append(clade) self.e.append(phylogeny)
def svg_icon(loader, name, css_class=''): """ Return inline SVG markup for an icon. This is a helper for generating inline SVGs for rendering icons in HTML that can be customized via CSS. See https://github.com/blog/2112-delivering-octicons-with-svg :param loader: Callable accepting an icon name and returning XML markup for the SVG. :param name: The name of the SVG file to render :param css_class: CSS class attribute for the returned `<svg>` element """ # Register SVG as the default namespace. This avoids a problem where # ElementTree otherwise serializes SVG elements with an 'ns0' namespace (eg. # '<ns0:svg>...') and browsers will not render the result as SVG. # See http://stackoverflow.com/questions/8983041 ElementTree.register_namespace('', SVG_NAMESPACE_URI) root = ElementTree.fromstring(loader(name)) if css_class: root.set('class', css_class) # If the SVG has its own title, ignore it in favor of the title attribute # of the <svg> or its containing element, which is usually a link. title_el = root.find('{{{}}}title'.format(SVG_NAMESPACE_URI)) if title_el is not None: root.remove(title_el) return Markup(ElementTree.tostring(root))
def track_to_tcx(track, pretty=False): ns = tcx_ns root = xml.Element(ns('TrainingCenterDatabase')) root.set(xsi_ns('schemaLocation'), ' '.join([ _TCX_NS, _TCX_NS_XSD, _ACT_EXT_NS, _ACT_EXT_NS_XSD])) xml.register_namespace('', _TCX_NS) xml.register_namespace('_ns3', _ACT_EXT_NS) activities = xml.SubElement(root, ns('Activities')) activity = xml.SubElement(activities, ns('Activity')) activity.set(ns('Sport'), 'Biking') xml.SubElement(activity, ns('Id')).text = \ format_timestamp(track.timestamp) create_laps(track, activity, ns) if pretty: indent_element_tree(root, ws=' ') out = xml.tostring(root) # ElementTree doesn't let me set prefix ns3 and a lot of software # seems to be hardcoded to use ns3 so have to use this little hack. out = out.replace('_ns3:', 'ns3:').replace('xmlns:_ns3', 'xmlns:ns3') return "<?xml version='1.0' encoding='utf-8'?>\n" + out
def to_string(self, endpoints): """ Converts the given endpoint description beans into a string :param endpoints: A list of EndpointDescription beans :return: A string containing an XML document """ # Make the ElementTree root = self._make_xml(endpoints) tree = ElementTree.ElementTree(root) # Force the default name space ElementTree.register_namespace("", EDEF_NAMESPACE) # Make the XML for encoding in ('unicode', 'UTF-8'): # Prepare a StringIO output output = StringIO() try: # Try to write with a correct encoding tree.write(output, encoding=encoding, xml_declaration=True, method="xml") break except LookupError: # 'unicode' is needed in Python 3, but unknown in Python 2... continue else: raise LookupError("Couldn't find a valid encoding") return output.getvalue()
def remove_invalid_point_name(fname, output_dir): print fname print LINE_SEPARATOR_CHAR_NUM * LINE_SEPARATOR_CHAR ElementTree.register_namespace('', "http://www.w3.org/2000/svg") tree = ElementTree.parse(fname) for node in tree.iter('{http://www.w3.org/2000/svg}text'): point_name = node.attrib.get('tag') # tag="LYRD.DLP.1TR5433" if point_name is not None: point_name = point_name.strip().split('.') point_name = point_name[len(point_name)-1] if point_name is not None and point_name in g_invalid_point_db_list: print point_name # 1TE5501-15 patterns = r'\d\w{2}\d{4}-\d{2}' match_obj = re.match(patterns, point_name, re.I) if match_obj: print match_obj.group() new_point_name = point_name.replace('-','',1) node.set('tag', 'LYRD.DLP.' + new_point_name) else: node.clear() tree.write(output_dir+fname.split("/")[3]) print LINE_SEPARATOR_CHAR_NUM * LINE_SEPARATOR_CHAR
def readworld(): from xml.etree import cElementTree as ElementTree ElementTree.register_namespace('xsi', 'http://www.w3.org/2001/XMLScheme-instance') namespace = {'xsi': 'http://www.w3.org/2001/XMLScheme-instance'} xmlPath = 'e:\\test.xml' xmlRoot = ElementTree.parse(xmlPath).getroot() results = xmlRoot.findall(".//SectorObjects/MyObjectBuilder_EntityBase[StorageName]") try: for result in results: roidname = result.find('StorageName').text for pos in result.iter('Position'): pos = pos.attrib posx = pos.get('x') posx = float(posx) posy = pos.get('y') posy = float(posy) posz = pos.get('z') posz = float(posz) #Do the damn thing createAsteroid(posx,posy,posz,roidname) #print(posz) print(roidname) except AttributeError as aE: print('no')
def __init__(self, content, device=None): self.content = content ET.register_namespace('', 'urn:psialliance-org') self.xml_node = ET.fromstring(self.content) print(self.xml_node) self.psia_ns = {'root_ns':'urn:psialliance-org'} if device is not None: self.device = device
def diff_locale_origin(locale_dir, origin_dir): origin_xml_files = os.listdir(os.path.join(origin_path, origin_dir)) locale_xml_files = os.listdir(os.path.join(origin_path, locale_dir)) locale_miss_dir = os.path.join(miss_path, locale_dir) for xml_file in origin_xml_files: o_xml_path = os.path.join(os.path.join(origin_path, origin_dir), xml_file) o_xml_tree = ET.ElementTree(file=o_xml_path) o_xml_root = o_xml_tree.getroot() l_xml_path = os.path.join(os.path.join(origin_path, locale_dir), xml_file) miss_xml_path = os.path.join(os.path.join(miss_path, locale_dir), xml_file) remove_list = [] if not os.path.exists(l_xml_path): for o_ele in o_xml_root: if o_ele.tag not in ['string', 'string-array', 'plurals']: remove_list.append(o_ele) continue o_ele_name = o_ele.attrib.get('name') translatable = o_ele.attrib.get('translatable') if translatable == 'false': remove_list.append(o_ele) continue i18n_enable = o_ele.attrib.get('i18n') if i18n_enable == 'false': remove_list.append(o_ele) continue for r_ele in remove_list: o_xml_root.remove(r_ele) else: l_xml_tree = ET.ElementTree(file=l_xml_path) l_xml_root = l_xml_tree.getroot() for o_ele in o_xml_root: if o_ele.tag not in ['string', 'string-array', 'plurals']: remove_list.append(o_ele) continue o_ele_name = o_ele.attrib.get('name') translatable = o_ele.attrib.get('translatable') if translatable == 'false': remove_list.append(o_ele) continue i18n_enable = o_ele.attrib.get('i18n') if i18n_enable == 'false': remove_list.append(o_ele) continue if l_xml_tree.find('%s[@name="%s"]' % (o_ele.tag, o_ele_name))\ is not None: remove_list.append(o_ele) for r_ele in remove_list: o_xml_root.remove(r_ele) if len(o_xml_root) > 0: ET.register_namespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2') ET.register_namespace('tools', 'http://schemas.android.com/tools') ET.ElementTree(o_xml_root).write(miss_xml_path, encoding='utf-8', xml_declaration=True)
def updatePomVersion(pomFile, newVersion): tree = ElementTree.parse(pomFile) ElementTree.register_namespace('', 'http://maven.apache.org/POM/4.0.0') root = tree.getroot() for child in root: if child.tag.endswith('version'): print('Updating Indigo version from {0} to {1} in {2}...'.format(child.text, newVersion, pomFile)) child.text = newVersion break tree.write(pomFile)
def _parse_token(self): if self._wstrust_version == WSTrustVersion.WSTRUST2005: token_type_nodes_xpath = 's:Body/t:RequestSecurityTokenResponse/t:TokenType' security_token_xpath = 't:RequestedSecurityToken' else: token_type_nodes_xpath = 's:Body/wst:RequestSecurityTokenResponseCollection/wst:RequestSecurityTokenResponse/wst:TokenType' security_token_xpath = 'wst:RequestedSecurityToken' token_type_nodes = xmlutil.xpath_find(self._dom, token_type_nodes_xpath) if not token_type_nodes: raise AdalError("No TokenType nodes found in RSTR") for node in token_type_nodes: if self.token: self._log.warn("Found more than one returned token. Using the first.") break token_type = xmlutil.find_element_text(node) if not token_type: self._log.warn("Could not find token type in RSTR token.") requested_token_node = xmlutil.xpath_find(self._parents[node], security_token_xpath) if len(requested_token_node) > 1: raise AdalError("Found too many RequestedSecurityToken nodes for token type: {}".format(token_type)) if not requested_token_node: self._log.warn( "Unable to find RequestsSecurityToken element associated with TokenType element: %(token_type)s", {"token_type": token_type}) continue # Adjust namespaces (without this they are autogenerated) so this is understood # by the receiver. Then make a string repr of the element tree node. # See also http://blog.tomhennigan.co.uk/post/46945128556/elementtree-and-xmlns ET.register_namespace('saml', 'urn:oasis:names:tc:SAML:1.0:assertion') ET.register_namespace('ds', 'http://www.w3.org/2000/09/xmldsig#') token = ET.tostring(requested_token_node[0][0]) if token is None: self._log.warn( "Unable to find token associated with TokenType element: %(token_type)s", {"token_type": token_type}) continue self.token = token self.token_type = token_type self._log.info( "Found token of type: %(token_type)s", {"token_type": self.token_type}) if self.token is None: raise AdalError("Unable to find any tokens in RSTR.")
def to_string(self, nspair=None): """Converts the Saml object to a string containing XML.""" if nspair: for prefix, uri in nspair.items(): try: ElementTree.register_namespace(prefix, uri) except AttributeError: # Backwards compatibility with ET < 1.3 ElementTree._namespace_map[uri] = prefix return ElementTree.tostring(self._to_element_tree(), encoding="UTF-8")
def import_uj(self, filename): with open(filename, 'r') as f: raw = f.readlines() self.first_import_line = raw[0] ET.register_namespace('', SCHEME_PREFIX[1:-1]) tree = ET.parse(filename) root = tree.getroot() self.name = root.attrib['NAME'] self.app_name = root.attrib['APPNAME'] self.version = root.attrib['VERSION'] created = root.find(SCHEME_PREFIX+'CREATED') self.created = created.text self.uj_global_settings = [] for nvp in root.findall(SCHEME_PREFIX+'NVP'): self.uj_global_settings.append({'NAME': nvp.attrib['NAME'], 'PLUGIN': nvp.attrib['PLUGIN'], 'TYPE': nvp.attrib['TYPE'], 'text': nvp.text}) #### Dynamic Data Items ### dditems_element = root.find(SCHEME_PREFIX+'DYNAMICDATA') self.dditems = [] for ddi in dditems_element.findall(SCHEME_PREFIX+'DDITEM'): ddi_type = ddi.find(SCHEME_PREFIX+'SOURCE').attrib['TYPE'] self.dditems.append(TYPE_TO_CLASS_MAP[ddi_type](ddi)) #### Steps & Step-Groups #### # stepgroups are not defined in the XML, so to construct a stepgroup, we need list of all the steps steps_element = root.find(SCHEME_PREFIX+'STEPS') stepgroups = [] stepgroup_steps = [] lead_step = None last_step_stepgroup_id = -1 for step in steps_element.findall(SCHEME_PREFIX+'STEP'): current_step = Step(step) if last_step_stepgroup_id == -1: # adjust for starting element lead_step = current_step last_step_stepgroup_id = current_step.stepgroup_id if current_step.stepgroup_id != last_step_stepgroup_id: stepgroups.append(StepGroup(lead_step, stepgroup_steps)) lead_step = current_step stepgroup_steps = [current_step] last_step_stepgroup_id = current_step.id else: stepgroup_steps.append(current_step) # finalize after last step stepgroups.append(StepGroup(lead_step, stepgroup_steps)) self.stepgroups = stepgroups
def dict2xml(tag, dict): ''' Converts dict of key/value pairs into XML ''' if sys.version_info >= (2, 7): xml_ns = "http://zanata.org/namespace/config/" ET.register_namespace('', xml_ns) elem = ET.Element(tag) for key, val in dict.items(): child = ET.Element(key) child.text = str(val) elem.append(child) return ToolBox.prettify(elem)
def _prepare_document(self): ''' Build the main document node and set xml namespaces. ''' self._xml = ET.Element("Document") self._xml.set("xmlns", "urn:iso:std:iso:20022:tech:xsd:pain.008.003.02") self._xml.set("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-intance") ET.register_namespace( "", "urn:iso:std:iso:20022:tech:xsd:pain.008.003.02") ET.register_namespace("xsi", "http://www.w3.org/2001/XMLSchema-intance") CstmrDrctDbtInitn_node = ET.Element("CstmrDrctDbtInitn") self._xml.append(CstmrDrctDbtInitn_node)
def docbook_to_markdown(doc_tag): # We need to convert the element to an XML string # And tostring doesn't like the namespaces for some reason... if doc_tag is None: return None if 'xmlns:map' in doc_tag.attrib: for prefix, namespace in doc_tag.attrib['xmlns:map'].iteritems(): ET.register_namespace(prefix, namespace) del doc_tag.attrib['xmlns:map'] for e in doc_tag.iter(): if 'xmlns:map' in e.attrib: del e.attrib['xmlns:map'] doc_tag_source = ET.tostring(doc_tag) markdown = pypandoc.convert(doc_tag_source, 'markdown_github', format="docbook") return pypandoc.convert(doc_tag_source, 'markdown_github', format="docbook")
def to_string(self, nspair=None): """Converts the Saml object to a string containing XML. :param nspair: A dictionary of prefixes and uris to use when constructing the text representation. :return: String representation of the object """ if nspair: for prefix, uri in nspair.items(): try: ElementTree.register_namespace(prefix, uri) except AttributeError: # Backwards compatibility with ET < 1.3 ElementTree._namespace_map[uri] = prefix return ElementTree.tostring(self._to_element_tree(), encoding="UTF-8")
def register_prefix(self, nspair): """ Register with ElementTree a set of namespaces :param nspair: A dictionary of prefixes and uris to use when constructing the text representation. :return: """ for prefix, uri in nspair.items(): try: ElementTree.register_namespace(prefix, uri) except AttributeError: # Backwards compatibility with ET < 1.3 ElementTree._namespace_map[uri] = prefix except ValueError: pass
def _soap_request(self): def sub_ns(parent, tag, ns): return ET.SubElement(parent, "{%s}%s" % (ns, tag)) def sub_common(parent, tag): return sub_ns(parent, tag, "http://schemas.itv.com/2009/05/Common") def sub_soap(parent, tag): return sub_ns(parent, tag, "http://schemas.xmlsoap.org/soap/envelope/") def sub_item(parent, tag): return sub_ns(parent, tag, "http://tempuri.org/") def sub_itv(parent, tag): return sub_ns( parent, tag, "http://schemas.datacontract.org/2004/07/Itv.BB.Mercury.Common.Types" ) production_id = self.production_id channel_id = self.channel_id ET.register_namespace("com", "http://schemas.itv.com/2009/05/Common") ET.register_namespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/") ET.register_namespace("tem", "http://tempuri.org/") ET.register_namespace( "itv", "http://schemas.datacontract.org/2004/07/Itv.BB.Mercury.Common.Types" ) # Start of XML root = ET.Element( "{http://schemas.xmlsoap.org/soap/envelope/}Envelope") sub_soap(root, "Header") body = sub_soap(root, "Body") # build request get_playlist = sub_item(body, "GetPlaylist") request = sub_item(get_playlist, "request") prode = sub_itv(request, "ProductionId") if production_id: # request -> ProductionId prode.text = production_id # request -> RequestGuid sub_itv(request, "RequestGuid").text = str(uuid4()).upper() vodcrid = sub_itv(request, "Vodcrid") # request -> Vodcrid -> Id vod_id = sub_common(vodcrid, "Id") # request -> Vodcrid -> Partition sub_common(vodcrid, "Partition").text = "itv.com" if channel_id: vod_id.text = "sim{0}".format(channel_id) # build userinfo userinfo = sub_item(get_playlist, "userInfo") sub_itv(userinfo, "Broadcaster").text = "Itv" sub_itv( userinfo, "RevenueScienceValue").text = "ITVPLAYER.2.18.14.+build.a778cd30ac" sub_itv(userinfo, "SessionId") sub_itv(userinfo, "SsoToken") sub_itv(userinfo, "UserToken") # GeoLocationToken -> Token # sub_itv(sub_itv(userinfo, "GeoLocationToken"), "Token") # build siteinfo siteinfo = sub_item(get_playlist, "siteInfo") sub_itv(siteinfo, "AdvertisingRestriction").text = "None" sub_itv(siteinfo, "AdvertisingSite").text = "ITV" sub_itv(siteinfo, "AdvertisingType").text = "Any" sub_itv( siteinfo, "Area" ).text = "ITVPLAYER.VIDEO" # "channels.itv{0}".format(channel_id) if channel_id else "ITVPLAYER.VIDEO" sub_itv(siteinfo, "Category") sub_itv(siteinfo, "Platform").text = "DotCom" sub_itv(siteinfo, "Site").text = "ItvCom" # build deviceInfo deviceinfo = sub_item(get_playlist, "deviceInfo") sub_itv(deviceinfo, "ScreenSize").text = "Big" # build playerinfo playerinfo = sub_item(get_playlist, "playerInfo") sub_itv(playerinfo, "Version").text = "2" return ET.tostring(root)
class PackageListItem(object): def __init__(self, name, title, description): self.name = name self.title = title self.description = description @staticmethod def from_element(element): xml_lang_attr = "{http://www.w3.org/XML/1998/namespace}lang" name = element.find(Updater.qual_tag("name")).text title_els = element.findall(Updater.qual_tag("title")) title = LocalizableTextValue() for title_el in title_els: lang = title_el.get(xml_lang_attr) title.set_translation(lang, title_el.text) description_els = element.findall(Updater.qual_tag("description")) description = LocalizableTextValue() for description_el in description_els: lang = description_el.get(xml_lang_attr) description.set_translation(lang, description_el.text) return PackageListItem(name, title, description) #################################################################################################### ET.register_namespace("updater", Updater.NS_URI)
def _register(): ElementTree.register_namespace('', dom.SVG_NS)
def writeCombXML(React, path): spinMult = 2 cml = ChemDyME.Tools.getCML(React.combProd, React.combProdName) spinMult = ChemDyME.Tools.getSpinMult(React.CombProd, React.CombProdName) freqs = str(React.CombProdFreqs) symb = "".join(React.CombProd.get_chemical_symbols()) potE = React.CombProductEnergy - React.energyDictionary[symb] #Convert from ev to kJ potE = potE * 96.485 freqs = freqs[1:-1] freqs = freqs.replace(',', '') ET.register_namespace('me', 'http://www.chem.leeds.ac.uk/mesmer') ET.register_namespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance') ET.register_namespace('', 'http://www.xml-cml.org/schema') with open(path, 'r') as myfile: data = myfile.read().replace('\n', '') tree = ET.fromstring(cml) bigTree = ET.fromstring(data) prop = ET.Element("propertyList") lumpedSpecies = ET.SubElement(prop, "property", dictRef="me:lumpedSpecies") lumpArrays = ET.SubElement(lumpedSpecies, "array") lumpArrays.text = " " vib = ET.SubElement(prop, "property", dictRef="me:vibFreqs") vibarrays = ET.SubElement(vib, "array", units="cm-1") vibarrays.text = str(freqs) ene = ET.SubElement(prop, "property", dictRef="me:ZPE") enedata = ET.SubElement(ene, "scalar", units="kJ/mol") enedata.text = str(potE) Mult = ET.SubElement(prop, "property", dictRef="me:spinMultiplicity") multi = ET.SubElement(Mult, "scalar", units="cm-1") multi.text = str(spinMult) epsilon = ET.SubElement(prop, "property", dictRef="me:epsilon") epsilondata = ET.SubElement(epsilon, "scalar") epsilondata.text = '473.17' sigma = ET.SubElement(prop, "property", dictRef="me:sigma") sigmadata = ET.SubElement(sigma, "scalar") sigmadata.text = '5.09' eneTrans = ET.Element('me:energyTransferModel') eneTrans.set("{http://www.w3.org/2001/XMLSchema-instance}type", "me:ExponentialDown") eTran = ET.SubElement(eneTrans, "scalar", units="cm-1") eTran.text = '250' # Append the new "data" elements to the root element of the XML document tree.append(prop) tree.append(eneTrans) children = bigTree.getchildren() children[1] = children[1].append(tree) # Now we have a new well-formed XML document. It is not very nicely formatted... out = ET.tostring(bigTree) # ...so we'll use minidom to make the output a little prettier dom = minidom.parseString(out) ChemDyME.Tools.prettyPrint(dom, path)
if_name = interface_elem.find(Stats.qual_tag("name")).text is_up = interface_elem.find( Stats.qual_tag("up")) is not None is_down = interface_elem.find( Stats.qual_tag("down")) is not None interfaces[if_name] = { 'is_up': True if is_up else False if is_down else None } elif elem.tag == Stats.qual_tag("ucollect-sending"): sending = stats.data.setdefault("sending", {}) Stats._update_sending(elem, "ucollect", sending) elif elem.tag == Stats.qual_tag("firewall-sending"): sending = stats.data.setdefault("sending", {}) Stats._update_sending(elem, "firewall", sending) # do postprocessing of data Stats.__postprocess_data(stats.data) return stats @property def key(self): return "stats" def __str__(self): return "Device stats" #################################################################################################### ET.register_namespace("stats", Stats.NS_URI)
def __init__(self): # In reverse order to work down until the acceptable version is found on the server, through login process self.supported_versions = (u'2019.1', u'2018.3', u'2018.2', u'2018.1', u"10.5", u"10.4", u"10.3", u"10.2", u"10.1", u"10.0", u"9.3", u"9.2", u"9.1", u"9.0") self.logger = None self.luid_pattern = r"[0-9a-fA-F]*-[0-9a-fA-F]*-[0-9a-fA-F]*-[0-9a-fA-F]*-[0-9a-fA-F]*" # Defaults, will get updated with each update. Overwritten by set_tableau_server_version self.version = u"2018.2" self.api_version = u"3.1" self.tableau_namespace = u'http://tableau.com/api' self.ns_map = {'t': 'http://tableau.com/api'} self.ns_prefix = '{' + self.ns_map['t'] + '}' etree.register_namespace(u't', self.ns_map['t']) self.site_roles = ( u'Interactor', u'Publisher', u'SiteAdministrator', u'Unlicensed', u'UnlicensedWithPublish', # This was sunset at some point u'Viewer', u'ViewerWithPublish', u'ServerAdministrator', u'ReadOnly', u'Explorer', u'ExplorerCanPublish', u'SiteAdministratorExplorer', u'Creator', u'SiteAdministratorCreator') server_content_roles_2_0 = { u"project": (u'Viewer', u'Interactor', u'Editor', u'Data Source Connector', u'Data Source Editor', u'Publisher', u'Project Leader'), u"workbook": (u'Viewer', u'Interactor', u'Editor'), u"datasource": (u'Data Source Connector', u'Data Source Editor') } server_content_roles_2_1 = { u"project": (u'Viewer', u'Publisher', u'Project Leader'), u"workbook": (u'Viewer', u'Interactor', u'Editor'), u"datasource": (u'Editor', u'Connector') } self.server_content_roles = { u"2.0": server_content_roles_2_0, u"2.1": server_content_roles_2_1, u"2.2": server_content_roles_2_1, u"2.3": server_content_roles_2_1, u"2.4": server_content_roles_2_1, u"2.5": server_content_roles_2_1, u"2.6": server_content_roles_2_1, u"2.7": server_content_roles_2_1, u"2.8": server_content_roles_2_1, u'3.0': server_content_roles_2_1, u'3.1': server_content_roles_2_1, u'3.2': server_content_roles_2_1, u'3.3': server_content_roles_2_1 } self.server_to_rest_capability_map = { u'Add Comment': u'AddComment', u'Move': u'ChangeHierarchy', u'Set Permissions': u'ChangePermissions', u'Connect': u'Connect', u'Delete': u'Delete', u'View Summary Data': u'ExportData', u'Download Summary Data': u'ExportData', u'Export Image': u'ExportImage', u'Download Image/PDF': u'ExportImage', u'Download': u'ExportXml', u'Download Workbook/Save As': u'ExportXml', u'Filter': u'Filter', u'Project Leader': u'ProjectLeader', u'View': u'Read', u'Share Customized': u'ShareView', u'View Comments': u'ViewComments', u'View Underlying Data': u'ViewUnderlyingData', u'Download Full Data': u'ViewUnderlyingData', u'Web Edit': u'WebAuthoring', u'Save': u'Write', u'Inherited Project Leader': u'InheritedProjectLeader', u'all': u'all' # special command to do everything } capabilities_2_0 = { u"project": (u'AddComment', u'ChangeHierarchy', u'ChangePermissions', u'Connect', u'Delete', u'ExportData', u'ExportImage', u'ExportXml', u'Filter', u'ProjectLeader', u'Read', u'ShareView', u'ViewComments', u'ViewUnderlyingData', u'WebAuthoring', u'Write'), u"workbook": (u'AddComment', u'ChangeHierarchy', u'ChangePermissions', u'Delete', u'ExportData', u'ExportImage', u'ExportXml', u'Filter', u'Read', u'ShareView', u'ViewComments', u'ViewUnderlyingData', u'WebAuthoring', u'Write'), u"datasource": (u'ChangePermissions', u'Connect', u'Delete', u'ExportXml', u'Read', u'Write') } capabilities_2_1 = { u"project": (u"Read", u"Write", u'ProjectLeader'), u"workbook": ( u'Read', u'ExportImage', u'ExportData', u'ViewComments', u'AddComment', u'Filter', u'ViewUnderlyingData', u'ShareView', u'WebAuthoring', u'Write', u'ExportXml', u'ChangeHierarchy', u'Delete', u'ChangePermissions', ), u"datasource": (u'Read', u'Connect', u'Write', u'ExportXml', u'Delete', u'ChangePermissions') } capabilities_2_8 = { u"project": (u"Read", u"Write", u'ProjectLeader', u'InheritedProjectLeader'), u"workbook": ( u'Read', u'ExportImage', u'ExportData', u'ViewComments', u'AddComment', u'Filter', u'ViewUnderlyingData', u'ShareView', u'WebAuthoring', u'Write', u'ExportXml', u'ChangeHierarchy', u'Delete', u'ChangePermissions', ), u"datasource": (u'Read', u'Connect', u'Write', u'ExportXml', u'Delete', u'ChangePermissions') } capabilities_3_3 = { u"project": (u"Read", u"Write", u'ProjectLeader', u'InheritedProjectLeader'), u"workbook": ( u'Read', u'ExportImage', u'ExportData', u'ViewComments', u'AddComment', u'Filter', u'ViewUnderlyingData', u'ShareView', u'WebAuthoring', u'Write', u'ExportXml', u'ChangeHierarchy', u'Delete', u'ChangePermissions', ), u"datasource": (u'Read', u'Connect', u'Write', u'ExportXml', u'Delete', u'ChangePermissions'), u'flow': (u'ChangeHierarchy', u'ChangePermissions', u'Delete', u'ExportXml', u'Read', u'Write') } self.available_capabilities = { u"2.0": capabilities_2_0, u"2.1": capabilities_2_1, u"2.2": capabilities_2_1, u'2.3': capabilities_2_1, u'2.4': capabilities_2_1, u'2.5': capabilities_2_1, u'2.6': capabilities_2_1, u'2.7': capabilities_2_1, u'2.8': capabilities_2_8, u'3.0': capabilities_2_8, u'3.1': capabilities_2_8, u'3.2': capabilities_2_8, u'3.3': capabilities_3_3 } self.datasource_class_map = { u"Actian Vectorwise": u"vectorwise", u"Amazon EMR": u"awshadoophive", u"Amazon Redshift": u"redshift", u"Aster Database": u"asterncluster", u"Cloudera Hadoop": u"hadoophive", u"DataStax Enterprise": u"datastax", u"EXASolution": u"exasolution", u"Firebird": u"firebird", u"Generic ODBC": u"genericodbc", u"Google Analytics": u"google-analytics", u"Google BigQuery": u"bigquery", u"Hortonworks Hadooop Hive": u"hortonworkshadoophive", u"HP Vertica": u"vertica", u"IBM BigInsights": u"bigsql", u"IBM DB2": u"db2", u"JavaScript Connector": u"jsconnector", u"MapR Hadoop Hive": u"maprhadoophive", u"MarkLogic": u"marklogic", u"Microsoft Access": u"msaccess", u"Microsoft Analysis Services": u"msolap", u"Microsoft Excel": u"excel-direct", u"Microsoft PowerPivot": u"powerpivot", u"Microsoft SQL Server": u"sqlserver", u"MySQL": u"mysql", u"IBM Netezza": u"netezza", u"OData": u"odata", u"Oracle": u"oracle", u"Oracle Essbase": u"essbase", u"ParAccel": u"paraccel", u"Pivotal Greenplum": u"greenplum", u"PostgreSQL": u"postgres", u"Progress OpenEdge": u"progressopenedge", u"SAP HANA": u"saphana", u"SAP Netweaver Business Warehouse": u"sapbw", u"SAP Sybase ASE": u"sybasease", u"SAP Sybase IQ": u"sybaseiq", u"Salesforce": u"salesforce", u"Spark SQL": u"spark", u"Splunk": u"splunk", u"Statistical File": u"", u"Tableau Data Extract": u"dataengine", u"Teradata": u"teradata", u"Text file": u"textscan", u"Hyper": u'hyper' } self.permissionable_objects = (u'datasource', u'project', u'workbook', u'flow')
def parseXmlString(xmlString): """ View for generating a spdx license xml returns a dictionary with the xmlString license fields values """ data = {} tree = ET.ElementTree(ET.fromstring(xmlString)) try: root = tree.getroot() except Exception as e: return try: if (len(root) > 0 and 'isOsiApproved' in root[0].attrib): data['osiApproved'] = root[0].attrib['isOsiApproved'] else: data['osiApproved'] = '-' except Exception as e: data['osiApproved'] = '-' data['crossRefs'] = [] try: if (len( ('{http://www.spdx.org/license}license/{http://www.spdx.org/license}crossRefs' )) > 0): crossRefs = tree.findall( '{http://www.spdx.org/license}license/{http://www.spdx.org/license}crossRefs' )[0] for crossRef in crossRefs: data['crossRefs'].append(crossRef.text) except Exception as e: data['crossRefs'] = [] try: if (len( tree.findall( '{http://www.spdx.org/license}license/{http://www.spdx.org/license}notes' )) > 0): data['notes'] = tree.findall( '{http://www.spdx.org/license}license/{http://www.spdx.org/license}notes' )[0].text else: data['notes'] = '' except Exception as e: data['notes'] = '' try: if (len( tree.findall( '{http://www.spdx.org/license}license/{http://www.spdx.org/license}standardLicenseHeader' )) > 0): data['standardLicenseHeader'] = tree.findall( '{http://www.spdx.org/license}license/{http://www.spdx.org/license}standardLicenseHeader' )[0].text else: data['standardLicenseHeader'] = '' except Exception as e: data['standardLicenseHeader'] = '' try: if (len( tree.findall( '{http://www.spdx.org/license}license/{http://www.spdx.org/license}text' )) > 0): textElem = tree.findall( '{http://www.spdx.org/license}license/{http://www.spdx.org/license}text' )[0] ET.register_namespace('', "http://www.spdx.org/license") textStr = ET.tostring(textElem).strip() if (len(textStr) >= 49 and textStr[:42] == '<text xmlns="http://www.spdx.org/license">' and textStr[-7:] == '</text>'): textStr = textStr[42:] textStr = textStr[:-7].strip().replace('<', '<').replace( '>', '>').strip() data['text'] = textStr.strip() else: data['text'] = '' except Exception as e: data['text'] = '' return data
def alt_workbookrels_text(db, filepath): """ Takes a xl/_rels/workbook.xml.rels and returns a db altered text version of the xml :param pylightxl.Database db: pylightxl database that contains data to update xml file :param str filepath: file path for xl/_rels/workbook.xml.rels :return str: returns the updated xml text """ # extract text from existing app.xml ns = xml_namespace(filepath) tree = ET.parse(filepath) root = tree.getroot() # hold existing non-sheet relations (calcChain, sharedStrings, etc.) elements_nonsheet = [] # sheet type that is replaced by actual xml read sheet type element_sheet_type = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet' # book keeping to check if a sharedStrings was read in the elements_nonsheet # (if no and db has sharedStrings then we need to add a sharedStrings in) bool_sharedStrings = False for element in root.findall('./default:Relationship', ns): if 'worksheets/sheet' not in element.get('Target'): if 'sharedStrings.xml' == element.get('Target'): # there already is a sharedStrings.xml tag in this rels file, dont add another bool_sharedStrings = True # log existing non-sheet elements to append at the end of rId#s after sheets elements_nonsheet.append(element) root.find('./default:Relationship', ns).remove(element) else: # sheet names, remove them then add new ones element_sheet_type = element.get('Type') root.find('./default:Relationship', ns).remove(element) # these rId's have to match rId's on workbook.xml for sheet_num, sheet_name in enumerate(db.ws_names, 1): element = ET.Element("Relationship") element.set( 'Target', 'worksheets/sheet{sheet_num}.xml'.format(sheet_num=sheet_num)) element.set('Type', element_sheet_type) element.set('Id', 'rId{sheet_num}'.format(sheet_num=sheet_num)) root.append(element) # these rId's are not referenced on any of the xml files, they are incremented after sheets for i, element in enumerate(elements_nonsheet, 1): rId = len(db.ws_names) + i element.set('Id', 'rId{rId}'.format(rId=rId)) root.append(element) if bool_sharedStrings is False and db._sharderStrings: element = ET.Element('Relationship') element.set('Target', 'sharedStrings.xml') element.set( 'Type', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings' ) element.set( 'Id', 'rId{rId}'.format(rId=len(db.ws_names) + len(elements_nonsheet) + 1)) # reset default namespace ET.register_namespace('', ns['default']) # roll up entire xml file as text text = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + ET.tostring( root) return text
def handle_android_manifest(): xml_file_path = "./" + apk_project_name + "/AndroidManifest.xml" if os.path.exists(xml_file_path) != True: print "AndroidManifest.xml is missing in " + apk_project_name return False #some init work for xml handle android_namespace = "http://schemas.android.com/apk/res/android" ET.register_namespace("android", android_namespace) android_manifest_tree = ET.ElementTree(file=xml_file_path) manifest_root = android_manifest_tree.getroot() meta_node3 = ET.Element( "uses-permission", {"android:name": "android.permission.READ_EXTERNAL_STORAGE"}) meta_node3.tail = "\n" meta_node4 = ET.Element( "uses-permission", {"android:name": "android.permission.SYSTEM_ALERT_WINDOW"}) meta_node4.tail = "\n" manifest_root.append(meta_node3) manifest_root.append(meta_node4) clean_manifest_root(manifest_root, android_namespace) global package_name package_name = manifest_root.get("package") print "Get original apk's package name: " + package_name #get application element node for child in manifest_root: if child.tag == "application": application_node = child break app_name_key = "{" + android_namespace + "}name" app_name_val = application_node.get(app_name_key) if app_name_val == None: app_name_val = "android.app.Application" meta_key1 = app_name_key meta_key2 = "{" + android_namespace + "}value" meta_node1 = ET.Element("meta-data", { meta_key1: "APPLICATION_CLASS_NAME", meta_key2: app_name_val }) launch_activity_name = get_launch_activity(application_node, android_namespace) print "launch activity name: " + launch_activity_name meta_node2 = ET.Element("meta-data", { meta_key1: "LAUNCH_ACTIVITY_NAME", meta_key2: launch_activity_name }) Logservice_name = package_name + ".Logservice" StopService_name = package_name + ".StopService" Service_node = ET.Element("service", {"android:name": Logservice_name}) Service1_node = ET.Element("service", { "android:name": StopService_name, "android:process": ".StopService" }) application_node.append(Service1_node) application_node.append(Service_node) application_node.append(meta_node1) application_node.append(meta_node2) version_lst = get_sdk_version() sdk_config_node = create_sdk_config_node(version_lst, android_namespace) if sdk_config_node != None: manifest_root.append(sdk_config_node) #android_manifest_tree.write(xml_file_path) #replace/add the application node's android:name attribute shell_application_name = package_name + ".ShellApplication" application_node.set(app_name_key, shell_application_name) android_manifest_tree.write(xml_file_path)
def register_namespaces(namespaces=namespaces): for name, url in namespaces.items(): if name != 'svg': # creates invalid xml et.register_namespace(name, url)
import sys from xml.dom.minidom import parse import os import xml.etree.cElementTree as et from functools import reduce et.register_namespace('', "http://maven.apache.org/POM/4.0.0") et.register_namespace('xsi', "http://www.w3.org/2001/XMLSchema-instance") class Pom(object): def __init__(self, pom_path): self.pom_path = pom_path self.element_tree = et.parse(self.pom_path) @staticmethod def get_children_by_name(element, name): return list( filter(lambda e: e.tag.endswith(name), element.getchildren())) def remove_elements_by_path(self, path): elements = [self.element_tree.getroot()] for name in path[:-1]: elements = reduce( list.__add__, list( map(lambda elem: Pom.get_children_by_name(elem, name), elements)), []) for element in elements: for child in element.getchildren(): if child.tag.endswith(path[-1]): element.remove(child)
import xml.etree.cElementTree as et except ImportError: import xml.etree.ElementTree as et # change this values to change visible lines style, default is black lines with 2px thickness color = "black" width = 2 _HEADER = """\ <?xml version="1.0" encoding="UTF-8" standalone="no"?>\n """ _ROOT = '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" width="%d" height="%d"></svg>\n' SVG_NS = "http://www.w3.org/2000/svg" et.register_namespace("", SVG_NS) et.register_namespace("sodipodi", "http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd") et.register_namespace("inkscape", "http://www.inkscape.org/namespaces/inkscape") scene = getCurrentScene() current_frame = scene.frame_current w = scene.render.resolution_x * scene.render.resolution_percentage / 100 h = scene.render.resolution_y * scene.render.resolution_percentage / 100 path = re.sub(r'\.blend$|$', '.svg' , bpy.data.filepath) # write header if it does not yet exists try: with open(path) as f: pass except IOError: f = open(path, "w")
def metadata_gen(title, dateIni, dateEnd, geographicDesc, westBounding, eastBounding, northBounding, southBounding, params): #EML-XML Header ET.register_namespace('eml', "eml://ecoinformatics.org/eml-2.1.1") #some name eml = ET.Element("{eml://ecoinformatics.org/eml-2.1.1}eml", system="knb") #eml = ET.Element("eml:eml",system="knb",xmlns="eml://ecoinformatics.org/eml-2.1.1") #-Access acceso = ET.SubElement(eml, "access", authSystem="knb", order="allowFirst") permiso = ET.SubElement(acceso, "allow") ET.SubElement(permiso, "principal").text = "public" ET.SubElement(permiso, "permission").text = "read" #-Dataset Module dataset = ET.SubElement(eml, "dataset") ET.SubElement(dataset, "title").text = title #--Coverage coverage = ET.SubElement(dataset, "coverage") #---Geographic Coverage coverageG = ET.SubElement(coverage, "geographicCoverage", id='id') ET.SubElement(coverageG, "geographicDescription").text = geographicDesc ET.SubElement(coverageG, "westBoundingCoordinate").text = westBounding ET.SubElement(coverageG, "eastBoundingCoordinate").text = eastBounding ET.SubElement(coverageG, "northBoundingCoordinate").text = northBounding ET.SubElement(coverageG, "southBoundingCoordinate").text = southBounding #---Temporal Coverage coverageT = ET.SubElement(coverage, "temporalCoverage") #---SingleData #----TODO #---rangeOfDates rangeOfDates = ET.SubElement(coverageT, "rangeOfDates") #----beginDate ET.SubElement(ET.SubElement(rangeOfDates, "beginDate"), "calendarDate").text = dateIni #---endDate ET.SubElement(ET.SubElement(rangeOfDates, "endDate"), "calendarDate").text = dateEnd #--Dataset type fileFormat = "csv" if fileFormat == "csv": #filename.split(".")[-1] dataTable = ET.SubElement(dataset, "dataTable") ET.SubElement(dataTable, "FileName").text = title + ".csv" dataTable = file_block_csv(title, params, dataTable) tree = ET.ElementTree(eml) #Escribimos los datos en un archivo or onedata attachement tree.write(config.datasets_path + '/' + geographicDesc + '/' + title + ".xml", encoding='UTF-8', xml_declaration=True) if (config.onedata_mode == 1): try: token = os.environ['ONECLIENT_AUTHORIZATION_TOKEN'] except KeyError: token = 'MDAxNWxvY2F00aW9uIG9uZXpvbmUKMDAzMGlkZW500aWZpZXIgOTAwNGNlNzBiYWQyMTYzYzY1YWY4NTNhZjQyMGJlYWEKMDAxYWNpZCB00aW1lIDwgMTU4MzkxODYyOQowMDJmc2lnbmF00dXJlICmASYmuGx6CSPHwkf3s9pXW2szUqJPBPoFEXIKOZ2L00Cg' header_json = { 'X-Auth-Token': token, 'Content-type': 'application/json' } try: print(config.onedata_url + config.onedata_api + 'metadata/' + config.onedata_space + '/' + geographicDesc + '/' + title) print( eml_to_json(config.datasets_path + '/' + geographicDesc + '/' + title + ".xml")) r = requests.put(config.onedata_url + config.onedata_api + 'metadata/' + config.onedata_space + '/' + geographicDesc + '/' + title, headers=header_json, data=eml_to_json(title + ".xml")) print("Metadata attachement: %i" % r.status_code) os.remove(title + ".xml") except requests.exceptions.RequestException as e: print(e) print(tree)
# You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. from base import YinElement from xml.etree import cElementTree as ET class Password(YinElement): NS_URI = "http://www.nic.cz/ns/router/password" def __init__(self, user, password): super(Password, self).__init__() self.user = user if isinstance(password, str): password = password.decode("utf8") self.password = unicode(password) @property def rpc_set(self): set_tag = Password.qual_tag("set") element = ET.Element(set_tag) user_elem = ET.SubElement(element, Password.qual_tag("user")) user_elem.text = self.user password_elem = ET.SubElement(element, Password.qual_tag("password")) password_elem.text = self.password return element #################################################################################################### ET.register_namespace("password", Password.NS_URI)
from botocore.exceptions import ClientError from datetime import datetime from functools import reduce from mozilla_version.maven import MavenVersion from mozilla_version.errors import PatternNotMatchedError from xml.etree import cElementTree as ET # logging doesn't work on AWS Lambda, at first print('Loading function') s3 = boto3.resource('s3') cloudfront = boto3.client('cloudfront') METADATA_BASE_FILE_NAME = 'maven-metadata.xml' ET.register_namespace('', 'http://maven.apache.org/POM/4.0.0') XML_NAMESPACES = { 'm': 'http://maven.apache.org/POM/4.0.0', } POM_TIMESTAMP = '%Y%m%d%H%M%S' SNAPSHOT_FILE_TIMESTAMP = '%Y%m%d.%H%M%S' def lambda_handler(event, context): print('Processing a new event...') print('Event: {}. Context: {}'.format(event, context)) bucket_name = event['Records'][0]['s3']['bucket']['name'] key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')
def alt_worksheet_text(db, filepath, sheet_name): """ Takes a xl/worksheets/sheet#.xml and returns a db altered text version of the xml :param pylightxl.Database db: pylightxl database that contains data to update xml file :param str filepath: file path for xl/worksheets/sheet#.xml :return str: returns the updated xml text """ # TODO: python 2 does not preserve list index in terms of appended values sharedStrings needs to be fixed, here and in write new # extract text from existing app.xml ns = xml_namespace(filepath) tree = ET.parse(filepath) root = tree.getroot() ws_size = db.ws(sheet_name).size if ws_size == [0, 0] or ws_size == [1, 1]: sheet_size_address = 'A1' else: sheet_size_address = 'A1:' + index2address(ws_size[0], ws_size[1]) # update size of sheet e_dim = root.findall('./default:dimension', ns)[0] e_dim.set('ref', '{}'.format(sheet_size_address)) # remove the existing element under "row" elements # (row elements are kept to keep existing formatting) for e_row in root.findall('./default:sheetData/default:row', ns): for e_c in e_row.findall('./default:c', ns): e_row.remove(e_c) # unload the db for row_i, row in enumerate(db.ws[sheet_name].rows(), 1): empty_row = ''.join(row) if empty_row: # xml has data for this but the db does not, remove it try: e_row = root.findall( './default:sheetData/default:row[@r={}]'.format(row_i), ns)[0] except IndexError: # existing xml did not have data for this row either pass else: e_sheetData = root.findall('./default:sheetData', ns) e_sheetData.remove(e_row) else: # db has data in this row, check if there is already an xml row element, else create one try: e_row = root.findall( './default:sheetData/default:row[@r={}]'.format(len(row)), ns)[0] except IndexError: # existing xml did not have data for this row, create one e_row = ET.Element('row') # db data exists, write xml elements for col_i, cell in enumerate(row): e_c = ET.Element('c') e_c.set('r', '{}'.format(index2address(row_i, col_i))) e_v = ET.SubElement(e_c, 'v') if type(cell) is str and cell != '': if cell[0] == '=': # formula e_c.set('t', 'str') e_f = ET.SubElement(e_c, 'f') e_f.text = cell[1:] e_v.text = 'pylightxl - open excel file and save it for formulas to calculate' else: # string, add it to sharedStrings e_c.set('t', 's') # check if str is already part of the sharedStrings index, else add it try: sharedStrings_index = db._sharedStrings.index(cell) except ValueError: db._sharedStrings.append(cell) sharedStrings_index = db._sharedStrings.index(cell) # sharedStrings index becomes the value of the cell (sharedStrings starts from 0) e_v.text = str(sharedStrings_index) elif cell != '': # int or real write it directly to cell value e_v.text = str(cell) e_row.append(e_c) # reset default namespace ET.register_namespace('', ns['default']) # roll up entire xml file as text text = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + ET.tostring( root) return text
along with this program. If not, see http://www.gnu.org/licenses/. Wrapper class for an xml resource file, as part of the RMH corpus. """ import xml.etree.cElementTree as ET from collections import namedtuple from pathlib import Path import urllib.parse as urlparse URI = "http://www.tei-c.org/ns/1.0" TEI = "{" + URI + "}" NS = {"tei": URI} ET.register_namespace("", URI) Sentence = namedtuple("Sentence", "index tokens") Token = namedtuple("Token", "text, lemma, tag") class RMHFile: """An xml file that is part of the RMH corpus""" def __init__(self, path): self.path = path if isinstance(path, Path) else Path(path) self._root = None self._header = None self._source_desc = None self._idno = None self._title = None
def inline_svg_images(svg) -> str: """ Inline IMAGE tag refs in graphviz/dot -> SVG generated files. Convert all .svg image tag refs directly under g tags like: <g id="node1" class="node"> <image xlink:href="/tmp/node4.svg" width="45px" height="76px" preserveAspectRatio="xMinYMin meet" x="76" y="-80"/> </g> to <g id="node1" class="node"> <svg width="45px" height="76px" viewBox="0 0 49.008672 80.826687" preserveAspectRatio="xMinYMin meet" x="76" y="-80"> XYZ </svg> </g> where XYZ is taken from ref'd svg image file: <?xml version="1.0" encoding="utf-8" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <!-- Created with matplotlib (http://matplotlib.org/) --> <svg height="80.826687pt" version="1.1" viewBox="0 0 49.008672 80.826687" width="49.008672pt" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> XYZ </svg> Note that width/height must be taken image ref tag and put onto svg tag. We also need the viewBox or it gets clipped a bit. :param svg: SVG string with <image/> tags. :return: svg with <image/> tags replaced with content of referenced svg image files. """ ns = {"svg": "http://www.w3.org/2000/svg"} root = ET.fromstring(svg) tree = ET.ElementTree(root) parent_map = {c: p for p in tree.iter() for c in p} # Find all image tags in document (must use svg namespace) image_tags = tree.findall(".//svg:g/svg:image", ns) for img in image_tags: # load ref'd image and get svg root svgfilename = img.attrib["{http://www.w3.org/1999/xlink}href"] with open(svgfilename, encoding='UTF-8') as f: imgsvg = f.read() imgroot = ET.fromstring(imgsvg) for k, v in img.attrib.items( ): # copy IMAGE tag attributes to svg from image file if k not in {"{http://www.w3.org/1999/xlink}href"}: imgroot.attrib[k] = v # replace IMAGE with SVG tag p = parent_map[img] # print("BEFORE " + ', '.join([str(c) for c in p])) p.append(imgroot) p.remove(img) # print("AFTER " + ', '.join([str(c) for c in p])) ET.register_namespace('', "http://www.w3.org/2000/svg") ET.register_namespace('xlink', "http://www.w3.org/1999/xlink") xml_str = ET.tostring(root).decode() return xml_str
#!/usr/bin/env python3 import sys import re import xml.etree.cElementTree as et et.register_namespace('dc', "http://purl.org/dc/elements/1.1/") et.register_namespace('cc', "http://creativecommons.org/ns#") et.register_namespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#") et.register_namespace('svg', "http://www.w3.org/2000/svg") et.register_namespace('', "http://www.w3.org/2000/svg") et.register_namespace('sodipodi', "http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd") et.register_namespace('inkscape', "http://www.inkscape.org/namespaces/inkscape") if len(sys.argv) < 2: print("usage: %s SVG-FILE" % sys.argv[0]) sys.exit(1) svg_doc = et.parse(sys.argv[1]) svg_obj = svg_doc.getroot() ns = {} if svg_obj.tag[0] == '{': tagspl = svg_obj.tag[1:].split('}') ns[tagspl[1]] = tagspl[0]; g_tag = '{' + ns['svg'] + '}g' show_layers = [] hide_layers = []
def init(): ET.register_namespace('gr', READER_NS) ET.register_namespace('atom', ATOM_NS) ET.register_namespace('coop', 'http://www.google.com/coop/namespace') ET.register_namespace('gd', 'http://schemas.google.com/g/2005') ET.register_namespace('idx', 'urn:atom-extension:indexing') ET.register_namespace('media', 'http://search.yahoo.com/mrss/') ET.register_namespace('thr', 'http://purl.org/syndication/thread/1.0')
def writeReactionXML(React, path, printTS2): ET.register_namespace('me', 'http://www.chem.leeds.ac.uk/mesmer') ET.register_namespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance') ET.register_namespace('', 'http://www.xml-cml.org/schema') name = React.ReacName + '_' + React.ProdName with open(path, 'r') as myfile: data = myfile.read().replace('\n', '') bigTree = ET.fromstring(data) act = 0 symb = "".join(React.CombReac.get_chemical_symbols()) #buffer is added to rxn element as the print function removes the third word rxn = ET.Element("reaction", id=name, active='true') if React.is_bimol_reac == False and React.is_bimol_prod == False: rct = ET.SubElement(rxn, "reactant") ml1 = ET.SubElement(rct, "molecule", ref=React.ReacName) ml1.set("{http://www.chem.leeds.ac.uk/mesmer}type", "modelled") elif React.is_bimol_reac == False and React.is_bimol_prod == True: rct = ET.SubElement(rxn, "product") ml1 = ET.SubElement(rct, "molecule", ref=React.ReacName) ml1.set("{http://www.chem.leeds.ac.uk/mesmer}type", "modelled") else: rct1 = ET.SubElement(rxn, "reactant") ml1_1 = ET.SubElement(rct1, "molecule", ref=React.ReacName) ml1_1.set("{http://www.chem.leeds.ac.uk/mesmer}type", "modelled") rct2 = ET.SubElement(rxn, "reactant") ml1_2 = ET.SubElement(rct2, "molecule", ref=React.biReacName) ml1_2.set("{http://www.chem.leeds.ac.uk/mesmer}type", "excessReactant") if React.barrierlessReaction: act = 0 else: React.barrierlessReaction = True act = float(( (React.forwardBarrier - React.energyDictionary[symb]) * 96.45 ) - ( (React.reactantEnergy - React.energyDictionary[symb]) * 96.45)) if act < 0: act = 0 React.barrierlessReaction = True if React.is_bimol_prod == False: prod = ET.SubElement(rxn, "product") pml1 = ET.SubElement(prod, "molecule", ref=React.ProdName) pml1.set("{http://www.chem.leeds.ac.uk/mesmer}type", "modelled") elif React.is_bimol_reac == False: prod1 = ET.SubElement(rxn, "reactant") pml1_1 = ET.SubElement(prod1, "molecule", ref=React.ProdName) pml1_1.set("{http://www.chem.leeds.ac.uk/mesmer}type", "modelled") prod2 = ET.SubElement(rxn, "reactant") pml1_2 = ET.SubElement(prod2, "molecule", ref=React.biProdName) pml1_2.set("{http://www.chem.leeds.ac.uk/mesmer}type", "excessReactant") if React.barrierlessReaction: act = 0 else: React.barrierlessReaction = True act = float( ((React.forwardBarrier - React.energyDictionary[symb]) * 96.45) - ((React.productEnergy - React.energyDictionary[symb]) * 96.45)) if act < 0: act = 0 else: prod1 = ET.SubElement(rxn, "product") pml1_1 = ET.SubElement(prod1, "molecule", ref=React.ProdName) pml1_1.set("{http://www.chem.leeds.ac.uk/mesmer}type", "modelled") # Transition state section, if not present then ILT if React.barrierlessReaction == False: TS = ET.SubElement( rxn, "{http://www.chem.leeds.ac.uk/mesmer}transitionState") if printTS2: ts1 = ET.SubElement(TS, "molecule", ref=('TS2_' + React.ReacName + '_' + React.ProdName)) else: ts1 = ET.SubElement(TS, "molecule", ref=('TS_' + React.ReacName + '_' + React.ProdName)) ts1.set("{http://www.chem.leeds.ac.uk/mesmer}type", "transitionState") RRKM = ET.SubElement(rxn, "{http://www.chem.leeds.ac.uk/mesmer}MCRCMethod", name="SimpleRRKM") else: ILT = ET.SubElement(rxn, "{http://www.chem.leeds.ac.uk/mesmer}MCRCMethod") ILT.set("{http://www.w3.org/2001/XMLSchema-instance}type", "MesmerILT") preExp = ET.SubElement( ILT, "{http://www.chem.leeds.ac.uk/mesmer}preExponential", units="cm3 molecule-1 s-1") preExp.text = '1E-10' preExp = ET.SubElement( ILT, "{http://www.chem.leeds.ac.uk/mesmer}activationEnergy", units="kJ/mol") preExp.text = str(act) preExp = ET.SubElement( ILT, "{http://www.chem.leeds.ac.uk/mesmer}TInfinity") preExp.text = '298.0' preExp = ET.SubElement( ILT, "{http://www.chem.leeds.ac.uk/mesmer}nInfinity") preExp.text = '0.0' excess = ET.SubElement( rxn, "{http://www.chem.leeds.ac.uk/mesmer}excessReactantConc") excess.text = '1E18' # Append the new "data" elements to the root element of the XML document children = bigTree.getchildren() children[2] = children[2].append(rxn) # Now we have a new well-formed XML document. It is not very nicely formatted... out = ET.tostring(bigTree) # ...so we'll use minidom to make the output a little prettier dom = minidom.parseString(out) ChemDyME.Tools.prettyPrint(dom, path)
parser.add_option("-s", "--subscription-id", dest="subscription_id") parser.add_option("-t", "--tenant-id", dest="tenant_id") parser.add_option("-a", "--aad-client-id", dest="aad_client_id") parser.add_option("-c", "--aad-client-secret", dest="aad_client_secret") parser.add_option("-g", "--group-name", dest="group_name") parser.add_option("-l", "--cluster-tag", dest="cluster_tag") parser.add_option("-k", "--cluster-port", dest="cluster_port") parser.add_option("-f", "--filename", dest="filename") if len(sys.argv) > 0: opts, args = parser.parse_args(sys.argv) ## Opening the hazelcast.xml file try: ## f = open(opts.filename) ## Registering default namespace hazelcast_ns = "http://www.hazelcast.com/schema/config" ET.register_namespace('', hazelcast_ns) ns = {'hazelcast_ns': hazelcast_ns} ## Loading the root parser tree = ET.parse(opts.filename) root = tree.getroot() ## Finding and replacing the old group tag with the new one for group in root.iter(str(QName(hazelcast_ns, "group"))): group.find("hazelcast_ns:name", ns).text = opts.cluster_name print("Updated cluster user name...") for network in root.iter(str(QName(hazelcast_ns, "network"))): network.find("hazelcast_ns:port", ns).text = opts.cluster_port print("Updated cluster port...") print("Updating Azure discovery...")
def parse_pepxml(self): peptides = [] namespaces = { 'pepxml_ns': "http://regis-web.systemsbiology.net/pepXML" } ET.register_namespace('', "http://regis-web.systemsbiology.net/pepXML") context = iterparse(self.pepxml_file, events=("end", )) for event, elem in context: if elem.tag == "{http://regis-web.systemsbiology.net/pepXML}msms_run_summary": base_name = posixpath.basename( ntpath.basename(elem.attrib['base_name'])) # only proceed if base_name matches if base_name == self.base_name: # find decoy prefix decoy_prefix = "" for search_summary in elem.findall( './/pepxml_ns:search_summary', namespaces): for parameter in search_summary.findall( './/pepxml_ns:parameter', namespaces): if parameter.attrib['name'] == 'decoy_prefix': decoy_prefix = parameter.attrib['value'] # go through all spectrum queries for spectrum_query in elem.findall( './/pepxml_ns:spectrum_query', namespaces): index = spectrum_query.attrib['index'] start_scan = spectrum_query.attrib['start_scan'] end_scan = spectrum_query.attrib['end_scan'] assumed_charge = spectrum_query.attrib[ 'assumed_charge'] retention_time_sec = spectrum_query.attrib[ 'retention_time_sec'] ion_mobility = np.nan if 'ion_mobility' in spectrum_query.attrib: ion_mobility = spectrum_query.attrib[ 'ion_mobility'] for search_result in spectrum_query.findall( ".//pepxml_ns:search_result", namespaces): prev_pep = None for search_hit in search_result.findall( ".//pepxml_ns:search_hit", namespaces): hit_rank = search_hit.attrib['hit_rank'] massdiff = search_hit.attrib['massdiff'] # parse peptide and protein information peptide = search_hit.attrib['peptide'] unprocessed_proteins = [ search_hit.attrib['protein'] ] for alternative_protein in search_hit.findall( './/pepxml_ns:alternative_protein', namespaces): unprocessed_proteins.append( alternative_protein.attrib['protein']) # remove decoy results from mixed target/decoy hits has_targets = False has_decoys = False for prot in unprocessed_proteins: if decoy_prefix in prot: has_decoys = True else: has_targets = True processed_proteins = [] for prot in unprocessed_proteins: if has_targets and has_decoys: if decoy_prefix not in prot: processed_proteins.append(prot) else: processed_proteins.append(prot) num_tot_proteins = len(processed_proteins) is_decoy = False if has_decoys and not has_targets: is_decoy = True proteins = {} for prot in processed_proteins: # Remove UniProt prefixes if necessary if decoy_prefix + "sp|" in prot: proteins[decoy_prefix + prot.split("|")[1]] = "" elif "sp|" in prot: proteins[prot.split("|")[ 1]] = prot.split("|")[2].split( " ")[0].split("_")[0] else: proteins[prot] = prot protein = "" gene = "" for key in sorted(proteins): if protein == "": protein = key else: protein = protein + ";" + key if gene == "": gene = proteins[key] else: gene = gene + ";" + proteins[key] # parse PTM information modifications = "M" nterm_modification = "" cterm_modification = "" for modification_info in search_hit.findall( './/pepxml_ns:modification_info', namespaces): if 'mod_nterm_mass' in modification_info.attrib: nterm_modification = float( modification_info. attrib['mod_nterm_mass']) if 'mod_cterm_mass' in modification_info.attrib: cterm_modification = float( modification_info. attrib['mod_cterm_mass']) for mod_aminoacid_mass in modification_info.findall( './/pepxml_ns:mod_aminoacid_mass', namespaces): modifications = modifications + "|" + mod_aminoacid_mass.attrib[ 'position'] + "$" + mod_aminoacid_mass.attrib[ 'mass'] # parse search engine score information scores = {} for search_score in search_hit.findall( './/pepxml_ns:search_score', namespaces): scores[ "var_" + search_score.attrib['name']] = float( search_score.attrib['value']) # parse PeptideProphet or iProphet results if available for analysis_result in search_hit.findall( './/pepxml_ns:analysis_result', namespaces): if analysis_result.attrib[ 'analysis'] == 'interprophet': for interprophet_result in analysis_result.findall( './/pepxml_ns:interprophet_result', namespaces): scores["pep"] = 1.0 - float( interprophet_result. attrib['probability']) prev_pep = scores["pep"] elif analysis_result.attrib[ 'analysis'] == 'peptideprophet': for peptideprophet_result in analysis_result.findall( './/pepxml_ns:peptideprophet_result', namespaces): scores["pep"] = 1.0 - float( peptideprophet_result. attrib['probability']) prev_pep = scores["pep"] if prev_pep is not None and "pep" not in scores: # If 2 search hits have the same rank only the first one has the analysis_result explicitly written out. scores["pep"] = prev_pep if prev_pep is not None: peptides.append({ **{ 'run_id': base_name, 'scan_id': int(start_scan), 'hit_rank': int(hit_rank), 'massdiff': float(massdiff), 'precursor_charge': int(assumed_charge), 'retention_time': float(retention_time_sec), 'ion_mobility': float(ion_mobility), 'peptide_sequence': peptide, 'modifications': modifications, 'nterm_modification': nterm_modification, 'cterm_modification': cterm_modification, 'protein_id': protein, 'gene_id': gene, 'num_tot_proteins': num_tot_proteins, 'decoy': is_decoy }, **scores }) elem.clear() df = pd.DataFrame(peptides) return (df)
def __init__(self, xmlfile): ET.register_namespace('c', "http://www.ansys.com/EDBBuilderUtils/SetupInfo") self.tree = ET.ElementTree(file=xmlfile) self.dir=os.path.dirname(xmlfile) self.reset()
def misp_events(xml_file): return_stix_pakage = True as_object = False isotime = dt.now().isoformat() for event, el in etree.iterparse(xml_file, ("start", "end", "start-ns")): if event == "start": if el.tag in ( "{http://stix.mitre.org/stix-1}Indicator", "{http://stix.mitre.org/Incident-1}Related_Indicator", "{http://cybox.mitre.org/cybox-2}Related_Object"): isotime = el.attrib.get("timestamp", isotime) attr = {} elif el.tag == "{http://cybox.mitre.org/cybox-2}Related_Objects": saved_attr = attr as_object = True elif el.tag in ("{http://stix.mitre.org/stix-1}Package", "{http://stix.mitre.org/stix-1}STIX_Package"): isotime = el.attrib.get("timestamp", isotime) misp_event = { "Attribute": [], "Object": [], "analysis": "0", "distribution": "3", "info": "", "threat_level_id": "2" } package_id = el.attrib.get("id", "") if len(package_id) > 36: misp_event["uuid"] = package_id[-36:] if ":" in package_id: misp_event["orgc"] = package_id.split(":")[0] elif event == "end": if el.tag in ( "{http://stix.mitre.org/stix-1}Indicator", "{http://stix.mitre.org/Incident-1}Related_Indicator", "{http://cybox.mitre.org/cybox-2}Related_Object"): timestamp = fromisoformat(isotime).timestamp() if el.tag.endswith("Related_Object") and "id" in el.attrib: attr["uuid"] = el.attrib.get("id")[-36:] if "Attribute" in attr: attr.pop("value", None) to_ids = attr.pop("to_ids", True) d_c = attr.pop("disable_correlation", True) for idx in range(len(attr["Attribute"])): if "to_ids" not in attr["Attribute"][idx]: attr["Attribute"][idx]["to_ids"] = to_ids if "disable_correlation" not in attr["Attribute"][idx]: attr["Attribute"][idx]["disable_correlation"] = d_c if "distribution" not in attr["Attribute"][idx]: attr["Attribute"][idx]["distribution"] = "5" attr["Attribute"][idx]["timestamp"] = timestamp attr["timestamp"] = timestamp misp_event["Object"].append(attr) elif "value" in attr and attr.get("type") != "other": if "to_ids" not in attr: attr["to_ids"] = True if "disable_correlation" not in attr: attr["disable_correlation"] = False if "distribution" not in attr: attr["distribution"] = "5" attr["timestamp"] = timestamp misp_event["Attribute"].append(attr) el.clear() elif el.tag == "{http://cybox.mitre.org/cybox-2}Properties": _type = el.attrib.get( "{http://www.w3.org/2001/XMLSchema-instance}type") if _type == "URIObj:URIObjectType": attr["type"] = "url" elif _type == "HostnameObj:HostnameObjectType": attr["type"] = "hostname" elif _type == "DomainNameObj:DomainNameObjectType": attr["type"] = "domain" elif _type == "AddressObj:AddressObjectType": attr = processAddressObj(el, attr, as_object) elif _type == "FileObj:FileObjectType": attr = processFileObj(el, attr, as_object) elif _type == "EmailMessageObj:EmailMessageObjectType": attr = processEmailObj(el, attr, as_object) elif _type == "NetworkConnectionObj:NetworkConnectionObjectType": attr = processNetworkConnectionObj(el, attr, as_object) elif el.tag == "{http://stix.mitre.org/common-1}Value": attr["to_ids"] = el.text == "High" elif el.tag.endswith("}Value"): attr["value"] = el.text elif el.tag == "{http://cybox.mitre.org/cybox-2}Object": if "id" in el.attrib: attr["uuid"] = el.attrib.get("id")[-36:] elif el.tag == "{http://cybox.mitre.org/cybox-2}Related_Objects": attr = saved_attr as_object = False elif el.tag == "{http://cybox.mitre.org/cybox-2}Title": attr["comment"] = el.text elif el.tag == "{http://stix.mitre.org/Incident-1}Title": misp_event["info"] = el.text elif el.tag == "{http://stix.mitre.org/stix-1}Title": if not misp_event["info"]: misp_event["info"] = el.text elif el.tag == "{http://stix.mitre.org/stix-1}Package": misp_event["timestamp"] = fromisoformat(isotime).timestamp() if not misp_event["info"]: misp_event["info"] = "STIX Indicators" return_stix_pakage = False yield {"Event": misp_event} elif el.tag == "{http://stix.mitre.org/stix-1}STIX_Package": misp_event["timestamp"] = fromisoformat(isotime).timestamp() if return_stix_pakage: if not misp_event["info"]: misp_event["info"] = "STIX Indicators" yield {"Event": misp_event} elif event == "start-ns": etree.register_namespace(*el)
# it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import re, datetime from xml.etree import cElementTree as ElementTree ElementTree.register_namespace("","http://www.w3.org/2000/svg") ElementTree.register_namespace("xlink", "http://www.w3.org/1999/xlink") class Extend: def __init__(self): self.minx = None self.maxx = None self.miny = None self.maxy = None def __add__(self, v): x, y = v res = Extend() res.minx = self.minx + x res.maxx = self.maxx + x
if background: ET.SubElement(rpc_el, NuciTLS.qual_tag("background")) return rpc_el @staticmethod def rpc_new_client(name, background=True): rpc_el = ET.Element(NuciTLS.qual_tag("new-client")) name_el = ET.SubElement(rpc_el, NuciTLS.qual_tag("name")) name_el.text = name if background: ET.SubElement(rpc_el, NuciTLS.qual_tag("background")) return rpc_el @staticmethod def rpc_revoke_client(name): rpc_el = ET.Element(NuciTLS.qual_tag("revoke-client")) name_el = ET.SubElement(rpc_el, NuciTLS.qual_tag("name")) name_el.text = name return rpc_el @staticmethod def rpc_get_token(name): rpc_el = ET.Element(NuciTLS.qual_tag("get-token")) name_el = ET.SubElement(rpc_el, NuciTLS.qual_tag("name")) name_el.text = name return rpc_el #################################################################################################### ET.register_namespace("nuci-tls", NuciTLS.NS_URI)
formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("-p", "--port", type=int, default=8000, help="HTTP port for this server") parser.add_argument("-u", "--url", default=INT_URL, help="URL of the real Branding Service") t186group = parser.add_argument_group("T186 fake itemno generation") t186group.add_argument("--fake-t186", action="store_true", help="Return a fake T186 itemno for each brand") t186group.add_argument("--t186-offset", type=int, default=384000, help="Fake itemnos will start at OFFSET", metavar="OFFSET") args = parser.parse_args() ET.register_namespace("", XMLNS) ET.register_namespace("ANCS", XMLNS_ANCS) serve_proxy(args.port, args.url, args.fake_t186, args.t186_offset) # vim:ai:et:sw=4
for sizedir in [f for f in os.listdir() if f.endswith('px')]: for filename in [f for f in os.listdir(sizedir) if f.endswith('.svg')]: current_file = os.path.join(sizedir, filename) size = int(sizedir[0:-2]) name = filename[0:-4].lower().replace(' ', '_') if size != 25: name += str(size) export_file = os.path.join("exported", name + ".svg") export_file_background = os.path.join("exported", name + "_background.svg") cmd = "inkscape --batch-process --verb=\"EditSelectAll;StrokeToPath;SelectionUnGroup;SelectionUnGroup;SelectionUnGroup;SelectionUnGroup;EditSelectNext;EditSelectSameFillColor;SelectionUnion;EditSelectNext;EditSelectSameFillColor;SelectionUnion;\" -o {export_file} \"{import_file}\"" os.system(cmd.format(export_file=export_file, import_file=current_file)) ET.register_namespace("", "http://www.w3.org/2000/svg") xml = open(export_file) it = ET.iterparse(StringIO(xml.read())) for _, el in it: _, _, el.tag = el.tag.rpartition('}') root = it.root allpaths = '' for elem in root.findall('.//path'): allpaths += elem.attrib.get('d') path = parse_path(allpaths) bbox = path.bbox() scale = 1000 / size
@staticmethod def from_element(element): local = element.find(Time.qual_tag("local")).text timezone = element.find(Time.qual_tag("timezone")).text utc = element.find(Time.qual_tag("utc")).text return Time(local, timezone, utc) @property def key(self): return "time" @staticmethod def rpc_set_iso8601(iso_time): set_tag = Time.qual_tag("set") element = ET.Element(set_tag) time_tag = Time.qual_tag("time") time_elem = ET.SubElement(element, time_tag) time_elem.text = iso_time # handling ISO dates with Python stdlib is pain... is_utc = iso_time.endswith("Z") or iso_time.endswith("UTC") or iso_time.endswith("+00") \ or iso_time.endswith("+00:00") or iso_time.endswith("+0000") if is_utc: utc_tag = Time.qual_tag("utc") ET.SubElement(element, utc_tag) return element #################################################################################################### ET.register_namespace("time", Time.NS_URI)