def save_xml(img_name, cat_list, pointsList, save_dir, width, height, channel): has_objects = False node_root = Element('annotation') node_folder = SubElement(node_root, 'folder') node_folder.text = 'JPEGImages' node_filename = SubElement(node_root, 'filename') node_filename.text = img_name node_size = SubElement(node_root, 'size') node_width = SubElement(node_size, 'width') node_width.text = '%s' % width node_height = SubElement(node_size, 'height') node_height.text = '%s' % height node_depth = SubElement(node_size, 'depth') node_depth.text = '%s' % channel count = 0 for points in pointsList: coef = points[9:] bbox_center_x, bbox_center_y = points[3], points[4] bbox_w, bbox_h = points[5], points[6] bbox_xmin, bbox_ymin = bbox_center_x - bbox_w / 2.0, bbox_center_y - bbox_h / 2.0 bbox_xmax, bbox_ymax = bbox_center_x + bbox_w / 2.0, bbox_center_y + bbox_h / 2.0 coef_center_x, coef_center_y = points[7], points[8] coef = points[9:] coef_str = str(points[9:]) coef_str = coef_str[1:-1] node_object = SubElement(node_root, 'object') node_name = SubElement(node_object, 'name') node_name.text = labels[cat_list[count] - 1] node_difficult = SubElement(node_object, 'difficult') node_difficult.text = '0' node_bndbox = SubElement(node_object, 'bndbox') node_xmin = SubElement(node_bndbox, 'xmin') node_xmin.text = '%s' % bbox_xmin node_ymin = SubElement(node_bndbox, 'ymin') node_ymin.text = '%s' % bbox_ymin node_xmax = SubElement(node_bndbox, 'xmax') node_xmax.text = '%s' % bbox_xmax node_ymax = SubElement(node_bndbox, 'ymax') node_ymax.text = '%s' % bbox_ymax node_coef_center_x = SubElement(node_bndbox, 'coef_center_x') node_coef_center_x.text = '%s' % coef_center_x node_coef_center_y = SubElement(node_bndbox, 'coef_center_y') node_coef_center_y.text = '%s' % coef_center_y node_polygon = SubElement(node_object, 'coef') node_polygon.text = '%s' % coef_str count += 1 has_objects = True xml = tostring(node_root, pretty_print=True) dom = parseString(xml) if has_objects: with open(os.path.join(root, 'coef_8_success.txt'), 'a') as f: f.write(img_name + '\n') save_xml = os.path.join(save_dir, img_name + '.xml') with open(save_xml, 'wb') as f: f.write(xml)
def _format_docdata_doc_id(self, article, docdata): SubElement(docdata, 'doc-id', attrib={'id-string': article.get('guid', '')})
def _format_subjects(self, article, tobject): for subject in article.get('subject', []): SubElement(tobject, 'tobject.subject', {'tobject.subject.refnum': subject.get('qcode', '')})
def _build_sig(self, sig_root, reference_uris, c14n_inputs): signed_info = SubElement(sig_root, ds_tag("SignedInfo"), nsmap=self.namespaces) SubElement(signed_info, ds_tag("CanonicalizationMethod"), Algorithm=self.c14n_alg) if self.sign_alg.startswith("hmac-"): algorithm_id = self.known_hmac_digest_tags[self.sign_alg] else: algorithm_id = self.known_signature_digest_tags[self.sign_alg] SubElement(signed_info, ds_tag("SignatureMethod"), Algorithm=algorithm_id) for i, reference_uri in enumerate(reference_uris): reference = SubElement(signed_info, ds_tag("Reference"), URI=reference_uri) transforms = SubElement(reference, ds_tag("Transforms")) if self.method == methods.enveloped: SubElement(transforms, ds_tag("Transform"), Algorithm=namespaces.ds + "enveloped-signature") SubElement(transforms, ds_tag("Transform"), Algorithm=self.c14n_alg) else: SubElement(transforms, ds_tag("Transform"), Algorithm=self.c14n_alg) SubElement(reference, ds_tag("DigestMethod"), Algorithm=self.known_digest_tags[self.digest_alg]) digest_value = SubElement(reference, ds_tag("DigestValue")) payload_c14n = self._c14n(c14n_inputs[i], algorithm=self.c14n_alg) digest = self._get_digest( payload_c14n, self._get_digest_method_by_tag(self.digest_alg)) digest_value.text = digest signature_value = SubElement(sig_root, ds_tag("SignatureValue")) return signed_info, signature_value
def _format_tobject(self, article, head): return SubElement(head, 'tobject', {'tobject.type': 'news'})
def _format_media(self, article, main_news_component, media_type): """ Create an NewsComponent element related to media :param dict article: :param Element main_news_component: """ media_news_component = SubElement(main_news_component, "NewsComponent") SubElement(media_news_component, 'Role', {'FormalName': media_type}) for rendition, value in article.get('renditions').items(): news_component = SubElement(media_news_component, "NewsComponent") SubElement(news_component, 'Role', {'FormalName': rendition}) content_item = SubElement(news_component, "ContentItem", attrib={'Href': value.get('href', '')}) SubElement(content_item, 'MediaType', {'FormalName': media_type}) SubElement(content_item, 'Format', {'FormalName': value.get('mimetype', '')}) characteristics = SubElement(content_item, 'Characteristics') if rendition == 'original' and get_filemeta(article, 'length'): SubElement(characteristics, 'SizeInBytes').text = str(get_filemeta(article, 'length')) if article.get(ITEM_TYPE) == CONTENT_TYPE.PICTURE: if value.get('width'): SubElement(characteristics, 'Property', attrib={'FormalName': 'Width', 'Value': str(value.get('width'))}) if value.get('height'): SubElement(characteristics, 'Property', attrib={'FormalName': 'Height', 'Value': str(value.get('height'))}) elif article.get(ITEM_TYPE) in {CONTENT_TYPE.VIDEO, CONTENT_TYPE.AUDIO}: if get_filemeta(article, 'width'): SubElement(characteristics, 'Property', attrib={'FormalName': 'Width', 'Value': str(get_filemeta(article, 'width'))}) if get_filemeta(article, 'height'): SubElement(characteristics, 'Property', attrib={'FormalName': 'Height', 'Value': str(get_filemeta(article, 'height'))}) duration = self._get_total_duration(get_filemeta(article, 'duration')) if duration: SubElement(characteristics, 'Property', attrib={'FormalName': 'TotalDuration', 'Value': str(duration)})
def sign(self, method=methods.enveloped, algorithm="rsa-sha256", key=None, passphrase=None, cert=None, c14n_algorithm=default_c14n_algorithm, reference_uri=None): """ Sign the data and return the root element of the resulting XML tree. :param method: ``signxml.methods.enveloped``, ``signxml.methods.enveloping``, or ``signxml.methods.detached``. See the list of signature types under `XML Signature Syntax and Processing Version 2.0, Definitions <http://www.w3.org/TR/xmldsig-core2/#sec-Definitions>`_. :type method: :py:class:`methods` :param algorithm: Algorithm that will be used to generate the signature, composed of the signature algorithm and the digest algorithm, separated by a hyphen. All algorthm IDs listed under the `Algorithm Identifiers and Implementation Requirements <http://www.w3.org/TR/xmldsig-core1/#sec-AlgID>`_ section of the XML Signature 1.1 standard are supported. :type algorithm: string :param key: Key to be used for signing. When signing with a certificate or RSA/DSA/ECDSA key, this can be a string containing a PEM-formatted key, or a :py:class:`cryptography.hazmat.primitives.interfaces.RSAPublicKey`, :py:class:`cryptography.hazmat.primitives.interfaces.DSAPublicKey`, or :py:class:`cryptography.hazmat.primitives.interfaces.EllipticCurvePublicKey` object. When signing with a HMAC, this should be a string containing the shared secret. :type key: string, :py:class:`cryptography.hazmat.primitives.interfaces.RSAPublicKey`, :py:class:`cryptography.hazmat.primitives.interfaces.DSAPublicKey`, or :py:class:`cryptography.hazmat.primitives.interfaces.EllipticCurvePublicKey` object :param passphrase: Passphrase to use to decrypt the key, if any. :type passphrase: string :param cert: X.509 certificate to use for signing. This should be a string containing a PEM-formatted certificate, or an array of strings or OpenSSL.crypto.X509 objects containing the certificate and a chain of intermediate certificates. :type cert: string, array of strings, or array of OpenSSL.crypto.X509 objects :param c14n_algorithm: Canonicalization (c14n) algorithm to use. Supported algorithms are listed in the class variable ``xmldsig.known_c14n_algorithms``. :type c14n_algorithm: string :param reference_uri: Custom reference URI to incorporate into the signature. Only used when ``method`` is set to ``detached``. :type reference_uri: string :returns: A :py:class:`lxml.etree.Element` object representing the root of the XML tree containing the signature and the payload data. To specify the location of an enveloped signature within **data**, insert a `<Signature Id="placeholder"></Signature>` element in **data**. This element will be replaced by the generated signature, and excised when generating the digest. """ self.signature_alg = algorithm self.key = key self._reference_uri = reference_uri if not isinstance(method, methods): raise InvalidInput("Unknown signature method {}".format(method)) if isinstance(cert, (str, bytes)): cert_chain = [cert] else: cert_chain = cert self.payload_c14n = self._get_payload_c14n(method, c14n_algorithm=c14n_algorithm) self.digest = self._get_digest(self.payload_c14n, self._get_digest_method_by_tag(self.digest_alg)) signed_info = SubElement(self.sig_root, ds_tag("SignedInfo"), nsmap=self.namespaces) c14n_method = SubElement(signed_info, ds_tag("CanonicalizationMethod"), Algorithm=c14n_algorithm) if self.signature_alg.startswith("hmac-"): algorithm_id = self.known_hmac_digest_tags[self.signature_alg] else: algorithm_id = self.known_signature_digest_tags[self.signature_alg] signature_method = SubElement(signed_info, ds_tag("SignatureMethod"), Algorithm=algorithm_id) reference = SubElement(signed_info, ds_tag("Reference"), URI=self._reference_uri) if method == methods.enveloped: transforms = SubElement(reference, ds_tag("Transforms")) SubElement(transforms, ds_tag("Transform"), Algorithm=XMLDSIG_NS + "enveloped-signature") SubElement(transforms, ds_tag("Transform"), Algorithm=c14n_algorithm) digest_method = SubElement(reference, ds_tag("DigestMethod"), Algorithm=self.known_digest_tags[self.digest_alg]) digest_value = SubElement(reference, ds_tag("DigestValue")) digest_value.text = self.digest signature_value = SubElement(self.sig_root, ds_tag("SignatureValue")) signed_info_c14n = self._c14n(signed_info, algorithm=c14n_algorithm) if self.signature_alg.startswith("hmac-"): from cryptography.hazmat.primitives.hmac import HMAC signer = HMAC(key=self.key, algorithm=self._get_hmac_digest_method_by_tag(self.signature_alg), backend=default_backend()) signer.update(signed_info_c14n) signature_value.text = ensure_str(b64encode(signer.finalize())) self.sig_root.append(signature_value) elif self.signature_alg.startswith("dsa-") or self.signature_alg.startswith("rsa-") or self.signature_alg.startswith("ecdsa-"): if isinstance(self.key, (str, bytes)): from cryptography.hazmat.primitives.serialization import load_pem_private_key key = load_pem_private_key(self.key, password=passphrase, backend=default_backend()) else: key = self.key hash_alg = self._get_signature_digest_method_by_tag(self.signature_alg) if self.signature_alg.startswith("dsa-"): signer = key.signer(signature_algorithm=hash_alg) elif self.signature_alg.startswith("ecdsa-"): signer = key.signer(signature_algorithm=ec.ECDSA(algorithm=hash_alg)) elif self.signature_alg.startswith("rsa-"): signer = key.signer(padding=PKCS1v15(), algorithm=hash_alg) else: raise NotImplementedError() signer.update(signed_info_c14n) signature = signer.finalize() if self.signature_alg.startswith("dsa-"): # Note: The output of the DSA signer is a DER-encoded ASN.1 sequence of two DER integers. decoded_signature = der_decoder.decode(signature)[0] r = decoded_signature.getComponentByPosition(0) s = decoded_signature.getComponentByPosition(1) signature = long_to_bytes(r).rjust(32, b"\0") + long_to_bytes(s).rjust(32, b"\0") signature_value.text = ensure_str(b64encode(signature)) key_info = SubElement(self.sig_root, ds_tag("KeyInfo")) if cert_chain is None: self._serialize_key_value(key, key_info) else: x509_data = SubElement(key_info, ds_tag("X509Data")) for cert in cert_chain: x509_certificate = SubElement(x509_data, ds_tag("X509Certificate")) if isinstance(cert, (str, bytes)): x509_certificate.text = strip_pem_header(cert) else: from OpenSSL.crypto import dump_certificate, FILETYPE_PEM x509_certificate.text = dump_certificate(FILETYPE_PEM, cert) else: raise NotImplementedError() if method == methods.enveloped: return self.payload elif method == methods.enveloping: self.sig_root.append(self.payload) return self.sig_root elif method == methods.detached: return self.sig_root
def add_bindings_for_methods(self, service, root, service_name, cb_binding): pref_tns = self.interface.get_namespace_prefix(self.interface.get_tns()) input_binding_ns = ns.get_binding_ns(self.interface.app.in_protocol.type) output_binding_ns = ns.get_binding_ns(self.interface.app.out_protocol.type) def inner(method, binding): operation = etree.Element(WSDL11("operation")) operation.set('name', method.operation_name) soap_operation = SubElement(operation, input_binding_ns("operation")) soap_operation.set('soapAction', method.operation_name) soap_operation.set('style', 'document') # get input input = SubElement(operation, WSDL11("input")) input.set('name', method.in_message.get_element_name()) soap_body = SubElement(input, input_binding_ns("body")) soap_body.set('use', 'literal') # get input soap header in_header = method.in_header if in_header is None: in_header = service.__in_header__ if not (in_header is None): if isinstance(in_header, (list, tuple)): in_headers = in_header else: in_headers = (in_header,) if len(in_headers) > 1: in_header_message_name = ''.join((method.name, _in_header_msg_suffix)) else: in_header_message_name = in_headers[0].get_type_name() for header in in_headers: soap_header = SubElement(input, input_binding_ns('header')) soap_header.set('use', 'literal') soap_header.set('message', '%s:%s' % ( header.get_namespace_prefix(self.interface), in_header_message_name)) soap_header.set('part', header.get_type_name()) if not (method.is_async or method.is_callback): output = SubElement(operation, WSDL11("output")) output.set('name', method.out_message.get_element_name()) soap_body = SubElement(output, output_binding_ns("body")) soap_body.set('use', 'literal') # get output soap header out_header = method.out_header if out_header is None: out_header = service.__out_header__ if not (out_header is None): if isinstance(out_header, (list, tuple)): out_headers = out_header else: out_headers = (out_header,) if len(out_headers) > 1: out_header_message_name = ''.join((method.name, _out_header_msg_suffix)) else: out_header_message_name = out_headers[0].get_type_name() for header in out_headers: soap_header = SubElement(output, output_binding_ns("header")) soap_header.set('use', 'literal') soap_header.set('message', '%s:%s' % ( header.get_namespace_prefix(self.interface), out_header_message_name)) soap_header.set('part', header.get_type_name()) if not (method.faults is None): for f in method.faults: wsdl_fault = SubElement(operation, WSDL11("fault")) wsdl_fault.set('name', f.get_type_name()) soap_fault = SubElement(wsdl_fault, input_binding_ns("fault")) soap_fault.set('name', f.get_type_name()) soap_fault.set('use', 'literal') if method.is_callback: relates_to = SubElement(input, input_binding_ns("header")) relates_to.set('message', '%s:RelatesToHeader' % pref_tns) relates_to.set('part', 'RelatesTo') relates_to.set('use', 'literal') cb_binding.append(operation) else: if method.is_async: rt_header = SubElement(input, input_binding_ns("header")) rt_header.set('message', '%s:ReplyToHeader' % pref_tns) rt_header.set('part', 'ReplyTo') rt_header.set('use', 'literal') mid_header = SubElement(input, input_binding_ns("header")) mid_header.set('message', '%s:MessageIDHeader' % pref_tns) mid_header.set('part', 'MessageID') mid_header.set('use', 'literal') binding.append(operation) port_type_list = service.get_port_types() if len(port_type_list) > 0: for port_type_name in port_type_list: # create binding nodes binding = SubElement(root, WSDL11("binding")) binding.set('name', self._get_binding_name(port_type_name)) binding.set('type', '%s:%s'% (pref_tns, port_type_name)) transport = SubElement(binding, input_binding_ns("binding")) transport.set('style', 'document') transport.set('transport', self.interface.app.transport) for m in service.public_methods.values(): if m.port_type == port_type_name: inner(m, binding) else: # here is the default port. if cb_binding is None: cb_binding = SubElement(root, WSDL11("binding")) cb_binding.set('name', service_name) cb_binding.set('type', '%s:%s'% (pref_tns, service_name)) transport = SubElement(cb_binding, input_binding_ns("binding")) transport.set('style', 'document') transport.set('transport', self.interface.app.transport) for m in service.public_methods.values(): inner(m, cb_binding) return cb_binding
def inner(method, binding): operation = etree.Element(WSDL11("operation")) operation.set('name', method.operation_name) soap_operation = SubElement(operation, input_binding_ns("operation")) soap_operation.set('soapAction', method.operation_name) soap_operation.set('style', 'document') # get input input = SubElement(operation, WSDL11("input")) input.set('name', method.in_message.get_element_name()) soap_body = SubElement(input, input_binding_ns("body")) soap_body.set('use', 'literal') # get input soap header in_header = method.in_header if in_header is None: in_header = service.__in_header__ if not (in_header is None): if isinstance(in_header, (list, tuple)): in_headers = in_header else: in_headers = (in_header,) if len(in_headers) > 1: in_header_message_name = ''.join((method.name, _in_header_msg_suffix)) else: in_header_message_name = in_headers[0].get_type_name() for header in in_headers: soap_header = SubElement(input, input_binding_ns('header')) soap_header.set('use', 'literal') soap_header.set('message', '%s:%s' % ( header.get_namespace_prefix(self.interface), in_header_message_name)) soap_header.set('part', header.get_type_name()) if not (method.is_async or method.is_callback): output = SubElement(operation, WSDL11("output")) output.set('name', method.out_message.get_element_name()) soap_body = SubElement(output, output_binding_ns("body")) soap_body.set('use', 'literal') # get output soap header out_header = method.out_header if out_header is None: out_header = service.__out_header__ if not (out_header is None): if isinstance(out_header, (list, tuple)): out_headers = out_header else: out_headers = (out_header,) if len(out_headers) > 1: out_header_message_name = ''.join((method.name, _out_header_msg_suffix)) else: out_header_message_name = out_headers[0].get_type_name() for header in out_headers: soap_header = SubElement(output, output_binding_ns("header")) soap_header.set('use', 'literal') soap_header.set('message', '%s:%s' % ( header.get_namespace_prefix(self.interface), out_header_message_name)) soap_header.set('part', header.get_type_name()) if not (method.faults is None): for f in method.faults: wsdl_fault = SubElement(operation, WSDL11("fault")) wsdl_fault.set('name', f.get_type_name()) soap_fault = SubElement(wsdl_fault, input_binding_ns("fault")) soap_fault.set('name', f.get_type_name()) soap_fault.set('use', 'literal') if method.is_callback: relates_to = SubElement(input, input_binding_ns("header")) relates_to.set('message', '%s:RelatesToHeader' % pref_tns) relates_to.set('part', 'RelatesTo') relates_to.set('use', 'literal') cb_binding.append(operation) else: if method.is_async: rt_header = SubElement(input, input_binding_ns("header")) rt_header.set('message', '%s:ReplyToHeader' % pref_tns) rt_header.set('part', 'ReplyTo') rt_header.set('use', 'literal') mid_header = SubElement(input, input_binding_ns("header")) mid_header.set('message', '%s:MessageIDHeader' % pref_tns) mid_header.set('part', 'MessageID') mid_header.set('use', 'literal') binding.append(operation)
def write_building(lod, name, geometry_list, bldg_class=None, function=None, usage=None, yr_constr=None, rooftype=None, height=None, stry_abv_grd=None, stry_blw_grd=None, generic_attrib_dict=None): """ This function writes the building cityobject member. Currently, only works for lod1 building. Parameters ---------- lod : str The level of detail of the geometry of the building. The string should be in this form: "lod1". name : str The name of the building. geometry_list : list of SurfaceMember The geometry of the building. bldg_class : str, optional The building class of the building in gml code e.g. for habitation specify "1000", Default = None. Refer to CityGML documentation for more information. https://www.citygml.org/ function : str, optional The function of the building in gml code e.g. for residential specify "1000", Default = None. Refer to CityGML documentation for more information. https://www.citygml.org/ usage : str, optional The usage of the building in gml code e.g. for residential specify "1000", Default = None. Refer to CityGML documentation for more information. https://www.citygml.org/ yr_constr : str, optional The year the building is constructed e.g. "2017", Default = None. rooftype : str, optional The rooftype of the building in gml code e.g. for flat roof specify "1000", Default = None. Refer to CityGML documentation for more information. https://www.citygml.org/ height : str, optional The height of the building, e.g. "48", Default = None. stry_abv_grd : str, optional The number of storey of the building above ground, e.g. "12", Default = None. stry_blw_grd : str, optional The number of storey of the building below ground, e.g. "2", Default = None. generic_attrib_dict : dictionary, optional Extra attributes to be appended to the object, Default = None. The dictionary must have a string key which will be the name of the generic attributes, and either an int, float or str value. """ cityObjectMember = Element('cityObjectMember') bldg_Building = SubElement(cityObjectMember, "{" + XMLNamespaces.bldg + "}" + "Building") bldg_Building.attrib["{" + XMLNamespaces.gml + "}" + 'id'] = name #======================================================================================================= #attrib #======================================================================================================= if bldg_class != None: b_class = SubElement(bldg_Building, "{" + XMLNamespaces.bldg + "}" + 'class') b_class.text = bldg_class if function != None: bldg_function = SubElement(bldg_Building, "{" + XMLNamespaces.bldg + "}" + 'function') bldg_function.text = function if usage != None: bldg_usage = SubElement(bldg_Building, "{" + XMLNamespaces.bldg + "}" + 'usage') bldg_usage.text = usage if yr_constr != None: bldg_yearOfConstruction = SubElement( bldg_Building, "{" + XMLNamespaces.bldg + "}" + 'yearOfConstruction') bldg_yearOfConstruction.text = yr_constr if rooftype != None: bldg_roofType = SubElement(bldg_Building, "{" + XMLNamespaces.bldg + "}" + 'roofType') bldg_roofType.text = rooftype if height != None: bldg_measuredHeight = SubElement( bldg_Building, "{" + XMLNamespaces.bldg + "}" + 'measuredHeight') bldg_measuredHeight.attrib['uom'] = "m" bldg_measuredHeight.text = height if stry_abv_grd != None: bldg_storeysAboveGround = SubElement( bldg_Building, "{" + XMLNamespaces.bldg + "}" + 'storeysAboveGround') bldg_storeysAboveGround.text = stry_abv_grd if stry_blw_grd != None: bldg_storeysBelowGround = SubElement( bldg_Building, "{" + XMLNamespaces.bldg + "}" + 'storeysBelowGround') bldg_storeysBelowGround.text = stry_blw_grd if generic_attrib_dict != None: write_gen_attribute(bldg_Building, generic_attrib_dict) #======================================================================================================= #geometries #======================================================================================================= if lod == "lod1": bldg_lod1Solid = SubElement( bldg_Building, "{" + XMLNamespaces.bldg + "}" + 'lod1Solid') gml_Solid = SubElement(bldg_lod1Solid, "{" + XMLNamespaces.gml + "}" + 'Solid') gml_exterior = SubElement(gml_Solid, "{" + XMLNamespaces.gml + "}" + 'exterior') gml_CompositeSurface = SubElement( gml_exterior, "{" + XMLNamespaces.gml + "}" + 'CompositeSurface') for geometry in geometry_list: gml_CompositeSurface.append(geometry.construct()) return cityObjectMember
def write_cityfurniture(lod, name, geometry_list, furn_class=None, function=None, generic_attrib_dict=None): """ This function writes the city furniture cityobjectmemeber. Currently, only works for lod1. Parameters ---------- lod : str The level of detail of the geometry of the furniture. The string should be in this form: "lod1". name : str The name of the furniture. geometry_list : list of SurfaceMember The geometry of the furniture. furn_class : str, optional The furniture class of the city furniture in gml code, for traffic specify "1000", Default = None. Refer to CityGML documentation for more information. https://www.citygml.org/ function : str, optional The function of the city furniture in gml code, for bus stop specify "1110",Default = None. Refer to CityGML documentation for more information. https://www.citygml.org/ generic_attrib_dict : dictionary, optional Extra attributes to be appended to the object, Default = None. The dictionary must have a string key which will be the name of the generic attributes, and either an int, float or str value. """ cityObjectMember = Element('cityObjectMember') frn_CityFurniture = SubElement( cityObjectMember, "{" + XMLNamespaces.frn + "}" + 'CityFurniture') frn_CityFurniture.attrib["{" + XMLNamespaces.gml + "}" + 'id'] = name creationDate = SubElement(frn_CityFurniture, 'creationDate') creationDate.text = str(datetime.datetime.now()) #======================================================================================================= #attrib #======================================================================================================= if furn_class != None: frn_class = SubElement(frn_CityFurniture, "{" + XMLNamespaces.frn + "}" + 'class') frn_class.text = furn_class if function != None: frn_function = SubElement(frn_CityFurniture, "{" + XMLNamespaces.frn + "}" + 'function') frn_function.text = function if generic_attrib_dict != None: write_gen_attribute(frn_CityFurniture, generic_attrib_dict) #======================================================================================================= #geometries #======================================================================================================= if lod == "lod1": lod1Geometry = SubElement( frn_CityFurniture, "{" + XMLNamespaces.frn + "}" + 'lod1Geometry') gml_Solid = SubElement(lod1Geometry, "{" + XMLNamespaces.gml + "}" + 'Solid') gml_exterior = SubElement(gml_Solid, "{" + XMLNamespaces.gml + "}" + 'exterior') gml_CompositeSurface = SubElement( gml_exterior, "{" + XMLNamespaces.gml + "}" + 'CompositeSurface') for geometry in geometry_list: gml_CompositeSurface.append(geometry.construct()) ''' #================================================================== #reference geometries script TODO: Make it work #================================================================== frn_lod1ImplicitRepresentation = SubElement(frn_CityFurniture,"{" + XMLNamespaces.frn+ "}" + 'lod1ImplicitRepresentation') ImplicitGeometry = SubElement(frn_lod1ImplicitRepresentation,'ImplicitGeometry') mimeType = SubElement(ImplicitGeometry,'mimeType') mimeType.text = geometry_type libraryObject = SubElement(ImplicitGeometry,'libraryObject') libraryObject.text = ref_geometry referencePoint = SubElement(ImplicitGeometry,'referencePoint') referencePoint.append(ref_pt.construct()) ''' return cityObjectMember
def write_transportation(trpt_type, lod, name, geometry_list, rd_class=None, function=None, generic_attrib_dict=None): """ This function writes the transportation cityobject member. Transportation object includes road, railway, track and square. Currently only works for lod0 transportation network. Parameters ---------- trpt_type : str The transportation type. The options are: "Road", "Railway", "Track", "Square". lod : str The level of detail of the geometry of the transportation. The string should be in this form: "lod0". name : str The name of the transportation object. geometry_list : list of Linestring The geometry of the landuse. rd_class : str, optional The class of the transportation gml code, e.g. for road specify "1000", Default = None. Refer to CityGML documentation for more information. https://www.citygml.org/ function : str, optional The function of the transportation gml code, e.g. for road specify "1000", Default = None. Refer to CityGML documentation for more information. https://www.citygml.org/ generic_attrib_dict : dictionary, optional Extra attributes to be appended to the object, Default = None. The dictionary must have a string key which will be the name of the generic attributes, and either an int, float or str value. """ cityObjectMember = Element('cityObjectMember') tran_trpt_type = SubElement(cityObjectMember, "{" + XMLNamespaces.trans + "}" + trpt_type) tran_trpt_type.attrib["{" + XMLNamespaces.gml + "}" + 'id'] = name gml_name = SubElement(tran_trpt_type, "{" + XMLNamespaces.gml + "}" + 'name') gml_name.text = name #======================================================================================================= #attrib #======================================================================================================= if rd_class != None: tran_class = SubElement(tran_trpt_type, "{" + XMLNamespaces.trans + "}" + 'class') tran_class.text = rd_class if function != None: tran_function = SubElement( tran_trpt_type, "{" + XMLNamespaces.trans + "}" + 'function') tran_function.text = function if generic_attrib_dict != None: write_gen_attribute(tran_trpt_type, generic_attrib_dict) #======================================================================================================= #geometries #======================================================================================================= if lod == "lod0": tran_lod0Network = SubElement( tran_trpt_type, "{" + XMLNamespaces.trans + "}" + 'lod0Network') gml_GeometricComplex = SubElement( tran_lod0Network, "{" + XMLNamespaces.gml + "}" + 'GeometricComplex') for geometry in geometry_list: gml_element = SubElement(gml_GeometricComplex, "{" + XMLNamespaces.gml + "}" + 'element') gml_element.append(geometry.construct()) gml_GeometricComplex.append(gml_element) return cityObjectMember
def write_landuse(lod, name, geometry_list, function=None, generic_attrib_dict=None): """ This function writes the landuse cityobject member. Currently, only works for lod1. Parameters ---------- lod : str The level of detail of the geometry of the landuse. The string should be in this form: "lod1". name : str The name of the landuse. geometry_list : list of SurfaceMember The geometry of the landuse. function : str, optional The function of the landuse in gml code, e.g. for residential specify "1010", Default = None. Refer to CityGML documentation for more information. https://www.citygml.org/ generic_attrib_dict : dictionary, optional Extra attributes to be appended to the object, Default = None. The dictionary must have a string key which will be the name of the generic attributes, and either an int, float or str value. """ cityObjectMember = Element('cityObjectMember') luse = SubElement(cityObjectMember, "{" + XMLNamespaces.luse + "}" + 'LandUse') luse.attrib["{" + XMLNamespaces.gml + "}" + 'id'] = name gml_name = SubElement(luse, "{" + XMLNamespaces.gml + "}" + 'name') gml_name.text = name #======================================================================================================= #attribs #======================================================================================================= if function != None: luse_function = SubElement(luse, "{" + XMLNamespaces.luse + "}" + 'function') luse_function.text = function if generic_attrib_dict != None: write_gen_attribute(luse, generic_attrib_dict) #======================================================================================================= #geometries #======================================================================================================= if lod == "lod1": luse_lod1MultiSurface = SubElement( luse, "{" + XMLNamespaces.luse + "}" + 'lod1MultiSurface') gml_MultiSurface = SubElement( luse_lod1MultiSurface, "{" + XMLNamespaces.gml + "}" + 'MultiSurface') gml_MultiSurface.attrib["{" + XMLNamespaces.gml + "}" + 'id'] = 'UUID_' + str(uuid.uuid1()) for geometry in geometry_list: gml_MultiSurface.append(geometry.construct()) return cityObjectMember
def create_xml(name, size, annotations): node_root = Element('annotation') node_folder = SubElement(node_root, 'folder') node_folder.text = 'JPEGImages' node_filename = SubElement(node_root, 'filename') filename = name + ".jpg" node_filename.text = filename node_path = SubElement(node_root, 'path') node_path.text = '' node_size = SubElement(node_root, 'size') node_width = SubElement(node_size, 'width') node_width.text = str(size[0]) node_height = SubElement(node_size, 'height') node_height.text = str(size[1]) node_depth = SubElement(node_size, 'depth') node_depth.text = str(3) node_segmented = SubElement(node_root, 'segmented') node_segmented.text = '0' for box in annotations: check_border(box, size[0], size[1]) node_object = SubElement(node_root, 'object') node_name = SubElement(node_object, 'name') caption = labels[box[4]] node_name.text = caption node_pose = SubElement(node_object, 'pose') node_pose.text = 'Unspecified' node_truncated = SubElement(node_object, 'truncated') node_truncated.text = '0' node_difficult = SubElement(node_object, 'difficult') node_difficult.text = '0' node_bndbox = SubElement(node_object, 'bndbox') node_xmin = SubElement(node_bndbox, 'xmin') node_xmin.text = str(int(box[0])) node_ymin = SubElement(node_bndbox, 'ymin') node_ymin.text = str(int(box[1])) node_xmax = SubElement(node_bndbox, 'xmax') node_xmax.text = str(int(box[2])) node_ymax = SubElement(node_bndbox, 'ymax') node_ymax.text = str(int(box[3])) xml = tostring(node_root, pretty_print=True) dom = parseString(xml) return dom
def map_to_xml(anno_data, dist_path): assert "folder" in anno_data assert "filename" in anno_data assert "size" in anno_data assert "width" in anno_data["size"] assert "height" in anno_data["size"] assert "depth" in anno_data["size"] segmented = "0" pose = "Unspecified" truncated = "0" difficult = "0" root = Element("annotation") SubElement(root, 'folder').text = anno_data["folder"] SubElement(root, 'filename').text = anno_data["filename"] source = SubElement(root, 'source') SubElement(source, 'database').text = "The VOC2007 Database" SubElement(source, 'annotation').text = "PASCAL VOC2007" SubElement(source, 'image').text = "flickr" size = SubElement(root, 'size') SubElement(size, 'width').text = str(anno_data["size"]["width"]) SubElement(size, 'height').text = str(anno_data["size"]["height"]) SubElement(size, 'depth').text = str(anno_data["size"]["depth"]) SubElement(root, 'segmented').text = segmented for object in anno_data["objects"]: obj = SubElement(root, 'object') SubElement(obj, 'name').text = object["name"] SubElement(obj, 'pose').text = pose SubElement(obj, 'truncated').text = truncated SubElement(obj, 'difficult').text = difficult bndbox = SubElement(obj, 'bndbox') SubElement(bndbox, 'xmin').text = str(object["bndbox"]["xmin"]) SubElement(bndbox, 'ymin').text = str(object["bndbox"]["ymin"]) SubElement(bndbox, 'xmax').text = str(object["bndbox"]["xmax"]) SubElement(bndbox, 'ymax').text = str(object["bndbox"]["ymax"]) tree = ElementTree(root) tree.write(dist_path, encoding='utf-8',pretty_print=True)
def _add_callbacks(self, service, root, types, service_name, url): ns_tns = self.interface.get_tns() pref_tns = 'tns' input_binding_ns = ns.get_binding_ns(self.interface.app.in_protocol.type) cb_port_type = None # add necessary async headers # WS-Addressing -> RelatesTo ReplyTo MessageID # callback porttype if service._has_callbacks(): wsa_schema = SubElement(types, XSD("schema")) wsa_schema.set("targetNamespace", '%sCallback' % ns_tns) wsa_schema.set("elementFormDefault", "qualified") import_ = SubElement(wsa_schema, XSD("import")) import_.set("namespace", NS_WSA) import_.set("schemaLocation", NS_WSA) relt_message = SubElement(root, WSDL11("message")) relt_message.set('name', 'RelatesToHeader') relt_part = SubElement(relt_message, WSDL11("part")) relt_part.set('name', 'RelatesTo') relt_part.set('element', '%s:RelatesTo' % PREF_WSA) reply_message = SubElement(root, WSDL11("message")) reply_message.set('name', 'ReplyToHeader') reply_part = SubElement(reply_message, WSDL11("part")) reply_part.set('name', 'ReplyTo') reply_part.set('element', '%s:ReplyTo' % PREF_WSA) id_header = SubElement(root, WSDL11("message")) id_header.set('name', 'MessageIDHeader') id_part = SubElement(id_header, WSDL11("part")) id_part.set('name', 'MessageID') id_part.set('element', '%s:MessageID' % PREF_WSA) # make portTypes cb_port_type = SubElement(root, WSDL11("portType")) cb_port_type.set('name', '%sCallback' % service_name) cb_service_name = '%sCallback' % service_name cb_service = SubElement(root, WSDL11("service")) cb_service.set('name', cb_service_name) cb_wsdl_port = SubElement(cb_service, WSDL11("port")) cb_wsdl_port.set('name', cb_service_name) cb_wsdl_port.set('binding', '%s:%s' % (pref_tns, cb_service_name)) cb_address = SubElement(cb_wsdl_port, input_binding_ns("address")) cb_address.set('location', url) return cb_port_type
def _format_news_management(self, article, news_item): """ Create a NewsManagement element :param dict article: :param Element news_item: """ news_management = SubElement(news_item, "NewsManagement") SubElement(news_management, 'NewsItemType', {'FormalName': 'News'}) SubElement(news_management, 'FirstCreated').text = \ article['firstcreated'].strftime('%Y%m%dT%H%M%S+0000') SubElement(news_management, 'ThisRevisionCreated').text = \ article['versioncreated'].strftime('%Y%m%dT%H%M%S+0000') if article.get(EMBARGO): SubElement(news_management, 'Status', {'FormalName': 'Embargoed'}) status_will_change = SubElement(news_management, 'StatusWillChange') SubElement(status_will_change, 'FutureStatus', {'FormalName': article['pubstatus']}) SubElement(status_will_change, 'DateAndTime').text = \ get_utc_schedule(article, EMBARGO).isoformat() else: SubElement(news_management, 'Status', {'FormalName': article['pubstatus']}) if article.get('urgency'): SubElement(news_management, 'Urgency', {'FormalName': str(article['urgency'])}) if article['state'] == 'corrected': SubElement(news_management, 'Instruction', {'FormalName': 'Correction'}) else: SubElement(news_management, 'Instruction', {'FormalName': 'Update'})
def transform_html(input_html_file, template_html_file, sitting_date_iso, output_folder=''): # template tree output_tree = html.parse(template_html_file) output_root = output_tree.getroot() # input html (from InDesign) input_tree = html.parse(input_html_file) # InDesign outputs xhtml5 we can convert to html5 # html.xhtml_to_html(input_tree) input_root = input_tree.getroot() # expecting the date to be marked up like the below # <div id="_idContainer000"> # <h1 class="Title" lang="en-US"><strong class="Bold">Issued on:</strong> 21 April at 7.00pm</h1> # </div> issued_date = input_root.xpath('//div[@id="_idContainer000"]/h1') if len(issued_date) > 0: issued_date = issued_date[0] if iselement(issued_date): issued_date_text = issued_date.text_content() issued_date.getparent().remove(issued_date) else: issued_date_text = '' warning( 'Expected to find issued date in the input HTML file. The issued at time will be missing from the bottom of the output HMTML. Check that the InDesign template has not been tampored with.' ) # should return a formatted string in the form 26th April 2017 # prepared_date = format_date(prepared_date_iso, strftime='%d %b %Y') # also get the date in the form YYMMDD for the output file name # clean up the HTML # convert bullet to square bullet for ele in input_root.xpath('.//span[@class="pythonFindBullet"]'): if ele.text == '\u2022': ele.text = '\u25A0 ' # remove filename for internal hyperlinks # e.g. href="cmsilist2.html#_idTextAnchor000" -> href="#_idTextAnchor000" # usualy filename before # iD_file_name = os.path.basename(input_html_file) # but sometimes people change the filename but the original filename is in the <title> iD_file_title = input_root.findtext('head/title', default='') if iD_file_title != '': iD_file_title = '{}.html'.format(iD_file_title) all_links = input_root.xpath('//a') for link in all_links: link_href = link.get('href', default=None) if link_href: link.set( 'href', link_href.replace(iD_file_name, '').replace(iD_file_title, '')) # put all the html from the input file into the proper place in the output file # get the location in the output_root we want to append to append_point = output_root.xpath('//div[@id="content-goes-here"]') if len(append_point) < 1: print( 'ERROR: Script can\'t find <div id="content-goes-here"> in the template.' ' This is needed as this is where we are going inject html from the input html' ) exit() else: append_point = append_point[0] # change the title to be like # <h1 class="mainTitle" id="MainTitleWithDate"> h1s = input_root.xpath('//h1') if len(h1s) < 1: print('WARNING: at least one element with a h1 tag was expected. ' 'The title may not appear in the output') else: h1 = h1s[0] h1.set('class', 'mainTitle') h1.set('id', 'MainTitleWithDate') br = h1.find('br') if iselement(br): if br.tail: br.tail = ' ' + br.tail # add space br.drop_tag() # clean up ToC by removing page numbers # for element in input_root.xpath('//p[@class="TableOfContents_Toc2"]/a'): # # remove any <br> as uterwise cant remove page numbers from the end # brs = element.findall('.//br') # for br in brs: # br.drop_tag() # span = element.find('span') # if iselement(span): # if span.text: # span.text += ' ' # if span.tail: # span.tail = span.tail.rstrip('1234567890') # elif element.text: # element.text = element.text.rstrip('1234567890') # remove Indesign toc for element in input_root.xpath('//div[contains(@class,"ToC-Box")]'): element.drop_tree() # sort the numbers (hanging indents etc) numbers = input_root.xpath('//p[@class="paraQuestion"]/span[1]') for number in numbers: # cosider changing this in InDesign number.set('class', 'charBallotNumber') new_span = html.fromstring( '<span style="display : block; float : left; width : 2.1em; height : 1em;"></span>' ) number_parent = number.getparent() new_span.append(number) number_parent.insert(0, new_span) # get the container divs in the input root that contain the html container_divs = input_root.xpath('//div[contains(@id,"_idContainer")]') for div in container_divs: # this line will put all the child elements of the container div in to the output html append_point.append(div) # change what is in the <title> element in the output title_element = output_root.xpath('//title') if len(title_element) and iselement(title_element[0]): title_element = title_element[0] date_formatted = format_date(sitting_date_iso, strftime="%A %d %B %Y") if title_element.text: title_element.text += f' for {date_formatted}' else: title_element.text += f'Call List for {date_formatted}' # sort tables out xpath = '//table[contains(@class, "Call-Sheet-Table")]' \ '|//table[contains(@class, "Basic-Table")]' for table in output_root.xpath(xpath): table.classes.update( ['table', 'table-bordered', 'table-responsive-md']) thead = table.find('thead') if iselement(thead): thead.classes.add('thead-light') # Add IDs and perminant ancors to the html # Added at the request of IDMS # need to get all the heading elements xpath = '//h3[@class="paraBusinessSub-SectionHeading"]' headings = output_root.xpath(xpath) for i, heading in enumerate(headings): # generate id text id_text = f'anchor-{i}' if heading.get('id', default=None): heading.set('name', heading.get('id')) heading.set('id', id_text) anchor = SubElement(heading, 'a') permalink_for = 'Permalink for ' + heading.text_content() anchor.set('href', '#' + id_text) anchor.set('aria-label', 'Anchor') anchor.set('title', permalink_for) anchor.set('data-anchor-icon', '§') anchor.set('class', 'anchor-link') # I also feel like removing paraBusinessSub-SectionHeading heading.classes.remove('paraBusinessSub-SectionHeading') # find where to put the Toc nav_xpath_results = output_root.xpath('//nav[@id="toc"]') # create new toc h3s = output_root.xpath('//*[contains(@class, "js-toc-content")]//h3') if len(nav_xpath_results): toc_injection_point = nav_xpath_results[0] ol = SubElement(toc_injection_point, 'ol') ol.set('class', 'toc-list') for h3 in h3s: li = SubElement(ol, 'li') li.set('class', 'toc-list-item') a = SubElement(li, 'a') a.set('href', '#' + h3.get('id', '')) a.set('class', 'toc-link') a.text = h3.text_content() # finally change the prepared date at the bottom of the page # footerblock = output_root.xpath('//div[@id="footerBlockDate"]/p') # if len(footerblock) < 1: # warning('Can\'t find the footer block to append the prepared date to.') # elif footerblock[0].text is None: # footerblock[0].text = issued_date_text # else: # footerblock[0].text = '{}{}'.format(footerblock[0].text, issued_date_text) # get the output file path input_file_base_path = os.path.dirname(input_html_file) # date in PPU forn date_ppu = format_date(sitting_date_iso, strftime='%y%m%d') output_file_name = '{}{}{}'.format(OUTPUTFILE_BASENAME, date_ppu, FILE_EXTENSION) if output_folder: output_file_path = os.path.join(output_folder, output_file_name) else: output_file_path = os.path.join(input_file_base_path, output_file_name) # output the file for writing bytes output_tree.write(output_file_path, encoding='UTF-8', method="html", xml_declaration=False) # output_file = open(output_file_path, 'wb') # output_file.write(html.tostring(output_tree)) # output_file.close() print('Output file path: ', output_file_path, '\n') return Path(output_file_path)
def produceXMl(imgname,height,width,busbars_coords): node_root = Element('annotation') node_folder = SubElement(node_root, 'folder') node_folder.text = 'PVD' node_filename = SubElement(node_root, 'filename') node_filename.text = imgname node_size = SubElement(node_root, 'size') node_width = SubElement(node_size, 'width') node_width.text = str(width) node_height = SubElement(node_size, 'height') node_height.text = str(height) node_depth = SubElement(node_size, 'depth') node_depth.text = '3' for busbars in busbars_coords: node_object = SubElement(node_root, 'object') node_name = SubElement(node_object, 'name') node_name.text = 'pedestrian' node_difficult = SubElement(node_object, 'difficult') node_difficult.text = '0' node_bndbox = SubElement(node_object, 'bndbox') node_xmin = SubElement(node_bndbox, 'xmin') node_xmin.text = str(busbars[0]) node_ymin = SubElement(node_bndbox, 'ymin') node_ymin.text = str(busbars[1]) node_xmax = SubElement(node_bndbox, 'xmax') node_xmax.text = str(busbars[2]) node_ymax = SubElement(node_bndbox, 'ymax') node_ymax.text = str(busbars[3]) xml = tostring(node_root, pretty_print=True) # 格式化显示,该换行的换行 dom = parseString(xml) # print(xml) return xml
def map_obj_to_ele(module, want): element = Element('system') login = SubElement(element, 'login') for item in want: if item['state'] != 'present': if item['name'] == 'root': module.fail_json(msg="cannot delete the 'root' account.") operation = 'delete' else: operation = 'merge' if item['name'] != 'root': user = SubElement(login, 'user', {'operation': operation}) SubElement(user, 'name').text = item['name'] else: user = auth = SubElement(element, 'root-authentication', {'operation': operation}) if operation == 'merge': if item['name'] == 'root' and (not item['active'] or item['role'] or item['full_name']): module.fail_json( msg= "'root' account cannot be deactivated or be assigned a role and a full name" ) if item['active']: user.set('active', 'active') else: user.set('inactive', 'inactive') if item['role']: SubElement(user, 'class').text = item['role'] if item.get('full_name'): SubElement(user, 'full-name').text = item['full_name'] if item.get('sshkey'): auth = SubElement(user, 'authentication') if 'ssh-rsa' in item['sshkey']: ssh_rsa = SubElement(auth, 'ssh-rsa') elif 'ssh-dss' in item['sshkey']: ssh_rsa = SubElement(auth, 'ssh-dsa') elif 'ecdsa-sha2' in item['sshkey']: ssh_rsa = SubElement(auth, 'ssh-ecdsa') elif 'ssh-ed25519' in item['sshkey']: ssh_rsa = SubElement(auth, 'ssh-ed25519') SubElement(ssh_rsa, 'name').text = item['sshkey'] if item.get('encrypted_password'): auth = SubElement(user, 'authentication') SubElement( auth, 'encrypted-password').text = item['encrypted_password'] return element
def sign(self, data, key=None, passphrase=None, cert=None, reference_uri=None, key_name=None, key_info=None, id_attribute=None, always_add_key_value=False): """ Sign the data and return the root element of the resulting XML tree. :param data: Data to sign :type data: String, file-like object, or XML ElementTree Element API compatible object :param key: Key to be used for signing. When signing with a certificate or RSA/DSA/ECDSA key, this can be a string/bytes containing a PEM-formatted key, or a :py:class:`cryptography.hazmat.primitives.interfaces.RSAPrivateKey`, :py:class:`cryptography.hazmat.primitives.interfaces.DSAPrivateKey`, or :py:class:`cryptography.hazmat.primitives.interfaces.EllipticCurvePrivateKey` object. When signing with a HMAC, this should be a string containing the shared secret. :type key: string, bytes, :py:class:`cryptography.hazmat.primitives.interfaces.RSAPrivateKey`, :py:class:`cryptography.hazmat.primitives.interfaces.DSAPrivateKey`, or :py:class:`cryptography.hazmat.primitives.interfaces.EllipticCurvePrivateKey` object :param passphrase: Passphrase to use to decrypt the key, if any. :type passphrase: string :param cert: X.509 certificate to use for signing. This should be a string containing a PEM-formatted certificate, or an array of strings or OpenSSL.crypto.X509 objects containing the certificate and a chain of intermediate certificates. :type cert: string, array of strings, or array of OpenSSL.crypto.X509 objects :param reference_uri: Custom reference URI or list of reference URIs to incorporate into the signature. When ``method`` is set to ``detached`` or ``enveloped``, reference URIs are set to this value and only the referenced elements are signed. :type reference_uri: string or list :param key_name: Add a KeyName element in the KeyInfo element that may be used by the signer to communicate a key identifier to the recipient. Typically, KeyName contains an identifier related to the key pair used to sign the message. :type key_name: string :param key_info: A custom KeyInfo element to insert in the signature. Use this to supply ``<wsse:SecurityTokenReference>`` or other custom key references. An example value can be found here: https://github.com/XML-Security/signxml/blob/master/test/wsse_keyinfo.xml :type key_info: :py:class:`lxml.etree.Element` :param id_attribute: Name of the attribute whose value ``URI`` refers to. By default, SignXML will search for "Id", then "ID". :type id_attribute: string :param always_add_key_value: Write the key value to the KeyInfo element even if a X509 certificate is present. Use of this parameter is discouraged, as it introduces an ambiguity and a security hazard. The public key used to sign the document is already encoded in the certificate (which is in X509Data), so the verifier must either ignore KeyValue or make sure it matches what's in the certificate. This parameter is provided for compatibility purposes only. :type always_add_key_value: boolean :returns: A :py:class:`lxml.etree.Element` object representing the root of the XML tree containing the signature and the payload data. To specify the location of an enveloped signature within **data**, insert a ``<ds:Signature Id="placeholder"></ds:Signature>`` element in **data** (where "ds" is the "http://www.w3.org/2000/09/xmldsig#" namespace). This element will be replaced by the generated signature, and excised when generating the digest. """ if id_attribute is not None: self.id_attributes = (id_attribute, ) if isinstance(cert, (str, bytes)): cert_chain = list(iterate_pem(cert)) else: cert_chain = cert if isinstance(reference_uri, (str, bytes)): reference_uris = [reference_uri] else: reference_uris = reference_uri sig_root, doc_root, c14n_inputs, reference_uris = self._unpack( data, reference_uris) signed_info_element, signature_value_element = self._build_sig( sig_root, reference_uris, c14n_inputs) if key is None: raise InvalidInput('Parameter "key" is required') signed_info_c14n = self._c14n(signed_info_element, algorithm=self.c14n_alg) if self.sign_alg.startswith("hmac-"): from cryptography.hazmat.primitives.hmac import HMAC signer = HMAC(key=key, algorithm=self._get_hmac_digest_method_by_tag( self.sign_alg), backend=default_backend()) signer.update(signed_info_c14n) signature_value_element.text = ensure_str( b64encode(signer.finalize())) sig_root.append(signature_value_element) elif any( self.sign_alg.startswith(i) for i in ["dsa-", "rsa-", "ecdsa-"]): if isinstance(key, (str, bytes)): from cryptography.hazmat.primitives.serialization import load_pem_private_key key = load_pem_private_key(ensure_bytes(key), password=passphrase, backend=default_backend()) hash_alg = self._get_signature_digest_method_by_tag(self.sign_alg) if self.sign_alg.startswith("dsa-"): signature = key.sign(signed_info_c14n, algorithm=hash_alg) elif self.sign_alg.startswith("ecdsa-"): signature = key.sign( signed_info_c14n, signature_algorithm=ec.ECDSA(algorithm=hash_alg)) elif self.sign_alg.startswith("rsa-"): signature = key.sign(signed_info_c14n, padding=PKCS1v15(), algorithm=hash_alg) else: raise NotImplementedError() if self.sign_alg.startswith("dsa-"): # Note: The output of the DSA signer is a DER-encoded ASN.1 sequence of two DER integers. from asn1crypto.algos import DSASignature decoded_signature = DSASignature.load(signature).native r = decoded_signature['r'] s = decoded_signature['s'] signature = long_to_bytes(r).rjust( 32, b"\0") + long_to_bytes(s).rjust(32, b"\0") signature_value_element.text = ensure_str(b64encode(signature)) if key_info is None: key_info = SubElement(sig_root, ds_tag("KeyInfo")) if key_name is not None: keyname = SubElement(key_info, ds_tag("KeyName")) keyname.text = key_name if cert_chain is None or always_add_key_value: self._serialize_key_value(key, key_info) if cert_chain is not None: x509_data = SubElement(key_info, ds_tag("X509Data")) for cert in cert_chain: x509_certificate = SubElement( x509_data, ds_tag("X509Certificate")) if isinstance(cert, (str, bytes)): x509_certificate.text = strip_pem_header(cert) else: from OpenSSL.crypto import dump_certificate, FILETYPE_PEM x509_certificate.text = strip_pem_header( dump_certificate(FILETYPE_PEM, cert)) else: sig_root.append(key_info) else: raise NotImplementedError() if self.method == methods.enveloping: for c14n_input in c14n_inputs: doc_root.append(c14n_input) return doc_root if self.method == methods.enveloped else sig_root
def map_obj_to_ele(module, want, top, value_map=None, param=None): if not HAS_LXML: module.fail_json(msg="lxml is not installed.") if not param: param = module.params root = Element("root") top_ele = top.split("/") ele = SubElement(root, top_ele[0]) if len(top_ele) > 1: for item in top_ele[1:-1]: ele = SubElement(ele, item) container = ele state = param.get("state") active = param.get("active") if active: oper = "active" else: oper = "inactive" # build xml subtree if container.tag != top_ele[-1]: node = SubElement(container, top_ele[-1]) else: node = container for fxpath, attributes in want.items(): for attr in attributes: tag_only = attr.get("tag_only", False) leaf_only = attr.get("leaf_only", False) value_req = attr.get("value_req", False) is_key = attr.get("is_key", False) parent_attrib = attr.get("parent_attrib", True) value = attr.get("value") field_top = attr.get("top") # operation 'delete' is added as element attribute # only if it is key or leaf only node if state == "absent" and not (is_key or leaf_only): continue # convert param value to device specific value if value_map and fxpath in value_map: value = value_map[fxpath].get(value) if (value is not None) or tag_only or leaf_only: ele = node if field_top: # eg: top = 'system/syslog/file' # field_top = 'system/syslog/file/contents' # <file> # <name>test</name> # <contents> # </contents> # </file> ele_list = root.xpath(top + "/" + field_top) if not len(ele_list): fields = field_top.split("/") ele = node for item in fields: inner_ele = root.xpath(top + "/" + item) if len(inner_ele): ele = inner_ele[0] else: ele = SubElement(ele, item) else: ele = ele_list[0] if value is not None and not isinstance(value, bool): value = to_text(value, errors="surrogate_then_replace") if fxpath: tags = fxpath.split("/") for item in tags: ele = SubElement(ele, item) if tag_only: if state == "present": if not value: # if value of tag_only node is false, delete the node ele.set("delete", "delete") elif leaf_only: if state == "present": ele.set(oper, oper) ele.text = value else: ele.set("delete", "delete") # Add value of leaf node if required while deleting. # in some cases if value is present while deleting, it # can result in error, hence the check if value_req: ele.text = value if is_key: par = ele.getparent() par.set("delete", "delete") else: ele.text = value par = ele.getparent() if parent_attrib: if state == "present": # set replace attribute at parent node if not par.attrib.get("replace"): par.set("replace", "replace") # set active/inactive at parent node if not par.attrib.get(oper): par.set(oper, oper) else: par.set("delete", "delete") return root.getchildren()[0]
def main(): """ main entry point for module execution """ neighbors_spec = dict(host=dict(), port=dict()) element_spec = dict(name=dict(), description=dict(), enabled=dict(default=True, type='bool'), speed=dict(), mtu=dict(type='int'), duplex=dict(choices=['full', 'half', 'auto']), tx_rate=dict(), rx_rate=dict(), neighbors=dict(type='list', elements='dict', options=neighbors_spec), delay=dict(default=10, type='int'), state=dict(default='present', choices=['present', 'absent', 'up', 'down']), active=dict(default=True, type='bool')) aggregate_spec = deepcopy(element_spec) aggregate_spec['name'] = dict(required=True) # remove default in aggregate spec, to handle common arguments remove_default_spec(aggregate_spec) argument_spec = dict(aggregate=dict(type='list', elements='dict', options=aggregate_spec), ) argument_spec.update(element_spec) argument_spec.update(junos_argument_spec) required_one_of = [['name', 'aggregate']] mutually_exclusive = [['name', 'aggregate']] module = AnsibleModule(argument_spec=argument_spec, required_one_of=required_one_of, mutually_exclusive=mutually_exclusive, supports_check_mode=True) warnings = list() result = {'changed': False} if warnings: result['warnings'] = warnings top = 'interfaces/interface' param_to_xpath_map = collections.OrderedDict() param_to_xpath_map.update([('name', { 'xpath': 'name', 'is_key': True }), ('description', 'description'), ('speed', 'speed'), ('mtu', 'mtu'), ('duplex', 'link-mode'), ('disable', { 'xpath': 'disable', 'tag_only': True })]) choice_to_value_map = { 'link-mode': { 'full': 'full-duplex', 'half': 'half-duplex', 'auto': 'automatic' } } params = to_param_list(module) requests = list() for param in params: # if key doesn't exist in the item, get it from module.params for key in param: if param.get(key) is None: param[key] = module.params[key] item = param.copy() state = item.get('state') item['disable'] = True if not item.get('enabled') else False if state in ('present', 'up', 'down'): item['state'] = 'present' validate_param_values(module, param_to_xpath_map, param=item) want = map_params_to_obj(module, param_to_xpath_map, param=item) requests.append( map_obj_to_ele(module, want, top, value_map=choice_to_value_map, param=item)) diff = None with locked_config(module): for req in requests: diff = load_config(module, tostring(req), warnings, action='merge') # issue commit after last configuration change is done commit = not module.check_mode if diff: if commit: commit_configuration(module) else: discard_changes(module) result['changed'] = True if module._diff: result['diff'] = {'prepared': diff} failed_conditions = [] neighbors = None for item in params: state = item.get('state') tx_rate = item.get('tx_rate') rx_rate = item.get('rx_rate') want_neighbors = item.get('neighbors') if state not in ( 'up', 'down' ) and tx_rate is None and rx_rate is None and want_neighbors is None: continue element = Element('get-interface-information') intf_name = SubElement(element, 'interface-name') intf_name.text = item.get('name') if result['changed']: sleep(item.get('delay')) reply = exec_rpc(module, tostring(element), ignore_warning=False) if state in ('up', 'down'): admin_status = reply.xpath( 'interface-information/physical-interface/admin-status') if not admin_status or not conditional( state, admin_status[0].text.strip()): failed_conditions.append('state ' + 'eq(%s)' % state) if tx_rate: output_bps = reply.xpath( 'interface-information/physical-interface/traffic-statistics/output-bps' ) if not output_bps or not conditional( tx_rate, output_bps[0].text.strip(), cast=int): failed_conditions.append('tx_rate ' + tx_rate) if rx_rate: input_bps = reply.xpath( 'interface-information/physical-interface/traffic-statistics/input-bps' ) if not input_bps or not conditional( rx_rate, input_bps[0].text.strip(), cast=int): failed_conditions.append('rx_rate ' + rx_rate) if want_neighbors: if neighbors is None: element = Element('get-lldp-interface-neighbors') intf_name = SubElement(element, 'interface-device') intf_name.text = item.get('name') reply = exec_rpc(module, tostring(element), ignore_warning=False) have_host = [ item.text for item in reply.xpath( 'lldp-neighbors-information/lldp-neighbor-information/lldp-remote-system-name' ) ] have_port = [ item.text for item in reply.xpath( 'lldp-neighbors-information/lldp-neighbor-information/lldp-remote-port-id' ) ] for neighbor in want_neighbors: host = neighbor.get('host') port = neighbor.get('port') if host and host not in have_host: failed_conditions.append('host ' + host) if port and port not in have_port: failed_conditions.append('port ' + port) if failed_conditions: msg = 'One or more conditional statements have not been satisfied' module.fail_json(msg=msg, failed_conditions=failed_conditions) module.exit_json(**result)
def to_xml(yaml_tree): tree = lxml.etree.fromstring("""<keyboard locale="%s"/>""" % yaml_tree["internalName"]) # TODO generate both these validly SubElement(tree, "version") SubElement(tree, "generation") names = SubElement(tree, "names") SubElement(names, "name", value=yaml_tree["displayNames"][yaml_tree["locale"]]) for mode, key_map in yaml_tree["modes"].items(): if not mode.startswith("iso-"): continue mod = mode[4:] if mod == "default": node = SubElement(tree, "keyMap") else: node = SubElement(tree, "keyMap", modifiers=mod) deadkey_set = { x for x in itertools.chain.from_iterable( yaml_tree["deadKeys"].values()) } if isinstance(key_map, dict): for k, v in sorted(key_map.items(), key=key_cmp): key_node = SubElement(node, "map", iso=str(k), to=str(v)) # TODO make this more optimal, chaining all lists and only # assigning when it makes sense to do so if v in deadkey_set and v not in yaml_tree["deadKeys"].get( mode, {}): key_node.attrib["transform"] = "no" else: chain = keyboard_range() for iso, to in zip(chain, re.split(r"[\s\n]+", key_map)): # Ignore nulls if to == r"\u{0}": continue key_node = SubElement(node, "map", iso=iso, to=to) if to in deadkey_set and to not in yaml_tree["deadKeys"].get( mode, {}): key_node.attrib["transform"] = "no" # Space special case! space = yaml_tree.get("special", {}).get("space", {}).get(mode, " ") SubElement(node, "map", iso="A03", to=space) transforms = SubElement(tree, "transforms", type="simple") for base, o in yaml_tree["transforms"].items(): for tr_from, tr_to in o.items(): n = SubElement(transforms, "transform") n.attrib["from"] = "%s%s" % (base, tr_from) n.attrib["to"] = tr_to out = lxml.etree.tostring(tree, xml_declaration=True, encoding="utf-8", pretty_print=True).decode() return ENTITY_REGEX.sub( lambda x: "\\u{%s}" % hex(int(x.group(1)))[2:].upper(), out)
def _format_docdata_dateissue(self, article, docdata): SubElement(docdata, 'date.issue', {'norm': str(article.get('firstcreated', ''))})
def sign(self, algorithm="rsa-sha256", key=None, passphrase=None, cert=None, enveloped=True, c14n_algorithm=default_c14n_algorithm): """ Sign the data and return the root element of the resulting XML tree. :param algorithm: Algorithm that will be used to generate the signature, composed of the signature algorithm and the digest algorithm, separated by a hyphen. All algorthm IDs listed under the `Algorithm Identifiers and Implementation Requirements <http://www.w3.org/TR/xmldsig-core1/#sec-AlgID>`_ section of the XML Signature 1.1 standard are supported. :type algorithm: string :param key: Key to be used for signing. When signing with a certificate or RSA/DSA/ECDSA key, this can be a string containing a PEM-formatted key, or a :py:class:`cryptography.hazmat.primitives.interfaces.RSAPublicKey`, :py:class:`cryptography.hazmat.primitives.interfaces.DSAPublicKey`, or :py:class:`cryptography.hazmat.primitives.interfaces.EllipticCurvePublicKey` object. When signing with a HMAC, this should be a string containing the shared secret. :type key: string, :py:class:`cryptography.hazmat.primitives.interfaces.RSAPublicKey`, :py:class:`cryptography.hazmat.primitives.interfaces.DSAPublicKey`, or :py:class:`cryptography.hazmat.primitives.interfaces.EllipticCurvePublicKey` object :param passphrase: Passphrase to use to decrypt the key, if any. :type passphrase: string :param cert: X.509 certificate to use for signing. This should be a string containing a PEM-formatted certificate, or an array containing the certificate and a chain of intermediate certificates. :type cert: string or array of strings :param c14n_algorithm: Canonicalization (c14n) algorithm to use. Supported algorithms are listed in the class variable ``xmldsig.known_c14n_algorithms``. :type c14n_algorithm: string :param enveloped: If `True`, the enveloped signature signing method will be used. If `False`, the enveloping signature method will be used. :type enveloped: boolean :returns: A :py:class:`lxml.etree.Element` object representing the root of the XML tree containing the signature and the payload data. To specify the location of an enveloped signature within **data**, insert a `<Signature Id="placeholder"></Signature>` element in **data**. This element will be replaced by the generated signature, and excised when generating the digest. """ self.signature_alg = algorithm self.key = key if isinstance(cert, (str, bytes)): cert_chain = [cert] else: cert_chain = cert self._get_payload_c14n(enveloped, c14n_algorithm=c14n_algorithm) self.digest = self._get_digest( self.payload_c14n, self._get_digest_method_by_tag(self.digest_alg)) signed_info = SubElement(self.sig_root, ds_tag("SignedInfo"), nsmap=dict(ds=XMLDSIG_NS)) c14n_method = SubElement(signed_info, ds_tag("CanonicalizationMethod"), Algorithm=c14n_algorithm) if self.signature_alg.startswith("hmac-"): algorithm_id = self.known_hmac_digest_tags[self.signature_alg] else: algorithm_id = self.known_signature_digest_tags[self.signature_alg] signature_method = SubElement(signed_info, ds_tag("SignatureMethod"), Algorithm=algorithm_id) reference = SubElement(signed_info, ds_tag("Reference"), URI=self._reference_uri) if enveloped: transforms = SubElement(reference, ds_tag("Transforms")) SubElement(transforms, ds_tag("Transform"), Algorithm=XMLDSIG_NS + "enveloped-signature") SubElement(transforms, ds_tag("Transform"), Algorithm=c14n_algorithm) digest_method = SubElement( reference, ds_tag("DigestMethod"), Algorithm=self.known_digest_tags[self.digest_alg]) digest_value = SubElement(reference, ds_tag("DigestValue")) digest_value.text = self.digest signature_value = SubElement(self.sig_root, ds_tag("SignatureValue")) signed_info_c14n = self._c14n(signed_info, algorithm=c14n_algorithm) if self.signature_alg.startswith("hmac-"): from cryptography.hazmat.primitives.hmac import HMAC signer = HMAC(key=self.key, algorithm=self._get_hmac_digest_method_by_tag( self.signature_alg), backend=default_backend()) signer.update(signed_info_c14n) signature_value.text = ensure_str(b64encode(signer.finalize())) self.sig_root.append(signature_value) elif self.signature_alg.startswith( "dsa-") or self.signature_alg.startswith( "rsa-") or self.signature_alg.startswith("ecdsa-"): if isinstance(self.key, (str, bytes)): from cryptography.hazmat.primitives.serialization import load_pem_private_key key = load_pem_private_key(self.key, password=passphrase, backend=default_backend()) else: key = self.key hash_alg = self._get_signature_digest_method_by_tag( self.signature_alg) if self.signature_alg.startswith("dsa-"): signer = key.signer(signature_algorithm=hash_alg) elif self.signature_alg.startswith("ecdsa-"): signer = key.signer(signature_algorithm=ec.ECDSA( algorithm=hash_alg)) elif self.signature_alg.startswith("rsa-"): signer = key.signer(padding=PKCS1v15(), algorithm=hash_alg) else: raise NotImplementedError() signer.update(signed_info_c14n) signature = signer.finalize() if self.signature_alg.startswith("dsa-"): # Note: The output of the DSA signer is a DER-encoded ASN.1 sequence of two DER integers. # Bytes 0-1: DER sequence header and length # Bytes 2-3: DER integer header and length (r_len) # Bytes 4-4+r_len-1: r (first half of DSA signature) # Bytes 4+r_len-5+r_len: DER integer header and length # Bytes 6+r_len to the end: s (second half of DSA signature) r_len = bytes_to_long(signature[3]) r, s = signature[4:4 + r_len], signature[6 + r_len:] signature = r.rjust(32, b"\0") + s.rjust(32, b"\0") signature_value.text = ensure_str(b64encode(signature)) key_info = SubElement(self.sig_root, ds_tag("KeyInfo")) if cert_chain is None: self._serialize_key_value(key, key_info) else: x509_data = SubElement(key_info, ds_tag("X509Data")) for cert in cert_chain: x509_certificate = SubElement(x509_data, ds_tag("X509Certificate")) if isinstance(cert, (str, bytes)): x509_certificate.text = strip_pem_header(cert) else: from OpenSSL.crypto import dump_certificate, FILETYPE_PEM x509_certificate.text = dump_certificate( FILETYPE_PEM, cert) else: raise NotImplementedError() if enveloped: return self.payload else: self.sig_root.append(self.payload) return self.sig_root
def _format_title(self, article, head): title = SubElement(head, 'title') title.text = article.get('headline', '')
def vis_detections(im, im_name, class_name, dets, ax, thresh=0.5): """Draw detected bounding boxes.""" #选取候选框score大于阈值的dets inds = np.where(dets[:, -1] >= thresh)[0] seps = im_name.split('/') if len(inds) == 0: return with open('{}.xml'.format(im_name[0:-4]), 'w') as f: #IMAGE FOLDER NAME PATH node_root = Element('annotation') node_folder = SubElement(node_root, 'folder') node_folder.text = seps[-2] node_filename = SubElement(node_root, 'filename') node_filename.text = seps[-1] node_filepath = SubElement(node_root, 'path') node_filepath.text = im_name node_size = SubElement(node_root, 'size') node_width = SubElement(node_size, 'width') node_width.text = str(im.shape[1]) node_height = SubElement(node_size, 'height') node_height.text = str(im.shape[0]) node_depth = SubElement(node_size, 'depth') node_depth.text = str(im.shape[2]) for i in inds: bbox = dets[i, :4] score = dets[i, -1] node_object = SubElement(node_root, 'object') node_name = SubElement(node_object, 'name') node_name.text = 'person' node_difficult = SubElement(node_object, 'difficult') node_difficult.text = '0' node_bndbox = SubElement(node_object, 'bndbox') node_xmin = SubElement(node_bndbox, 'xmin') node_xmin.text = str(int(bbox[0] + 1)) node_ymin = SubElement(node_bndbox, 'ymin') node_ymin.text = str(int(bbox[1] + 1)) node_xmax = SubElement(node_bndbox, 'xmax') node_xmax.text = str(int(bbox[2] + 1)) node_ymax = SubElement(node_bndbox, 'ymax') node_ymax.text = str(int(bbox[3] + 1)) #ax.add_patch( #plt.Rectangle((bbox[0], bbox[1]), # bbox[2] - bbox[0], # bbox[3] - bbox[1], fill=False, # edgecolor='red', linewidth=3.5) #) #ax.text(bbox[0], bbox[1] - 2, # '{:s} {:.3f}'.format(class_name, score), # bbox=dict(facecolor='blue', alpha=0.5), # fontsize=14, color='white') #ax.set_title(('{} detections with ' # 'p({} | box) >= {:.1f}').format(class_name, class_name, # thresh), # fontsize=14) xml = tostring(node_root, pretty_print=True) # 格式化显示,该换行的换行 f.write(xml)
def _format_keywords(self, article, head): if article.get('keywords'): keylist = SubElement(head, 'key-list') for keyword in article['keywords']: SubElement(keylist, 'keyword', {'key': keyword})
def fetch_xml_format(src_img_data, f_name, anno_list): img_height, img_width, img_channle = src_img_data.shape node_root = Element('annotation') node_folder = SubElement(node_root, 'folder') node_folder.text = 'LSD10' node_filename = SubElement(node_root, 'filename') node_filename.text = f_name node_size = SubElement(node_root, 'size') node_width = SubElement(node_size, 'width') node_width.text = str(img_width) node_height = SubElement(node_size, 'height') node_height.text = str(img_height) node_depth = SubElement(node_size, 'depth') node_depth.text = str(img_channle) for anno_target in anno_list: node_object = SubElement(node_root, 'object') node_name = SubElement(node_object, 'name') node_name.text = anno_target[-1] node_difficult = SubElement(node_object, 'difficult') node_difficult.text = '0' node_bndbox = SubElement(node_object, 'bndbox') node_xmin = SubElement(node_bndbox, 'xmin') node_xmin.text = str(1 if anno_target[0] < 0 else anno_target[0]) node_ymin = SubElement(node_bndbox, 'ymin') node_ymin.text = str(1 if anno_target[1] < 0 else anno_target[1]) node_xmax = SubElement(node_bndbox, 'xmax') node_xmax.text = str( img_width - 1 if anno_target[2] >= img_width else anno_target[2]) node_ymax = SubElement(node_bndbox, 'ymax') node_ymax.text = str( img_height - 1 if anno_target[3] >= img_height else anno_target[3]) xml_obj = tostring(node_root, pretty_print=True) xml_obj = xml_obj.decode("utf8") return xml_obj