def build_rss_feed(comic_info: RawConfigParser, comic_data_dicts: List[Dict]): global cdata_dict if not comic_info.getboolean("RSS Feed", "Build RSS feed"): return if "GITHUB_REPOSITORY" not in os.environ: raise ValueError( "Set GITHUB_REPOSITORY in your environment variables before building your RSS feed locally" ) register_namespace("atom", "http://www.w3.org/2005/Atom") register_namespace("dc", "http://purl.org/dc/elements/1.1/") root = ElementTree.Element("rss") root.set("version", "2.0") channel = ElementTree.SubElement(root, "channel") # Build comic URL repo_author, repo_name = os.environ["GITHUB_REPOSITORY"].split("/") comic_url = "https://{}.github.io/{}/".format(repo_author, repo_name) add_base_tags_to_channel(channel, comic_url, comic_info) add_image_tag(channel, comic_url, comic_info) for comic_data in comic_data_dicts: add_item(channel, comic_data, comic_url, comic_info) pretty_string = pretty_xml(root) # Replace CDATA manually, because XML is stupid and I can't figure out how to insert raw text pretty_string = pretty_string.format(**cdata_dict) with open("feed.xml", 'wb') as f: f.write(bytes(pretty_string, "utf-8"))
def write(self, x3d_file_name): # Doctype and root element with namespaces etc XSD = 'http://www.w3.org/2001/XMLSchema-instance' register_namespace('xsd', XSD) x3d = Element('X3D', {'profile': 'Interchange', 'version': '3.3'}) x3d.set( '{%s}noNamespaceSchemaLocation' % XSD, 'http://www.web3d.org/specifications/x3d-3.3.xsd', ) # Metadata head = SubElement(x3d, 'head') SubElement( head, 'meta', { 'name': 'generator', 'content': 'Ocellaris iso_line_to_xd3.py' }, ) # The Scene scene = SubElement(x3d, 'Scene') # SubElement(scene, 'Background', {'skyColor': '1 1 1'}) # SubElement(scene, 'Viewpoint', {'orientation': '0 -1 0 0.53', 'position': '-2.28 0.29 4.06'}) for mesh in self.meshes: mesh._write(scene) xml = '<?xml version="1.0" encoding="UTF-8"?>\n' xml += '<!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.3//EN" "http://www.web3d.org/specifications/x3d-3.3.dtd">\n' xml += tostring(x3d, encoding='utf-8') with open(x3d_file_name, 'wt') as out: out.write(xml)
def __init__(self, uri, prefix=None): # We want to avoid polluting the instance dict as much as possible, # since attribute access is how we produce QualifiedNames. self.__uri = uri self.__prefix = prefix if self.__prefix is not None: register_namespace(self.__prefix, self.__uri)
def alterMainClass(mainClassName): filepath="pom.xml" # register the namespace register_namespace('', "http://maven.apache.org/POM/4.0.0") register_namespace('xsi', "http://www.w3.org/2001/XMLSchema-instance") # get the root of the xml tree tree = parse(filepath) root = tree.getroot() # set the tag prefix prefix = "{http://maven.apache.org/POM/4.0.0}" build = root.find(prefix + "build") plugins = build.find(prefix + "plugins") for plugin in plugins: executions = plugin.find(prefix + "executions") if executions is not None: execution = executions.find(prefix + "execution") if execution is not None: configuration = execution.find(prefix + "configuration") if configuration is not None: transformers = configuration.find(prefix + "transformers") for transformer in transformers: mainClass = transformer.find(prefix + 'mainClass') if mainClass is not None: mainClass.text = mainClassName # write the changes tree.write(filepath) print "mainclass has been changed to " + mainClassName + "."
def build_rss_feed(comic_info: RawConfigParser, comic_data_dicts: List[Dict]): global cdata_dict if not comic_info.getboolean("RSS Feed", "Build RSS feed"): return register_namespace("atom", "http://www.w3.org/2005/Atom") register_namespace("dc", "http://purl.org/dc/elements/1.1/") root = ElementTree.Element("rss") root.set("version", "2.0") channel = ElementTree.SubElement(root, "channel") # Build comic URL comic_url, _ = get_comic_url(comic_info) add_base_tags_to_channel(channel, comic_url, comic_info) add_image_tag(channel, comic_url, comic_info) for comic_data in comic_data_dicts: add_item(channel, comic_data, comic_url, comic_info) pretty_string = pretty_xml(root) # Replace CDATA manually, because XML is stupid and I can't figure out how to insert raw text pretty_string = pretty_string.format(**cdata_dict) with open("feed.xml", 'wb') as f: f.write(bytes(pretty_string, "utf-8"))
def parse_response(self, action_name, content, encoding=None, envelope_attrib=None, typed=None): """ Parses a response from the server with the given action name and content. """ register_namespace('', None) if encoding is None: encoding = self._encoding if envelope_attrib is None: envelope_attrib = self._envelope_attrib if typed is None: typed = self._typed try: docNode = xml_fromstring(content) except ParseError: # Try removing any extra XML declarations in case there are more than one. # This sometimes happens when a device sends its own XML config files. content = remove_extraneous_xml_declarations(content) docNode = xml_fromstring(content) except ValueError: # This can occur when requests returns a `str` (unicode) but there's also an XML # declaration, which lxml doesn't like. docNode = xml_fromstring(content.encode('utf8')) resp_body = None if typed: resp_body = docNode.find(".//{%s}%sResponse" % (typed, action_name)) else: resp_body = docNode.find(".//%sResponse" % action_name) if resp_body is None: msg = ( 'Returned XML did not include an element which matches namespace %r and tag name' ' \'%sResponse\'.' % (typed, action_name)) print(msg + '\n' + xml_tostring( docNode, short_empty_elements=False).decode('utf8')) raise SOAPProtocolError(msg) # Sometimes devices return XML strings as their argument values without escaping them with # CDATA. This checks to see if the argument has been parsed as XML and un-parses it if so. resp_dict = {} for arg in resp_body.getchildren(): children = arg.getchildren() if children: resp_dict[arg.tag] = "\n".join( xml_tostring(x) for x in children) else: if arg.text is None: resp_dict[arg.tag] = "" else: resp_dict[arg.tag] = arg.text return resp_dict
def create_response(self, action_name: str, arguments: dict, encoding=None, envelope_attrib=None, typed=None): """ Creates a Soap response to the action with the specified arguments. """ register_namespace('', None) if encoding is None: encoding = self._encoding if envelope_attrib is None: envelope_attrib = self._envelope_attrib if typed is None: typed = self._typed envelope = Element("s:Envelope") if envelope_attrib: for eakey, eaval in envelope_attrib: envelope.attrib.update({eakey: eaval}) else: envelope.attrib.update({'xmlns:s': NS_SOAP_ENV}) envelope.attrib.update({'s:encodingStyle': encoding}) body = SubElement(envelope, "s:Body") action_name_tag_name = action_name + "Response" methElement = SubElement(body, action_name_tag_name) if encoding: methElement.set(NS_SOAP_ENV + "encodingStyle", encoding) if arguments: for arg_name, arg_val in arguments.items(): py_type = type(arg_val) soap_type = PYTHON_TO_SOAP_TYPE_MAP[py_type] if soap_type == 'xsd:string': arg_val = arg_val # pylint: disable=self-assigning-variable elif soap_type == 'xsd:int' or soap_type == 'xsd:float': arg_val = str(arg_val) elif soap_type == 'xsd:boolean': arg_val = "1" if arg_val else "0" argElement = SubElement(methElement, arg_name) if typed and soap_type: if not isinstance(type, QName): arg_type = QName(NS_XSD, soap_type) argElement.set(NS_XSI + "type", soap_type) argElement.text = arg_val else: methElement.text = "" envelope_content = xml_tostring(envelope, short_empty_elements=False) content = XML_DOCUMENT_DECLARATION + "\n" + str_cast(envelope_content) return content
def register_namespaces(name_spaces: Tuple[Tuple[str, str]]) -> None: """Register `n` number of xml namespaces :param name_spaces: Tuple :return: """ for name_space in name_spaces: register_namespace(*name_space)
def main(): parser = argparse.ArgumentParser(description='Checker for XML Veracode Reports.') parser.add_argument('file', help='XML file to check') parser.add_argument('clear', action='store_true', help='Remove all XML files in working directory') parser.add_argument('-o', '--output', action='store_true', help='Output a new XML File with certain applied fixes') parser.add_argument('-l', '--list', action='store_true', help='Output a list of flaws for Asana') parser.add_argument('-sc', '--spellcheck', action='store_true', help='Enable spellcheck') args = parser.parse_args() xmlFile = args.file if args.file.lower() == 'clear': val = input("Delete all XML files in your directory? (Y or N)\n") if (val.lower() == "yes" or val.lower() == "y"): removeXMLs() print("XML files successfully deleted") else: print("Cancelled") exit() asanaOutName = "AsanaList.txt" path_to_schema = "manualflawfeed.xsd" register_namespace("", "http://www.veracode.com/schema/import") spellcheck_whitelist = "spellcheck_whitelist.txt" fileExists(xmlFile) fileExists(path_to_schema) fileExists(spellcheck_whitelist) if args.spellcheck: global spellcheck_bool spellcheck_bool = True et = parse(xmlFile) newFile = xmlReport() newFile.processFlaws(et) newFile.Analyze() if args.list: #print(newFile.content) writeToFile(asanaOutName, newFile.content) if args.output: print("="*50+"\n") newFile.cruftRemoval(et.getroot()) newXML = writeToXML(xmlFile, et) if not xsdValidate(newXML, path_to_schema): print("[*]\t XSD check failed, fixes to the XML may have been applied.\ Work off of {} and try again.".format(newXML)) if newFile.codeBlocksDeleted: print("[*]\t Empty code blocks were discovered and removed. Utilize the NEW file for future reporting.")
def create_xml(domain, uuid, memory, mac): register_namespace("qemu", "http://libvirt.org/schemas/domain/qemu/1.0") tree = parse(BACKUP_XML_PATH) root = tree.getroot() root.find("name").text = domain root.find("uuid").text = uuid root.find("memory").text = memory root.find("currentMemory").text = memory root.find("devices").find("disk").find("source").set("file", XML_PATH + domain + ".qcow2") root.find("devices").find("interface").find("mac").set("address", mac) tree.write(domain + ".xml")
def create_xml(domain, uuid, memory, mac): register_namespace("qemu", "http://libvirt.org/schemas/domain/qemu/1.0") tree = parse(BACKUP_XML_PATH) root = tree.getroot() root.find("name").text = domain root.find("uuid").text = uuid root.find("memory").text = memory root.find("currentMemory").text = memory root.find("devices").find("disk").find("source").set( "file", XML_PATH + domain + ".qcow2") root.find("devices").find("interface").find("mac").set("address", mac) tree.write(domain + ".xml")
def main(): register_namespace('svg', NS_SVG[1:-1]) register_namespace('xlink', NS_XLINK[1:-1]) parser = argparse.ArgumentParser() parser.add_argument('file', type=str) parser.add_argument('-s', '--scale', type=float, default=1.0) args = parser.parse_args() context = parse_svg(op.abspath(args.file)) buffer = render(context, scale=args.scale) outname = op.splitext(op.basename(args.file))[0] + '.png' imsave(outname, buffer)
def update_rss(show: Show, filename: str, file_size: int) -> None: """Add an episode to the RSS XML""" rss_location = join(show.output.directory, "feed.xml") register_namespace("itunes", ITUNES_NAMESPACE) xml = _get_xml(rss_location) root = xml.getroot() channel = root.find("channel") _add_item(channel, show, filename, file_size) _clean_up(channel, rss_location) xml.write(rss_location) chmod(rss_location, 0x744)
def _proceed(self): def build(elt, tag): return SubElement(elt, tag) def browse(tree, parent, cb): if isinstance(tree, str): parent.text = tree if isinstance(tree, dict): for key, val in tree.items(): m = re.match('^{(.+)}(\w+)$', key) if m and len(m.groups()) == 2: ns = m.groups()[0] tag = m.groups()[1] if ns in self.ns.keys(): key = '{{{0}}}{1}'.format(self.ns[ns], tag) if key.startswith(self.attrib_tag): k = re.sub('({0})(.+)'.format(self.attrib_tag), '\g<2>', key) parent.set(k, val) continue if key == self.text_tag: parent.text = val continue if isinstance(val, list): for elt in val: browse(elt, cb(parent, key), cb) continue browse(val, cb(parent, key), cb) for prefix, uri in self.ns.items(): register_namespace(prefix, uri) base = None if len(self.obj.keys()) < 1: pass elif len(self.obj.keys()) > 1: base = Element('root') else: for root, sub in self.obj.items(): base = Element(root) browse(sub, base, build) return base
def create_root(self): """ Create "IDMEF-Message" root with attributes and with namespace if needed :return: root element """ root_attributes = {"version": "1.0"} namespace = "" if self.idmef_namespace: register_namespace("idmef", IDMEF_NAMESPACE_ATTRIBUTE) namespace = IDMEF_NAMESPACE_TAG root_attributes["xmlns:idmef"] = IDMEF_NAMESPACE_ATTRIBUTE root = Element(namespace + "IDMEF-Message") save_attributes_into_element(root, {"version": "1.0"}) return root
def main(): parser = argparse.ArgumentParser( description='Checker for XML Veracode Reports.') parser.add_argument('file', help='XML file to check') parser.add_argument( '-o', '--output', action='store_true', help='Output a new XML File with certain applied fixes') parser.add_argument('-l', '--list', action='store_true', help='Output a list of flaws for Asana') args = parser.parse_args() xmlFile = args.file asanaOutName = "AsanaList.txt" path_to_schema = "manualflawfeed.xsd" register_namespace("", "http://www.veracode.com/schema/import") fileExists(xmlFile) fileExists(path_to_schema) et = parse(xmlFile) newFile = xmlReport() newFile.processFlaws(et) newFile.Analyze() if args.list: #print(newFile.content) writeToFile(asanaOutName, newFile.content) if args.output: print("=" * 50 + "\n") newFile.cruftRemoval(et.getroot()) newXML = writeToXML(xmlFile, et) if not xsdValidate(newXML, path_to_schema): print( "[*]\t XSD check failed, fixes to the XML may have been applied.\ Work off of {} and try again.".format(newXML)) if newFile.codeBlocksDeleted: print( "[*]\t Empty code blocks were discovered and removed. Utilize the NEW file for future reporting." )
def get_prefix_uri(self) -> str: from xml.etree.ElementTree import register_namespace uri = "" if self.namespaces: for p, n in self.namespaces.items(): if isinstance(p, str) and isinstance(n, str): register_namespace(p, n) if self.prefix: try: uri = f"{{{self.namespaces[self.prefix]}}}" except KeyError: raise KeyError(f"{self.prefix} is not included in namespaces") else: uri = f'{{{self.namespaces[""]}}}' return uri
def assert_import_equals_export(self, _roundtrip): _result = test_utils.import_xml(_roundtrip) with open(_roundtrip) as _import_file: _import_xml = _import_file.read() register_namespace("", SCHEMA_NAMESPACE) _import_xml = to_xml_string(fromstring(_import_xml), encoding="utf-8") _export_xml = to_xml_string(_result.export_to_elementtree(), encoding="utf-8") # cfedermann: uncomment these lines to dump import/export XML to file. # # with open('/tmp/_import.xml', 'wb') as _out: # _out.write(_import_xml.encode('utf-8')) # with open('/tmp/_export.xml', 'wb') as _out: # _out.write(_export_xml.encode('utf-8')) diff = "\n".join(unified_diff(_import_xml.split("\n"), _export_xml.split("\n"))) self.assertEqual( _import_xml, _export_xml, msg="For file {0}, export differs from import:\n{1}".format(_roundtrip, diff) )
def assert_import_equals_export(self, _roundtrip): _result = test_utils.import_xml(_roundtrip) with open(_roundtrip) as _import_file: _import_xml = _import_file.read() register_namespace('', SCHEMA_NAMESPACE) _import_xml = tostring(fromstring(_import_xml)) _export_xml = tostring(_result[0].export_to_elementtree()) # cfedermann: uncomment these lines to dump import/export XML to file. # #with open('/tmp/_import.xml', 'w') as _out: # _out.write(pretty_xml(_import_xml).encode('utf-8')) #with open('/tmp/_export.xml', 'w') as _out: # _out.write(pretty_xml(_export_xml).encode('utf-8')) _pretty_import = pretty_xml(_import_xml).strip().encode('utf-8') _pretty_export = pretty_xml(_export_xml).strip().encode('utf-8') diff = '\n'.join(unified_diff(_pretty_import.split('\n'), _pretty_export.split('\n'))) self.assertEqual(_pretty_import, _pretty_export, msg='For file {0}, export differs from import:\n{1}'.format(_roundtrip, diff))
def __init__(self, graph=None, encoding="utf-8", prettyprint=True, version="1.2draft"): self.construct_types() self.prettyprint = prettyprint self.encoding = encoding self.set_version(version) self.xml = Element( "gexf", { "xmlns": self.NS_GEXF, "xmlns:xsi": self.NS_XSI, "xsi:schemaLocation": self.SCHEMALOCATION, "version": self.VERSION, }, ) # Make meta element a non-graph element # Also add lastmodifieddate as attribute, not tag meta_element = Element("meta") subelement_text = f"NetworkX {nx.__version__}" SubElement(meta_element, "creator").text = subelement_text meta_element.set("lastmodifieddate", time.strftime("%Y-%m-%d")) self.xml.append(meta_element) register_namespace("viz", self.NS_VIZ) # counters for edge and attribute identifiers self.edge_id = itertools.count() self.attr_id = itertools.count() self.all_edge_ids = set() # default attributes are stored in dictionaries self.attr = {} self.attr["node"] = {} self.attr["edge"] = {} self.attr["node"]["dynamic"] = {} self.attr["node"]["static"] = {} self.attr["edge"]["dynamic"] = {} self.attr["edge"]["static"] = {} if graph is not None: self.add_graph(graph)
def __init__(self): bwbidlist_filename = 'BWBIdList.xml' # bwbidlist_filename = 'bwbidlist_test.xml' bwbidlist_url = 'http://wetten.overheid.nl/BWBIdService/BWBIdList.xml.zip' pickle_filename = 'pickles/bwbid_list_{0}.pickle'.format(date.today()) if glob(pickle_filename): logging.info( "Today's BWB ID list dictionary pickle already exists, loading directly from pickle ..." ) self.bwbid_list = pickle.load(open(pickle_filename, 'r')) return logging.info( "Loading zipped BWB ID list from {0}.".format(bwbidlist_url)) response = urllib2.urlopen(bwbidlist_url) bwbidlist_zipped_string = response.read() bwbidlist_zipped_file = StringIO.StringIO(bwbidlist_zipped_string) bwbidlist_zip = zipfile.ZipFile(bwbidlist_zipped_file) logging.info( "Unzipping BWB ID list to {0}.".format(bwbidlist_filename)) bwbidlist_zip.extract(bwbidlist_filename) logging.info("Checking validity of BWB ID list in {0}".format( bwbidlist_filename)) bwbidlist_checked_filename = self.checkBWBXML(bwbidlist_filename) logging.info("Loading and parsing BWB ID list from {0}.".format( bwbidlist_checked_filename)) register_namespace('', 'http://schemas.overheid.nl/bwbidservice') tree = ElementTree() tree.parse(bwbidlist_checked_filename) self.pickFromTree(tree) # pprint(self.bwbid_list) logging.info( "Dumping BWB ID list dictionary to {0}.".format(pickle_filename)) outfile = open(pickle_filename, 'w') pickle.dump(self.bwbid_list, outfile)
def ____modifyAttr(manifest, packageName): # 防止namespace变更 register_namespace('android', "http://schemas.android.com/apk/res/android") doc = parse(manifest) root = doc.getroot() application = root.find('application') # 修改activity activitys = application.findall('activity') ____updateFramework(activitys, packageName) # 修改service services = application.findall('service') ____updateFramework(services, packageName) # 修改provide providers = application.findall('provider') ____updateFramework(providers, packageName) # 修改广播器 receivers = application.findall('receiver') ____updateFramework(receivers, packageName) doc.write(manifest) return
def ____modifyAttr(manifest, packageName): # 防止namespace变更 register_namespace('android', "http://schemas.android.com/apk/res/android") doc = parse(manifest) root = doc.getroot() application = root.find('application') # 修改activity activitys = application.findall('activity') ____updateFramework(activitys, packageName) # 修改service services = application.findall('service') ____updateFramework(services, packageName) # 修改provide providers = application.findall('provider') ____updateFramework(providers, packageName) # 修改广播器 receivers = application.findall('receiver'); ____updateFramework(receivers, packageName) doc.write(manifest) return
def main(argv): hostn = 'http://localhost:8080' #localhost at default port output = 'odfdump.xml' #default file try: opts, args = getopt.getopt(argv,"ho:") except getopt.GetoptError: print('getAllData.py -o <outputfile> host') sys.exit(2) for opt, arg in opts: if opt == '-h': print('getAllData.py -o <outputfile> host') sys.exit() elif opt == '-o': output = arg if len(args) >= 1: hostn = args[0] #request for odf hierarchy hierarchyRequest = """<omiEnvelope xmlns="http://www.opengroup.org/xsd/omi/1.0/" version="1.0" ttl="0"> <read msgformat="odf"> <msg> <Objects xmlns="http://www.opengroup.org/xsd/odf/1.0/"/> </msg> </read> </omiEnvelope>""" #request for 9000 newest values(should be enough for now) fullRequest = """<omiEnvelope xmlns="http://www.opengroup.org/xsd/omi/1.0/" version="1.0" ttl="0"> <read msgformat="odf" newest="9000"> <msg> </msg> </read> </omiEnvelope>""" #register namespaces so that we don't get wrong namespaces in the request register_namespace("omi","omi.xsd") register_namespace("odf", "odf.xsd") register_namespace("", "http://www.w3.org/2001/XMLSchema-instance") headers = {'Content-Type': 'application/xml'} #current hierarchy r = requests.post(hostn, data = hierarchyRequest, headers = headers).text root = fromstring(r) objects = root.find(".//{http://www.opengroup.org/xsd/odf/1.0/}Objects") #remove values and add metadata and description tags update_odf(objects) fullRoot = fromstring(fullRequest) fullRoot.find(".//{http://www.opengroup.org/xsd/omi/1.0/}msg").append(objects) #write result to file. note: result might be big so iterate over the result with open(output,'wb') as handle: r2 = requests.post(hostn, data = tostring(fullRoot, encoding="utf-8"), headers = headers, stream = True) if not r2.ok: print("INVALID RESPONSE") for block in r2.iter_content(1024): handle.write(block)
def assert_import_equals_export(self, _roundtrip): _result = test_utils.import_xml(_roundtrip) with open(_roundtrip) as _import_file: _import_xml = _import_file.read() register_namespace('', SCHEMA_NAMESPACE) _import_xml = to_xml_string(fromstring(_import_xml), encoding="utf-8") _export_xml = to_xml_string(_result.export_to_elementtree(), encoding="utf-8") # cfedermann: uncomment these lines to dump import/export XML to file. # #with open('/tmp/_import.xml', 'wb') as _out: # _out.write(_import_xml.encode('utf-8')) #with open('/tmp/_export.xml', 'wb') as _out: # _out.write(_export_xml.encode('utf-8')) diff = '\n'.join( unified_diff(_import_xml.split('\n'), _export_xml.split('\n'))) self.assertEqual( _import_xml, _export_xml, msg='For file {0}, export differs from import:\n{1}'.format( _roundtrip, diff.encode('utf-8')))
def registerNamespaces(self): for key, value in tags.items(): register_namespace(key, value) if self.year >= 2022: for key, value in tags_v2.items(): register_namespace(key, value) for key, value in tags_v2k.items(): register_namespace(key, value)
def get_response(self): register_namespace('', 'http://www.w3.org/2005/Atom') top = Element('feed') top.set('xmlns', 'http://www.w3.org/2005/Atom') title_elem = SubElement(top, "title") title_elem.text = self.title title_elem.set('type', 'text') id_elem = SubElement(top, 'id') id_elem.text = self.feed_url updated_elem = SubElement(top, 'updated') updated_elem.text = self.last_updated.isoformat() link1_elem = SubElement(top, 'link') link1_elem.set('href', self.url) link2_elem = SubElement(top, 'link') link2_elem.set('href', self.feed_url) link2_elem.set('rel', 'self') author_elem = SubElement(top, 'author') author_name_elem = SubElement(author_elem, 'name') author_name_elem.text = self.author icon_elem = SubElement(top, 'icon') icon_elem.text = self.icon for entry in self.entries: entry.generate(top) encoded = tostring(top, xml_declaration=True, encoding='utf-8', method='xml') return Response(encoded, mimetype='text/atom')
def cleanup(source): target = source + ".tmp" options = parse_args([ "--enable-viewboxing", "--remove-metadata", "--enable-id-stripping", "--enable-comment-stripping", "--strip-xml-space", "--strip-xml-prolog", "--shorten-ids", "-i", source, "-o", target ]) input, output = getInOut(options) start(options, input, output) os.remove(source) os.rename(target, source) register_namespace("", "http://www.w3.org/2000/svg") tree = ElementTree() tree.parse(source) svg = next(tree.iter()) for attr in ["style", "verstion", "x", "y"]: drop(svg, attr) svg.attrib["width"] = "100%" svg.attrib["height"] = "100%" tree.write(source)
def __init__(self, geopackage_path, log=None): """ :param geopackage_path: the file path for the geopackaging we are working on :param srs_name: The code value for the SRS (CODE: 4326 in EPSG:4326) :param srs_org: The organization specifying the SRS (ORG: EPSG in EPSG:4326) :param log: an optional log file passed through from an outer context """ self.geopackage_path = geopackage_path self.log = log existing_metadata = self.__load_metadata() self.existing_metadata = dict() for _entry in existing_metadata: self.existing_metadata[_entry["id"]] = _entry # register the namespaces we will be using as we manipulate the data [ register_namespace(namespace.value[0], namespace.value[1]) for namespace in NameSpaces ] # check to see if there is existing data or not if len(self.existing_metadata): try: self.tree = ElementTree.fromstring( self.existing_metadata[1]["metadata"].encode( "ascii", "ignore")) except Exception: # metadata is invalid or not what we want self.tree = ElementTree.parse( source=MD_TREE).getroot() # Load base MD XML # set the creation date self.tree.set(QName(NameSpaces.ISM.value[1], "createDate"), datetime.date.today().isoformat()) else: self.tree = ElementTree.parse( source=MD_TREE).getroot() # Load base MD XML # set the creation date self.tree.set(QName(NameSpaces.ISM.value[1], "createDate"), datetime.date.today().isoformat()) # Fill out our DateStamp elem date_element = self.__build_tag(namespace=NameSpaces.GMD.value[1], tag=Tags.DATE_STAMP) self.__build_tag( namespace=NameSpaces.GCO.value[1], tag="Date", parent=date_element, text=str(datetime.date.today().isoformat()), ) # insert as the 3rd element bc spec self.tree.insert(3, date_element)
def __init__(self): bwbidlist_filename = 'BWBIdList.xml' # bwbidlist_filename = 'bwbidlist_test.xml' bwbidlist_url = 'http://wetten.overheid.nl/BWBIdService/BWBIdList.xml.zip' pickle_filename = 'pickles/bwbid_list_{0}.pickle'.format(date.today()) if glob(pickle_filename) : logging.info("Today's BWB ID list dictionary pickle already exists, loading directly from pickle ...") self.bwbid_list = pickle.load(open(pickle_filename, 'r')) return logging.info("Loading zipped BWB ID list from {0}.".format(bwbidlist_url)) response = urllib2.urlopen(bwbidlist_url) bwbidlist_zipped_string = response.read() bwbidlist_zipped_file = StringIO.StringIO(bwbidlist_zipped_string) bwbidlist_zip = zipfile.ZipFile(bwbidlist_zipped_file) logging.info("Unzipping BWB ID list to {0}.".format(bwbidlist_filename)) bwbidlist_zip.extract(bwbidlist_filename) logging.info("Checking validity of BWB ID list in {0}".format(bwbidlist_filename)) bwbidlist_checked_filename = self.checkBWBXML(bwbidlist_filename) logging.info("Loading and parsing BWB ID list from {0}.".format(bwbidlist_checked_filename)) register_namespace('','http://schemas.overheid.nl/bwbidservice') tree = ElementTree() tree.parse(bwbidlist_checked_filename) self.pickFromTree(tree) # pprint(self.bwbid_list) logging.info("Dumping BWB ID list dictionary to {0}.".format(pickle_filename)) outfile = open(pickle_filename, 'w') pickle.dump(self.bwbid_list,outfile)
def _find_value(self, path, namespaces) -> Union[str, None]: """ Lookup a node using the path specified and then get its text value. :param path: The path to the node that is being found. :param namespaces: A dictionary of namespaces to use when processing the XML elements. :returns: The text value of the node associated with the specified path or None. """ # pylint: disable=broad-except rtnval = None try: valNode = self._ref_node.find(path, namespaces=namespaces) if valNode is not None: rtnval = valNode.text except Exception as err: print(str(err)) register_namespace('', None) xmlcontent = xml_tostring(self._ref_node) print(xmlcontent) print("") return rtnval
letters = "HALFTENTIMEACQUARTERTOTWENTYLFIVEPASTOMAFOUREIGHTWONEGOTENINEATSIXATHREELEVENTWELVENWAKESEVENFIVEUPSLEEPOCLOCK"; class SVG(object): def __getattr__(self, name): def f(*children, **kwargs): qname = QName("http://www.w3.org/2000/svg", name) e = Element(qname, **kwargs) e.extend(children) return e return f svg = SVG() register_namespace('svg', "http://www.w3.org/2000/svg") root = svg.svg( # fill svg.rect(x="0", y="0", width="85", height="85", fill="#000000"), width="8.5in", height="11in", viewBox="0 0 85 110", version="1.1", ); letters_elem = svg.g() circles_elem = svg.g() x = 0 y = 0 for letter in letters:
... <ItemGroup> <--- ItemGroup to delete <Compile Include="@(Antlr4GeneratedCodeFiles)" /> <_GeneratedCodeFiles Include="@(Antlr4GeneratedCodeFiles)" /> </ItemGroup> </Target> </Project> """ def isToDelete(node): return len([ n for n in node.getchildren() if n.tag.endswith('_GeneratedCodeFiles') ]) > 0 if __name__ == '__main__': path = sys.argv[1] register_namespace('', 'http://schemas.microsoft.com/developer/msbuild/2003') dom = parse(path) root = dom.getroot() #Project target = [ n for n in root.getchildren() if getattr(n, 'Name') == "Antlr4CompileAddFilesGenerated" ][0] for itemgroup in target.getchildren(): if isToDelete(itemgroup): target.remove(itemgroup) dom.write(path, xml_declaration=True)
def process_arg(arg): """Process the arg file""" print "Processing %s..." % arg tree = parse(arg) root = tree.getroot() global TOPO_SCHEMA TOPO_SCHEMA = get_topo_schema(root) register_namespace("", TOPO_SCHEMA) global WPT_TAG WPT_TAG = make_topo_tag("wpt") print "wpt_tag = %s" % WPT_TAG global RTEPT_TAG RTE_TAG = make_topo_tag("rte") global RTEPT_TAG RTEPT_TAG = make_topo_tag("rtept") waypoints = root.findall(WPT_TAG) # create a route from existing waypoints rte = Element(RTE_TAG, text="\n", tail="\n") rte = create_new_route(rte, waypoints) d = { "name": "Name on GPS", "cmt": "Comment", "desc": "Description", "src": "Source", "href": "https://drive.google.com/#folders/0ByBZFYeNib-uVG16bk92M3lodU0", "r_link_text": "r_link_text", "r_link_type": "r_link_type", "r_link": "link" } r_name = Element(make_topo_tag("name"), text="\n", tail="\n") r_name.text = d["name"] rte.append(r_name) r_cmt = Element(make_topo_tag("cmt"), text="\n", tail="\n") r_cmt.text = d["cmt"] rte.append(r_cmt) r_desc = Element(make_topo_tag("desc"), text="\n", tail="\n") r_desc.text = d["desc"] rte.append(r_desc) r_src = Element(make_topo_tag("src"), text="\n", tail="\n") r_src.text = d["src"] rte.append(r_src) r_link = Element(make_topo_tag("link"), text="\n", tail="\n") r_link.attrib["href"] = d["href"] r_link_text = Element(make_topo_tag("text"), text="\n", tail="\n") r_link_text.text = d["r_link_text"] r_link.append(r_link_text) r_link_type = Element(make_topo_tag("type"), text="\n", tail="\n") r_link_type.text = d["r_link_type"] r_link.append(r_link_type) r_link.text = d["r_link"] rte.append(r_link) r_number = Element(make_topo_tag("number"), text="\n", tail="\n") r_number.text = "1" rte.append(r_number) r_type = Element(make_topo_tag("type"), text="\n", tail="\n") r_type.text = "Route" rte.append(r_type) # create a new gpx Element gpx = Element(root.tag) version = Element(make_topo_tag("version")) version.text = "1.1" gpx.append(version) creator = Element(make_topo_tag("creator")) creator.text = "make_route.py by R. Oelschlaeger" gpx.append(creator) gpx.append(Element(make_topo_tag("metadata"))) # gpx.append(Element(make_topo_tag("wpt"))) gpx.append(rte) # gpx.append(Element(make_topo_tag("trk"))) newtree = ElementTree(gpx) # route.write('route.gpx', default_namespace=topo_schema) newtree.write('route.gpx', xml_declaration=True) # with open("xroute.gpx", "w") as sys.stdout: dump(newtree)
def _register_global_namespaces(self, namespace_dictionary): """ Register namespaces to allow output to include readable namespace aliases """ for prefix in namespace_dictionary: uri = namespace_dictionary[prefix] register_namespace(prefix, uri)
node.set('style', 'display:none;') except TypeError: pass try: for xpath in show: for node in document.findall(xpath, ns): print('Found node:', xpath, file=sys.stderr) node.set('style', 'display:inline;') except TypeError: pass document.write(sys.stdout) if __name__ == '__main__': parser = argparse.ArgumentParser(description='SVG transform') parser.add_argument('--show', action='append') parser.add_argument('--hide', action='append') parser.add_argument('filename') args = parser.parse_args() register_namespace('svg', 'http://www.w3.org/2000/svg') register_namespace('dc', 'http://purl.org/dc/elements/1.1/') register_namespace('cc', 'http://creativecommons.org/ns#') register_namespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#') register_namespace('xlink', 'http://www.w3.org/1999/xlink') register_namespace('sodipodi', 'http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd') register_namespace('inkscape', 'http://www.inkscape.org/namespaces/inkscape') register_namespace('', 'http://www.w3.org/2000/svg') document = parse(args.filename) transform_svg(document, args.hide, args.show)
from .parser import parse_path from .parser import parse_transform from .svg_to_paths import (path2pathd, ellipse2pathd, line2pathd, polyline2pathd, polygon2pathd, rect2pathd) from .misctools import open_in_browser from .path import * # To maintain forward/backward compatibility try: str = basestring except NameError: pass # Let xml.etree.ElementTree know about the SVG namespace SVG_NAMESPACE = {'svg': 'http://www.w3.org/2000/svg'} register_namespace('svg', 'http://www.w3.org/2000/svg') # THESE MUST BE WRAPPED TO OUTPUT ElementTree.element objects CONVERSIONS = { 'path': path2pathd, 'circle': ellipse2pathd, 'ellipse': ellipse2pathd, 'line': line2pathd, 'polyline': polyline2pathd, 'polygon': polygon2pathd, 'rect': rect2pathd } CONVERT_ONLY_PATHS = {'path': path2pathd} SVG_GROUP_TAG = 'svg:g'
"""Document structure <Project> ... <Target Name="Antlr4CompileAddFilesGenerated" AfterTargets="Antlr4Compile" Condition="'@(Antlr4)' != ''"> ... <ItemGroup> <--- ItemGroup to delete <Compile Include="@(Antlr4GeneratedCodeFiles)" /> <_GeneratedCodeFiles Include="@(Antlr4GeneratedCodeFiles)" /> </ItemGroup> </Target> </Project> """ def isToDelete(node): return len([n for n in node.getchildren() if n.tag.endswith('_GeneratedCodeFiles')]) > 0 if __name__ == '__main__': path = sys.argv[1] register_namespace('', 'http://schemas.microsoft.com/developer/msbuild/2003') dom = parse(path) root = dom.getroot() #Project target = [n for n in root.getchildren() if getattr(n, 'Name') == "Antlr4CompileAddFilesGenerated"][0] for itemgroup in target.getchildren(): if isToDelete(itemgroup): target.remove(itemgroup) dom.write(path, xml_declaration=True)
try: for xpath in show: for node in document.findall(xpath, ns): print('Found node:', xpath, file=sys.stderr) node.set('style', 'display:inline;') except TypeError: pass document.write(sys.stdout) if __name__ == '__main__': parser = argparse.ArgumentParser(description='SVG transform') parser.add_argument('--show', action='append') parser.add_argument('--hide', action='append') parser.add_argument('filename') args = parser.parse_args() register_namespace('svg', 'http://www.w3.org/2000/svg') register_namespace('dc', 'http://purl.org/dc/elements/1.1/') register_namespace('cc', 'http://creativecommons.org/ns#') register_namespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#') register_namespace('xlink', 'http://www.w3.org/1999/xlink') register_namespace('sodipodi', 'http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd') register_namespace('inkscape', 'http://www.inkscape.org/namespaces/inkscape') register_namespace('', 'http://www.w3.org/2000/svg') document = parse(args.filename) transform_svg(document, args.hide, args.show)
def define_namespace(prefix, url): register_namespace(prefix, url) return url