def data_import(self, odm): """ Return result of request to import data to an instance. Required params: :param odm: odm xml data for import, must be utf-8 encoded and unescaped. :type odm: str. :returns: contents of the method response in an OrderedDict, or None. """ envelope_copy = et.fromstring(self.envelope) body = [ i for i in envelope_copy.iterfind('.//se:Body', {'se': self.ns_se}) ][0] method_name = 'importRequest' import_request = et.Element(et.QName(self.ns_data, method_name)) body.append(import_request) odm_node = et.Element('odm') odm_node.append(et.fromstring(odm)) import_request.append(odm_node) envelope = et.tostring(envelope_copy) response = self.request(self.ocws_url, envelope, method_name, self.ns_data) return response
def add_alias(xml, appid, alias, subsystem="windows"): app = _get_app(xml, appid) e = find_or_add(app, "m:Extensions") e = find_or_add(e, "uap5:Extension", ("Category", "windows.appExecutionAlias")) e = find_or_add(e, "uap5:AppExecutionAlias") e.set(ET.QName(APPXMANIFEST_NS["desktop4"], "Subsystem"), subsystem) e = find_or_add(e, "uap5:ExecutionAlias", ("Alias", alias))
def testQNamePreEscaping(self): """ Test QName that is already partially escaped. """ qname = etree.QName('<&"test escaping">', 'div') el = etree.Element(qname) self.assertEqual( markdown.serializers.to_xhtml_string(el), '<div xmlns="<&"test escaping">"></div>')
def testQNameTag(self): """ Test serialization of QName tag. """ div = etree.Element('div') qname = etree.QName('http://www.w3.org/1998/Math/MathML', 'math') math = etree.SubElement(div, qname) math.set('display', 'block') sem = etree.SubElement(math, 'semantics') msup = etree.SubElement(sem, 'msup') mi = etree.SubElement(msup, 'mi') mi.text = 'x' mn = etree.SubElement(msup, 'mn') mn.text = '2' ann = etree.SubElement(sem, 'annotations') ann.text = 'x^2' self.assertEqual( markdown.serializers.to_xhtml_string(div), '<div>' '<math display="block" xmlns="http://www.w3.org/1998/Math/MathML">' '<semantics>' '<msup>' '<mi>x</mi>' '<mn>2</mn>' '</msup>' '<annotations>x^2</annotations>' '</semantics>' '</math>' '</div>')
def add_Annotation(self, start_datetime: datetime, end_datetime: datetime, AnnotationTypeId=0, note='', type='GlobalAnnotation'): start_s = start_datetime.strftime("%Y-%m-%dT%H:%M:%S.%f") end_s = end_datetime.strftime("%Y-%m-%dT%H:%M:%S.%f") Annotation = self.Annotations.add_child(myXML_Annotation) Annotation.attributes[ET.QName(xsi, "type")] = type ID = Annotation.add_child(myXML_Id, param=str(uuid.uuid4())) StartTimeUtc = Annotation.add_child(myXML_StartTimeUtc, param=start_s) endTimeUtc = Annotation.add_child(myXML_endTimeUtc, param=end_s) if isinstance(AnnotationTypeId, str): is_AType_set = False for idx, ATypepName in enumerate(self.AnnotationTypeKeys): if ATypepName == AnnotationTypeId: AnnotationTypeId = idx is_AType_set = True if is_AType_set is False: raise KeyError( 'AnnotationType cannot be assigned to an AnnotationGroup \"' + AnnotationTypeId + '\". Available AnnotationGroups \"' + str(self.AnnotationGroupKeys)) ATypeId_s = self.AnnotationTypeIDs[AnnotationTypeId] AnnotationTypeId = Annotation.add_child(myXML_AnnotationTypeId, param=ATypeId_s) if note.__len__() > 0: note = Annotation.add_child(myXML_Note, param=note)
def testQNameEscaping(self): """ Test QName escaping. """ qname = etree.QName('<&"test\nescaping">', 'div') el = etree.Element(qname) self.assertEqual( markdown.serializers.to_xhtml_string(el), '<div xmlns="<&"test escaping">"></div>')
def run_zbarimg(images): """invoke zbarimg for the specified files. return results as an ET.Element """ args = [zbarimg] args.extend(zbarimg_args) args.extend(images) if debug: print 'running:', ' '.join(args) # FIXME should be able to pipe (feed) parser straight from output child = Popen(args, stdout=PIPE, stderr=PIPE) (xml, err) = child.communicate() rc = child.returncode if debug: print 'zbarimg returned', rc # FIXME trim usage from error msg assert rc in (0, 4), \ 'zbarimg returned error status (%d)\n' % rc + err assert not err, err result = ET.XML(xml) assert result.tag == ET.QName(BC, 'barcodes') return result
def compare_sources(expect, actual): assert actual.tag == ET.QName(BC, 'source') assert actual.get('href').endswith(expect.get('href')), \ 'source href mismatch: %s != %s' % (acthref, exphref) # FIXME process/trim test:* contents def map_source(src): if not len(src) or src[0].tag != ET.QName(BC, 'index'): # insert artificial hierarchy syms = src[:] del src[:] idx = ET.SubElement(src, ET.QName(BC, 'index'), num='0') idx[:] = syms exc = src.get(str(ET.QName(TS, 'exception'))) if exc is not None: idx.set(str(ET.QName(TS, 'exception')), exc) return {'0': idx} elif len(src): assert src[0].tag != ET.QName(BC, 'symbol'), \ 'invalid source element: ' + \ 'expecting "index" or "symbol", got "%s"' % fixtag(src[0]) srcmap = {} for idx in src: srcmap[idx.get('num')] = idx return srcmap compare_maps(map_source(expect), map_source(actual), compare_indices)
def _fixup_sccd(ns, sccd, new_hash=None): if not new_hash: return sccd NS = dict(s="http://schemas.microsoft.com/appx/2016/sccd") with open(sccd, "rb") as f: xml = ET.parse(f) pfn = get_packagefamilyname(APPX_DATA["Name"], APPX_DATA["Publisher"]) ae = xml.find("s:AuthorizedEntities", NS) ae.clear() e = ET.SubElement(ae, ET.QName(NS["s"], "AuthorizedEntity")) e.set("AppPackageFamilyName", pfn) e.set("CertificateSignatureHash", new_hash) for e in xml.findall("s:Catalog", NS): e.text = "FFFF" sccd = ns.temp / sccd.name sccd.parent.mkdir(parents=True, exist_ok=True) with open(sccd, "wb") as f: xml.write(f, encoding="utf-8") return sccd
def manifest_to_list(self, name): """Convert the manifest.xml to a list. List of tuples : (name, mimetype) @param name: the manifest filename @type name: string @return: a list of typles (name, mimetype) """ l = [] tree = ET.parse(name) for e in tree.getroot(): if e.tag == ET.QName(MANIFEST, 'file-entry'): l.append((e.attrib[ET.QName(MANIFEST, 'full-path')], e.attrib[ET.QName(MANIFEST, 'media-type')])) return l
def _create_track_point_extensions_element(track_point, ns=gpx_ns, tag="extensions"): extensions = etree.Element(etree.QName(ns, tag)) extensions.append( _create_garmin_track_point_extension_element(track_point)) return extensions
def study_get_metadata(self, study_identifier): """ Return result of query for metadata for a study. Attempts to parse the ODM raw xml into an OrderedDict as well. Required params: :param study_identifier: unique identifier (not OID) of study. :type study_identifier: str. :returns: contents of the method response in an OrderedDict, or None. """ envelope_copy = et.fromstring(self.envelope) body = [ i for i in envelope_copy.iterfind('.//se:Body', {'se': self.ns_se}) ][0] method_name = 'getMetadataRequest' get_metadata = et.Element(et.QName(self.ns_study, method_name)) body.append(get_metadata) study_metadata = et.Element(et.QName(self.ns_study, 'studyMetadata')) get_metadata.append(study_metadata) study_ident = et.Element(et.QName(self.ns_beans, 'identifier')) study_ident.text = study_identifier study_metadata.append(study_ident) envelope = et.tostring(envelope_copy) response = self.request(self.ocws_url, envelope, method_name, self.ns_study) try: ns_odm = { self.ns_oc_odm: None, self.ns_oc: None, self.ns_ocrules: None } response['odm'] = xmltodict.parse(response['odm'], process_namespaces=True, namespaces=ns_odm) except TypeError: # Request failed and the response was None (can't subscript None) pass except KeyError: # Call failed and 'odm' is not in response (bad study_identifier) pass return response
def _from_xml_node(self, node, listnames): """Convert a minidom node to a simple Python type. :param node: minidom node name :param listnames: list of XML node names whose subnodes should be considered list items. """ attrNil = node.get(str(etree.QName(constants.XSI_NAMESPACE, "nil"))) attrType = node.get( str(etree.QName(self.metadata.get('xmlns'), "type"))) if (attrNil and attrNil.lower() == 'true'): return None elif not len(node) and not node.text: if (attrType and attrType == constants.TYPE_DICT): return {} elif (attrType and attrType == constants.TYPE_LIST): return [] else: return '' elif (len(node) == 0 and node.text): converters = { constants.TYPE_BOOL: lambda x: x.lower() == 'true', constants.TYPE_INT: lambda x: int(x), constants.TYPE_LONG: lambda x: long(x), constants.TYPE_FLOAT: lambda x: float(x) } if attrType and attrType in converters: return converters[attrType](node.text) else: return node.text elif self._get_key(node.tag) in listnames: return [self._from_xml_node(n, listnames) for n in node] else: result = dict() for attr in node.keys(): if (attr == 'xmlns' or attr.startswith('xmlns:') or attr == constants.XSI_ATTR or attr == constants.TYPE_ATTR): continue result[self._get_key(attr)] = node.get(attr) children = list(node) for child in children: result[self._get_key(child.tag)] = self._from_xml_node( child, listnames) return result
def __init__(self): self.dirPath = "" self.ns = {"n1": "http://cqt.barographe.fr/Datas"} ET.register_namespace('n1', self.ns["n1"]) self.root = ET.Element(ET.QName(self.ns["n1"], "Datas")) self.dhInit = datetime.datetime.utcnow() self.tree = '' self.absPath = ""
def _create_track_point_element(track_point, ns=gpx_ns, tag='trkpt'): trkpt = etree.Element(etree.QName(ns, tag)) trkpt.set("lat", str(track_point.latitude)) trkpt.set("lon", str(track_point.longitude)) trkpt.append(_create_decimal_element(gpx_ns, "ele", track_point.altitude)) trkpt.append(_create_datetime_element(gpx_ns, "time", track_point.date)) trkpt.append(_create_track_point_extensions_element(track_point)) return trkpt
def list_to_manifest(self, manifest): """Generate the XML representation of the manifest. @param manifest: the list of files @type manifest: list @return: the XML representation of the manifest @rtype: string """ root = ET.Element(ET.QName(MANIFEST, 'manifest')) ET.SubElement( root, ET.QName(MANIFEST, 'file-entry'), { ET.QName(MANIFEST, 'full-path'): '/', ET.QName(MANIFEST, 'media-type'): MIMETYPE, }) for f in manifest: if f == 'mimetype' or f == 'META-INF/manifest.xml': continue (mimetype, encoding) = mimetypes.guess_type(f) if mimetype is None: mimetype = 'text/plain' ET.SubElement( root, ET.QName(MANIFEST, 'file-entry'), { ET.QName(MANIFEST, 'full-path'): str(f), ET.QName(MANIFEST, 'media-type'): str(mimetype), }) return root
def make_path(self): """Creates path element consisting of QR points""" subpaths = self._generate_subpaths() return ET.Element(ET.QName("path"), style=self.QR_PATH_STYLE, d=' '.join(subpaths), id="qr-path")
def __init__(self, *args, **kwargs): self.text = kwargs.pop('text', None) namespace = kwargs.pop('namespace', None) if namespace: super(ElementFactory, self).__init__(etree.QName(namespace, args[0]), **kwargs) self.namespace = namespace else: super(ElementFactory, self).__init__(*args, **kwargs)
def _generate_chat(self, to, from_, body): data = _FormData() data.add_text('from', from_, 'plain') data.add_text('to', to, 'plain') data.add_text('body', body, 'plain') message_element = ElementTree.Element( ElementTree.QName('jabber:client', 'message'), {'from': from_, 'to': to, 'type': 'chat'}) body_element = ElementTree.SubElement( message_element, ElementTree.QName('jabber:client', 'body')) body_element.text = body data.add_text('stanza', ElementTree.tostring(message_element, encoding='utf-8'), 'xml') return data
def nodename(self, name): #self.debug("name: %s, QName: %s" %(name, ET.QName(self.root, name).text)) """Extends name with the xmlns-prefix to a valid nodename.""" found = re.match('{.*({.*}).*}(.*$)', ET.QName(self.root, name).text) if found: # self.debug("result: " + found.group(1) + found.group(2)) return found.group(1) + found.group(2) else: return ""
def baselines_to_xml_string(self) -> str: """ creates the xml to the given baselines :return: xml-string of baselines """ def make_id_gen(identifier): return map(lambda x: f"{identifier}{x}", itertools.count(start=1)) textline_id_gen = make_id_gen("TextLine") xmlns_uris = { 'pc': 'http://schema.primaresearch.org/PAGE/gts/pagecontent/2017-07-15' } attr_qname = ET.QName("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation") root_node = ET.Element( "PcGts", { attr_qname: "http://schema.primaresearch.org/PAGE/gts/pagecontent/2019-07-15 http://schema.primaresearch.org/PAGE/gts/pagecontent/2019-07-15/pagecontent.xsd" }) for prefix, uri in xmlns_uris.items(): root_node.attrib['xmlns'] = uri page_node = ET.SubElement(root_node, "Page", imageFilename=str(self.imageFilename), imageWidth=str(self.imageWidth), imageHeight=str(self.imageHeight)) reading_order_node = ET.SubElement(page_node, "ReadingOrder") ordered_group_node = ET.SubElement(reading_order_node, "OrderedGroup", id="ro1", caption="Region reading order") for t_ind, t in enumerate(self.regions): tr_node = ET.SubElement(page_node, "TextRegion", id="TextRegion" + str(t_ind)) ET.SubElement(ordered_group_node, "RegionRefIndexed", index=str(t_ind), regionRef="TextRegion" + str(t_ind)) if t.coords is not None: ET.SubElement(tr_node, "Coords", points=coords_to_string(t.coords)) for i_ind, i in enumerate(t.textLines): tl_node = ET.SubElement(tr_node, "TextLine", id=next(textline_id_gen)) ET.SubElement(tl_node, "Coords", points=coords_to_string(i.coords)) ET.SubElement(tl_node, "Baseline", points=coords_to_string(i.baseline.coords)) return minidom.parseString( ET.tostring(root_node)).toprettyxml(indent=' ')
def parse_element(self, root): """Parse an ElementTree Element. """ if root.tag != ET.QName(config.data.namespace, 'adhoc'): raise Exception("Invalid adhoc view definition" + root.tag) self.view_id = root.attrib['id'] for e in root: if e.tag == ET.QName(config.data.namespace, 'option'): name = e.attrib['name'] value = unicode(urllib.unquote(e.attrib['value']), 'utf8') self.options[name] = value elif e.tag == ET.QName(config.data.namespace, 'argument'): name = e.attrib['name'] value = unicode(urllib.unquote(e.attrib['value']), 'utf8') self.arguments.append((name, value)) else: print "Unknown tag %s in AdhocViewParametersParser" % e.tag
def make_border(self): """Creates black thin border around QR code""" scaled_inside = self.inside_border * self.SCALE scaled_outside = self.outside_border * self.SCALE scaled_width = self.width * self.SCALE horizontal_line = 'M {x0} {y0} h {length} v {width} h -{length} z' vertical_line = 'M {x0} {y0} v {length} h {width} v -{length} z' subpaths = list() # top line subpaths.append( horizontal_line.format(x0=scaled_outside, y0=scaled_outside, length=scaled_inside * 2 + scaled_width + 2 * self.line_size, width=self.line_size)) # bottom line - first segment subpaths.append( horizontal_line.format(x0=scaled_outside, y0=scaled_outside + self.line_size + 2 * scaled_inside + scaled_width, length=self.SCALE * 2, width=self.line_size)) # bottom line - second segment subpaths.append( horizontal_line.format( x0=scaled_outside + 22 * self.SCALE, y0=scaled_outside + self.line_size + 2 * scaled_inside + scaled_width, length=scaled_width + 2 * scaled_inside + 2 * self.line_size - 22 * self.SCALE, # 22 = 2 + 2 + 16 + 2 width=self.line_size)) # left line subpaths.append( vertical_line.format(x0=scaled_outside, y0=scaled_outside + self.line_size, length=scaled_width + 2 * scaled_inside, width=self.line_size)) # right line subpaths.append( vertical_line.format(x0=scaled_outside + self.line_size + 2 * scaled_inside + scaled_width, y0=scaled_outside + self.line_size, length=scaled_width + 2 * scaled_inside, width=self.line_size)) return ET.Element(ET.QName("path"), style=self.QR_PATH_STYLE, d=' '.join(subpaths), id="qrplatba-border")
def _create_gpx_element(tracks): gpx = etree.Element('gpx') gpx.set('version', '1.1') gpx.set("creator", 'https://github.com/tnachstedt/sq100') gpx.set(etree.QName(xsi_ns, "schemaLocation"), "%s %s %s %s" % (gpx_ns, gpx_ns_def, tpex_ns, tpex_ns_def)) gpx.append(_create_metadata_element(bounds=calc_tracks_bounds(tracks))) for i, track in enumerate(tracks): gpx.append(_create_track_element(track=track, number=i)) return gpx
def __init__(self, input_file_path, output_directory): self.input_file_path = input_file_path self.output_file_path = self.__get_out_invoice_file_path(input_file_path, output_directory) self.dom = ElementTree.parse(self.input_file_path) self.root = self.dom.getroot() self.total_sum = 0 [ElementTree.register_namespace(key, val) for key, val in NAMESPACE_MAPPING.items()] for tag_name, tag_prefix in TAG_MAPPING.items(): tag_qualified_name = ElementTree.QName(NAMESPACE_MAPPING[tag_prefix], tag_name) setattr(self, tag_name.lower(), tag_qualified_name)
def parameters_to_element(self, options=None, arguments=None): """Generate an ET.Element representing the view and its parameters. """ root = ET.Element(ET.QName(config.data.namespace, 'adhoc'), id=self.view_id) if options: for n, v in options.iteritems(): ET.SubElement(root, ET.QName(config.data.namespace, 'option'), name=n, value=urllib.quote(unicode(v).encode('utf8'))) if arguments: for n, v in arguments: ET.SubElement(root, ET.QName(config.data.namespace, 'argument'), name=n, value=urllib.quote(unicode(v).encode('utf8'))) return root
def map_source(src): if not len(src) or src[0].tag != ET.QName(BC, 'index'): # insert artificial hierarchy syms = src[:] del src[:] idx = ET.SubElement(src, ET.QName(BC, 'index'), num='0') idx[:] = syms exc = src.get(str(ET.QName(TS, 'exception'))) if exc is not None: idx.set(str(ET.QName(TS, 'exception')), exc) return { '0': idx } elif len(src): assert src[0].tag != ET.QName(BC, 'symbol'), \ 'invalid source element: ' + \ 'expecting "index" or "symbol", got "%s"' % fixtag(src[0]) srcmap = { } for idx in src: srcmap[idx.get('num')] = idx return srcmap
def add_file_type(xml, appid, name, suffix, parameters='"%1"'): app = _get_app(xml, appid) e = find_or_add(app, "m:Extensions") e = find_or_add(e, "uap3:Extension", ("Category", "windows.fileTypeAssociation")) e = find_or_add(e, "uap3:FileTypeAssociation", ("Name", name)) e.set("Parameters", parameters) e = find_or_add(e, "uap:SupportedFileTypes") if isinstance(suffix, str): suffix = [suffix] for s in suffix: ET.SubElement(e, ET.QName(APPXMANIFEST_NS["uap"], "FileType")).text = s
def _create_bounds_element( value, ns=gpx_ns, tag='bounds', ): elem = etree.Element(etree.QName(ns, tag)) elem.set("minlat", str(value.min.latitude)) elem.set("minlon", str(value.min.longitude)) elem.set("maxlat", str(value.max.latitude)) elem.set("maxlon", str(value.max.longitude)) return elem
def build_soap_call(method, arguments, encoding=NS_SOAP_ENC, envelope_attrib=None, typed=None, header_args=None): """ Builds a soap call. @param method: method for the soap call. If set to None, the method element will be omitted and arguments will be added directly to the body (error message) @param arguments: arguments for the call @param encoding: encoding for the call @param envelope_attrib: envelope attribute @param typed: True if typed @param header_args: header arguments if any. It should be dict of dict @type method: string or None @type arguments: dict or ElementTree.Element @type encoding: string @type envelope_attrib: list @type typed: boolean or None @type header_args: dict or ElementTree.Element or None @return: soap call @rtype: string """ envelope = ElementTree.Element("s:Envelope") if envelope_attrib: for n in envelope_attrib: envelope.attrib.update({n[0]: n[1]}) else: envelope.attrib.update({'s:encodingStyle': NS_SOAP_ENC}) envelope.attrib.update({'xmlns:s': NS_SOAP_ENV}) qname_ns = NS_XSD if header_args and isinstance(header_args, dict) and len(header_args) > 0: header = ElementTree.SubElement(envelope, "s:Header") if encoding: header.set("{%s}encodingStyle" % NS_SOAP_ENV, encoding) try: for header_name, header_args_dict in header_args.iteritems(): header_name_elem = ElementTree.SubElement(header, header_name) if isinstance(header_args_dict, dict): for header_arg_name, header_arg_val in header_args_dict.iteritems(): header_arg_type = map_upnp_type(header_arg_val) header_arg_val = map_upnp_value(header_arg_val) he = ElementTree.SubElement(header_name_elem, header_arg_name) if typed and arg_type: if not isinstance(type, ElementTree.QName): header_arg_type = ElementTree.QName(qname_ns, header_arg_type) he.set('{%s}type' % NS_XSI, header_arg_type) he.text = header_arg_val except Exception, e: log.error("Ignoring soap header due to malformed header_args dict") print str(e)