def record_add_field(rec, tag, ind1='', ind2='', subfields=[], controlfield_value=''): doc = Document() datafield = doc.createElement('datafield') datafield.setAttribute('tag', tag) datafield.setAttribute('ind1', ind1) datafield.setAttribute('ind2', ind2) for subfield in subfields: field = doc.createElement('subfield') field.setAttribute('code', subfield[0]) ## In order to be parsed it needs to a propper XML data = "<dummy>" + escape_for_xml(subfield[1]) + "</dummy>" try: data = parseString(data).firstChild for child in data.childNodes: field.appendChild(child.cloneNode(child)) except ExpatError: field.appendChild(doc.createTextNode(str(subfield[1]))) datafield.appendChild(field) if controlfield_value: controlfield = doc.createElement('controlfield') controlfield.setAttribute('tag', tag) controlfield.appendChild(doc.createTextNode(str(controlfield_value))) rec.appendChild(controlfield) else: rec.appendChild(datafield) return rec
def OutputXML(self,id,idlist,titlelist,geolist,refinelist): fname = gconfig.outputdir + '/' + '%s.xml' % id setid = set(idlist) setgeo = set(geolist) - setid settitle = set(titlelist) - setid setrefinelist = set(refinelist) - setid sets = [setid,setgeo,settitle,setrefinelist] tmpinfo = ["query by ID","query by Geo - ID", "query by Title - ID"," Pruning and Refine"] doc = Document() query = doc.createElement("query") query.setAttribute("id",id) doc.appendChild(query) results = doc.createElement("PhotoSets") for tmpset,info in zip(sets,tmpinfo): photoset = doc.createElement("photoset") photoset.setAttribute('query',info) photoset.setAttribute('photoNum',str(len(tmpset))) for photo in tmpset: ph = doc.createElement("photo") ph.setAttribute('url', photo) photoset.appendChild(ph) results.appendChild(photoset) query.appendChild(results) f = open(fname, "w") f.write(doc.toprettyxml(encoding='UTF-8')) f.close()
def getUpdates(): cache = apt.Cache() cache.upgrade(dist_upgrade=True) updates = cache.get_changes() doc = Document() host = doc.createElement("host") host.setAttribute("name", getfqdn()) doc.appendChild(host) for update in updates: u = doc.createElement("package") u.setAttribute("name", update.name) if update.installed: u.setAttribute("current_version", update.installed.version) u.setAttribute("new_version", update.candidate.version) u.setAttribute("source_name", update.candidate.source_name) try: origin = update.candidate.origins[0].origin u.setAttribute("origin", origin) except IndexError: pass if isSecurityUpgrade(update.candidate): u.setAttribute("is_security", "true") host.appendChild(u) return doc.toxml().replace('\n','')
class Output: def __init__(self, filename): self.filename = filename.replace(".", "_") + ".xml" self.doc = Document() self.rootNode = self.doc.createElement("package") self.doc.appendChild(self.rootNode) def insert(self, label, content = None, parent = None): item = self.doc.createElement(label) if content != None: item_content = self.doc.createTextNode(str(content)) item.appendChild(item_content) if parent == None: parent = self.rootNode parent.appendChild(item) return item def get_rootNode(self): pass def write(self): dir_ = "report/" if not os.path.isdir(dir_): os.makedirs(dir_) file = open(dir_ + self.filename, "a") file.write(self.doc.toprettyxml(indent="\t")) file.close()
def CopyBinaries(out_dir, out_project_dir, src_package, shared): # Copy jar files to libs. libs_dir = os.path.join(out_project_dir, 'libs') if not os.path.exists(libs_dir): os.mkdir(libs_dir) if shared: libs_to_copy = ['xwalk_core_library_java_app_part.jar'] elif src_package: libs_to_copy = ['jsr_305_javalib.jar', ] else: libs_to_copy = ['xwalk_core_library_java.jar', ] for lib in libs_to_copy: source_file = os.path.join(out_dir, 'lib.java', lib) target_file = os.path.join(libs_dir, lib) shutil.copyfile(source_file, target_file) if shared: return print 'Copying binaries...' # Copy assets. res_raw_dir = os.path.join(out_project_dir, 'res', 'raw') res_value_dir = os.path.join(out_project_dir, 'res', 'values') if not os.path.exists(res_raw_dir): os.mkdir(res_raw_dir) if not os.path.exists(res_value_dir): os.mkdir(res_value_dir) paks_to_copy = [ 'icudtl.dat', # Please refer to XWALK-3516, disable v8 use external startup data, # reopen it if needed later. # 'natives_blob.bin', # 'snapshot_blob.bin', 'xwalk.pak', ] pak_list_xml = Document() resources_node = pak_list_xml.createElement('resources') string_array_node = pak_list_xml.createElement('string-array') string_array_node.setAttribute('name', 'xwalk_resources_list') pak_list_xml.appendChild(resources_node) resources_node.appendChild(string_array_node) for pak in paks_to_copy: source_file = os.path.join(out_dir, pak) target_file = os.path.join(res_raw_dir, pak) shutil.copyfile(source_file, target_file) item_node = pak_list_xml.createElement('item') item_node.appendChild(pak_list_xml.createTextNode(pak)) string_array_node.appendChild(item_node) pak_list_file = open(os.path.join(res_value_dir, 'xwalk_resources_list.xml'), 'w') pak_list_xml.writexml(pak_list_file, newl='\n', encoding='utf-8') pak_list_file.close() # Copy native libraries. source_dir = os.path.join(out_dir, XWALK_CORE_SHELL_APK, 'libs') distutils.dir_util.copy_tree(source_dir, libs_dir)
def storeToFile(self, suffix=""): if not self.dataToStore: return doc = Document() results = doc.createElement("results") doc.appendChild(results) for a in self.dataToStore: item = doc.createElement("item") for k, d in a.items(): tag = doc.createElement(k) if k == "author": tag2 = doc.createElement("name") data = doc.createTextNode(d) tag2.appendChild(data) tag.appendChild(tag2) else: data = doc.createTextNode(" ".join(d.split())) tag.appendChild(data) item.appendChild(tag) results.appendChild(item) timestamp = time.strftime("%H%M%S_") f = open("output/" + timestamp + self.board + "_" + self.section + "_" + str(suffix) + ".xml", "w") f.write(doc.toprettyxml(indent=" ", encoding="utf-8")) f.close() self.dataToStore = []
def write_xml_hypergraph(hypergraph): """ Return a string specifying the given hypergraph as a XML document. @type hypergraph: hypergraph @param hypergraph: Hypergraph. @rtype: string @return: String specifying the graph as a XML document. """ # Document root grxml = Document() grxmlr = grxml.createElement('hypergraph') grxml.appendChild(grxmlr) # Each node... nodes = hypergraph.nodes() hyperedges = hypergraph.get_hyperedges() for each_node in (nodes + hyperedges): if (each_node in nodes): node = grxml.createElement('node') else: node = grxml.createElement('hyperedge') node.setAttribute('id',str(each_node)) grxmlr.appendChild(node) # and its outgoing edge for each_edge in hypergraph.get_links(each_node): edge = grxml.createElement('link') edge.setAttribute('to',str(each_edge)) node.appendChild(edge) return grxml.toprettyxml()
def distributePlan(txn, targets, plan): m = Document() message = m.appendChild(m.createElement('message')) message.setAttribute('kind', 'distribution') targetNodes = message.appendChild(m.createElement('targets')) planNode = message.appendChild(m.createElement('plan')) for t in targets: newTarget = m.createElement('target') for k,v in t.itervalues(): newTarget.setAttribute(k,v) targetNodes.appendChild(newTarget) for k,v in plan.itervalues(): if k is 'plan': continue planNode.setAttribute(k,v) planNode.appendChild(m.createTextNode(plan['plan'])) peers = txn.execute( 'SELECT peer FROM healing_group ORDER BY peer' ).fetchall() writer = BundleWriter('dtnhealing:') for p in peers: writer.write(p['peer'], m.toxml()) return targets, plan
def genXML( instlist, ssinfo ): """Write Ingres instance data out in XML.""" # Create document for instance data and base element instdoc = Document() pkginfo = instdoc.createElement( "IngresPackageInfo" ) instdoc.appendChild( pkginfo ) # Saveset element saveset = instdoc.createElement("saveset") saveset.setAttribute( "product", product ) pkginfo.appendChild( saveset ) # Instances element instances = instdoc.createElement("instances") pkginfo.appendChild( instances ) # write out saveset info first genXMLTagsFromDict( instdoc, saveset, ssinfo ) # loop through the list of instances and add to DOM for inst in instlist: # Create instance instance = instdoc.createElement("instance") instances.appendChild( instance ) # now iterate over all the keys using the keys as XML genXMLTagsFromDict( instdoc, instance, inst ) if pprint: print instdoc.toprettyxml(indent=" ") return writeXML( outfile, instdoc )
def test_node_to_dict2(): """ should return dict of node double nested """ doc = Document() outer = doc.createElement("outer") doc.appendChild(outer) mid1 = doc.createElement("mid1") outer.appendChild(mid1) mid2 = doc.createElement("mid2") outer.appendChild(mid2) inner1 = doc.createElement("inner1") inner2 = doc.createElement("inner2") mid1.appendChild(inner1) mid2.appendChild(inner2) inner1_text = doc.createTextNode("one") inner2_text = doc.createTextNode("two") inner1.appendChild(inner1_text) inner2.appendChild(inner2_text) expected_dict = {'outer': {'mid2': {'inner2': 'two'}, 'mid1': {'inner1': 'one'}}} xml_dict = xml.node_to_dict(doc) assert xml_dict == expected_dict
def save(self, filename): # ... create xml doc from xml.dom.minidom import Document # Create the minidom document doc = Document() # Create the <caid> base element rootElt = doc.createElement("field") # set camera attributs # eye = self.viewer.lookAt.GetEye() # rootElt.setAttribute("eye", str(eye)) doc.appendChild(rootElt) from caid.io import XML io = XML() # ... geometry geoElt = doc.createElement("geometry") geo = self.geometry doc = io.geotoxml(geo, doc, geoElt) rootElt.appendChild(geoElt) # ... geometry valuesElt = doc.createElement("values") values = self.values doc = io.geotoxml(values, doc, valuesElt) rootElt.appendChild(valuesElt) if filename is not None: f = open(filename, 'wr') s = doc.toprettyxml() f.write(s) f.close() else: print("No file was specified")
def parallalElement(deviceType, path, port): """ Creates a character device element, a <parallel> element, as a child of L{device element<devicesElement>}, that specifies the parallel character device to be used. @param deviceType: device type @type deviceType: String @param path: source path @type path: String @param port: port number @type port: String @return: <parallel> element @rtype: DOM Element """ document = Document() deviceElem = document.createElement('parallel') deviceElem.setAttribute('type', deviceType) portElem = document.createElement('source') portElem.setAttribute('path', path) portElem = document.createElement('target') portElem.setAttribute('port', port) deviceElem.appendChild(portElem) return deviceElem
def questions_to_aiml(questions): punctuation = "\"`~!@#$%^&()-_=+[{]}\|;:',<.>/?" _puncStripRE = re.compile("[" + re.escape(punctuation) + "]") # Create the minidom document doc = Document() # Create the <aiml> base element aiml = doc.createElement("aiml") doc.appendChild(aiml) for question, answer in questions: patterns = (re.sub(_puncStripRE, "", question), re.sub(_puncStripRE, "*", question)) for p in patterns: category = doc.createElement("category") pattern = doc.createElement("pattern") pattern.appendChild(doc.createTextNode(p.upper())) template = doc.createElement("template") template.appendChild(doc.createTextNode(answer)) category.appendChild(pattern) category.appendChild(template) aiml.appendChild(category) # Print our newly created XML return doc.toxml()
def featuresElement(pae=False, nonpae=False, acpi=False, apic=False): """ Creates a <features> element, a child of L{domain element <domainElement>}, that specifies mutually exclusive hypervisor features. @param pae: physical address extension mode @type pae: boolean @param nonpae: physical address extension mode @type nonpae: boolean @param acpi: acpi support @type acpi: boolean @param apic: apic support @type apic: boolean @return: <features> element @rtype: DOM Element """ document = Document() features = document.createElement('features') if pae: features.appendChild(document.createElement('pae')) if nonpae: features.appendChild(document.createElement('nonpae')) if acpi: features.appendChild(document.createElement('acpi')) if apic: features.appendChild(document.createElement('apic')) return features
def consoleElement(deviceType, port): """ Creates a character device element, a <console> element, as a child of L{device element<devicesElement>}, that specifies the console character device to be used. Examples from libvirt.org: Domain Logfile: This disables all input on the character device, and sends output into the virtual machine's logfile. - type: stdio - port: 1 @param deviceType: device type @type deviceType: String @param port: port number @type port: String @return: <console> element @rtype: String """ document = Document() deviceElem = document.createElement('console') deviceElem.setAttribute('type', deviceType) portElem = document.createElement('target') portElem.setAttribute('port', port) deviceElem.appendChild(portElem) return deviceElem
def Close(job_track_id): try: print "<close> job_track_id: %s" %job_track_id # Prepare the answer XML doc = Document() # Connect to the database db = MySQLdb.connect(sgjp_db_host,sgjp_db_user,sgjp_db_password,sgjp_db_name) cursor=db.cursor() # Get default file check step (seconds) and application name query="""update sgjp_tracked_jobs set end_ts=now() where job_tracking_id=%s;""" % job_track_id # Execute the SQL command cursor.execute(query) db.commit() db.close() doc_track = doc.createElement("result") doc_track.setAttribute("status", "OK") doc.appendChild(doc_track) except MySQLdb.Error, e: print 'exception!!!' # Rollback in case there is any error db.rollback() db.close() doc_error = doc.createElement("error") doc.appendChild(doc_error) doc_error.setAttribute("message", "Unable to close tracking with id: '%s'" %job_tracking_id) doc_error.setAttribute("sqlerr", repr(e)+"\n"+query)
def snapshots(job_tracking_id,file_id): try: print "<snapshots> job_tracking_id: %s - file_id: %s" % (job_tracking_id,file_id) # Prepare the answer XML doc = Document() # Connect to the database db = MySQLdb.connect(sgjp_db_host,sgjp_db_user,sgjp_db_password,sgjp_db_name) cursor=db.cursor() # Prepares the query query="""select snapshot_ts from sgjp_snapshots where job_tracking_id=%s and file_id=%s order by 1 asc;""" % (job_tracking_id,file_id) print "query: %s"%query cursor.execute(query) results = cursor.fetchall() doc_snapshots = doc.createElement("snapshots") doc.appendChild(doc_snapshots) for row in results: snapshot_ts = row[0] print "\t<snapshot_record> job_tracking_id: %s - file_id: %s - snapshot_ts: %s" % (job_tracking_id,file_id,snapshot_ts) # Put output into the XML doc_snapshots_snapshot = doc.createElement("snapshot") doc_snapshots_snapshot.setAttribute("job_tracking_id", "%s"%job_tracking_id) doc_snapshots_snapshot.setAttribute("file_id", "%s"%file_id) doc_snapshots_snapshot.setAttribute("snapshot_ts", "%s"%snapshot_ts) doc_snapshots.appendChild(doc_snapshots_snapshot) # Close connection db.close() except MySQLdb.Error, e: print 'exception!!!' db.close() doc_error = doc.createElement("error") doc.appendChild(doc_error) doc_error.setAttribute("message", "Unable to get job files") doc_error.setAttribute("sqlerr", repr(e)) doc_error.setAttribute("query", query)
def list_bucket(self): dirL = self.user.list_bucket(self.bucketName, self.request.args) doc = Document() # Create the <wml> base element xList = doc.createElement("ListBucketResult") xList.setAttribute("xmlns", "http://doc.s3.amazonaws.com/2006-03-01") doc.appendChild(xList) # Create the main <card> element xName = doc.createElement("Name") xList.appendChild(xName) xNameText = doc.createTextNode(str(self.bucketName)) xName.appendChild(xNameText) xIsTruncated = doc.createElement("IsTruncated") xList.appendChild(xIsTruncated) xIsTText = doc.createTextNode('false') xIsTruncated.appendChild(xIsTText) for obj in dirL: xObj = obj.create_xml_element(doc) xList.appendChild(xObj) x = doc.toxml(); self.send_xml(x) self.finish(self.request)
def end_copy(self): try: self.user.put_object(self.dst_file, self.dstBucketName, self.dstObjectName) self.grant_public_permissions(self.dstBucketName, self.dstObjectName) doc = Document() cor = doc.createElement("CopyObjectResult") doc.appendChild(cor) lm = doc.createElement("LastModified") cor.appendChild(lm) lmText = doc.createTextNode(datetime(*self.src_ctm[:6]).isoformat()) lm.appendChild(lmText) lm = doc.createElement("ETag") cor.appendChild(lm) lmText = doc.createTextNode(str(self.src_md5)) lm.appendChild(lmText) x = doc.toxml(); self.setHeader(self.request, 'x-amz-copy-source-version-id', "1") self.setHeader(self.request, 'x-amz-version-id', "1") self.send_xml(x) self.request.finish() except cbException, (ex): ex.sendErrorResponse(self.request, self.requestId) traceback.print_exc(file=sys.stdout)
def parse_type_of(obj): """ @return: A C{ParsedDocstring} that encodes the type of the given object. @rtype: L{ParsedDocstring} @param obj: The object whose type should be returned as DOM document. @type obj: any """ # This is a bit hackish; oh well. :) from epydoc.markup.epytext import ParsedEpytextDocstring from xml.dom.minidom import Document doc = Document() epytext = doc.createElement('epytext') para = doc.createElement('para') doc.appendChild(epytext) epytext.appendChild(para) if type(obj) is types.InstanceType: link = doc.createElement('link') name = doc.createElement('name') target = doc.createElement('target') para.appendChild(link) link.appendChild(name) link.appendChild(target) name.appendChild(doc.createTextNode(str(obj.__class__.__name__))) target.appendChild(doc.createTextNode(str(obj.__class__))) else: code = doc.createElement('code') para.appendChild(code) code.appendChild(doc.createTextNode(type(obj).__name__)) return ParsedEpytextDocstring(doc)
def formXml(self): """Form the XML to be written to RssFeeds.xml""" #create the document doc = Document() #create root element rssfeedsTag = doc.createElement('rssfeeds') doc.appendChild(rssfeedsTag) #create comments c1Tag = doc.createComment('RSS feeds. To have multiple feeds, just add a feed to the set. You can also have multiple sets.') c2Tag = doc.createComment('To use different sets in your skin, each must be called from skin with a unique id.') rssfeedsTag.appendChild(c1Tag) rssfeedsTag.appendChild(c2Tag) for setNum in sorted(self.feedsList.keys()): #create sets setTag = doc.createElement('set') #create attributes setTag.setAttribute('id', self.feedsList[setNum]['attrs']['id']) #only write rtl tags if they've been explicitly set if 'rtl' in self.feedsList[setNum]['attrs']: setTag.setAttribute('rtl', self.feedsList[setNum]['attrs']['rtl']) rssfeedsTag.appendChild(setTag) #create feed elements for feed in self.feedsList[setNum]['feedslist']: feedTag = doc.createElement('feed') feedTag.setAttribute('updateinterval', feed['updateinterval']) feedUrl = doc.createTextNode(feed['url']) feedTag.appendChild(feedUrl) setTag.appendChild(feedTag) return doc.toprettyxml(indent = ' ', encoding = 'UTF-8')
def encode(self): # Create the XML document doc = Document() signed_cred = doc.createElement("signed-credential") # Declare namespaces # Note that credential/policy.xsd are really the PG schemas # in a PL namespace. # Note that delegation of credentials between the 2 only really works # cause those schemas are identical. # Also note these PG schemas talk about PG tickets and CM policies. signed_cred.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") signed_cred.setAttribute("xsi:noNamespaceSchemaLocation", "http://www.geni.net/resources/credential/2/credential.xsd") signed_cred.setAttribute("xsi:schemaLocation", "http://www.planet-lab.org/resources/sfa/ext/policy/1 http://www.planet-lab.org/resources/sfa/ext/policy/1/policy.xsd") # PG says for those last 2: # signed_cred.setAttribute("xsi:noNamespaceSchemaLocation", "http://www.protogeni.net/resources/credential/credential.xsd") # signed_cred.setAttribute("xsi:schemaLocation", "http://www.protogeni.net/resources/credential/ext/policy/1 http://www.protogeni.net/resources/credential/ext/policy/1/policy.xsd") doc.appendChild(signed_cred) # Fill in the <credential> bit cred = doc.createElement("credential") cred.setAttribute("xml:id", self.get_refid()) signed_cred.appendChild(cred) append_sub(doc, cred, "type", "abac") # Stub fields append_sub(doc, cred, "serial", "8") append_sub(doc, cred, "owner_gid", '') append_sub(doc, cred, "owner_urn", '') append_sub(doc, cred, "target_gid", '') append_sub(doc, cred, "target_urn", '') append_sub(doc, cred, "uuid", "") if not self.expiration: self.set_expiration(datetime.datetime.utcnow() + datetime.timedelta(seconds=DEFAULT_CREDENTIAL_LIFETIME)) self.expiration = self.expiration.replace(microsecond=0) if self.expiration.tzinfo is not None and self.expiration.tzinfo.utcoffset(self.expiration) is not None: # TZ aware. Make sure it is UTC self.expiration = self.expiration.astimezone(tz.tzutc()) append_sub(doc, cred, "expires", self.expiration.strftime('%Y-%m-%dT%H:%M:%SZ')) # RFC3339 abac = doc.createElement("abac") rt0 = doc.createElement("rt0") abac.appendChild(rt0) cred.appendChild(abac) append_sub(doc, rt0, "version", "1.1") head = self.createABACElement(doc, "head", self.get_head()) rt0.appendChild(head) for tail in self.get_tails(): tailEle = self.createABACElement(doc, "tail", tail) rt0.appendChild(tailEle) # Create the <signatures> tag signatures = doc.createElement("signatures") signed_cred.appendChild(signatures) # Get the finished product self.xml = doc.toxml("utf-8")
def doStartSession(self, args={}): url = self.base_url + "/start.php" doc = Document() sessionNode = doc.createElement("session") sessionNode.setAttribute("mode", "desktop") userNode = doc.createElement("user") userNode.setAttribute("login", self.conf["login"]) userNode.setAttribute("password", self.conf["password"]) sessionNode.appendChild(userNode) if args.has_key("start-apps"): # launch applications at the session startup startappsNode = doc.createElement("start") for appid in args["start-apps"]: appNode = doc.createElement("application") appNode.setAttribute("id", appid) startappsNode.appendChild(appNode) sessionNode.appendChild(startappsNode) doc.appendChild(sessionNode) request = urllib2.Request(url, doc.toxml()) try: url = self.urlOpener.open(request) except urllib2.HTTPError, exc: if exc.code == 500: Logger.info("The service is not available") return False Logger.debug("HTTP request return code %d (%s)" % (exc.code, exc.msg)) return False
def save(self, filename=None): if filename is None: filename = self.filename # this means that self.filename is also None if filename is None: # Create a save file dialog from global_vars import CAIDWorkGroupwildcard dialog = wx.FileDialog ( None\ , style = wx.SAVE | wx.OVERWRITE_PROMPT\ , wildcard=CAIDWorkGroupwildcard) # Show the dialog and get user input if dialog.ShowModal() == wx.ID_OK: filename = dialog.GetPath() self.filename = filename # The user did not select anything else: print('Nothing was selected.') # Destroy the dialog dialog.Destroy() # ... create xml doc from xml.dom.minidom import Document # Create the minidom document doc = Document() # Create the <theme> base element rootElt = self.viewer.theme.save(doc=doc) # Create the <caid> base element rootElt = doc.createElement("caid") # set camera attributs eye = self.viewer.lookAt.GetEye() rootElt.setAttribute("eye", str(eye)) center = self.viewer.lookAt.GetCenter() rootElt.setAttribute("center", str(center)) up = self.viewer.lookAt.GetUp() rootElt.setAttribute("up", str(up)) doc.appendChild(rootElt) # ... # ... themeElt, doc = self.viewer.theme.save(doc=doc) rootElt.appendChild(themeElt) # ... # ... from caid.io import XML io = XML() for geo in self.list_geo: geo.save_attributs() geoElt = doc.createElement("geometry") doc = io.geotoxml(geo, doc, geoElt) rootElt.appendChild(geoElt) # ... if filename is not None: with open( filename, 'w' ) as f: f.write( doc.toprettyxml() ) else: print("No file was specified")
def CopyBinaries(out_dir): """cp out/Release/<pak> out/Release/xwalk_core_library/res/raw/<pak> cp out/Release/lib.java/<lib> out/Release/xwalk_core_library/libs/<lib> cp out/Release/xwalk_core_shell_apk/libs/* out/Release/xwalk_core_library/libs """ print 'Copying binaries...' # Copy assets. res_raw_dir = os.path.join( out_dir, LIBRARY_PROJECT_NAME, 'res', 'raw') res_value_dir = os.path.join( out_dir, LIBRARY_PROJECT_NAME, 'res', 'values') if not os.path.exists(res_raw_dir): os.mkdir(res_raw_dir) if not os.path.exists(res_value_dir): os.mkdir(res_value_dir) paks_to_copy = [ 'icudtl.dat', 'xwalk.pak', ] pak_list_xml = Document() resources_node = pak_list_xml.createElement('resources') string_array_node = pak_list_xml.createElement('string-array') string_array_node.setAttribute('name', 'xwalk_resources_list') pak_list_xml.appendChild(resources_node) resources_node.appendChild(string_array_node) for pak in paks_to_copy: source_file = os.path.join(out_dir, pak) target_file = os.path.join(res_raw_dir, pak) shutil.copyfile(source_file, target_file) item_node = pak_list_xml.createElement('item') item_node.appendChild(pak_list_xml.createTextNode(pak)) string_array_node.appendChild(item_node) pak_list_file = open(os.path.join(res_value_dir, 'xwalk_resources_list.xml'), 'w') pak_list_xml.writexml(pak_list_file, newl='\n', encoding='utf-8') pak_list_file.close() # Copy jar files to libs. libs_dir = os.path.join(out_dir, LIBRARY_PROJECT_NAME, 'libs') if not os.path.exists(libs_dir): os.mkdir(libs_dir) libs_to_copy = [ 'xwalk_core_library_java_app_part.jar', 'xwalk_core_library_java_library_part.jar', ] for lib in libs_to_copy: source_file = os.path.join(out_dir, 'lib.java', lib) target_file = os.path.join(libs_dir, lib) shutil.copyfile(source_file, target_file) # Copy native libraries. source_dir = os.path.join(out_dir, XWALK_CORE_SHELL_APK, 'libs') target_dir = libs_dir distutils.dir_util.copy_tree(source_dir, target_dir)
def printInherTree(self): """Creates and print out minidom structure => inheritance tree of whole Model""" # create minidom-document doc = Document() # create model element model = doc.createElement("model") doc.appendChild(model) # loop through all parent/base classes for cl in self.classInstances: if len(cl.inheritance) == 0: entry = doc.createElement("class") entry.setAttribute("name", cl.name) entry.setAttribute("kind", cl.kind) model.appendChild(entry) children = self.__findInheritanceRecursive(cl.name) if len(children) != 0: for ch in children: entry.appendChild(ch) # else: # entry.appendChild(Document().createTextNode("")) # insert empty node, to prevent having self closing node elif len(cl.inheritance) > 1: # check if conflict in the cl is possible if self.detectConflict(cl, cl): # print("conflict") raise Exception("Conflict in class: "+cl.name) doc.writexml(outputSrc, "", " "*indentation, "\n", encoding="utf-8")
def generate_profile_xml(self, user_info, pac_url, vpn_bypass_except): doc = Document() root = doc.createElement('parameters') doc.appendChild(root) item0 = doc.createElement('extra') item0.setAttribute('key', 'com.websense.android.wms.extra.PROXY_CONFIGURATION_ACTION') item0.setAttribute('value', 'com.websense.android.wms.SET_PROXY_CONFIGURATION') root.appendChild(item0) item1 = doc.createElement('extra') item1.setAttribute('key', 'com.websense.android.wms.extra.PROXY_PAC_FILE_URL') #add param for the url updated_pac_url = self.update_pacfile_type(pac_url) item1.setAttribute('value', updated_pac_url) root.appendChild(item1) item2 = doc.createElement('extra') item2.setAttribute('key', 'com.websense.android.wms.extra.PROXY_AUTH_STRING') item2.setAttribute('value', str(user_info)) root.appendChild(item2) item3 = doc.createElement('extra') item3.setAttribute('key', 'com.websense.android.wms.extra.PACKAGE_NAME') item3.setAttribute('value', 'com.thirdparty.mdmapplication') root.appendChild(item3) #add rules for local network accesss self.add_vpn_ondemandrules(doc, root, vpn_bypass_except) dd = doc.toprettyxml(indent="\t").encode('UTF-8') return dd
def txt_export(filepath): doc = Document() root = doc.createElement('data') doc.appendChild(root) for sce in bpy.data.scenes : #create a scene scene = doc.createElement('scene') scene.setAttribute('name', sce.name) root.appendChild(scene) for obj in sce.objects : if obj.type == 'FONT': #add object element object = doc.createElement('object') object.setAttribute('name', obj.name) txt_node = doc.createTextNode(obj.data.body) object.appendChild(txt_node) scene.appendChild(object) #write to a file file_handle = open(filepath,"wb") file_handle.write(bytes(doc.toprettyxml(indent='\t'), 'UTF-8')) file_handle.close()
def todaysNews(): query = News.all() query.order("-createdAt") results = query.fetch(limit=1) mostRecentNews = results.pop() # Create the minidom level document doc = Document() newsElement = doc.createElement("news") doc.appendChild(newsElement) #headline headlineElement = doc.createElement("headline") headline = doc.createTextNode(mostRecentNews.headline) headlineElement.appendChild(headline) newsElement.appendChild(headlineElement) #content contentElement = doc.createElement("content") content = doc.createTextNode(mostRecentNews.content) contentElement.appendChild(content) newsElement.appendChild(contentElement) #date dateElement = doc.createElement("date") date = doc.createTextNode(str(mostRecentNews.createdAt)) dateElement.appendChild(date) newsElement.appendChild(dateElement) out = doc.toxml() return out
class DictToXml(object): # pylint: disable=too-few-public-methods """Map dictionary into XML""" def __init__(self, structure): self.doc = Document() root_name = str(structure.keys()[0]) root = self.doc.createElement(root_name) self.doc.appendChild(root) self._build(root, structure[root_name]) def _build(self, parent, structure): if isinstance(structure, dict): for node_name in structure: tag = self.doc.createElement(node_name) parent.appendChild(tag) self._build(tag, structure[node_name]) elif isinstance(structure, list): for node_structure in structure: self._build(parent, node_structure) elif structure is None: return else: node_data = str(structure) tag = self.doc.createTextNode(node_data) parent.appendChild(tag) def __str__(self): # TODO implement separate method for pretty print return self.doc.toxml()
def createXML(metaDict): doc = Document() XMLNS_NS = u'stgermainmeta.xsd' XSI_NS = u'stgermainmeta.xsd' eMeta = doc.createElement(u'meta') eMeta.setAttribute(u'xmlns', 'urn:stgermainmeta-schema') eMeta.setAttributeNS(XMLNS_NS, u'xmlns:dc', u'http://purl.org/dc/elements/1.1/') eMeta.setAttributeNS(XMLNS_NS, u'xmlns:xsi', u'http://www.w3.org/2001/XMLSchema-instance') eMeta.setAttributeNS(XMLNS_NS, u'xmlns:xsd', u'http://www.w3.org/2001/XMLSchema') eMeta.setAttributeNS( XSI_NS, u'xsi:schemaLocation', u'http://purl.org/dc/elements/1.1/ dc.xsd http://www.w3.org/2001/XMLSchema XMLSchema.xsd urn:stgermainmeta-schema stgermainmeta.xsd' ) doc.appendChild(eMeta) eInfo = doc.createElement(u'info') eTitle = doc.createElement(u'dc:title') eCreator = doc.createElement(u'dc:creator') ePublisher = doc.createElement(u'dc:publisher') eRights = doc.createElement(u'dc:rights') eSource = doc.createElement(u'dc:source') eSubject = doc.createElement(u'dc:subject') eDescription = doc.createElement(u'dc:description') eInfo.appendChild(eTitle) eInfo.appendChild(eCreator) eInfo.appendChild(ePublisher) eInfo.appendChild(eRights) eInfo.appendChild(eSource) eInfo.appendChild(eSubject) eInfo.appendChild(eDescription) eMeta.appendChild(eInfo) eCode = doc.createElement(u'code') eExAnn = doc.createElement(u'xsd:annotation') eExDoc = doc.createElement(u'xsd:documentation') eExApp = doc.createElement(u'xsd:appinfo') eInherits = doc.createElement(u'inherits') eCode.appendChild(eExAnn) eExAnn.appendChild(eExDoc) eExAnn.appendChild(eExApp) eCode.appendChild(eInherits) eMeta.appendChild(eCode) eImplements = doc.createElement(u'implements') eReference = doc.createElement(u'reference') eEquation = doc.createElement(u'equation') eImplements.appendChild(eReference) eImplements.appendChild(eEquation) eMeta.appendChild(eImplements) eParameters = doc.createElement(u'parameters') eMeta.appendChild(eParameters) eAssociations = doc.createElement(u'associations') eMeta.appendChild(eAssociations) # Content... # Info (all required i.e. let is except if dictionary entry not there) ... eTitleTxt = doc.createTextNode(metaDict["info"]["title"]) eTitle.appendChild(eTitleTxt) eCreatorTxt = doc.createTextNode(metaDict["info"]["creator"]) eCreator.appendChild(eCreatorTxt) ePublisherTxt = doc.createTextNode(metaDict["info"]["publisher"]) ePublisher.appendChild(ePublisherTxt) eRightsTxt = doc.createTextNode(metaDict["info"]["rights"]) eRights.appendChild(eRightsTxt) eSourceTxt = doc.createTextNode(metaDict["info"]["source"]) eSource.appendChild(eSourceTxt) eSubjectTxt = doc.createTextNode(metaDict["info"]["subject"]) eSubject.appendChild(eSubjectTxt) eDescriptionTxt = doc.createCDATASection(metaDict["info"]["description"]) eDescription.appendChild(eDescriptionTxt) # Code (Not all required i.e. catch except if dictionary entry not there) ... try: eExDocTxt = doc.createCDATASection( metaDict["code"]["example-documentation"]) eExDoc.appendChild(eExDocTxt) except KeyError: pass try: eExAppTxt = doc.createCDATASection(metaDict["code"]["example-code"]) eExApp.appendChild(eExAppTxt) except KeyError: pass try: eInheritsTxt = doc.createTextNode(metaDict["code"]["inherits"]) eInherits.appendChild(eInheritsTxt) except KeyError: pass # Implements (Not all required i.e. catch except if dictionary entry not there) ... try: eReferenceTxt = doc.createTextNode(metaDict["implements"]["reference"]) eReference.appendChild(eReferenceTxt) except KeyError: pass try: eEquationTxt = doc.createCDATASection( metaDict["implements"]["equation"]) eEquation.appendChild(eEquationTxt) except KeyError: pass # Parameters (as this is an XML XSD type, the rules on requirment are determined by it) ... for param in metaDict["parameters"]: eParam = doc.createElement(u'xsd:element') eParam.setAttribute(u'name', param["name"]) eParam.setAttribute(u'type', param["type"]) try: eParam.setAttribute(u'default', param["default"]) except KeyError: pass try: eParamAnn = doc.createElement(u'xsd:annotation') eParamDoc = doc.createElement(u'xsd:documentation') eParamDocTxt = doc.createCDATASection(param["documentation"]) eParamDoc.appendChild(eParamDocTxt) eParamAnn.appendChild(eParamDoc) eParam.appendChild(eParamAnn) except KeyError: pass eParameters.appendChild(eParam) # Associations (as this is an XML XSD type, the rules on requirment are determined by it)... for assoc in metaDict["associations"]: eAssoc = doc.createElement(u'xsd:element') eAssoc.setAttribute(u'name', assoc["name"]) eAssoc.setAttribute(u'type', assoc["type"]) try: eAssoc.setAttribute(u'nillable', assoc["nillable"]) except KeyError: pass try: eAssocAnn = doc.createElement(u'xsd:annotation') eAssocDoc = doc.createElement(u'xsd:documentation') eAssocDocTxt = doc.createCDATASection(assoc["documentation"]) eAssocDoc.appendChild(eAssocDocTxt) eAssocAnn.appendChild(eAssocDoc) eAssoc.appendChild(eAssocAnn) except KeyError: pass eAssociations.appendChild(eAssoc) return doc
def save_to_xml(save_path, folder_name, im_width, im_height, im_depth, objects_axis, label_name): object_num = len(objects_axis) doc = Document() file_name = save_path.split('/')[-1] annotation = doc.createElement('annotation') doc.appendChild(annotation) folder = doc.createElement('folder') folder.appendChild(doc.createTextNode(folder_name)) annotation.appendChild(folder) path = doc.createElement('path') path.appendChild(doc.createTextNode(save_path)) annotation.appendChild(path) filename = doc.createElement('filename') filename.appendChild(doc.createTextNode(file_name)) annotation.appendChild(filename) source = doc.createElement('source') annotation.appendChild(source) database = doc.createElement('database') database.appendChild(doc.createTextNode('Unknown')) source.appendChild(database) size = doc.createElement('size') annotation.appendChild(size) # 需要修改的就是这部分,宽高 width = doc.createElement('width') width.appendChild(doc.createTextNode(str(im_width))) height = doc.createElement('height') height.appendChild(doc.createTextNode(str(im_height))) depth = doc.createElement('depth') depth.appendChild(doc.createTextNode(str(im_depth))) size.appendChild(width) size.appendChild(height) size.appendChild(depth) segmented = doc.createElement('segmented') segmented.appendChild(doc.createTextNode('0')) annotation.appendChild(segmented) # 需要添加目标 for i in range(object_num): objects = doc.createElement('object') annotation.appendChild(objects) object_name = doc.createElement('name') object_name.appendChild( doc.createTextNode(label_name[int(objects_axis[i][4])])) objects.appendChild(object_name) pose = doc.createElement('pose') pose.appendChild(doc.createTextNode('Unspecified')) objects.appendChild(pose) truncated = doc.createElement('truncated') truncated.appendChild(doc.createTextNode('0')) objects.appendChild(truncated) difficult = doc.createElement('difficult') difficult.appendChild(doc.createTextNode('0')) objects.appendChild(difficult) bndbox = doc.createElement('bndbox') objects.appendChild(bndbox) xmin = doc.createElement('xmin') xmin.appendChild(doc.createTextNode(str(objects_axis[i][0]))) bndbox.appendChild(xmin) ymin = doc.createElement('ymin') ymin.appendChild(doc.createTextNode(str(objects_axis[i][1]))) bndbox.appendChild(ymin) xmax = doc.createElement('xmax') xmax.appendChild(doc.createTextNode(str(objects_axis[i][2]))) bndbox.appendChild(xmax) ymax = doc.createElement('ymax') ymax.appendChild(doc.createTextNode(str(objects_axis[i][3]))) bndbox.appendChild(ymax) return doc
def get_scheme(self): """ Get the scheme of the inputs parameters and return as a string. """ # Create the XML document doc = Document() # Create the <scheme> base element element_scheme = doc.createElement("scheme") doc.appendChild(element_scheme) # Create the title element element_title = doc.createElement("title") element_scheme.appendChild(element_title) element_title_text = doc.createTextNode(self.title) element_title.appendChild(element_title_text) # Create the description element element_desc = doc.createElement("description") element_scheme.appendChild(element_desc) element_desc_text = doc.createTextNode(self.description) element_desc.appendChild(element_desc_text) # Create the use_external_validation element element_external_validation = doc.createElement( "use_external_validation") element_scheme.appendChild(element_external_validation) element_external_validation_text = doc.createTextNode( self.use_external_validation) element_external_validation.appendChild( element_external_validation_text) # Create the streaming_mode element element_streaming_mode = doc.createElement("streaming_mode") element_scheme.appendChild(element_streaming_mode) element_streaming_mode_text = doc.createTextNode(self.streaming_mode) element_streaming_mode.appendChild(element_streaming_mode_text) # Create the use_single_instance element element_use_single_instance = doc.createElement("use_single_instance") element_scheme.appendChild(element_use_single_instance) element_use_single_instance_text = doc.createTextNode( self.use_single_instance) element_use_single_instance.appendChild( element_use_single_instance_text) # Create the elements to stored args element element_endpoint = doc.createElement("endpoint") element_scheme.appendChild(element_endpoint) element_args = doc.createElement("args") element_endpoint.appendChild(element_args) # Create the argument elements self.add_xml_args(doc, element_args) # Return the content as a string return doc.toxml()
class XMLDump(object): ''' This class purpose is to dump the data into an xml Format. The format of the xml file is described in the scheme file xml/sqlmap.xsd ''' def __init__(self): self._outputFile = None self._outputFP = None self.__root = None self.__doc = Document() def _addToRoot(self, element): ''' Adds element to the root element ''' self.__root.appendChild(element) def __write(self, data, n=True): ''' Writes the data into the file ''' if n: self._outputFP.write("%s\n" % data) else: self._outputFP.write("%s " % data) self._outputFP.flush() kb.dataOutputFlag = True def _getRootChild(self, elemName): ''' Returns the child of the root with the described name ''' elements = self.__root.getElementsByTagName(elemName) if elements: return elements[0] return elements def _createTextNode(self, data): ''' Creates a text node with utf8 data inside. The text is escaped to an fit the xml text Format. ''' if data is None: return self.__doc.createTextNode(u'') else: escaped_data = saxutils.escape(data, ENTITIES) return self.__doc.createTextNode(escaped_data) def _createAttribute(self, attrName, attrValue): ''' Creates an attribute node with utf8 data inside. The text is escaped to an fit the xml text Format. ''' attr = self.__doc.createAttribute(attrName) if attrValue is None: attr.nodeValue = u'' else: attr.nodeValue = getUnicode(attrValue) return attr def string(self, header, data, sort=True): ''' Adds string element to the xml. ''' if isinstance(data, (list, tuple, set)): self.lister(header, data, sort) return messagesElem = self._getRootChild(MESSAGES_ELEM_NAME) if (not (messagesElem)): messagesElem = self.__doc.createElement(MESSAGES_ELEM_NAME) self._addToRoot(messagesElem) if data: data = self._formatString(data) else: data = "" elem = self.__doc.createElement(MESSAGE_ELEM) elem.setAttributeNode(self._createAttribute(TYPE_ATTR, header)) elem.appendChild(self._createTextNode(data)) messagesElem.appendChild(elem) def lister(self, header, elements, sort=True): ''' Adds information formatted as list element ''' lstElem = self.__doc.createElement(LST_ELEM_NAME) lstElem.setAttributeNode(self._createAttribute(TYPE_ATTR, header)) if elements: if sort: try: elements = set(elements) elements = list(elements) elements.sort(key=lambda x: x.lower()) except: pass for element in elements: memberElem = self.__doc.createElement(MEMBER_ELEM) lstElem.appendChild(memberElem) if isinstance(element, basestring): memberElem.setAttributeNode( self._createAttribute(TYPE_ATTR, "string")) memberElem.appendChild(self._createTextNode(element)) elif isinstance(element, (list, tuple, set)): memberElem.setAttributeNode( self._createAttribute(TYPE_ATTR, "list")) for e in element: memberElemStr = self.__doc.createElement(MEMBER_ELEM) memberElemStr.setAttributeNode( self._createAttribute(TYPE_ATTR, "string")) memberElemStr.appendChild( self._createTextNode(getUnicode(e))) memberElem.appendChild(memberElemStr) listsElem = self._getRootChild(LSTS_ELEM_NAME) if not (listsElem): listsElem = self.__doc.createElement(LSTS_ELEM_NAME) self._addToRoot(listsElem) listsElem.appendChild(lstElem) def technic(self, technicType, data): ''' Adds information about the technic used to extract data from the db ''' technicElem = self.__doc.createElement(TECHNIC_ELEM_NAME) technicElem.setAttributeNode( self._createAttribute(TYPE_ATTR, technicType)) textNode = self._createTextNode(data) technicElem.appendChild(textNode) technicsElem = self._getRootChild(TECHNICS_ELEM_NAME) if not (technicsElem): technicsElem = self.__doc.createElement(TECHNICS_ELEM_NAME) self._addToRoot(technicsElem) technicsElem.appendChild(technicElem) def banner(self, data): ''' Adds information about the database banner to the xml. The banner contains information about the type and the version of the database. ''' bannerElem = self.__doc.createElement(BANNER_ELEM_NAME) bannerElem.appendChild(self._createTextNode(data)) self._addToRoot(bannerElem) def currentUser(self, data): ''' Adds information about the current database user to the xml ''' currentUserElem = self.__doc.createElement(CURRENT_USER_ELEM_NAME) textNode = self._createTextNode(data) currentUserElem.appendChild(textNode) self._addToRoot(currentUserElem) def currentDb(self, data): ''' Adds information about the current database is use to the xml ''' currentDBElem = self.__doc.createElement(CURRENT_DB_ELEM_NAME) textNode = self._createTextNode(data) currentDBElem.appendChild(textNode) self._addToRoot(currentDBElem) def dba(self, isDBA): ''' Adds information to the xml that indicates whether the user has DBA privileges ''' isDBAElem = self.__doc.createElement(IS_DBA_ELEM_NAME) isDBAElem.setAttributeNode( self._createAttribute(VALUE_ATTR, getUnicode(isDBA))) self._addToRoot(isDBAElem) def users(self, users): ''' Adds a list of the existing users to the xml ''' usersElem = self.__doc.createElement(USERS_ELEM_NAME) if isinstance(users, basestring): users = [users] if users: for user in users: userElem = self.__doc.createElement(DB_USER_ELEM_NAME) usersElem.appendChild(userElem) userElem.appendChild(self._createTextNode(user)) self._addToRoot(usersElem) def dbs(self, dbs): ''' Adds a list of the existing databases to the xml ''' dbsElem = self.__doc.createElement(DBS_ELEM_NAME) if dbs: for db in dbs: dbElem = self.__doc.createElement(DB_NAME_ELEM_NAME) dbsElem.appendChild(dbElem) dbElem.appendChild(self._createTextNode(db)) self._addToRoot(dbsElem) def userSettings(self, header, userSettings, subHeader): ''' Adds information about the user's settings to the xml. The information can be user's passwords, privileges and etc.. ''' self._areAdmins = set() userSettingsElem = self._getRootChild(USER_SETTINGS_ELEM_NAME) if (not (userSettingsElem)): userSettingsElem = self.__doc.createElement( USER_SETTINGS_ELEM_NAME) self._addToRoot(userSettingsElem) userSettingElem = self.__doc.createElement(USER_SETTING_ELEM_NAME) userSettingElem.setAttributeNode( self._createAttribute(TYPE_ATTR, header)) if isinstance(userSettings, (tuple, list, set)): self._areAdmins = userSettings[1] userSettings = userSettings[0] users = userSettings.keys() users.sort(key=lambda x: x.lower()) for user in users: userElem = self.__doc.createElement(USER_ELEM_NAME) userSettingElem.appendChild(userElem) if user in self._areAdmins: userElem.setAttributeNode( self._createAttribute(TYPE_ATTR, ADMIN_USER)) else: userElem.setAttributeNode( self._createAttribute(TYPE_ATTR, REGULAR_USER)) settings = userSettings[user] settings.sort() for setting in settings: settingsElem = self.__doc.createElement(SETTINGS_ELEM_NAME) settingsElem.setAttributeNode( self._createAttribute(TYPE_ATTR, subHeader)) settingTextNode = self._createTextNode(setting) settingsElem.appendChild(settingTextNode) userElem.appendChild(settingsElem) userSettingsElem.appendChild(userSettingElem) def dbTables(self, dbTables): ''' Adds information of the existing db tables to the xml ''' if not isinstance(dbTables, dict): self.string(TABLES_ELEM_NAME, dbTables) return dbTablesElem = self.__doc.createElement(DB_TABLES_ELEM_NAME) for db, tables in dbTables.items(): tables.sort(key=lambda x: x.lower()) dbElem = self.__doc.createElement(DATABASE_ELEM_NAME) dbElem.setAttributeNode(self._createAttribute(NAME_ATTR, db)) dbTablesElem.appendChild(dbElem) for table in tables: tableElem = self.__doc.createElement(DB_TABLE_ELEM_NAME) tableElem.appendChild(self._createTextNode(table)) dbElem.appendChild(tableElem) self._addToRoot(dbTablesElem) def dbTableColumns(self, tableColumns): ''' Adds information about the columns of the existing tables to the xml ''' columnsElem = self._getRootChild(COLUMNS_ELEM_NAME) if not (columnsElem): columnsElem = self.__doc.createElement(COLUMNS_ELEM_NAME) for db, tables in tableColumns.items(): if not db: db = DEFAULT_DB dbElem = self.__doc.createElement(DATABASE_COLUMNS_ELEM) dbElem.setAttributeNode(self._createAttribute(NAME_ATTR, db)) columnsElem.appendChild(dbElem) for table, columns in tables.items(): tableElem = self.__doc.createElement(TABLE_ELEM_NAME) tableElem.setAttributeNode( self._createAttribute(NAME_ATTR, table)) colList = columns.keys() colList.sort(key=lambda x: x.lower()) for column in colList: colType = columns[column] colElem = self.__doc.createElement(COLUMN_ELEM_NAME) if colType is not None: colElem.setAttributeNode( self._createAttribute(TYPE_ATTR, colType)) else: colElem.setAttributeNode( self._createAttribute(TYPE_ATTR, UNKNOWN_COLUMN_TYPE)) colElem.appendChild(self._createTextNode(column)) tableElem.appendChild(colElem) self._addToRoot(columnsElem) def dbTableValues(self, tableValues): ''' Adds the values of specific table to the xml. The values are organized according to the relevant row and column. ''' tableElem = self.__doc.createElement(DB_TABLE_VALUES_ELEM_NAME) if (tableValues is not None): db = tableValues["__infos__"]["db"] if not db: db = "All" table = tableValues["__infos__"]["table"] count = int(tableValues["__infos__"]["count"]) columns = tableValues.keys() columns.sort(key=lambda x: x.lower()) tableElem.setAttributeNode(self._createAttribute(DB_ATTR, db)) tableElem.setAttributeNode(self._createAttribute(NAME_ATTR, table)) for i in range(count): rowElem = self.__doc.createElement(ROW_ELEM_NAME) tableElem.appendChild(rowElem) for column in columns: if column != "__infos__": info = tableValues[column] value = info["values"][i] if re.search("^[\ *]*$", value): value = "NULL" cellElem = self.__doc.createElement(CELL_ELEM_NAME) cellElem.setAttributeNode( self._createAttribute(COLUMN_ATTR, column)) cellElem.appendChild(self._createTextNode(value)) rowElem.appendChild(cellElem) dbValuesElem = self._getRootChild(DB_VALUES_ELEM) if (not (dbValuesElem)): dbValuesElem = self.__doc.createElement(DB_VALUES_ELEM) self._addToRoot(dbValuesElem) dbValuesElem.appendChild(tableElem) logger.info("Table '%s.%s' dumped to XML file" % (db, table)) def dbColumns(self, dbColumns, colConsider, dbs): ''' Adds information about the columns ''' for column in dbColumns.keys(): printDbs = {} for db, tblData in dbs.items(): for tbl, colData in tblData.items(): for col, dataType in colData.items(): if column in col: if db in printDbs: if tbl in printDbs[db]: printDbs[db][tbl][col] = dataType else: printDbs[db][tbl] = {col: dataType} else: printDbs[db] = {} printDbs[db][tbl] = {col: dataType} continue self.dbTableColumns(printDbs) def query(self, query, queryRes): ''' Adds details of an executed query to the xml. The query details are the query itself and its results. ''' queryElem = self.__doc.createElement(QUERY_ELEM_NAME) queryElem.setAttributeNode(self._createAttribute(VALUE_ATTR, query)) queryElem.appendChild(self._createTextNode(queryRes)) queriesElem = self._getRootChild(QUERIES_ELEM_NAME) if (not (queriesElem)): queriesElem = self.__doc.createElement(QUERIES_ELEM_NAME) self._addToRoot(queriesElem) queriesElem.appendChild(queryElem) def registerValue(self, registerData): ''' Adds information about an extracted registry key to the xml ''' registerElem = self.__doc.createElement(REGISTER_DATA_ELEM_NAME) registerElem.appendChild(self._createTextNode(registerData)) registriesElem = self._getRootChild(REGISTERY_ENTRIES_ELEM_NAME) if (not (registriesElem)): registriesElem = self.__doc.createElement( REGISTERY_ENTRIES_ELEM_NAME) self._addToRoot(registriesElem) registriesElem.appendChild(registerElem) def rFile(self, filePath, data): ''' Adds an extracted file's content to the xml ''' fileContentElem = self.__doc.createElement(FILE_CONTENT_ELEM_NAME) fileContentElem.setAttributeNode( self._createAttribute(NAME_ATTR, filePath)) fileContentElem.appendChild(self._createTextNode(data)) self._addToRoot(fileContentElem) def setOutputFile(self): ''' Initiates the xml file from the configuration. ''' if (conf.xmlFile): try: self._outputFile = conf.xmlFile self.__root = None if os.path.exists(self._outputFile): try: self.__doc = xml.dom.minidom.parse(self._outputFile) self.__root = self.__doc.childNodes[0] except ExpatError: self.__doc = Document() self._outputFP = codecs.open(self._outputFile, "w+", UNICODE_ENCODING) if self.__root is None: self.__root = self.__doc.createElementNS( NAME_SPACE_ATTR, RESULTS_ELEM_NAME) self.__root.setAttributeNode( self._createAttribute(XMLNS_ATTR, NAME_SPACE_ATTR)) self.__root.setAttributeNode( self._createAttribute(SCHEME_NAME_ATTR, SCHEME_NAME)) self.__doc.appendChild(self.__root) except IOError: raise SqlmapFilePathException( "Wrong filename provided for saving the xml file: %s" % conf.xmlFile) def getOutputFile(self): return self._outputFile def finish(self, resultStatus, resultMsg=""): ''' Finishes the dumper operation: 1. Adds the session status to the xml 2. Writes the xml to the file 3. Closes the xml file ''' if ((self._outputFP is not None) and not (self._outputFP.closed)): statusElem = self.__doc.createElement(STATUS_ELEM_NAME) statusElem.setAttributeNode( self._createAttribute(SUCESS_ATTR, getUnicode(resultStatus))) if not resultStatus: errorElem = self.__doc.createElement(ERROR_ELEM_NAME) if isinstance(resultMsg, Exception): errorElem.setAttributeNode( self._createAttribute(TYPE_ATTR, type(resultMsg).__name__)) else: errorElem.setAttributeNode( self._createAttribute(TYPE_ATTR, UNHANDLED_PROBLEM_TYPE)) errorElem.appendChild( self._createTextNode(getUnicode(resultMsg))) statusElem.appendChild(errorElem) self._addToRoot(statusElem) self.__write( prettyprint.formatXML(self.__doc, encoding=UNICODE_ENCODING)) self._outputFP.close()
class TestXMLBuilder(object): """This class encapsulates most rules needed to create a XML test report behind a simple interface. """ def __init__(self): """Creates a new instance. """ self._xml_doc = Document() self._current_context = None def current_context(self): """Returns the current context. """ return self._current_context def begin_context(self, tag, name): """Begins a new context in the XML test report, which usually is defined by one on the tags 'testsuites', 'testsuite', or 'testcase'. """ context = TestXMLContext(self._xml_doc, self._current_context) context.begin(tag, name) self._current_context = context def context_tag(self): """Returns the tag represented by the current context. """ return self._current_context.element_tag() def _create_cdata_section(self, content): """Returns a new CDATA section containing the string defined in `content`. """ filtered_content = replace_nontext(content) return self._xml_doc.createCDATASection(filtered_content) def append_cdata_section(self, tag, content): """Appends a tag in the format <tag>CDATA</tag> into the tag represented by the current context. Returns the created tag. """ element = self._xml_doc.createElement(tag) pos = content.find(']]>') while pos >= 0: tmp = content[0:pos + 2] element.appendChild(self._create_cdata_section(tmp)) content = content[pos + 2:] pos = content.find(']]>') element.appendChild(self._create_cdata_section(content)) self._append_child(element) return element def append(self, tag, content, **kwargs): """Apends a tag in the format <tag attr='val' attr2='val2'>CDATA</tag> into the tag represented by the current context. Returns the created tag. """ element = self._xml_doc.createElement(tag) for key, value in kwargs.items(): filtered_value = replace_nontext(six.text_type(value)) element.setAttribute(key, filtered_value) if content: element.appendChild(self._create_cdata_section(content)) self._append_child(element) return element def _append_child(self, element): """Appends a tag object represented by `element` into the tag represented by the current context. """ if self._current_context: self._current_context.element.appendChild(element) else: self._xml_doc.appendChild(element) def increment_counter(self, counter_name): """Increments a counter in the current context and their parents. """ context = self._current_context while context: context.increment_counter(counter_name) context = context.parent def end_context(self): """Ends the current context and sets the current context as being the previous one (if it exists). Also, when a context ends, its tag is appended in the proper place inside the document. """ if not self._current_context: return False element = self._current_context.end() self._current_context = self._current_context.parent self._append_child(element) return True def finish(self): """Ends all open contexts and returns a pretty printed version of the generated XML document. """ while self.end_context(): pass return self._xml_doc.toprettyxml(indent='\t', encoding=UTF8)
def inject_events(number_of_events, device): # Variables out_send = [0 for x in range(number_of_events)] err_send = [0 for x in range(number_of_events)] max_uint64 = np.iinfo(np.uint64).max # Prepare XML log document doc = Document() root = doc.createElement('root') doc.appendChild(root) # Send events for x in range(0, number_of_events): eid = np.uint64(rnd.randint(0, max_uint64)) epara = np.uint64(rnd.randint(0, max_uint64)) process_send = subprocess.Popen([ "saft-ctl", device, "inject", str(eid), str(epara), "0", "-v", "-x", ], stdout=subprocess.PIPE) out_send[x], err_send[x] = process_send.communicate() out_send_split = out_send[x].split() #print "send string:" print out_send[x] # Dump feedback (send stuff) to XML log file main = doc.createElement('event') root.appendChild(main) # Log ID if eid == int(out_send_split[3], 0): # check if given id was send p = doc.createElement('id') text = doc.createTextNode(out_send_split[3]) p.appendChild(text) main.appendChild(p) else: sys.exit(1) # Log parameter if epara == int(out_send_split[4], 0): # check if given parameter was send p = doc.createElement('parameter') text = doc.createTextNode(out_send_split[4]) p.appendChild(text) main.appendChild(p) else: sys.exit(1) # Log execution tme p = doc.createElement('time') text = doc.createTextNode(out_send_split[5]) p.appendChild(text) main.appendChild(p) # Save events to XML file f = open("injected_events.xml", "w") try: f.write(doc.toprettyxml(indent=" ")) finally: f.close() # Done return 0
def snoop_events(number_of_events, device): # Prepare XML log document doc = Document() root = doc.createElement('root') doc.appendChild(root) event_count = 0 # Snoop events process = subprocess.Popen( ["saft-ctl", device, "snoop", "0x0", "0x0", "0", "-x"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # Wait for events while True: line = process.stdout.readline() if not line: break else: event_count = event_count + 1 print line reception_string = line reception_string = reception_string.replace( "!", " ") # get rid of exclamation marks reception_string = reception_string.split() # Dump feedback (send stuff) to XML log file main = doc.createElement('event') root.appendChild(main) # Log id p = doc.createElement('id') text = doc.createTextNode(reception_string[3]) p.appendChild(text) main.appendChild(p) # Log parameter p = doc.createElement('parameter') text = doc.createTextNode(reception_string[5]) p.appendChild(text) main.appendChild(p) # Log execution time p = doc.createElement('time') text = doc.createTextNode(reception_string[1]) p.appendChild(text) main.appendChild(p) if (event_count == number_of_events): break # Close snoop process process.terminate() # Save events to XML file f = open("snooped_events.xml", "w") try: f.write(doc.toprettyxml(indent=" ")) finally: f.close() # Done return 0
class VulneraNetXMLReportGenerator(ReportGenerator): """ This class generates a report with the method printToFile(fileName) which contains the information of all the vulnerabilities notified to this object through the method logVulnerability(category,level,url,parameter,info). The format of the file is XML and it has the following structure: <report type="security"> <generatedBy id="Wapiti 2.3.0"/> <bugTypeList> <bugType name="SQL Injection"> <bugList/> <report> <vulnerabilityTypeList> <vulnerabilityType name="SQL Injection"> <vulnerabilityList> <vulnerability level="3"> <url>http://www.a.com</url> <parameters>id=23</parameters> <info>SQL Injection</info> </vulnerability> </vulnerabilityList> </vulnerabilityType> </vulnerabilityTypeList> </report> """ def __init__(self): self.__ts = datetime.datetime.now() self.__xmlDoc = Document() self.__vulnerabilityTypeList = None def setReportInfo(self, target, scope=None, date_string="", version=""): report = self.__xmlDoc.createElement("Report") report.setAttribute("generatedBy", version) report.setAttribute("generationDate", self.__ts.isoformat()) self.__vulnerabilityTypeList = self.__xmlDoc.createElement( "VulnerabilityTypeList") report.appendChild(self.__vulnerabilityTypeList) self.__xmlDoc.appendChild(report) def __addToVulnerabilityTypeList(self, vulnerabilityType): self.__vulnerabilityTypeList.appendChild(vulnerabilityType) def addVulnerabilityType(self, name, description="", solution="", references={}): """ This method adds a vulnerability type, it can be invoked to include in the report the type. The types are not stored previously, they are added when the method logVulnerability(category,level,url,parameter,info) is invoked and if there is no vulnerability of a type, this type will not be presented in the report """ vulnerabilityType = self.__xmlDoc.createElement("VulnerabilityType") vulnerabilityType.appendChild( self.__xmlDoc.createElement("VulnerabilityList")) vulTitleNode = self.__xmlDoc.createElement("Title") vulTitleNode.appendChild(self.__xmlDoc.createTextNode(name)) vulnerabilityType.appendChild(vulTitleNode) self.__addToVulnerabilityTypeList(vulnerabilityType) if description != "": descriptionNode = self.__xmlDoc.createElement("Description") descriptionNode.appendChild( self.__xmlDoc.createCDATASection(description)) vulnerabilityType.appendChild(descriptionNode) if solution != "": solutionNode = self.__xmlDoc.createElement("Solution") solutionNode.appendChild( self.__xmlDoc.createCDATASection(solution)) vulnerabilityType.appendChild(solutionNode) if references != "": referencesNode = self.__xmlDoc.createElement("References") for ref in references: referenceNode = self.__xmlDoc.createElement("Reference") nameNode = self.__xmlDoc.createElement("name") urlNode = self.__xmlDoc.createElement("url") nameNode.appendChild(self.__xmlDoc.createTextNode(ref)) urlNode.appendChild( self.__xmlDoc.createTextNode(references[ref])) referenceNode.appendChild(nameNode) referenceNode.appendChild(urlNode) referencesNode.appendChild(referenceNode) vulnerabilityType.appendChild(referencesNode) return vulnerabilityType def __addToVulnerabilityList(self, category, vulnerability): vulnerabilityType = None for node in self.__vulnerabilityTypeList.childNodes: titleNode = node.getElementsByTagName("Title") if (titleNode.length >= 1 and titleNode[0].childNodes.length == 1 and titleNode[0].childNodes[0].wholeText == category): vulnerabilityType = node break if vulnerabilityType is None: vulnerabilityType = self.addVulnerabilityType(category) vulnerabilityType.childNodes[0].appendChild(vulnerability) def logVulnerability(self, category=None, level=0, request=None, parameter="", info=""): """ Store the information about the vulnerability to be printed later. The method printToFile(fileName) can be used to save in a file the vulnerabilities notified through the current method. """ peer = None vulnerability = self.__xmlDoc.createElement("Vulnerability") if level == 1: stLevel = "Low" elif level == 2: stLevel = "Moderate" else: stLevel = "Important" levelNode = self.__xmlDoc.createElement("Severity") levelNode.appendChild(self.__xmlDoc.createTextNode(stLevel)) vulnerability.appendChild(levelNode) tsNode = self.__xmlDoc.createElement("DetectionDate") #tsNode.appendChild(self.__xmlDoc.createTextNode(ts.isoformat())) vulnerability.appendChild(tsNode) ## urlDetailNode = self.__xmlDoc.createElement("URLDetail") vulnerability.appendChild(urlDetailNode) urlNode = self.__xmlDoc.createElement("URL") urlNode.appendChild(self.__xmlDoc.createTextNode(request.url)) urlDetailNode.appendChild(urlNode) if peer is not None: peerNode = self.__xmlDoc.createElement("Peer") if isPeerAddrPort(peer): addrNode = self.__xmlDoc.createElement("Addr") addrNode.appendChild(self.__xmlDoc.createTextNode(peer[0])) peerNode.appendChild(addrNode) portNode = self.__xmlDoc.createElement("Port") portNode.appendChild(self.__xmlDoc.createTextNode(str( peer[1]))) peerNode.appendChild(portNode) else: addrNode = self.__xmlDoc.createElement("Addr") addrNode.appendChild(self.__xmlDoc.createTextNode(str(peer))) peerNode.appendChild(addrNode) urlDetailNode.appendChild(peerNode) parameterNode = self.__xmlDoc.createElement("Parameter") parameterNode.appendChild(self.__xmlDoc.createTextNode(parameter)) urlDetailNode.appendChild(parameterNode) ## infoNode = self.__xmlDoc.createElement("Info") info = info.replace("\n", "<br />") infoNode.appendChild(self.__xmlDoc.createTextNode(info)) urlDetailNode.appendChild(infoNode) self.__addToVulnerabilityList(category, vulnerability) def generateReport(self, filename): """ Create a xml file with a report of the vulnerabilities which have been logged with the method logVulnerability(category,level,url,parameter,info) """ f = open(filename, "w") try: f.write(self.__xmlDoc.toxml(encoding="UTF-8")) finally: f.close()
def make_v0(apps, apks, repodir, repodict, requestsdict, fdroid_signing_key_fingerprints): """ aka index.jar aka index.xml """ doc = Document() def addElement(name, value, doc, parent): el = doc.createElement(name) el.appendChild(doc.createTextNode(value)) parent.appendChild(el) def addElementNonEmpty(name, value, doc, parent): if not value: return addElement(name, value, doc, parent) def addElementIfInApk(name, apk, key, doc, parent): if key not in apk: return value = str(apk[key]) addElement(name, value, doc, parent) def addElementCheckLocalized(name, app, key, doc, parent, default=''): """Fill in field from metadata or localized block For name/summary/description, they can come only from the app source, or from a dir in fdroiddata. They can be entirely missing from the metadata file if there is localized versions. This will fetch those from the localized version if its not available in the metadata file. Attributes should be alpha-sorted, so they must be added in alpha- sort order. """ el = doc.createElement(name) value = app.get(key) lkey = key[:1].lower() + key[1:] localized = app.get('localized') if not value and localized: for lang in ['en-US'] + [x for x in localized.keys()]: if not lang.startswith('en'): continue if lang in localized: value = localized[lang].get(lkey) if value: break if not value and localized and len(localized) > 1: lang = list(localized.keys())[0] value = localized[lang].get(lkey) if not value: value = default if not value and name == 'name' and app.get('AutoName'): value = app['AutoName'] el.appendChild(doc.createTextNode(value)) parent.appendChild(el) root = doc.createElement("fdroid") doc.appendChild(root) repoel = doc.createElement("repo") repoel.setAttribute("icon", os.path.basename(repodict['icon'])) if 'maxage' in repodict: repoel.setAttribute("maxage", str(repodict['maxage'])) repoel.setAttribute("name", repodict['name']) pubkey, repo_pubkey_fingerprint = extract_pubkey() repoel.setAttribute("pubkey", pubkey.decode('utf-8')) repoel.setAttribute("timestamp", '%d' % repodict['timestamp'].timestamp()) repoel.setAttribute("url", repodict['address']) repoel.setAttribute("version", str(repodict['version'])) addElement('description', repodict['description'], doc, repoel) for mirror in repodict.get('mirrors', []): addElement('mirror', mirror, doc, repoel) root.appendChild(repoel) for command in ('install', 'uninstall'): for packageName in requestsdict[command]: element = doc.createElement(command) root.appendChild(element) element.setAttribute('packageName', packageName) for appid, appdict in apps.items(): app = metadata.App(appdict) if app.get('Disabled') is not None: continue # Get a list of the apks for this app... apklist = [] name_from_apk = None apksbyversion = collections.defaultdict(lambda: []) for apk in apks: if apk.get('versionCode') and apk.get('packageName') == appid: apksbyversion[apk['versionCode']].append(apk) if name_from_apk is None: name_from_apk = apk.get('name') for versionCode, apksforver in apksbyversion.items(): fdroidsig = fdroid_signing_key_fingerprints.get(appid, {}).get('signer') fdroid_signed_apk = None name_match_apk = None for x in apksforver: if fdroidsig and x.get('signer', None) == fdroidsig: fdroid_signed_apk = x if common.apk_release_filename.match(x.get('apkName', '')): name_match_apk = x # choose which of the available versions is most # suiteable for index v0 if fdroid_signed_apk: apklist.append(fdroid_signed_apk) elif name_match_apk: apklist.append(name_match_apk) else: apklist.append(apksforver[0]) if len(apklist) == 0: continue apel = doc.createElement("application") apel.setAttribute("id", app.id) root.appendChild(apel) addElement('id', app.id, doc, apel) if app.added: addElement('added', app.added.strftime('%Y-%m-%d'), doc, apel) if app.lastUpdated: addElement('lastupdated', app.lastUpdated.strftime('%Y-%m-%d'), doc, apel) addElementCheckLocalized('name', app, 'Name', doc, apel, name_from_apk) addElementCheckLocalized('summary', app, 'Summary', doc, apel) if app.icon: addElement('icon', app.icon, doc, apel) addElementCheckLocalized('desc', app, 'Description', doc, apel, 'No description available') addElement('license', app.License, doc, apel) if app.Categories: addElement('categories', ','.join(app.Categories), doc, apel) # We put the first (primary) category in LAST, which will have # the desired effect of making clients that only understand one # category see that one. addElement('category', app.Categories[0], doc, apel) addElement('web', app.WebSite, doc, apel) addElement('source', app.SourceCode, doc, apel) addElement('tracker', app.IssueTracker, doc, apel) addElementNonEmpty('changelog', app.Changelog, doc, apel) addElementNonEmpty('author', app.AuthorName, doc, apel) addElementNonEmpty('email', app.AuthorEmail, doc, apel) addElementNonEmpty('donate', app.Donate, doc, apel) addElementNonEmpty('bitcoin', app.Bitcoin, doc, apel) addElementNonEmpty('litecoin', app.Litecoin, doc, apel) addElementNonEmpty('flattr', app.FlattrID, doc, apel) addElementNonEmpty('liberapay', app.LiberapayID, doc, apel) addElementNonEmpty('openCollective', app.OpenCollective, doc, apel) # These elements actually refer to the current version (i.e. which # one is recommended. They are historically mis-named, and need # changing, but stay like this for now to support existing clients. addElement('marketversion', app.CurrentVersion, doc, apel) addElement('marketvercode', app.CurrentVersionCode, doc, apel) if app.Provides: pv = app.Provides.split(',') addElementNonEmpty('provides', ','.join(pv), doc, apel) if app.RequiresRoot: addElement('requirements', 'root', doc, apel) # Sort the apk list into version order, just so the web site # doesn't have to do any work by default... apklist = sorted(apklist, key=lambda apk: apk['versionCode'], reverse=True) if 'antiFeatures' in apklist[0]: app.AntiFeatures.extend(apklist[0]['antiFeatures']) if app.AntiFeatures: addElementNonEmpty('antifeatures', ','.join(app.AntiFeatures), doc, apel) # Check for duplicates - they will make the client unhappy... for i in range(len(apklist) - 1): first = apklist[i] second = apklist[i + 1] if first['versionCode'] == second['versionCode'] \ and first['sig'] == second['sig']: if first['hash'] == second['hash']: raise FDroidException( '"{0}/{1}" and "{0}/{2}" are exact duplicates!'.format( repodir, first['apkName'], second['apkName'])) else: raise FDroidException( 'duplicates: "{0}/{1}" - "{0}/{2}"'.format( repodir, first['apkName'], second['apkName'])) current_version_code = 0 current_version_file = None for apk in apklist: file_extension = common.get_file_extension(apk['apkName']) # find the APK for the "Current Version" if current_version_code < int(app.CurrentVersionCode): current_version_file = apk['apkName'] if current_version_code < apk['versionCode']: current_version_code = apk['versionCode'] apkel = doc.createElement("package") apel.appendChild(apkel) versionName = apk.get('versionName') if not versionName: versionCodeStr = str(apk['versionCode'] ) # TODO build.versionCode should be int! for build in app.get('Builds', []): if build[ 'versionCode'] == versionCodeStr and 'versionName' in build: versionName = build['versionName'] break if versionName: addElement('version', versionName, doc, apkel) addElement('versioncode', str(apk['versionCode']), doc, apkel) addElement('apkname', apk['apkName'], doc, apkel) addElementIfInApk('srcname', apk, 'srcname', doc, apkel) hashel = doc.createElement("hash") hashel.setAttribute('type', 'sha256') hashel.appendChild(doc.createTextNode(apk['hash'])) apkel.appendChild(hashel) addElement('size', str(apk['size']), doc, apkel) addElementIfInApk('sdkver', apk, 'minSdkVersion', doc, apkel) addElementIfInApk('targetSdkVersion', apk, 'targetSdkVersion', doc, apkel) addElementIfInApk('maxsdkver', apk, 'maxSdkVersion', doc, apkel) addElementIfInApk('obbMainFile', apk, 'obbMainFile', doc, apkel) addElementIfInApk('obbMainFileSha256', apk, 'obbMainFileSha256', doc, apkel) addElementIfInApk('obbPatchFile', apk, 'obbPatchFile', doc, apkel) addElementIfInApk('obbPatchFileSha256', apk, 'obbPatchFileSha256', doc, apkel) if 'added' in apk: addElement('added', apk['added'].strftime('%Y-%m-%d'), doc, apkel) if file_extension == 'apk': # sig is required for APKs, but only APKs addElement('sig', apk['sig'], doc, apkel) old_permissions = set() sorted_permissions = sorted(apk['uses-permission']) for perm in sorted_permissions: perm_name = perm[0] if perm_name.startswith("android.permission."): perm_name = perm_name[19:] old_permissions.add(perm_name) addElementNonEmpty('permissions', ','.join(sorted(old_permissions)), doc, apkel) for permission in sorted_permissions: permel = doc.createElement('uses-permission') if permission[1] is not None: permel.setAttribute('maxSdkVersion', '%d' % permission[1]) apkel.appendChild(permel) permel.setAttribute('name', permission[0]) for permission_sdk_23 in sorted(apk['uses-permission-sdk-23']): permel = doc.createElement('uses-permission-sdk-23') if permission_sdk_23[1] is not None: permel.setAttribute('maxSdkVersion', '%d' % permission_sdk_23[1]) apkel.appendChild(permel) permel.setAttribute('name', permission_sdk_23[0]) if 'nativecode' in apk: addElement('nativecode', ','.join(sorted(apk['nativecode'])), doc, apkel) addElementNonEmpty('features', ','.join(sorted(apk['features'])), doc, apkel) if current_version_file is not None \ and common.config['make_current_version_link'] \ and repodir == 'repo': # only create these namefield = common.config['current_version_name_source'] name = app.get(namefield) if not name and namefield == 'Name': name = app.get('localized', {}).get('en-US', {}).get('name') if not name: name = app.id sanitized_name = re.sub(b'''[ '"&%?+=/]''', b'', name.encode('utf-8')) apklinkname = sanitized_name + os.path.splitext( current_version_file)[1].encode('utf-8') current_version_path = os.path.join(repodir, current_version_file).encode( 'utf-8', 'surrogateescape') if os.path.islink(apklinkname): os.remove(apklinkname) os.symlink(current_version_path, apklinkname) # also symlink gpg signature, if it exists for extension in (b'.asc', b'.sig'): sigfile_path = current_version_path + extension if os.path.exists(sigfile_path): siglinkname = apklinkname + extension if os.path.islink(siglinkname): os.remove(siglinkname) os.symlink(sigfile_path, siglinkname) if common.options.pretty: output = doc.toprettyxml(encoding='utf-8') else: output = doc.toxml(encoding='utf-8') with open(os.path.join(repodir, 'index.xml'), 'wb') as f: f.write(output) if 'repo_keyalias' in common.config \ or (common.options.nosign and 'repo_pubkey' in common.config): if common.options.nosign: logging.info( _("Creating unsigned index in preparation for signing")) else: logging.info(_("Creating signed index with this key (SHA256):")) logging.info("%s" % repo_pubkey_fingerprint) # Create a jar of the index... jar_output = 'index_unsigned.jar' if common.options.nosign else 'index.jar' p = FDroidPopen(['jar', 'cf', jar_output, 'index.xml'], cwd=repodir) if p.returncode != 0: raise FDroidException("Failed to create {0}".format(jar_output)) # Sign the index... signed = os.path.join(repodir, 'index.jar') if common.options.nosign: # Remove old signed index if not signing if os.path.exists(signed): os.remove(signed) else: signindex.config = common.config signindex.sign_jar(signed) # Copy the repo icon into the repo directory... icon_dir = os.path.join(repodir, 'icons') repo_icon = common.config.get('repo_icon', common.default_config['repo_icon']) iconfilename = os.path.join(icon_dir, os.path.basename(repo_icon)) if os.path.exists(repo_icon): shutil.copyfile(common.config['repo_icon'], iconfilename) else: logging.warning( _('repo_icon %s does not exist, generating placeholder.') % repo_icon) os.makedirs(os.path.dirname(iconfilename), exist_ok=True) try: import qrcode qrcode.make(common.config['repo_url']).save(iconfilename) except Exception: exampleicon = os.path.join(common.get_examples_dir(), common.default_config['repo_icon']) shutil.copy(exampleicon, iconfilename)
def create_lane_changer_scenario(self, data, gf, out_path, xodr, osgb): """ 生成场景文件的主要函数 :param data: 预读取的数据 :param gf: GeneralFunc类实例 :param out_path: 输出路径 :param xodr: 地图文件 :param osgb: 地图数据库文件 :return: """ if not self.lane_changer(data['lane_offsets']): return filter_lane_offset = gf.draw_filter( data['lane_offsets'], 'ego', 'lane offsets: cm') # 滤波找峰值确定变道事件的区间 temp, temp1, min_val, max_val = 0, 0, 1000, 0 for i in range(len(filter_lane_offset)): if filter_lane_offset[i] < min_val: min_val = filter_lane_offset[i] temp = i if filter_lane_offset[i] > max_val: max_val = filter_lane_offset[i] temp1 = i left, right = min(temp, temp1), max(temp, temp1) if filter_lane_offset[left] < filter_lane_offset[right]: # 确定变道方向 direction = -1 else: direction = 1 ego_lane = list() el_lane = list() if direction == 1: # 筛选符合条件的对象 for idx, val in data['objects_range'].items(): if self.find_lane_cars(val[1], (-1.8, 1.8)): ego_lane.append(idx) if self.find_lane_cars(val[1], (1.8, 5.4)): el_lane.append(idx) else: for idx, val in data['objects_range'].items(): if self.find_lane_cars(val[1], (-1.8, 1.8)): ego_lane.append(idx) if self.find_lane_cars(val[1], (-5.4, -1.8)): el_lane.append(idx) ego_lane_target = 0.0 ego_target_init_x, ego_target_init_y, ego_target_init_speed = 1000, 0, 0 for i in ego_lane: x, y, s = gf.cal_init(data['ego_speed'], data['objects_speed'][i][0], data['objects_range'][i][0], data['objects_range'][i][1]) if x < ego_target_init_x: ego_lane_target, ego_target_init_x, ego_target_init_speed, ego_target_init_y = i, x, s, y el_lane_target = 0.0 el_target_init_x, el_target_init_y, el_target_init_speed = 1000, 0, 0 for i in el_lane: x, y, s = gf.cal_init(data['ego_speed'], data['objects_speed'][i][0], data['objects_range'][i][0], data['objects_range'][i][1]) if x < el_target_init_x: el_lane_target, el_target_init_x, el_target_init_speed, el_target_init_y = i, x, s, y kwargs = dict() entity = [ 'Ego', ] x_speed_list = {'Ego': data['ego_speed']} y_speed_list = dict() x_range_list = dict() y_range_list = dict() if ego_lane_target != 0.0: entity.append('Target' + str(ego_lane_target)) x_speed_list['Target' + str( ego_lane_target)] = data['objects_speed'][ego_lane_target][0] y_speed_list['Target' + str( ego_lane_target)] = data['objects_speed'][ego_lane_target][1] x_range_list['Target' + str( ego_lane_target)] = data['objects_range'][ego_lane_target][0] y_range_list['Target' + str( ego_lane_target)] = data['objects_range'][ego_lane_target][1] if el_lane_target != 0.0: entity.append('Target' + str(el_lane_target)) x_speed_list[ 'Target' + str(el_lane_target)] = data['objects_speed'][el_lane_target][0] y_speed_list[ 'Target' + str(el_lane_target)] = data['objects_speed'][el_lane_target][1] x_range_list[ 'Target' + str(el_lane_target)] = data['objects_range'][el_lane_target][0] y_range_list[ 'Target' + str(el_lane_target)] = data['objects_range'][el_lane_target][1] init_position = {'Ego': ['1.4', '-7.2', '0', '0', '0', '0']} init_speed = { 'Ego': x_speed_list['Ego'][0], 'Target' + str(ego_lane_target): ego_target_init_speed, 'Target' + str(el_lane_target): el_target_init_speed } init_position['Target' + str(ego_lane_target)] = [ str(1.4 + ego_target_init_x), str(-7.2 + ego_target_init_y), '0', '0', '0', '0' ] init_position['Target' + str(el_lane_target)] = [ str(1.4 + el_target_init_x), str(-7.2 + el_target_init_y), '0', '0', '0', '0' ] filter_x_list = dict() if ego_lane_target != 0.0: filter_x_list['Target' + str(ego_lane_target)] = gf.draw_filter( data['objects_speed'][ego_lane_target][0], '', '') if el_lane_target != 0.0: filter_x_list['Target' + str(el_lane_target)] = gf.draw_filter( data['objects_speed'][el_lane_target][0], '', '') filter_x_list['Ego'] = gf.draw_filter(data['ego_speed'], '', '') kwargs['out_path'] = out_path kwargs['GF'] = gf kwargs['xodr'] = xodr kwargs['osgb'] = osgb kwargs['entity'] = entity kwargs['event'] = {'Ego': (left, right, direction)} kwargs['speed'] = filter_x_list kwargs['init_speed'] = init_speed kwargs['init_pos'] = init_position doc = Document() GF = kwargs['GF'] # root node root = doc.createElement('OpenSCENARIO') doc.appendChild(root) # FileHeader header = GF.create_header(doc, 'cut in scenario') root.appendChild(header) # ParameterDeclaration pdcl = GF.create_parameter(doc) root.appendChild(pdcl) # Catalogs catalogs = GF.create_catalogs(doc) root.appendChild(catalogs) # RoadNetworks # '../Runtime/Tools/RodDistro_6710_Rod4.6.0/DefaultProject/Odr/city_quick_3.xodr' # '../Runtime/Tools/RodDistro_6710_Rod4.6.0/DefaultProject/Database/city_quick_3.opt.osgb' rnode = GF.create_road_network(doc, kwargs['xodr'], kwargs['osgb']) root.appendChild(rnode) # Entities entity_list = kwargs['entity'] enode = doc.createElement('Entities') for e in entity_list: obj_node = GF.create_entities(doc, e) enode.appendChild(obj_node) root.appendChild(enode) # StoryBoard snode = doc.createElement('Storyboard') # Init init_node = doc.createElement('Init') actions_node = doc.createElement('Actions') for i in entity_list: pr_node = GF.create_init_story(doc, kwargs['init_pos'][i], kwargs['init_speed'][i], i) actions_node.appendChild(pr_node) init_node.appendChild(actions_node) snode.appendChild(init_node) # Story story_node = doc.createElement('Story') story_node.setAttribute('name', 'LaneChangeStory') story_node.setAttribute('owner', '') act_node = doc.createElement('Act') act_node.setAttribute('name', 'LaneChangeAct') for key, value in kwargs['speed'].items(): # Sequence seq_node = doc.createElement('Sequence') seq_node.setAttribute('name', key + 'Sequence') seq_node.setAttribute('numberOfExecutions', '1') actors_node = doc.createElement('Actors') entity_node = doc.createElement('Entity') entity_node.setAttribute('name', key) actors_node.appendChild(entity_node) seq_node.appendChild(actors_node) maneuver_node = doc.createElement('Maneuver') maneuver_node.setAttribute('name', key + 'Maneuver') left, right, change = kwargs['event'].get(key, (-1, 0, 0)) if left >= 0: for idx in range(0, left): event_node = GF.create_event(doc, idx, key, value[idx]) maneuver_node.appendChild(event_node) event_node = doc.createElement('Event') event_node.setAttribute('name', 'LaneChanger' + str(change)) event_node.setAttribute('priority', 'overwrite') ac_node = self.create_lane_change_action_node( doc, { 'start': left, 'end': right, 'change_dir': change, 'obj': key }) ac_node.setAttribute('name', 'LaneChangeAction') event_node.appendChild(ac_node) scd_node = doc.createElement('StartConditions') cg_node = doc.createElement('ConditionGroup') cd_node = doc.createElement('Condition') cd_node.setAttribute('name', 'LaneChangeCondition') cd_node.setAttribute('delay', '0') cd_node.setAttribute('edge', 'rising') byvalue_node = doc.createElement('ByValue') simutime_node = doc.createElement('SimulationTime') simutime_node.setAttribute('value', str(left + 1)) simutime_node.setAttribute('rule', 'greater_than') byvalue_node.appendChild(simutime_node) cd_node.appendChild(byvalue_node) cg_node.appendChild(cd_node) scd_node.appendChild(cg_node) event_node.appendChild(scd_node) maneuver_node.appendChild(event_node) for index, val in enumerate(value[right:]): event_node = GF.create_event(doc, index + right, key, val) maneuver_node.appendChild(event_node) seq_node.appendChild(maneuver_node) act_node.appendChild(seq_node) # Conditions Sequence conditions_node = GF.create_conditions_node( doc, { 'value': '0', 'rule': 'greater_than', 'owner': '', 'root_name': 'Conditions' }) act_node.appendChild(conditions_node) story_node.appendChild(act_node) snode.appendChild(story_node) end_condition_node = doc.createElement('EndConditions') snode.appendChild(end_condition_node) root.appendChild(snode) with open(kwargs['out_path'], 'w') as f: doc.writexml(f, indent="\n", addindent="\t", encoding='utf-8')
def WriteLog(self, project): const_startend_time_format = "%04d%02d%02dT%02d%02d%02d" prj = project.strip() key_command = prj doc = Document() eleDoc = doc.createElement('Projects') eleParent = doc.createElement(self.projectName) if (self.start_time is not None): startLogNode = doc.createElement(const_start_time_node) time_log_lebel = const_startend_time_format % ( self.start_time.year, self.start_time.month, self.start_time.day, self.start_time.hour, self.start_time.minute, self.start_time.second) startLogNode.appendChild(doc.createTextNode(time_log_lebel)) eleParent.appendChild(startLogNode) # add start-time, end-time and the duration under each project node. end_time = datetime.now() if (self.end_time is not None): end_time = self.end_time endLogNode = doc.createElement('EndTime') time_log_lebel = const_startend_time_format % ( end_time.year, end_time.month, end_time.day, end_time.hour, end_time.minute, end_time.second) endLogNode.appendChild(doc.createTextNode(time_log_lebel)) eleParent.appendChild(endLogNode) durationLogNode = doc.createElement('TotalDuration') duration = end_time - self.start_time durationLogNode.appendChild( doc.createTextNode("%u" % (duration.total_seconds()))) eleParent.appendChild(durationLogNode) eleDoc.appendChild(eleParent) doc.appendChild(eleDoc) for key_ in self.command_order: if (key_ == prj or key_command == '#all'): if (key_command == '#all'): prj = key_ if (key_ != '__root'): eleProject = doc.createElement(prj) msgNode = None for key in self.projects[key_]: if (key == 'logs'): for msg in self.projects[prj]['logs']['message']: if ('text' in msg.keys()): nodeName = 'Message' if (msg['type'] == 'status'): nodeName = 'Status' eleMessage = doc.createElement(nodeName) eleText = doc.createTextNode(msg['text']) eleMessage.appendChild(eleText) msgNode = eleMessage elif ('error' in msg.keys()): eleError = doc.createElement('Error') eleErrorType = doc.createElement('type') eleErrorType.appendChild( doc.createTextNode(msg['error']['type'])) eleErrorText = doc.createElement('text') eleErrorText.appendChild( doc.createTextNode(msg['error']['text'])) eleError.appendChild(eleErrorType) eleError.appendChild(eleErrorText) # if warning/error begins without a parent 'Message' node, create an empty 'Message' parent node. if (msgNode is None): msgNode = doc.createElement('Message') eleMessage = msgNode msgNode.appendChild(eleError) if (key_ == '__root'): eleParent.appendChild(eleMessage) else: eleProject.appendChild(eleMessage) if ('DurationLabel' in self.projects[prj].keys()): durationLogNode = doc.createElement('Duration') durationLogNode.appendChild( doc.createTextNode( self.projects[prj]['DurationLabel'])) if (key_ == '__root'): eleParent.appendChild(durationLogNode) else: eleProject.appendChild(durationLogNode) if (key_ != '__root'): eleParent.appendChild(eleProject) eleDoc.appendChild(eleParent) try: # log reports can be saved uniquely named with date and time for easy review. ousr_date = datetime.now() prefix = self.logNamePrefix if (prefix == ''): prefix = 'log' recordUpdated = prefix + "_%04d%02d%02dT%02d%02d%02d.xml" % ( ousr_date.year, ousr_date.month, ousr_date.day, ousr_date.hour, ousr_date.minute, ousr_date.second) if (self.logFileName.strip() != ''): recordUpdated = self.logFileName if (recordUpdated[-4:].lower() != '.xml'): recordUpdated += '.xml' # try to create the log-folder if not found! if (os.path.exists(self.logFolder) == False): os.mkdir(self.logFolder) logPath = os.path.join(self.logFolder, recordUpdated) c = open(logPath, "w") c.write(doc.toprettyxml()) c.close() except: print("\nError creating log file.")
def GetXML(dictDonnees={}): """ Génération du fichier PES Recette ORMC """ doc = Document() # Génération du document XML racine = doc.createElement("n:PES_Aller") racine.setAttribute( "xsi:schemaLocation", "http://www.minefi.gouv.fr/cp/helios/pes_v2/recette/r0/aller") racine.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") racine.setAttribute("xmlns:xenc", "http://www.w3.org/2001/04/xmlenc#") racine.setAttribute("xmlns:xad", "http://uri.etsi.org/01903/v1.1.1#") racine.setAttribute( "xmlns:reca", "http://www.minefi.gouv.fr/cp/helios/pes_v2/recette/r0/aller") racine.setAttribute( "xmlns:n", "http://www.minefi.gouv.fr/cp/helios/pes_v2/Rev0/aller") racine.setAttribute("xmlns:cm", "http://www.minefi.gouv.fr/cp/helios/pes_v2/commun") racine.setAttribute( "xmlns:acta", "http://www.minefi.gouv.fr/cp/helios/pes_v2/etatactif/r0/aller") doc.appendChild(racine) # Enveloppe Enveloppe = doc.createElement("Enveloppe") racine.appendChild(Enveloppe) Parametres = doc.createElement("Parametres") Enveloppe.appendChild(Parametres) Version = doc.createElement("Version") Version.setAttribute("V", "2") Parametres.appendChild(Version) TypFic = doc.createElement("TypFic") TypFic.setAttribute("V", "PESORMC") Parametres.appendChild(TypFic) NomFic = doc.createElement("NomFic") NomFic.setAttribute("V", dictDonnees["nom_fichier"][:100]) Parametres.appendChild(NomFic) Emetteur = doc.createElement("Emetteur") Enveloppe.appendChild(Emetteur) Sigle = doc.createElement("Sigle") Sigle.setAttribute("V", "NOETHYS") Emetteur.appendChild(Sigle) versionLogiciel = FonctionsPerso.GetVersionLogiciel() Adresse = doc.createElement("Adresse") Adresse.setAttribute("V", _(u"Noethys %s") % versionLogiciel) Emetteur.appendChild(Adresse) # EnTetePES EnTetePES = doc.createElement("EnTetePES") racine.appendChild(EnTetePES) DteStr = doc.createElement("DteStr") DteStr.setAttribute("V", dictDonnees["date_emission"]) EnTetePES.appendChild(DteStr) IdPost = doc.createElement("IdPost") IdPost.setAttribute("V", dictDonnees["id_poste"][:7]) EnTetePES.appendChild(IdPost) IdColl = doc.createElement("IdColl") IdColl.setAttribute("V", dictDonnees["id_collectivite"][:14]) EnTetePES.appendChild(IdColl) CodCol = doc.createElement("CodCol") CodCol.setAttribute("V", dictDonnees["code_collectivite"][:3]) EnTetePES.appendChild(CodCol) CodBud = doc.createElement("CodBud") CodBud.setAttribute("V", dictDonnees["code_budget"][:10]) EnTetePES.appendChild(CodBud) # PES_RecetteAller PES_RecetteAller = doc.createElement("PES_RecetteAller") racine.appendChild(PES_RecetteAller) # EnTeteRecette EnTeteRecette = doc.createElement("EnTeteRecette") PES_RecetteAller.appendChild(EnTeteRecette) IdVer = doc.createElement("IdVer") IdVer.setAttribute("V", "2") EnTeteRecette.appendChild(IdVer) InfoDematerialisee = doc.createElement("InfoDematerialisee") InfoDematerialisee.setAttribute("V", "0") EnTeteRecette.appendChild(InfoDematerialisee) # Bordereau Bordereau = doc.createElement("Bordereau") PES_RecetteAller.appendChild(Bordereau) # Bloc Bordereau BlocBordereau = doc.createElement("BlocBordereau") Bordereau.appendChild(BlocBordereau) Exer = doc.createElement("Exer") Exer.setAttribute("V", dictDonnees["exercice"][:4]) BlocBordereau.appendChild(Exer) IdBord = doc.createElement("IdBord") IdBord.setAttribute("V", dictDonnees["id_bordereau"][:7]) BlocBordereau.appendChild(IdBord) DteBordEm = doc.createElement("DteBordEm") DteBordEm.setAttribute("V", dictDonnees["date_emission"]) BlocBordereau.appendChild(DteBordEm) TypBord = doc.createElement("TypBord") TypBord.setAttribute("V", "06") BlocBordereau.appendChild(TypBord) NbrPce = doc.createElement("NbrPce") NbrPce.setAttribute("V", str(len(dictDonnees["pieces"]))) BlocBordereau.appendChild(NbrPce) MtBordHt = doc.createElement("MtBordHt") MtBordHt.setAttribute("V", dictDonnees["montant_total"]) BlocBordereau.appendChild(MtBordHt) DteAsp = doc.createElement("DteAsp") DteAsp.setAttribute("V", dictDonnees["date_envoi"]) BlocBordereau.appendChild(DteAsp) Objet = doc.createElement("Objet") Objet.setAttribute("V", dictDonnees["objet_dette"][:160]) BlocBordereau.appendChild(Objet) # ----------------------- PIECES -------------------------------------------------------------------------------------------------------------------------- for dictPiece in dictDonnees["pieces"]: # Pièce Piece = doc.createElement("Piece") Bordereau.appendChild(Piece) BlocPiece = doc.createElement("BlocPiece") Piece.appendChild(BlocPiece) IdPce = doc.createElement("IdPce") IdPce.setAttribute("V", dictPiece["id_piece"][:8]) BlocPiece.appendChild(IdPce) TypPce = doc.createElement("TypPce") TypPce.setAttribute("V", "15") BlocPiece.appendChild(TypPce) NatPce = doc.createElement("NatPce") NatPce.setAttribute("V", "01") BlocPiece.appendChild(NatPce) if dictDonnees["pieces_jointes"] != False: Edition = doc.createElement("Edition") Edition.setAttribute("V", "03") BlocPiece.appendChild(Edition) ObjPce = doc.createElement("ObjPce") ObjPce.setAttribute("V", dictPiece["objet_piece"][:160]) BlocPiece.appendChild(ObjPce) NumDette = doc.createElement("NumDette") NumDette.setAttribute("V", dictPiece["num_dette"][:15]) BlocPiece.appendChild(NumDette) Per = doc.createElement("Per") Per.setAttribute("V", dictDonnees["mois"][:1]) BlocPiece.appendChild(Per) Cle1 = doc.createElement("Cle1") cle1 = GetCle_modulo11( (dictDonnees["code_collectivite"], dictDonnees["id_bordereau"], dictDonnees["exercice"][-2:], dictDonnees["mois"], u"{:0>13}".format(dictPiece["num_dette"]))) Cle1.setAttribute("V", str(cle1)) BlocPiece.appendChild(Cle1) Cle2 = doc.createElement("Cle2") cle2 = GetCle_modulo23( (dictDonnees["exercice"][-2:], dictDonnees["mois"], "00", u"{:0>13}".format(dictPiece["num_dette"]))) Cle2.setAttribute("V", cle2) BlocPiece.appendChild(Cle2) if dictDonnees["pieces_jointes"] != False: PJRef = doc.createElement("PJRef") BlocPiece.appendChild(PJRef) Support = doc.createElement("Support") Support.setAttribute("V", "01") PJRef.appendChild(Support) IdUnique = doc.createElement("IdUnique") IdUnique.setAttribute( "V", dictDonnees["pieces_jointes"][dictPiece["IDfacture"]] ["IdUnique"]) PJRef.appendChild(IdUnique) NomPJ = doc.createElement("NomPJ") NomPJ.setAttribute( "V", dictDonnees["pieces_jointes"][dictPiece["IDfacture"]]["NomPJ"]) PJRef.appendChild(NomPJ) NumeroFacture = doc.createElement("NumeroFacture") NumeroFacture.setAttribute("V", dictPiece["num_dette"][:20]) BlocPiece.appendChild(NumeroFacture) # NumeroMarche = doc.createElement("NumeroMarche") # NumeroMarche.setAttribute("V", "") # BlocPiece.appendChild(NumeroMarche) # NumeroEngagement = doc.createElement("NumeroEngagement") # NumeroEngagement.setAttribute("V", "") # BlocPiece.appendChild(NumeroEngagement) # CodeService = doc.createElement("CodeService") # CodeService.setAttribute("V", "") # BlocPiece.appendChild(CodeService) # NomService = doc.createElement("NomService") # NomService.setAttribute("V", "") # BlocPiece.appendChild(NomService) # Ligne de pièce LigneDePiece = doc.createElement("LigneDePiece") Piece.appendChild(LigneDePiece) BlocLignePiece = doc.createElement("BlocLignePiece") LigneDePiece.appendChild(BlocLignePiece) InfoLignePiece = doc.createElement("InfoLignePiece") BlocLignePiece.appendChild(InfoLignePiece) IdLigne = doc.createElement("IdLigne") IdLigne.setAttribute("V", "1") InfoLignePiece.appendChild(IdLigne) ObjLignePce = doc.createElement("ObjLignePce") ObjLignePce.setAttribute("V", dictPiece["objet_piece"][:160]) InfoLignePiece.appendChild(ObjLignePce) CodProdLoc = doc.createElement("CodProdLoc") CodProdLoc.setAttribute("V", dictDonnees["code_prodloc"][:4]) InfoLignePiece.appendChild(CodProdLoc) Nature = doc.createElement("Nature") Nature.setAttribute("V", "588") InfoLignePiece.appendChild(Nature) Majo = doc.createElement("Majo") Majo.setAttribute("V", "0") InfoLignePiece.appendChild(Majo) TvaIntraCom = doc.createElement("TvaIntraCom") TvaIntraCom.setAttribute("V", "0") InfoLignePiece.appendChild(TvaIntraCom) MtHT = doc.createElement("MtHT") MtHT.setAttribute("V", dictPiece["montant"]) InfoLignePiece.appendChild(MtHT) # Info prélèvement SEPA if dictPiece["prelevement"] == 1: InfoPrelevementSEPA = doc.createElement("InfoPrelevementSEPA") BlocLignePiece.appendChild(InfoPrelevementSEPA) NatPrel = doc.createElement("NatPrel") NatPrel.setAttribute("V", "01") InfoPrelevementSEPA.appendChild(NatPrel) PerPrel = doc.createElement("PerPrel") PerPrel.setAttribute("V", "07") InfoPrelevementSEPA.appendChild(PerPrel) DtePrel = doc.createElement("DtePrel") DtePrel.setAttribute("V", dictDonnees["date_prelevement"]) InfoPrelevementSEPA.appendChild(DtePrel) MtPrel = doc.createElement("MtPrel") MtPrel.setAttribute("V", dictPiece["montant"]) InfoPrelevementSEPA.appendChild(MtPrel) if dictPiece["sequence"] == "FRST": sequence = "02" elif dictPiece["sequence"] == "RCUR": sequence = "03" elif dictPiece["sequence"] == "FNAL": sequence = "04" else: sequence = "01" SequencePres = doc.createElement("SequencePres") SequencePres.setAttribute("V", sequence) InfoPrelevementSEPA.appendChild(SequencePres) DateSignMandat = doc.createElement("DateSignMandat") DateSignMandat.setAttribute("V", dictPiece["prelevement_date_mandat"]) InfoPrelevementSEPA.appendChild(DateSignMandat) RefUniMdt = doc.createElement("RefUniMdt") RefUniMdt.setAttribute("V", dictPiece["prelevement_rum"]) InfoPrelevementSEPA.appendChild(RefUniMdt) LibPrel = doc.createElement("LibPrel") LibPrel.setAttribute("V", dictPiece["prelevement_libelle"][:140]) InfoPrelevementSEPA.appendChild(LibPrel) # Tiers Tiers = doc.createElement("Tiers") LigneDePiece.appendChild(Tiers) InfoTiers = doc.createElement("InfoTiers") Tiers.appendChild(InfoTiers) if dictPiece["idtiers_helios"] != "": IdTiers = doc.createElement("IdTiers") IdTiers.setAttribute("V", dictPiece["idtiers_helios"]) InfoTiers.appendChild(IdTiers) if dictPiece["natidtiers_helios"] != "": NatIdTiers = doc.createElement("NatIdTiers") NatIdTiers.setAttribute("V", str(dictPiece["natidtiers_helios"])) InfoTiers.appendChild(NatIdTiers) if dictPiece["reftiers_helios"] != "": RefTiers = doc.createElement("RefTiers") RefTiers.setAttribute("V", dictPiece["reftiers_helios"]) InfoTiers.appendChild(RefTiers) CatTiers = doc.createElement("CatTiers") CatTiers.setAttribute("V", dictPiece["cattiers_helios"]) InfoTiers.appendChild(CatTiers) NatJur = doc.createElement("NatJur") NatJur.setAttribute("V", dictPiece["natjur_helios"]) InfoTiers.appendChild(NatJur) TypTiers = doc.createElement("TypTiers") TypTiers.setAttribute("V", "01") InfoTiers.appendChild(TypTiers) civilite = dictPiece["titulaire_civilite"] if civilite == "M.": civilite = "Mr" if civilite == "Melle": civilite = "Mme" if civilite not in (None, ""): Civilite = doc.createElement("Civilite") Civilite.setAttribute("V", civilite[:10]) InfoTiers.appendChild(Civilite) Nom = doc.createElement("Nom") Nom.setAttribute("V", dictPiece["titulaire_nom"][:38]) InfoTiers.appendChild(Nom) prenom = dictPiece["titulaire_prenom"] if prenom not in (None, ""): Prenom = doc.createElement("Prenom") Prenom.setAttribute("V", prenom[:38]) InfoTiers.appendChild(Prenom) # Adresse Adresse = doc.createElement("Adresse") Tiers.appendChild(Adresse) TypAdr = doc.createElement("TypAdr") TypAdr.setAttribute("V", "1") Adresse.appendChild(TypAdr) Adr2 = doc.createElement("Adr2") Adr2.setAttribute("V", dictPiece["titulaire_rue"][:38]) Adresse.appendChild(Adr2) CP = doc.createElement("CP") CP.setAttribute("V", dictPiece["titulaire_cp"][:5]) Adresse.appendChild(CP) Ville = doc.createElement("Ville") Ville.setAttribute("V", dictPiece["titulaire_ville"][:38]) Adresse.appendChild(Ville) CodRes = doc.createElement("CodRes") CodRes.setAttribute("V", "0") Adresse.appendChild(CodRes) # Compte bancaire if dictPiece["prelevement"] == 1: CpteBancaire = doc.createElement("CpteBancaire") Tiers.appendChild(CpteBancaire) BIC = doc.createElement("BIC") BIC.setAttribute("V", dictPiece["prelevement_bic"]) CpteBancaire.appendChild(BIC) IBAN = doc.createElement("IBAN") IBAN.setAttribute("V", dictPiece["prelevement_iban"]) CpteBancaire.appendChild(IBAN) TitCpte = doc.createElement("TitCpte") TitCpte.setAttribute("V", dictPiece["prelevement_titulaire"][:32]) CpteBancaire.appendChild(TitCpte) # ----------------------- PIECES JOINTES ------------------------------------------------------------------------------------------------------------------- if dictDonnees["pieces_jointes"] != False and len( dictDonnees["pieces_jointes"]) > 0: # PES_PJ PES_PJ = doc.createElement("PES_PJ") racine.appendChild(PES_PJ) # EnTetePES_PJ EnTetePES_PJ = doc.createElement("EnTetePES_PJ") PES_PJ.appendChild(EnTetePES_PJ) IdVer = doc.createElement("IdVer") IdVer.setAttribute("V", "1") EnTetePES_PJ.appendChild(IdVer) for IDfacture, dictPieceJointe in dictDonnees[ "pieces_jointes"].iteritems(): PJ = doc.createElement("PJ") PES_PJ.appendChild(PJ) Contenu = doc.createElement("Contenu") PJ.appendChild(Contenu) Fichier = doc.createElement("Fichier") Fichier.setAttribute("MIMEType", "application/pdf") Contenu.appendChild(Fichier) binaire = doc.createTextNode(dictPieceJointe["contenu"]) Fichier.appendChild(binaire) IdUnique = doc.createElement("IdUnique") IdUnique.setAttribute("V", dictPieceJointe["IdUnique"]) PJ.appendChild(IdUnique) NomPJ = doc.createElement("NomPJ") NomPJ.setAttribute("V", dictPieceJointe["NomPJ"]) PJ.appendChild(NomPJ) TypePJ = doc.createElement("TypePJ") TypePJ.setAttribute("V", "007") PJ.appendChild(TypePJ) Description = doc.createElement("Description") Description.setAttribute( "V", "FACTURE %s" % dictPieceJointe["numero_facture"]) PJ.appendChild(Description) return doc
def generate_name_id(value, sp_nq, sp_format=None, cert=None, debug=False, nq=None): """ Generates a nameID. :param value: fingerprint :type: string :param sp_nq: SP Name Qualifier :type: string :param sp_format: SP Format :type: string :param cert: IdP Public Cert to encrypt the nameID :type: string :param debug: Activate the xmlsec debug :type: bool :param nq: IDP Name Qualifier :type: string :returns: DOMElement | XMLSec nameID :rtype: string """ doc = Document() name_id_container = doc.createElementNS(OneLogin_Saml2_Constants.NS_SAML, 'container') name_id_container.setAttribute("xmlns:saml", OneLogin_Saml2_Constants.NS_SAML) name_id = doc.createElement('saml:NameID') if sp_nq is not None: name_id.setAttribute('SPNameQualifier', sp_nq) if nq is not None: name_id.setAttribute('NameQualifier', nq) if sp_format is not None: name_id.setAttribute('Format', sp_format) name_id.appendChild(doc.createTextNode(value)) name_id_container.appendChild(name_id) if cert is not None: xml = name_id_container.toxml() elem = fromstring(xml, forbid_dtd=True) error_callback_method = None if debug: error_callback_method = print_xmlsec_errors xmlsec.set_error_callback(error_callback_method) # Load the public cert mngr = xmlsec.KeysMngr() file_cert = OneLogin_Saml2_Utils.write_temp_file(cert) key_data = xmlsec.Key.load(file_cert.name, xmlsec.KeyDataFormatCertPem, None) key_data.name = basename(file_cert.name) mngr.addKey(key_data) file_cert.close() # Prepare for encryption enc_data = EncData(xmlsec.TransformAes128Cbc, type=xmlsec.TypeEncElement) enc_data.ensureCipherValue() key_info = enc_data.ensureKeyInfo() # enc_key = key_info.addEncryptedKey(xmlsec.TransformRsaPkcs1) enc_key = key_info.addEncryptedKey(xmlsec.TransformRsaOaep) enc_key.ensureCipherValue() # Encrypt! enc_ctx = xmlsec.EncCtx(mngr) enc_ctx.encKey = xmlsec.Key.generate(xmlsec.KeyDataAes, 128, xmlsec.KeyDataTypeSession) edata = enc_ctx.encryptXml(enc_data, elem[0]) newdoc = parseString(tostring(edata, encoding='unicode').encode('utf-8'), forbid_dtd=True) if newdoc.hasChildNodes(): child = newdoc.firstChild child.removeAttribute('xmlns') child.removeAttribute('xmlns:saml') child.setAttribute('xmlns:xenc', OneLogin_Saml2_Constants.NS_XENC) child.setAttribute('xmlns:dsig', OneLogin_Saml2_Constants.NS_DS) nodes = newdoc.getElementsByTagName("*") for node in nodes: if node.tagName == 'ns0:KeyInfo': node.tagName = 'dsig:KeyInfo' node.removeAttribute('xmlns:ns0') node.setAttribute('xmlns:dsig', OneLogin_Saml2_Constants.NS_DS) else: node.tagName = 'xenc:' + node.tagName encrypted_id = newdoc.createElement('saml:EncryptedID') encrypted_data = newdoc.replaceChild(encrypted_id, newdoc.firstChild) encrypted_id.appendChild(encrypted_data) return newdoc.saveXML(encrypted_id) else: return doc.saveXML(name_id)
def getXML(articleURL): com = commentsObj(articleURL) atitle = com.title() if atitle is None: return # Create the minidom document doc = Document() # Create the base element tw = doc.createElement("twither") doc.appendChild(tw) f = urllib.urlopen( 'http://twitterweather.media.mit.edu/blank.html' ) # if we don't use urlopen before the threads, this crashes Python in Snow Leopard. http://bugs.python.org/issue6851 # check if there's already a results file that's recent termq = urllib.quote(atitle, '&') if os.path.exists(servervar.CACHE_DIR + termq + '.xml') and time.time( ) - os.path.getmtime(servervar.CACHE_DIR + termq + '.xml') < servervar.REFRESH_PERIOD: g = open(servervar.CACHE_DIR + termq + '.xml') maincard = parse(g).firstChild tw.appendChild(maincard) g.close() else: weather = com.weather() # tuple title,score if weather is None: return asource = articleURL.split('/')[2] # get domain name out of URL # Create the main element maincard = doc.createElement("place") tw.appendChild(maincard) eterm = doc.createElement("term") maincard.appendChild(eterm) tterm = doc.createTextNode(cgi.escape(atitle)) eterm.appendChild(tterm) escore = doc.createElement("score") maincard.appendChild(escore) if weather[1] is not None: tscore = doc.createTextNode(str(weather[1])) escore.appendChild(tscore) eterm = doc.createElement("source") maincard.appendChild(eterm) tterm = doc.createTextNode(asource) eterm.appendChild(tterm) eterm = doc.createElement("url") maincard.appendChild(eterm) tterm = doc.createTextNode(cgi.escape(articleURL)) eterm.appendChild(tterm) # cache it g = open(servervar.CACHE_DIR + termq + '.xml', 'w') g.write(maincard.toprettyxml(indent=" ")) g.close() # Save/print our newly created XML xmldata = doc.toprettyxml(indent=" ") #doc.unlink() return xmldata
def testElement(): dom = Document() dom.appendChild(dom.createElement("abc")) confirm(dom.documentElement) dom.unlink()
xmax_new = xmax * w_new / w xmin_lst.append(xmin_new) xmax_lst.append(xmax_new) ymin_lst.append(ymin_new) ymax_lst.append(ymax_new) #cv2.rectangle(img_new, (xmin_new,ymin_new), (xmax_new,ymax_new), (0, 0, 255)) #cv2.imwrite("/opt/zhangjing/caffe/data/actions_new/benchmark/1.jpg", img_new) #cv2.imwrite(save_image_dir + "/" + file, img_new) #重写xml # 创建dom文档 doc = Document() # 创建根节点 orderlist = doc.createElement('annotation') # 根节点插入dom树 doc.appendChild(orderlist) # 每一组信息先创建节点<folder>,然后插入到父节点<orderlist>下 folder = doc.createElement('folder') orderlist.appendChild(folder) # 创建<folder>下的文本节点 folder_text = doc.createTextNode("VOC2007") # 将文本节点插入到<folder>下 folder.appendChild(folder_text) filename = doc.createElement('filename') orderlist.appendChild(filename) filename_text = doc.createTextNode(file) filename.appendChild(filename_text)
def save_to_xml(save_path, im_height, im_width, objects_axis, label_name): im_depth = 0 object_num = len(objects_axis) doc = Document() annotation = doc.createElement('annotation') doc.appendChild(annotation) folder = doc.createElement('folder') folder_name = doc.createTextNode('VOC2007') folder.appendChild(folder_name) annotation.appendChild(folder) filename = doc.createElement('filename') filename_name = doc.createTextNode('000024.jpg') filename.appendChild(filename_name) annotation.appendChild(filename) source = doc.createElement('source') annotation.appendChild(source) database = doc.createElement('database') database.appendChild(doc.createTextNode('The VOC2007 Database')) source.appendChild(database) annotation_s = doc.createElement('annotation') annotation_s.appendChild(doc.createTextNode('PASCAL VOC2007')) source.appendChild(annotation_s) image = doc.createElement('image') image.appendChild(doc.createTextNode('flickr')) source.appendChild(image) flickrid = doc.createElement('flickrid') flickrid.appendChild(doc.createTextNode('322409915')) source.appendChild(flickrid) owner = doc.createElement('owner') annotation.appendChild(owner) flickrid_o = doc.createElement('flickrid') flickrid_o.appendChild(doc.createTextNode('knautia')) owner.appendChild(flickrid_o) name_o = doc.createElement('name') name_o.appendChild(doc.createTextNode('gum')) owner.appendChild(name_o) size = doc.createElement('size') annotation.appendChild(size) width = doc.createElement('width') width.appendChild(doc.createTextNode(str(im_width))) height = doc.createElement('height') height.appendChild(doc.createTextNode(str(im_height))) depth = doc.createElement('depth') depth.appendChild(doc.createTextNode(str(im_depth))) size.appendChild(width) size.appendChild(height) size.appendChild(depth) segmented = doc.createElement('segmented') segmented.appendChild(doc.createTextNode('0')) annotation.appendChild(segmented) for i in range(object_num): objects = doc.createElement('object') annotation.appendChild(objects) object_name = doc.createElement('name') object_name.appendChild( doc.createTextNode(label_name[int(objects_axis[i][-1])])) objects.appendChild(object_name) pose = doc.createElement('pose') pose.appendChild(doc.createTextNode('Unspecified')) objects.appendChild(pose) truncated = doc.createElement('truncated') truncated.appendChild(doc.createTextNode('1')) objects.appendChild(truncated) difficult = doc.createElement('difficult') difficult.appendChild(doc.createTextNode('0')) objects.appendChild(difficult) bndbox = doc.createElement('bndbox') objects.appendChild(bndbox) x0 = doc.createElement('x0') x0.appendChild(doc.createTextNode(str((objects_axis[i][0])))) bndbox.appendChild(x0) y0 = doc.createElement('y0') y0.appendChild(doc.createTextNode(str((objects_axis[i][1])))) bndbox.appendChild(y0) x1 = doc.createElement('x1') x1.appendChild(doc.createTextNode(str((objects_axis[i][2])))) bndbox.appendChild(x1) y1 = doc.createElement('y1') y1.appendChild(doc.createTextNode(str((objects_axis[i][3])))) bndbox.appendChild(y1) x2 = doc.createElement('x2') x2.appendChild(doc.createTextNode(str((objects_axis[i][4])))) bndbox.appendChild(x2) y2 = doc.createElement('y2') y2.appendChild(doc.createTextNode(str((objects_axis[i][5])))) bndbox.appendChild(y2) x3 = doc.createElement('x3') x3.appendChild(doc.createTextNode(str((objects_axis[i][6])))) bndbox.appendChild(x3) y3 = doc.createElement('y3') y3.appendChild(doc.createTextNode(str((objects_axis[i][7])))) bndbox.appendChild(y3) f = open(save_path, 'w') f.write(doc.toprettyxml(indent='')) f.close()
def testAttributeRepr(): dom = Document() el = dom.appendChild(dom.createElement(u"abc")) node = el.setAttribute("abc", "def") confirm(str(node) == repr(node)) dom.unlink()
def convertimgset(img_set="train"): imgdir = rootdir + "/WIDER_" + img_set + "/images" gtfilepath = rootdir + "/wider_face_split/wider_face_" + img_set + "_bbx_gt.txt" imagesdir = rootdir + "/images" vocannotationdir = rootdir + "/Annotations" labelsdir = rootdir + "/labels" if not os.path.exists(imagesdir): os.mkdir(imagesdir) if convet2yoloformat: if not os.path.exists(labelsdir): os.mkdir(labelsdir) if convert2vocformat: if not os.path.exists(vocannotationdir): os.mkdir(vocannotationdir) index = 0 with open(gtfilepath, 'r') as gtfile: while (True): #and len(faces)<10 filename = gtfile.readline()[:-1] if (filename == ""): break sys.stdout.write("\r" + str(index) + ":" + filename + "\t\t\t") sys.stdout.flush() imgpath = imgdir + "/" + filename img = cv2.imread(imgpath) if not img.data: break imgheight = img.shape[0] imgwidth = img.shape[1] maxl = max(imgheight, imgwidth) paddingleft = (maxl - imgwidth) >> 1 paddingright = (maxl - imgwidth) >> 1 paddingbottom = (maxl - imgheight) >> 1 paddingtop = (maxl - imgheight) >> 1 saveimg = cv2.copyMakeBorder(img, paddingtop, paddingbottom, paddingleft, paddingright, cv2.BORDER_CONSTANT, value=0) showimg = saveimg.copy() numbbox = int(gtfile.readline()) bboxes = [] for i in range(numbbox): line = gtfile.readline() line = line.split() line = line[0:4] if (int(line[3]) <= 0 or int(line[2]) <= 0): continue x = int(line[0]) + paddingleft y = int(line[1]) + paddingtop width = int(line[2]) height = int(line[3]) bbox = (x, y, width, height) x2 = x + width y2 = y + height #face=img[x:x2,y:y2] if width >= minsize2select and height >= minsize2select: bboxes.append(bbox) cv2.rectangle(showimg, (x, y), (x2, y2), (0, 255, 0)) #maxl=max(width,height) #x3=(int)(x+(width-maxl)*0.5) #y3=(int)(y+(height-maxl)*0.5) #x4=(int)(x3+maxl) #y4=(int)(y3+maxl) #cv2.rectangle(img,(x3,y3),(x4,y4),(255,0,0)) else: cv2.rectangle(showimg, (x, y), (x2, y2), (0, 0, 255)) filename = filename.replace("/", "_") if len(bboxes) == 0: print("warrning: no face") continue cv2.imwrite(imagesdir + "/" + filename, saveimg) if convet2yoloformat: height = saveimg.shape[0] width = saveimg.shape[1] txtpath = labelsdir + "/" + filename txtpath = txtpath[:-3] + "txt" ftxt = open(txtpath, 'w') for i in range(len(bboxes)): bbox = bboxes[i] xcenter = (bbox[0] + bbox[2] * 0.5) / width ycenter = (bbox[1] + bbox[3] * 0.5) / height wr = bbox[2] * 1.0 / width hr = bbox[3] * 1.0 / height txtline = "0 " + str(xcenter) + " " + str( ycenter) + " " + str(wr) + " " + str(hr) + "\n" ftxt.write(txtline) ftxt.close() if convert2vocformat: xmlpath = vocannotationdir + "/" + filename xmlpath = xmlpath[:-3] + "xml" doc = Document() annotation = doc.createElement('annotation') doc.appendChild(annotation) folder = doc.createElement('folder') folder_name = doc.createTextNode('widerface') folder.appendChild(folder_name) annotation.appendChild(folder) filenamenode = doc.createElement('filename') filename_name = doc.createTextNode(filename) filenamenode.appendChild(filename_name) annotation.appendChild(filenamenode) source = doc.createElement('source') annotation.appendChild(source) database = doc.createElement('database') database.appendChild(doc.createTextNode('wider face Database')) source.appendChild(database) annotation_s = doc.createElement('annotation') annotation_s.appendChild(doc.createTextNode('PASCAL VOC2007')) source.appendChild(annotation_s) image = doc.createElement('image') image.appendChild(doc.createTextNode('flickr')) source.appendChild(image) flickrid = doc.createElement('flickrid') flickrid.appendChild(doc.createTextNode('-1')) source.appendChild(flickrid) owner = doc.createElement('owner') annotation.appendChild(owner) flickrid_o = doc.createElement('flickrid') flickrid_o.appendChild(doc.createTextNode('tsaith')) owner.appendChild(flickrid_o) name_o = doc.createElement('name') name_o.appendChild(doc.createTextNode('tsaith')) owner.appendChild(name_o) size = doc.createElement('size') annotation.appendChild(size) width = doc.createElement('width') width.appendChild(doc.createTextNode(str(saveimg.shape[1]))) height = doc.createElement('height') height.appendChild(doc.createTextNode(str(saveimg.shape[0]))) depth = doc.createElement('depth') depth.appendChild(doc.createTextNode(str(saveimg.shape[2]))) size.appendChild(width) size.appendChild(height) size.appendChild(depth) segmented = doc.createElement('segmented') segmented.appendChild(doc.createTextNode('0')) annotation.appendChild(segmented) for i in range(len(bboxes)): bbox = bboxes[i] objects = doc.createElement('object') annotation.appendChild(objects) object_name = doc.createElement('name') object_name.appendChild(doc.createTextNode('face')) objects.appendChild(object_name) pose = doc.createElement('pose') pose.appendChild(doc.createTextNode('Unspecified')) objects.appendChild(pose) truncated = doc.createElement('truncated') truncated.appendChild(doc.createTextNode('1')) objects.appendChild(truncated) difficult = doc.createElement('difficult') difficult.appendChild(doc.createTextNode('0')) objects.appendChild(difficult) bndbox = doc.createElement('bndbox') objects.appendChild(bndbox) xmin = doc.createElement('xmin') xmin.appendChild(doc.createTextNode(str(bbox[0]))) bndbox.appendChild(xmin) ymin = doc.createElement('ymin') ymin.appendChild(doc.createTextNode(str(bbox[1]))) bndbox.appendChild(ymin) xmax = doc.createElement('xmax') xmax.appendChild(doc.createTextNode(str(bbox[0] + bbox[2]))) bndbox.appendChild(xmax) ymax = doc.createElement('ymax') ymax.appendChild(doc.createTextNode(str(bbox[1] + bbox[3]))) bndbox.appendChild(ymax) f = open(xmlpath, "w") f.write(doc.toprettyxml(indent='')) f.close() #cv2.imshow("img",showimg) #cv2.waitKey() index = index + 1
def create_mhl(self, user, items, orin_path): """Create a MHL file from user info and items. Save MHL to orin_path""" # Start a XML document mhl = Document() # Set root and title for the root root = mhl.createElement("hashlist") root.setAttribute("version", "1.1") # Create user_info element user_info = mhl.createElement("creatorinfo") # Gets all elements in object user as a dictionary user_elements = user.get_all_elements() # Creates all elements for Creator Info name_t = mhl.createTextNode(user_elements["name"]) name = mhl.createElement("name") name.appendChild(name_t) username_t = mhl.createTextNode(user_elements["username"]) username = mhl.createElement("username") username.appendChild(username_t) hostname_t = mhl.createTextNode(user_elements["hostname"]) hostname = mhl.createElement("hostname") hostname.appendChild(hostname_t) tool_name_t = mhl.createTextNode(user_elements["tool_name"]) tool_name = mhl.createElement("tool") tool_name.appendChild(tool_name_t) s_date_t = mhl.createTextNode(user_elements["start_date"]) s_date = mhl.createElement("startdate") s_date.appendChild(s_date_t) e_date_t = mhl.createTextNode(user_elements["end_date"]) e_date = mhl.createElement("finishdate") e_date.appendChild(e_date_t) # Append them to user_info user_info.appendChild(name) user_info.appendChild(username) user_info.appendChild(hostname) user_info.appendChild(tool_name) user_info.appendChild(s_date) user_info.appendChild(e_date) # Append user_info to the root root.appendChild(user_info) # Loop to get all item for item in items: # Gets all elements in object item as a dictionary item_elements = item.get_all_elements() # Creates all elements for hash element fname = item_elements["file_name"].replace(orin_path, "")[1:] file_name_t = mhl.createTextNode(fname) file_name = mhl.createElement("file") file_name.appendChild(file_name_t) size_t = mhl.createTextNode(item_elements["file_size"]) size = mhl.createElement("size") size.appendChild(size_t) last_mod_date_t = mhl.createTextNode(item_elements["last_mod_date"]) last_mod_date = mhl.createElement("lastmodificationdate") last_mod_date.appendChild(last_mod_date_t) hash_type_t = mhl.createTextNode(item_elements["hash_hex"]) hash_type = mhl.createElement(item_elements["hash_type"]) hash_type.appendChild(hash_type_t) hash_date_t = mhl.createTextNode(item_elements["hash_date"]) hash_date = mhl.createElement("hashdate") hash_date.appendChild(hash_date_t) # Creates hash node and append all elements hash_node = mhl.createElement("hash") hash_node.appendChild(file_name) hash_node.appendChild(size) hash_node.appendChild(last_mod_date) hash_node.appendChild(hash_type) hash_node.appendChild(hash_date) # Append hash node to root root.appendChild(hash_node) # Append root to MHL file mhl.appendChild(root) # Create MHL file name: # Name of orin_path folder + date and time mhl_filename = os.path.basename(orin_path) + datetime.datetime.now().strftime("_%Y-%m-%d_%H%M%S") + ".mhl" # Path to MHL file mhl_path = os.path.join(orin_path, mhl_filename) # Writes the file mhl.writexml(open(mhl_path, "w", encoding="utf-8", errors="xmlcharrefreplace"), addindent=" ", newl="\n", encoding="UTF-8") # Unlink everything to save memory mhl.unlink()
class XMLReportGenerator(ReportGenerator): """ This class generates a report with the method printToFile(fileName) which contains the information of all the vulnerabilities notified to this object through the method add_vulnerability(vulnerabilityTypeName,level,url,parameter,info). The format of the file is XML and it has the following structure: <report type="security"> <generatedBy id="Wapiti 3.0.5"/> <vulnerabilityTypeList> <vulnerabilityType name="SQL Injection"> <vulnerabilityTypeList> <vulnerabilityType name="SQL Injection"> <vulnerabilityList> <vulnerability level="3"> <url>http://www.a.com</url> <parameters>id=23</parameters> <info>SQL Injection</info> </vulnerability> </vulnerabilityList> </vulnerabilityType> </vulnerabilityTypeList> </report> """ def __init__(self): super().__init__() self._xml_doc = Document() self._flaw_types = {} self._vulns = {} self._anomalies = {} self._additionals = {} # Vulnerabilities def add_vulnerability_type(self, name, description="", solution="", references=None): if name not in self._flaw_types: self._flaw_types[name] = { "desc": description, "sol": solution, "ref": references } if name not in self._vulns: self._vulns[name] = [] def add_vulnerability(self, module: str, category=None, level=0, request=None, parameter="", info="", auth=None): """ Store the information about the vulnerability to be printed later. The method printToFile(fileName) can be used to save in a file the vulnerabilities notified through the current method. """ vuln_dict = { "method": request.method, "path": request.file_path, "info": info, "level": level, "parameter": parameter, "referer": request.referer, "module": module, "auth": auth, "http_request": request.http_repr(left_margin=""), "curl_command": request.curl_repr, } if category not in self._vulns: self._vulns[category] = [] self._vulns[category].append(vuln_dict) # Anomalies def add_anomaly_type(self, name, description="", solution="", references=None): if name not in self._flaw_types: self._flaw_types[name] = { "desc": description, "sol": solution, "ref": references } if name not in self._anomalies: self._anomalies[name] = [] def add_anomaly(self, module: str, category=None, level=0, request=None, parameter="", info="", auth=None): """ Store the information about the vulnerability to be printed later. The method printToFile(fileName) can be used to save in a file the vulnerabilities notified through the current method. """ anom_dict = { "method": request.method, "path": request.file_path, "info": info, "level": level, "parameter": parameter, "referer": request.referer, "module": module, "auth": auth, "http_request": request.http_repr(left_margin=""), "curl_command": request.curl_repr, } if category not in self._anomalies: self._anomalies[category] = [] self._anomalies[category].append(anom_dict) # Additionals def add_additional_type(self, name, description="", solution="", references=None): """ This method adds an addtional type, it can be invoked to include in the report the type. """ if name not in self._flaw_types: self._flaw_types[name] = { "desc": description, "sol": solution, "ref": references } if name not in self._additionals: self._additionals[name] = [] def add_additional(self, module: str, category=None, level=0, request=None, parameter="", info="", auth=None): """ Store the information about the addtional to be printed later. The method printToFile(fileName) can be used to save in a file the addtionals notified through the current method. """ addition_dict = { "method": request.method, "path": request.file_path, "info": info, "level": level, "parameter": parameter, "referer": request.referer, "module": module, "auth": auth, "http_request": request.http_repr(left_margin=""), "curl_command": request.curl_repr, } if category not in self._additionals: self._additionals[category] = [] self._additionals[category].append(addition_dict) def generate_report(self, output_path): """ Create a xml file with a report of the vulnerabilities which have been logged with the method add_vulnerability(vulnerabilityTypeName,level,url,parameter,info) """ report = self._xml_doc.createElement("report") report.setAttribute("type", "security") self._xml_doc.appendChild(report) # Add report infos report_infos = self._xml_doc.createElement("report_infos") generator_name = self._xml_doc.createElement("info") generator_name.setAttribute("name", "generatorName") generator_name.appendChild(self._xml_doc.createTextNode("wapiti")) report_infos.appendChild(generator_name) generator_version = self._xml_doc.createElement("info") generator_version.setAttribute("name", "generatorVersion") generator_version.appendChild(self._xml_doc.createTextNode(self._infos["version"])) report_infos.appendChild(generator_version) scope = self._xml_doc.createElement("info") scope.setAttribute("name", "scope") scope.appendChild(self._xml_doc.createTextNode(self._infos["scope"])) report_infos.appendChild(scope) date_of_scan = self._xml_doc.createElement("info") date_of_scan.setAttribute("name", "dateOfScan") date_of_scan.appendChild(self._xml_doc.createTextNode(self._infos["date"])) report_infos.appendChild(date_of_scan) target = self._xml_doc.createElement("info") target.setAttribute("name", "target") target.appendChild(self._xml_doc.createTextNode(self._infos["target"])) report_infos.appendChild(target) report.appendChild(report_infos) vulnerabilities = self._xml_doc.createElement("vulnerabilities") anomalies = self._xml_doc.createElement("anomalies") additionals = self._xml_doc.createElement("additionals") # Loop on each flaw classification for flaw_type in self._flaw_types: container = None classification = "" flaw_dict = {} if flaw_type in self._vulns: container = vulnerabilities classification = "vulnerability" flaw_dict = self._vulns elif flaw_type in self._anomalies: container = anomalies classification = "anomaly" flaw_dict = self._anomalies elif flaw_type in self._additionals: container = additionals classification = "additional" flaw_dict = self._additionals # Child nodes with a description of the flaw type flaw_type_node = self._xml_doc.createElement(classification) flaw_type_node.setAttribute("name", flaw_type) flaw_type_desc = self._xml_doc.createElement("description") flaw_type_desc.appendChild(self._xml_doc.createCDATASection(self._flaw_types[flaw_type]["desc"])) flaw_type_node.appendChild(flaw_type_desc) flaw_type_solution = self._xml_doc.createElement("solution") flaw_type_solution.appendChild(self._xml_doc.createCDATASection(self._flaw_types[flaw_type]["sol"])) flaw_type_node.appendChild(flaw_type_solution) flaw_type_references = self._xml_doc.createElement("references") for ref in self._flaw_types[flaw_type]["ref"]: reference_node = self._xml_doc.createElement("reference") title_node = self._xml_doc.createElement("title") url_node = self._xml_doc.createElement("url") title_node.appendChild(self._xml_doc.createTextNode(ref)) url = self._flaw_types[flaw_type]["ref"][ref] url_node.appendChild(self._xml_doc.createTextNode(url)) reference_node.appendChild(title_node) reference_node.appendChild(url_node) flaw_type_references.appendChild(reference_node) flaw_type_node.appendChild(flaw_type_references) # And child nodes with each flaw of the current type entries_node = self._xml_doc.createElement("entries") for flaw in flaw_dict[flaw_type]: entry_node = self._xml_doc.createElement("entry") method_node = self._xml_doc.createElement("method") method_node.appendChild(self._xml_doc.createTextNode(flaw["method"])) entry_node.appendChild(method_node) path_node = self._xml_doc.createElement("path") path_node.appendChild(self._xml_doc.createTextNode(flaw["path"])) entry_node.appendChild(path_node) level_node = self._xml_doc.createElement("level") level_node.appendChild(self._xml_doc.createTextNode(str(flaw["level"]))) entry_node.appendChild(level_node) parameter_node = self._xml_doc.createElement("parameter") parameter_node.appendChild(self._xml_doc.createTextNode(flaw["parameter"])) entry_node.appendChild(parameter_node) info_node = self._xml_doc.createElement("info") info_node.appendChild(self._xml_doc.createTextNode(flaw["info"])) entry_node.appendChild(info_node) referer_node = self._xml_doc.createElement("referer") referer_node.appendChild(self._xml_doc.createTextNode(flaw["referer"])) entry_node.appendChild(referer_node) module_node = self._xml_doc.createElement("module") module_node.appendChild(self._xml_doc.createTextNode(flaw["module"])) entry_node.appendChild(module_node) auth_node = self._xml_doc.createElement("auth") if flaw["auth"]: auth_method_node = self._xml_doc.createElement("auth_method") auth_method_node.appendChild(self._xml_doc.createTextNode(flaw["auth"]["method"])) auth_node.appendChild(auth_method_node) auth_url_node = self._xml_doc.createElement("auth_url") auth_url_node.appendChild(self._xml_doc.createTextNode(flaw["auth"]["url"])) auth_node.appendChild(auth_url_node) else: auth_node.appendChild(self._xml_doc.createTextNode(str(flaw["auth"]))) entry_node.appendChild(auth_node) http_request_node = self._xml_doc.createElement("http_request") http_request_node.appendChild(self._xml_doc.createCDATASection(flaw["http_request"])) entry_node.appendChild(http_request_node) curl_command_node = self._xml_doc.createElement("curl_command") curl_command_node.appendChild(self._xml_doc.createCDATASection(flaw["curl_command"])) entry_node.appendChild(curl_command_node) entries_node.appendChild(entry_node) flaw_type_node.appendChild(entries_node) container.appendChild(flaw_type_node) report.appendChild(vulnerabilities) report.appendChild(anomalies) report.appendChild(additionals) with open(output_path, "w", errors="ignore") as xml_report_file: self._xml_doc.writexml(xml_report_file, addindent=" ", newl="\n")
class StateMachineXml(object): # 初始状态 init_state = 'init' # 当前状态机状态 current_state = 'init' # 字典状态 dic_state = 'dic_state' # 基本KEY状态 base_key_state = 'basekeystate' # 字符串值状态 stri_value_state = 'stringstate' # 整数值状态 int_value_state = 'intstate' # 布尔值状态 boolean_value_state = 'booleanstate' # 时间值状态 time_value_state = 'timestate' # 数组值状态 array_value_state = 'arraystate' # =状态 assign_state = 'assignstate' # \n换行状态 , 在数组值状态下,不会变成换行状态 next_line_state = 'nextlinestate' # 空白行 blank_state = 'blankstate' # 评论状态 comment_state = 'commentstate' # 上次记录的索引 previous_index = 0 # 当前索引 current_index = 0 # char_list = [] # 主XML节点 def __init__(self): try: self.tomfile = open("tomltestxml.toml", "rb") self.tomfiledata = self.tomfile.read() # 将文件的文本以行的形式加载到内存 self.sum = len(self.tomfiledata) self.doc = Document() self.toml = self.doc.createElement('toml') # 创建根元素 self.toml_copy = self.toml self.current_node = self.toml except: s = sys.exc_info() print "Error '%s' happened on line %d" % (s[1], s[2].tb_lineno) raise RuntimeError('没有toml格式的配置文件tomltestxml,请检查') def parse_to_xml(self): try: for index in range(self.sum): if self.current_state == self.init_state or self.current_state == self.next_line_state: # 这里都是处理key值得地方一种是基本数据类型的KEY,一种是字典类型的KEY if self.tomfiledata[index] == '[': self.current_state = self.dic_state self.previous_index = index if re.match('[a-zA-Z]' , self.tomfiledata[index]) : self.current_state = self.base_key_state self.previous_index = index if self.tomfiledata[index] == '#': self.current_state = self.comment_state # elif self.current_state == self.base_key_state: # 遇到空白的时候就能够生成完整的普通KEY if self.tomfiledata[index] == ' ': self.current_index = index self.product_normal_key() self.current_state = self.blank_state # 遇到“=”改变状态为assign_state elif self.current_state == self.dic_state: # 遇到]就生成字典类型的KEY if self.tomfiledata[index] == ']': self.current_index = index self.parse_dic_key() # 考虑遇到.的情况,这个时候是子级字典 if self.tomfiledata[index] == '\n': self.current_state = self.next_line_state elif self.current_state == self.blank_state: # 主要是作用于普通key后面的空白,下一个状态是= if self.tomfiledata[index] == '=': self.current_state = self.assign_state elif self.current_state == self.assign_state: # 遇到“为字符串值状态 if self.tomfiledata[index] == '"': self.previous_index = index self.current_state = self.stri_value_state # 遇到t or f 为布尔类型 if self.tomfiledata[index] == 't' or self.tomfiledata[index] == 'f' : self.previous_index = index self.char_list.append(self.tomfiledata[index]) self.current_state = self.boolean_value_state # 遇到数字为整形值状态 if re.match("\d", self.tomfiledata[index]): self.previous_index = index self.char_list.append(self.tomfiledata[index]) self.current_state = self.int_value_state # 遇到[为数组值类型 if self.tomfiledata[index] == '[': self.previous_index = index self.char_list.append(self.tomfiledata[index]) self.current_state = self.array_value_state # ---------------------------上面和是其它状态分组, 下面全是值类型分组,为了方便阅读代码--------------------------------------- elif self.current_state == self.stri_value_state: # 遇到“为字符串值状态结束,形成完整的值 , 并在遇到‘\n’改变状态 if self.tomfiledata[index] == '"': self.current_index = index self.parse_str_value() if self.tomfiledata[index] == '\n': self.current_state = self.next_line_state elif self.current_state == self.int_value_state : # 遇到空格整数值状态结束,形成完整的值 , 并在遇到‘\n’改变状态 if re.match("\d" , self.tomfiledata[index]): self.char_list.append( self.tomfiledata[index]) if self.tomfiledata[index] == '\n': self.parse_int_value() self.current_state = self.next_line_state self.char_list = [] # 遇到-的时候判定为时间状态 if self.tomfiledata[index] == '-': self.current_state = self.time_value_state self.char_list = [] elif self.current_state == self.boolean_value_state: # # 遇到空格布尔值状态结束,形成完整的值 , 并在遇到‘\n’改变状态 if re.match("[truefalse]", self.tomfiledata[index]): self.char_list.append(self.tomfiledata[index]) if self.tomfiledata[index] == '\n': self.parse_boolean_value() self.current_state = self.next_line_state self.char_list = [] elif self.current_state == self.array_value_state : # ‘数组类型有点特殊的地方是:可以跨行,这点要特殊处理, 结束的话暂时是考虑,使用相同数量的"[""]" # 统计出[ 号的数量 , 以及统计出]号的数量 left_brackets = 0 right_brackets = 0 for char in self.char_list: if char == '[': left_brackets +=1 elif char == ']': right_brackets +=1 if left_brackets == right_brackets : self.current_state = self.next_line_state self.parse_array_value() self.char_list = [] else: self.char_list.append(self.tomfiledata[index]) if index == self.sum-1: self.current_state = self.next_line_state self.parse_array_value() self.char_list = [] elif self.current_state == self.time_value_state : # 值为时间的状态 if self.tomfiledata[index] == 'Z': self.current_index = index+1 self.parse_time_value() if self.tomfiledata[index] == '\n': self.current_state = self.next_line_state elif self.current_state == self.comment_state : if self.tomfiledata[index] == '\n': self.current_state = self.next_line_state except Exception: s = sys.exc_info() print "Error '%s' happened on line %d" % (s[1], s[2].tb_lineno) def product_normal_key(self): try: normal_key = self.tomfiledata[self.previous_index : self.current_index] # print normal_key emlement = self.doc.createElement(normal_key) self.toml_copy.appendChild(emlement) self.current_node = emlement except Exception as e: s = sys.exc_info() print "Error '%s' happened on line %d" % (s[1], s[2].tb_lineno) def parse_dic_key(self): try: temporary = self.toml dic_keys = self.tomfiledata[self.previous_index + 1: self.current_index] dic_keys_list = dic_keys.split(".") flag = True for index, dic_key in enumerate(dic_keys_list): if temporary.childNodes : for index1 , tem in enumerate(temporary.childNodes): if tem.tagName == dic_key: temporary = tem flag = True dic_key_element = tem break else: flag = False if flag is False: dic_key_element = self.doc.createElement(dic_key) temporary.appendChild(dic_key_element) temporary = dic_key_element else: dic_key_element = self.doc.createElement(dic_key) temporary.appendChild(dic_key_element) temporary = dic_key_element if dic_key_element : self.current_node = dic_key_element self.toml_copy = dic_key_element except : s = sys.exc_info() print "Error '%s' happened on line %d" % (s[1], s[2].tb_lineno) def parse_str_value(self): try: str_value = self.tomfiledata[self.previous_index + 1 : self.current_index] text_node = self.doc.createTextNode(str_value) self.current_node.appendChild(text_node) except : s = sys.exc_info() print "Error '%s' happened on line %d" % (s[1], s[2].tb_lineno) def parse_int_value(self): try: int_value = "".join(self.char_list) text_node = self.doc.createTextNode(int_value) self.current_node.appendChild(text_node) # self.char_list = [] except: s = sys.exc_info() print "Error '%s' happened on line %d" % (s[1], s[2].tb_lineno) def parse_time_value(self): try: time_value = self.tomfiledata[self.previous_index : self.current_index] str_time_value = datetime.strptime(time_value, "%Y-%m-%dT%H:%M:%SZ").strftime("%Y-%m-%d %H:%M:%S") text_node = self.doc.createTextNode(str_time_value) self.current_node.appendChild(text_node) except: s = sys.exc_info() print "Error '%s' happened on line %d" % (s[1], s[2].tb_lineno) def parse_array_value(self): try: new_char_list = list(eval("".join(self.char_list))) length = len(new_char_list) for index in range(length): text_node = self.doc.createTextNode(str(new_char_list[index])) current_local_node = copy.deepcopy(self.current_node) self.current_node.appendChild(text_node) parent_node = self.current_node.parentNode self.current_node = current_local_node if index+1 != length : parent_node.appendChild(self.current_node) except: s = sys.exc_info() print "Error '%s' happened on line %d" % (s[1], s[2].tb_lineno) def parse_boolean_value(self): try: bool = "".join(self.char_list) text_node = self.doc.createTextNode(bool) self.current_node.appendChild(text_node) except : s = sys.exc_info() print "Error '%s' happened on line %d" % (s[1], s[2].tb_lineno) def print_to_xml(self): self.parse_to_xml() self.tomfile.close() print self.toml.toxml()
def genXml(self): # 生成coupler.xml doc = Document() root = doc.createElement('coupler') #doc.appendChild(root) for avDict in self.__couple: attrVect = doc.createElement('attrVect') name = self.dictDom(doc, 'name', avDict['name']) model = self.dictDom(doc, 'model', avDict['model']) attrVect.appendChild(name) attrVect.appendChild(model) # create fraction dom fraction = doc.createElement('fraction') fracList = self.dictDom(doc, 'fraclist', avDict['fraction']['fraclist']) init = self.dictDom(doc, 'init', avDict['fraction']['init']) update = self.dictDom(doc, 'update', avDict['fraction']['update']) fracs = doc.createElement('fracs') print avDict['fraction'] for frac in avDict['fraction']['fracs']: fracNode = doc.createElement('frac') nameNode = self.dictDom(doc, 'name', frac['name']) mapperNode = self.dictDom(doc, 'mapper', frac['mapper']) modelNode = self.dictDom(doc, 'model', frac['model']) fracNode.appendChild(nameNode) fracNode.appendChild(mapperNode) fracNode.appendChild(modelNode) fracs.appendChild(fracNode) fraction.appendChild(fracList) fraction.appendChild(init) fraction.appendChild(update) fraction.appendChild(fracs) attrVect.appendChild(fraction) srcs = doc.createElement('srcs') for src in avDict['src']: srcNode = doc.createElement('src') name = self.dictDom(doc, 'attrVect', src['attrVect']) field = "" try: field = self.dictDom(doc, 'field', src['field']) except: pass ##set mapper mapperNode = doc.createElement("mapper") mapper_name = self.dictDom(doc, 'name', src['mapper']['name']) mapper_type = self.dictDom(doc, 'type', src['mapper']['type']) mapper_w_file = self.dictDom(doc, 'w_file', src['mapper']['w_file']) mapperMethod = doc.createElement("method") for phase in src['mapper']['method']: phaseNode = doc.createElement("phase") phase_name = self.dictDom(doc, 'name', phase['name']) in_args = None if phase['in_args'] == []: in_args = self.dictDom(doc, 'in_args', '') else: in_args = doc.createElement("in_args") for in_arg in phase['in_args']: arg = self.dictDom(doc, 'arg', in_arg) in_args.appendChild(arg) out_args = None if phase['out_args'] == []: out_args = self.dictDom(doc, 'out_args', '') else: out_args = doc.createElement("out_args") for out_arg in phase['out_args']: arg = self.dictDom(doc, 'arg', out_arg) out_args.appendChild(arg) phaseNode.appendChild(phase_name) phaseNode.appendChild(in_args) phaseNode.appendChild(out_args) mapperMethod.appendChild(phaseNode) mapperNode.appendChild(mapper_name) mapperNode.appendChild(mapper_type) mapperNode.appendChild(mapper_w_file) mapperNode.appendChild(mapperMethod) srcNode.appendChild(name) srcNode.appendChild(field) srcNode.appendChild(mapperNode) srcs.appendChild(srcNode) attrVect.appendChild(srcs) mrg = avDict['mrg'] mrgNode = doc.createElement('mrg') name = self.dictDom(doc, 'name', mrg['name']) mrgNode.appendChild(name) in_args = doc.createElement('in_args') for arg in mrg['in_args']: argDom = self.dictDom(doc, 'arg', arg) in_args.appendChild(argDom) mrgNode.appendChild(in_args) out_args = doc.createElement('out_args') for arg in mrg['out_args']: argDom = self.dictDom(doc, 'arg', arg) out_args.appendChild(argDom) mrgNode.appendChild(out_args) attrVect.appendChild(mrgNode) root.appendChild(attrVect) doc.appendChild(root) f = open(self.__coupleFile, 'w') print 'write' doc.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')
def parse_data(data): items = [] page = html.fromstring(data) table = page.cssselect(".contentInner table#row.table")[0] rows = table.cssselect('tr') for row in rows: cells = row.cssselect('td') if len(cells) != 4: continue code = cells[0].text_content() title = cells[1].text_content().strip().replace('\n', '') date_issues = cells[2].text_content().strip() if 'replaced' in title or 'withdrawn' in title: continue link = "" print 'Processing', code pdfpage = "http://guidance.nice.org.uk/%s/QuickRefGuide/pdf/English" % ( code, ) pdfpagedata = None hc = hashlib.sha224(pdfpage).hexdigest() if os.path.exists(os.path.join('cache', hc)): print ' Using cached subpage ' pdfpagedata = open(os.path.join('cache', hc)).read() else: print ' Using and caching subpage ' pdfpagedata = urllib2.urlopen(pdfpage).read() with open(os.path.join('cache', hc), 'w') as f: f.write(pdfpagedata) pp = html.fromstring(pdfpagedata) links = pp.cssselect('a') for a in links: if code in a.text_content(): link = 'http://guidance.nice.org.uk' + a.attrib.get('href') item = { 'title': title, 'code': code, 'issue_date': date_issues, 'url': link } if link: pdfhash = hashlib.sha224(pdfpage).hexdigest() p = os.path.join('pdfs', code) + '.pdf' if not os.path.exists(p): print ' Fetching PDF' urllib.urlretrieve(link, p.lower()) else: continue items.append(item) with open('output.json', 'w') as f: json.dump(items, f) doc = Document() guidelines = doc.createElement("guidelines") doc.appendChild(guidelines) for item in items: node = doc.createElement("guide") titleNode = doc.createElement("title") titleText = doc.createTextNode(item['title']) titleNode.appendChild(titleText) urlNode = doc.createElement("url") urlText = doc.createTextNode(item['url']) urlNode.appendChild(urlText) node.appendChild(titleNode) node.appendChild(urlNode) guidelines.appendChild(node) with open('output.xml', 'w') as f: f.write(doc.toprettyxml(indent=" "))
def create(): for walk in os.walk(labels): for each in walk[2]: fidin = open(walk[0] + '/' + each, 'r') objIndex = 0 for data in islice(fidin, 1, None): objIndex += 1 data = data.strip('\n') datas = data.split(' ') if 5 != len(datas): print 'bounding box information error' continue pictureName = each.replace('.txt', '.jpg') imageFile = imgpath + pictureName img = cv2.imread(imageFile) imgSize = img.shape if 1 == objIndex: xmlName = each.replace('.txt', '.xml') f = open(xmlpath_new + xmlName, "w") doc = Document() annotation = doc.createElement('annotation') doc.appendChild(annotation) folder = doc.createElement('folder') folder.appendChild(doc.createTextNode(foldername)) annotation.appendChild(folder) filename = doc.createElement('filename') filename.appendChild(doc.createTextNode(pictureName)) annotation.appendChild(filename) source = doc.createElement('source') database = doc.createElement('database') database.appendChild(doc.createTextNode('My Database')) source.appendChild(database) source_annotation = doc.createElement('annotation') source_annotation.appendChild( doc.createTextNode(foldername)) source.appendChild(source_annotation) image = doc.createElement('image') image.appendChild(doc.createTextNode('flickr')) source.appendChild(image) flickrid = doc.createElement('flickrid') flickrid.appendChild(doc.createTextNode('NULL')) source.appendChild(flickrid) annotation.appendChild(source) owner = doc.createElement('owner') flickrid = doc.createElement('flickrid') flickrid.appendChild(doc.createTextNode('NULL')) owner.appendChild(flickrid) name = doc.createElement('name') name.appendChild(doc.createTextNode('idaneel')) owner.appendChild(name) annotation.appendChild(owner) size = doc.createElement('size') width = doc.createElement('width') width.appendChild(doc.createTextNode(str(imgSize[1]))) size.appendChild(width) height = doc.createElement('height') height.appendChild(doc.createTextNode(str(imgSize[0]))) size.appendChild(height) depth = doc.createElement('depth') depth.appendChild(doc.createTextNode(str(imgSize[2]))) size.appendChild(depth) annotation.appendChild(size) segmented = doc.createElement('segmented') segmented.appendChild(doc.createTextNode(str(0))) annotation.appendChild(segmented) annotation.appendChild(insertObject(doc, datas)) else: annotation.appendChild(insertObject(doc, datas)) try: f.write(doc.toprettyxml(indent=' ')) f.close() fidin.close() except: pass
def gen_car_carplate_offset(fileitem): num_car_with_carplate = 0 num_car_without_carplate = 0 dom = xml.dom.minidom.parse(os.path.join(root_dir, fileitem)) root = dom.documentElement # size size = root.getElementsByTagName('size')[0] width = int(size.getElementsByTagName('width')[0].childNodes[0].nodeValue) height = int( size.getElementsByTagName('height')[0].childNodes[0].nodeValue) depth = int(size.getElementsByTagName('depth')[0].childNodes[0].nodeValue) # 创建dom文档 doc = Document() # 创建根节点 annotation = doc.createElement('annotation') # 根节点插入dom树 doc.appendChild(annotation) # folder folder = doc.createElement('folder') annotation.appendChild(folder) folder.appendChild(doc.createTextNode('car carplate_offset')) # filename filename = doc.createElement('filename') annotation.appendChild(filename) filename.appendChild(doc.createTextNode(fileitem.split('.')[0] + '.jpg')) # source source = doc.createElement('source') annotation.appendChild(source) database_ = doc.createElement('database') database_.appendChild(doc.createTextNode('car carplate_offset')) source.appendChild(database_) # 创建size节点 size = doc.createElement('size') annotation.appendChild(size) width_ = doc.createElement('width') width_.appendChild(doc.createTextNode(str(width))) height_ = doc.createElement('height') height_.appendChild(doc.createTextNode(str(height))) depth_ = doc.createElement('depth') depth_.appendChild(doc.createTextNode(str(depth))) size.appendChild(width_) size.appendChild(height_) size.appendChild(depth_) # segmentation segmented = doc.createElement('segmented') annotation.appendChild(segmented) segmented.appendChild(doc.createTextNode(str(0))) # notice object and obj objects = root.getElementsByTagName('object') for index, obj in enumerate(objects): bndbox = obj.getElementsByTagName('bndbox')[0] misc = int(obj.getElementsByTagName('misc')[0].childNodes[0].nodeValue) if not misc: # 不要misc的车辆 car_xmin = int( bndbox.getElementsByTagName('xmin')[0].childNodes[0].nodeValue) car_ymin = int( bndbox.getElementsByTagName('ymin')[0].childNodes[0].nodeValue) car_xmax = int( bndbox.getElementsByTagName('xmax')[0].childNodes[0].nodeValue) car_ymax = int( bndbox.getElementsByTagName('ymax')[0].childNodes[0].nodeValue) # 限制不超过图片上下界 car_xmin = ex.limit_in_bounds(car_xmin, 1, width) car_ymin = ex.limit_in_bounds(car_ymin, 1, height) car_xmax = ex.limit_in_bounds(car_xmax, 1, width) car_ymax = ex.limit_in_bounds(car_ymax, 1, height) car_IsOccluded = int( obj.getElementsByTagName('occlusion') [0].childNodes[0].nodeValue) car_IsTruncated = int( obj.getElementsByTagName('truncated') [0].childNodes[0].nodeValue) car_IsDifficult = int( obj.getElementsByTagName('difficult') [0].childNodes[0].nodeValue) car_x_center = (car_xmin + car_xmax) / 2 car_y_center = (car_ymin + car_ymax) / 2 # carplate carplates = obj.getElementsByTagName('carplate') if len(carplates) == 0: num_car_without_carplate += 1 # car object = doc.createElement('object') annotation.appendChild(object) name = doc.createElement('name') name.appendChild(doc.createTextNode('car')) object.appendChild(name) # pose pose_ = doc.createElement('pose') pose_.appendChild(doc.createTextNode('Unspecified')) object.appendChild(pose_) # occluded occluded_ = doc.createElement('occluded') occluded_.appendChild(doc.createTextNode(str(car_IsOccluded))) object.appendChild(occluded_) # truncated truncated_ = doc.createElement('truncated') truncated_.appendChild(doc.createTextNode( str(car_IsTruncated))) object.appendChild(truncated_) # difficult difficult_ = doc.createElement('difficult') difficult_.appendChild(doc.createTextNode( str(car_IsDifficult))) object.appendChild(difficult_) # dont have carplate has_carplate_ = doc.createElement('has_carplate') has_carplate_.appendChild(doc.createTextNode(str(0))) object.appendChild(has_carplate_) # the bndbox bndbox = doc.createElement('bndbox') object.appendChild(bndbox) xmin_ = doc.createElement('xmin') xmin_.appendChild(doc.createTextNode(str(car_xmin))) bndbox.appendChild(xmin_) ymin_ = doc.createElement('ymin') ymin_.appendChild(doc.createTextNode(str(car_ymin))) bndbox.appendChild(ymin_) xmax_ = doc.createElement('xmax') xmax_.appendChild(doc.createTextNode(str(car_xmax))) bndbox.appendChild(xmax_) ymax_ = doc.createElement('ymax') ymax_.appendChild(doc.createTextNode(str(car_ymax))) bndbox.appendChild(ymax_) elif len(carplates) > 0: num_car_with_carplate += 1 for i in range(len(carplates)): # car object = doc.createElement('object') annotation.appendChild(object) name = doc.createElement('name') name.appendChild(doc.createTextNode('car')) object.appendChild(name) # pose pose_ = doc.createElement('pose') pose_.appendChild(doc.createTextNode('Unspecified')) object.appendChild(pose_) # occluded occluded_ = doc.createElement('occluded') occluded_.appendChild( doc.createTextNode(str(car_IsOccluded))) object.appendChild(occluded_) # truncated truncated_ = doc.createElement('truncated') truncated_.appendChild( doc.createTextNode(str(car_IsTruncated))) object.appendChild(truncated_) # difficult difficult_ = doc.createElement('difficult') difficult_.appendChild( doc.createTextNode(str(car_IsDifficult))) object.appendChild(difficult_) # has carplate has_carplate_ = doc.createElement('has_carplate') has_carplate_.appendChild(doc.createTextNode(str(1))) object.appendChild(has_carplate_) # carplate carplate = carplates[i] carplate_IsOccluded = int( carplate.getElementsByTagName('occlusion') [0].childNodes[0].nodeValue) carplate_IsDifficult = int( carplate.getElementsByTagName('difficult') [0].childNodes[0].nodeValue) carplate_IsUnrecognable = int( carplate.getElementsByTagName('unrecognable') [0].childNodes[0].nodeValue) # occluded occluded_ = doc.createElement('carplate_occluded') occluded_.appendChild( doc.createTextNode(str(carplate_IsOccluded))) object.appendChild(occluded_) # difficult difficult_ = doc.createElement('carplate_difficult') difficult_.appendChild( doc.createTextNode(str(carplate_IsDifficult))) object.appendChild(difficult_) # unrecognable unrecognable_ = doc.createElement('carplate_unrecognable') unrecognable_.appendChild( doc.createTextNode(str(carplate_IsUnrecognable))) object.appendChild(unrecognable_) carplate_x_top_left = round( float( carplate.getElementsByTagName('x_top_left') [0].childNodes[0].nodeValue)) carplate_y_top_left = round( float( carplate.getElementsByTagName('y_top_left') [0].childNodes[0].nodeValue)) carplate_x_top_right = round( float( carplate.getElementsByTagName('x_top_right') [0].childNodes[0].nodeValue)) carplate_y_top_right = round( float( carplate.getElementsByTagName('y_top_right') [0].childNodes[0].nodeValue)) carplate_x_bottom_right = round( float( carplate.getElementsByTagName('x_bottom_right') [0].childNodes[0].nodeValue)) carplate_y_bottom_right = round( float( carplate.getElementsByTagName('y_bottom_right') [0].childNodes[0].nodeValue)) carplate_x_bottom_left = round( float( carplate.getElementsByTagName('x_bottom_left') [0].childNodes[0].nodeValue)) carplate_y_bottom_left = round( float( carplate.getElementsByTagName('y_bottom_left') [0].childNodes[0].nodeValue)) # 将车牌四点顺序改为标准的左上右上右下左下 results = ex.exchange_four_points_to_std([ carplate_x_top_left, carplate_y_top_left, carplate_x_top_right, carplate_y_top_right, carplate_x_bottom_right, carplate_y_bottom_right, carplate_x_bottom_left, carplate_y_bottom_left ]) carplate_x_top_left = results['x_top_left'] carplate_y_top_left = results['y_top_left'] carplate_x_top_right = results['x_top_right'] carplate_y_top_right = results['y_top_right'] carplate_x_bottom_right = results['x_bottom_right'] carplate_y_bottom_right = results['y_bottom_right'] carplate_x_bottom_left = results['x_bottom_left'] carplate_y_bottom_left = results['y_bottom_left'] # 限制不超过图片上下界 carplate_x_top_left = ex.limit_in_bounds( carplate_x_top_left, 1, width) carplate_y_top_left = ex.limit_in_bounds( carplate_y_top_left, 1, height) carplate_x_top_right = ex.limit_in_bounds( carplate_x_top_right, 1, width) carplate_y_top_right = ex.limit_in_bounds( carplate_y_top_right, 1, height) carplate_x_bottom_right = ex.limit_in_bounds( carplate_x_bottom_right, 1, width) carplate_y_bottom_right = ex.limit_in_bounds( carplate_y_bottom_right, 1, height) carplate_x_bottom_left = ex.limit_in_bounds( carplate_x_bottom_left, 1, width) carplate_y_bottom_left = ex.limit_in_bounds( carplate_y_bottom_left, 1, height) carplate_xmin = min(carplate_x_top_left, carplate_x_bottom_left) carplate_ymin = min(carplate_y_top_left, carplate_y_top_right) carplate_xmax = max(carplate_x_bottom_right, carplate_x_top_right) carplate_ymax = max(carplate_y_bottom_right, carplate_y_bottom_left) carplate_x_center = (carplate_xmin + carplate_xmax) / 2 carplate_y_center = (carplate_ymin + carplate_ymax) / 2 # the bndbox bndbox = doc.createElement('bndbox') object.appendChild(bndbox) xmin_ = doc.createElement('xmin') xmin_.appendChild(doc.createTextNode(str(car_xmin))) bndbox.appendChild(xmin_) ymin_ = doc.createElement('ymin') ymin_.appendChild(doc.createTextNode(str(car_ymin))) bndbox.appendChild(ymin_) xmax_ = doc.createElement('xmax') xmax_.appendChild(doc.createTextNode(str(car_xmax))) bndbox.appendChild(xmax_) ymax_ = doc.createElement('ymax') ymax_.appendChild(doc.createTextNode(str(car_ymax))) bndbox.appendChild(ymax_) # carplate size and offset width_ = doc.createElement('width') width_.appendChild( doc.createTextNode(str(carplate_xmax - carplate_xmin))) bndbox.appendChild(width_) height_ = doc.createElement('height') height_.appendChild( doc.createTextNode(str(carplate_ymax - carplate_ymin))) bndbox.appendChild(height_) x_offset_ = doc.createElement('x_offset') x_offset_.appendChild( doc.createTextNode( str(carplate_x_center - car_x_center))) bndbox.appendChild(x_offset_) y_offset_ = doc.createElement('y_offset') y_offset_.appendChild( doc.createTextNode( str(carplate_y_center - car_y_center))) bndbox.appendChild(y_offset_) fp = open(os.path.join(target_dir, fileitem), 'w') doc.writexml(fp, indent='\t', addindent='\t', newl='\n', encoding="utf-8")
#!/usr/bin/python #coding:utf-8 from xml.dom.minidom import Document doc = Document() #<pais> root = doc.createElement('pais') sp = doc.createElement('estado') rj = doc.createElement('estado') frm = doc.createElement('Cidade') fcr = doc.createElement('Cidade') jund = doc.createElement('Cidade') #Setei os atr das minhas tags root.setAttribute('nome', 'Brasil') sp.setAttribute('nome', 'São Paulo') rj.setAttribute('nome', 'Rio de Janeiro') frm.setAttribute('nome', 'Francisco Morato') fcr.setAttribute('nome', 'Franco da Rocha') jund.setAttribute('nome', 'Jundiai') doc.appendChild(root) #ponto de partida root.appendChild(sp) root.appendChild(rj) sp.appendChild(frm) sp.appendChild(fcr) sp.appendChild(jund) #Criar habitantes em São Paulo
def CreateXMLFile(runlist, options, origQuery, datapath, xmlfname, xmllabel, svnversion='Unknown'): """ """ with timer('create RunStreamAndNeventsList'): # show number of events per stream per run ? ShowNumberOfEventsPerStreamPerRun = False ShowNumberOfEventsPerStreamSummary = True # find streams runstreamevents, runnrliststr = CreateRunStreamAndNeventsList( runlist) # { runnr: { stream: [(LUMIBLOCKNR, NREVENTS)] } } with timer('prepare document'): #################################### ## create XML file of GRL #################################### doc = Document() docType = DocumentType('LumiRangeCollection') docType.systemId = 'http://atlas-runquery.cern.ch/LumiRangeCollection.dtd' doc.appendChild(docType) # number of comments txt = ' Good-runs-list created by %s on %s ' % ( sys.argv[0].split('/')[-1], str(datetime.datetime.now())) doc.appendChild(doc.createComment(txt)) # root element lrc = doc.createElement('LumiRangeCollection') doc.appendChild(lrc) # NamedLumiRange namedLR = doc.createElement('NamedLumiRange') lrc.appendChild(namedLR) # name of NamedLumiRange namedLR.appendChild(TextElement('Name', xmllabel, doc)) # version of NamedLumiRange namedLR.appendChild(TextElement('Version', '2.1', doc)) # metadata of NamedLumiRange metadata = { 'Query': origQuery.split('/')[0], 'RQTSVNVersion': svnversion, 'RunList': runnrliststr } for md in metadata: mdelm = TextElement('Metadata', metadata[md], doc) mdelm.setAttribute('Name', md) namedLR.appendChild(mdelm) with timer('ShowNumberOfEventsPerStreamSummary'): if ShowNumberOfEventsPerStreamSummary: strsummdelm = doc.createElement('Metadata') strsummdelm.setAttribute('Name', 'StreamListInfo') namedLR.appendChild(strsummdelm) # lumiblock collections of NamedLumiRange streams_sum = {} streams_byrun = {} with timer('Loop over all runs'): for run in runlist: lbc = doc.createElement('LumiBlockCollection') # run number runnrelm = TextElement('Run', str(run.runNr), doc) if len(run.stats['SMK']['random']) == 2: (rd0, rd1) = run.stats['SMK']['random'][0:2] # protect against missing information if rd0 == 'n.a.': rd0 = 0 if rd1 == 'n.a.': rd1 = 0 runnrelm.setAttribute('PrescaleRD0', 0x1 << (3 + rd0)) runnrelm.setAttribute('PrescaleRD1', 0x1 << (3 + rd1)) else: (rd0, rd1, rd2, rd3) = run.stats['SMK']['random'][0:4] # protect against missing information if rd0 == 'n.a.': rd0 = 0 if rd1 == 'n.a.': rd1 = 0 if rd2 == 'n.a.': rd2 = 0 if rd3 == 'n.a.': rd3 = 0 runnrelm.setAttribute('Cut0', rd0) runnrelm.setAttribute('Cut1', rd1) runnrelm.setAttribute('Cut2', rd2) runnrelm.setAttribute('Cut3', rd3) lbc.appendChild(runnrelm) # streams (initialisation) streams = {} streams_byrun[run.runNr] = streams if runstreamevents.has_key( run.runNr ): # protection in case the run does not have any stream for stream in runstreamevents[run.runNr].keys(): if 'physics_' == stream[:8]: streams[stream] = [0, 0] # only for physics streams if not streams_sum.has_key(stream): streams_sum[stream] = [0, 0] # total number of events in stream for (nlb, nev) in runstreamevents[run.runNr][stream]: streams[stream][0] += nev # lumiblock ranges for lbrange in run.data.getLBRanges(activeOnly=True): lbrelm = TextElement('LBRange', '', doc) lbrelm.setAttribute('Start', lbrange[1]) lbrelm.setAttribute('End', lbrange[2] - 1) lbc.appendChild(lbrelm) # count nevents in streams if runstreamevents.has_key( run.runNr ): # protection in case the run does not have any stream for stream, lbnevts in runstreamevents[run.runNr].items(): if 'physics_' == stream[:8]: for (nlb, nev) in lbnevts: if nlb >= lbrange[1] and nlb < lbrange[2]: streams[stream][1] += nev # append stream element s = streams.keys() s.sort() strselm = doc.createElement('StreamsInRun') for stream in s: nevts = streams[stream] if ShowNumberOfEventsPerStreamPerRun: strelm = TextElement('Stream', '', doc) strelm.setAttribute('Name', stream) strelm.setAttribute('TotalNumOfEvents', nevts[0]) strelm.setAttribute('NumOfSelectedEvents', nevts[1]) strselm.appendChild(strelm) eff = 0 if nevts[0] > 0: eff = nevts[1] / float(nevts[0]) * 100.0 # collect total number of events streams_sum[stream][0] += nevts[0] streams_sum[stream][1] += nevts[1] # append streams if ShowNumberOfEventsPerStreamPerRun: lbc.appendChild(strselm) # append LumiBlickCollection namedLR.appendChild(lbc) with timer('Streams'): for stream in sorted(streams_sum.keys()): nevts = streams_sum[stream] if ShowNumberOfEventsPerStreamSummary: strelm = TextElement('Stream', '', doc) strelm.setAttribute('Name', stream) strelm.setAttribute('TotalNumOfEvents', nevts[0]) strelm.setAttribute('NumOfSelectedEvents', nevts[1]) strsummdelm.appendChild(strelm) with timer('Save GRL'): filename = '%s/%s' % (datapath, xmlfname) #print "Writing",filename xmlfile = open(filename, mode="w") xmlfile.write(doc.toprettyxml(' ')) xmlfile.close() with timer('Create HTML'): #################################### ## create HTML #################################### # provide also pretty html text output htmltext = '' hspace = ' ' htmltext += '<table style="width: auto; border: 0px solid; border-width: margin: 0 0 0 0; 0px; border-spacing: 0px; border-collapse: separate; padding: 0px;" font-family: sans-serif; font-size: 85%%">\n' htmltext += '<tr><td colspan="2"><b><font color="#999999">' + txt.strip( ) + '</font></b></td></tr>\n' htmltext += '<tr><td style="vertical-align:top">SVN Version: </td><td> ' + svnversion + '</td></tr>\n' htmltext += '<tr><td style="vertical-align:top">Query string:</td><td><b>' + origQuery.split( '/')[0] + '</b></td></tr>\n' htmltext += '<tr><td style="vertical-align:top">Run list:</td><td>' + runnrliststr + '</td></tr>\n' htmltext += '</table>' htmltext += '<hr color="#000000" size=1><font color="#777777">\n' htmltext += '<table style="width: auto; border: 0px solid; border-width: margin: 0 0 0 0; 0px; border-spacing: 0px; border-collapse: separate; padding: 0px;" font-family: sans-serif; font-size: 90%%">\n' # lumiblock collections of NamedLumiRange for run in runlist: # run number htmltext += '<tr><td style="text-align:left;height:25px;vertical-align:bottom">Run <b>%i</b>:</td><td></td></tr>\n' % run.runNr # lumiblock ranges for lbrange in run.data.getLBRanges(activeOnly=True): htmltext += '<tr><td></td><td style="text-align:left">LB range: [%5i-%5i]</td></tr>\n' % ( lbrange[1], lbrange[2] - 1) # append stream element htmltext += '<tr><td></td><td style="text-align:left"></td></tr>' htmltext += '<tr><td></td><td style="text-align:left"><table style="width: auto; border: 0px solid; border-width: margin: 0 0 0 0; 0px; border-spacing: 0px; border-collapse: separate; padding: 0px;font-family: sans-serif; font-size: 90%"><tr><td>Stream name</td><td>#Events total</td><td> #Events selected</td><td> Sel. fraction (%)</td></tr>\n' streams = streams_byrun[run.runNr] for stream in sorted(streams.keys()): nevts = streams[stream] eff = (nevts[1] / float(nevts[0]) * 100.0) if (nevts[0] > 0) else 0 htmltext += '<tr><td style="text-align:left"><i>%s</i></td><td style="text-align:right">%s</td><td style="text-align:right">%s</td><td style="text-align:right">%.4g</td></tr>\n' % ( stream, prettyNumber(nevts[0]), prettyNumber( nevts[1]), eff) htmltext += '</table></td></tr>\n' # append stream element htmltext += '</table>' htmltext += '<hr color="#000000" size=1><font color="#777777">\n' htmltext += '<b>Stream summary for all selected runs:</b><br>\n' htmltext += '<table style="width: auto; border: 0px solid; border-width: margin: 0 0 0 0; 0px; border-spacing: 0px; border-collapse: separate; padding: 0px;font-family: sans-serif; font-size: 95%"><tr><td>Stream name</td><td>#Events total</td><td> #Events selected</td><td> Sel. fraction (%)</td></tr>\n' for stream in sorted(streams_sum.keys()): nevts = streams_sum[stream] eff = 0 if nevts[0] > 0: eff = nevts[1] / float(nevts[0]) * 100.0 htmltext += '<tr><td style="text-align:left"><i>%s</i></td><td style="text-align:right">%s</td><td style="text-align:right">%s</td><td style="text-align:right">%.4g</td></tr>\n' % ( stream, prettyNumber(nevts[0]), prettyNumber(nevts[1]), eff) htmltext += '</table>\n' #print """======================================================== #%r #=========================================================== #""" % htmltext # provide also text output return htmltext
def open_port(service_url, external_port, internal_client, internal_port=None, protocol='TCP', duration=0, description=None, enabled=1): parsedurl = urlparse(service_url) if internal_port == None: internal_port = external_port if description == None: description = 'generated by port-forward.py' if not enabled: duration = 1 doc = Document() # create the envelope element and set its attributes envelope = doc.createElementNS('', 's:Envelope') envelope.setAttribute('xmlns:s', 'http://schemas.xmlsoap.org/soap/envelope/') envelope.setAttribute('s:encodingStyle', 'http://schemas.xmlsoap.org/soap/encoding/') # create the body element body = doc.createElementNS('', 's:Body') # create the function element and set its attribute fn = doc.createElementNS('', 'u:AddPortMapping') fn.setAttribute('xmlns:u', 'urn:schemas-upnp-org:service:WANIPConnection:1') # setup the argument element names and values # using a list of tuples to preserve order arguments = [ ('NewRemoteHost', ""), # unused - but required ('NewExternalPort', external_port), # specify port on router ('NewProtocol', protocol), # specify protocol ('NewInternalPort', internal_port), # specify port on internal host ('NewInternalClient', internal_client), # specify IP of internal host ('NewEnabled', enabled), # turn mapping ON ('NewPortMappingDescription', description), # add a description ('NewLeaseDuration', duration) ] # how long should it be opened? # NewEnabled should be 1 by default, but better supply it. # NewPortMappingDescription Can be anything you want, even an empty string. # NewLeaseDuration can be any integer BUT some UPnP devices don't support it, # so set it to 0 for better compatibility. # container for created nodes argument_list = [] # iterate over arguments, create nodes, create text nodes, # append text nodes to nodes, and finally add the ready product # to argument_list for k, v in arguments: v = str(v) tmp_node = doc.createElement(k) tmp_text_node = doc.createTextNode(v) tmp_node.appendChild(tmp_text_node) argument_list.append(tmp_node) # append the prepared argument nodes to the function element for arg in argument_list: fn.appendChild(arg) # append function element to the body element body.appendChild(fn) # append body element to envelope element envelope.appendChild(body) # append envelope element to document, making it the root element doc.appendChild(envelope) # our tree is ready, conver it to a string pure_xml = doc.toxml() # use the object returned by urlparse.urlparse to get the hostname and port conn = http.client.HTTPConnection(parsedurl.hostname, parsedurl.port) # use the path of WANIPConnection (or WANPPPConnection) to target that service, # insert the xml payload, # add two headers to make tell the server what we're sending exactly. conn.request( 'POST', parsedurl.path, pure_xml, { 'SOAPAction': '"urn:schemas-upnp-org:service:WANIPConnection:1#AddPortMapping"', 'Content-Type': 'text/xml' }) # wait for a response resp = conn.getresponse() return resp.status, resp.read()
class MathMLPrinter(Printer): """Prints an expression to the MathML markup language Whenever possible tries to use Content markup and not Presentation markup. References: http://www.w3.org/TR/MathML2/ """ printmethod = "_mathml" _default_settings = {"order": None, "encoding": "utf-8"} def __init__(self, settings=None): Printer.__init__(self, settings) from xml.dom.minidom import Document self.dom = Document() def doprint(self, expr): """ Prints the expression as MathML. """ mathML = Printer._print(self, expr) unistr = mathML.toxml() xmlbstr = unistr.encode('ascii', 'xmlcharrefreplace') res = xmlbstr.decode() return res def mathml_tag(self, e): """Returns the MathML tag for an expression.""" translate = { 'Add': 'plus', 'Mul': 'times', 'Derivative': 'diff', 'Number': 'cn', 'int': 'cn', 'Pow': 'power', 'Symbol': 'ci', 'Integral': 'int', 'Sum': 'sum', 'sin': 'sin', 'cos': 'cos', 'tan': 'tan', 'cot': 'cot', 'asin': 'arcsin', 'asinh': 'arcsinh', 'acos': 'arccos', 'acosh': 'arccosh', 'atan': 'arctan', 'atanh': 'arctanh', 'acot': 'arccot', 'atan2': 'arctan', 'log': 'ln', 'Equality': 'eq', 'Unequality': 'neq', 'GreaterThan': 'geq', 'LessThan': 'leq', 'StrictGreaterThan': 'gt', 'StrictLessThan': 'lt', } for cls in e.__class__.__mro__: n = cls.__name__ if n in translate: return translate[n] # Not found in the MRO set n = e.__class__.__name__ return n.lower() def _print_Mul(self, expr): if _coeff_isneg(expr): x = self.dom.createElement('apply') x.appendChild(self.dom.createElement('minus')) x.appendChild(self._print_Mul(-expr)) return x from sympy.simplify import fraction numer, denom = fraction(expr) if denom is not S.One: x = self.dom.createElement('apply') x.appendChild(self.dom.createElement('divide')) x.appendChild(self._print(numer)) x.appendChild(self._print(denom)) return x coeff, terms = expr.as_coeff_mul() if coeff is S.One and len(terms) == 1: # XXX since the negative coefficient has been handled, I don't # thing a coeff of 1 can remain return self._print(terms[0]) if self.order != 'old': terms = Mul._from_args(terms).as_ordered_factors() x = self.dom.createElement('apply') x.appendChild(self.dom.createElement('times')) if (coeff != 1): x.appendChild(self._print(coeff)) for term in terms: x.appendChild(self._print(term)) return x def _print_Add(self, expr, order=None): args = self._as_ordered_terms(expr, order=order) lastProcessed = self._print(args[0]) plusNodes = [] for arg in args[1:]: if _coeff_isneg(arg): #use minus x = self.dom.createElement('apply') x.appendChild(self.dom.createElement('minus')) x.appendChild(lastProcessed) x.appendChild(self._print(-arg)) #invert expression since this is now minused lastProcessed = x if (arg == args[-1]): plusNodes.append(lastProcessed) else: plusNodes.append(lastProcessed) lastProcessed = self._print(arg) if (arg == args[-1]): plusNodes.append(self._print(arg)) if len(plusNodes) == 1: return lastProcessed x = self.dom.createElement('apply') x.appendChild(self.dom.createElement('plus')) while len(plusNodes) > 0: x.appendChild(plusNodes.pop(0)) return x def _print_MatrixBase(self, m): x = self.dom.createElement('matrix') for i in range(m.lines): x_r = self.dom.createElement('matrixrow') for j in range(m.cols): x_r.appendChild(self._print(m[i, j])) x.appendChild(x_r) return x def _print_Rational(self, e): if e.q == 1: #don't divide x = self.dom.createElement('cn') x.appendChild(self.dom.createTextNode(str(e.p))) return x x = self.dom.createElement('apply') x.appendChild(self.dom.createElement('divide')) #numerator xnum = self.dom.createElement('cn') xnum.appendChild(self.dom.createTextNode(str(e.p))) #denomenator xdenom = self.dom.createElement('cn') xdenom.appendChild(self.dom.createTextNode(str(e.q))) x.appendChild(xnum) x.appendChild(xdenom) return x def _print_Limit(self, e): x = self.dom.createElement('apply') x.appendChild(self.dom.createElement(self.mathml_tag(e))) x_1 = self.dom.createElement('bvar') x_2 = self.dom.createElement('lowlimit') x_1.appendChild(self._print(e.args[1])) x_2.appendChild(self._print(e.args[2])) x.appendChild(x_1) x.appendChild(x_2) x.appendChild(self._print(e.args[0])) return x def _print_ImaginaryUnit(self, e): return self.dom.createElement('imaginaryi') def _print_EulerGamma(self, e): return self.dom.createElement('eulergamma') def _print_GoldenRatio(self, e): """We use unicode #x3c6 for Greek letter phi as defined here http://www.w3.org/2003/entities/2007doc/isogrk1.html""" x = self.dom.createElement('cn') x.appendChild(self.dom.createTextNode(u("\u03c6"))) return x def _print_Exp1(self, e): return self.dom.createElement('exponentiale') def _print_Pi(self, e): return self.dom.createElement('pi') def _print_Infinity(self, e): return self.dom.createElement('infinity') def _print_Negative_Infinity(self, e): x = self.dom.createElement('apply') x.appendChild(self.dom.createElement('minus')) x.appendChild(self.dom.createElement('infinity')) return x def _print_Integral(self, e): def lime_recur(limits): x = self.dom.createElement('apply') x.appendChild(self.dom.createElement(self.mathml_tag(e))) bvar_elem = self.dom.createElement('bvar') bvar_elem.appendChild(self._print(limits[0][0])) x.appendChild(bvar_elem) if len(limits[0]) == 3: low_elem = self.dom.createElement('lowlimit') low_elem.appendChild(self._print(limits[0][1])) x.appendChild(low_elem) up_elem = self.dom.createElement('uplimit') up_elem.appendChild(self._print(limits[0][2])) x.appendChild(up_elem) if len(limits[0]) == 2: up_elem = self.dom.createElement('uplimit') up_elem.appendChild(self._print(limits[0][1])) x.appendChild(up_elem) if len(limits) == 1: x.appendChild(self._print(e.function)) else: x.appendChild(lime_recur(limits[1:])) return x limits = list(e.limits) limits.reverse() return lime_recur(limits) def _print_Sum(self, e): # Printer can be shared because Sum and Integral have the # same internal representation. return self._print_Integral(e) def _print_Symbol(self, sym): ci = self.dom.createElement(self.mathml_tag(sym)) def join(items): if len(items) > 1: mrow = self.dom.createElement('mml:mrow') for i, item in enumerate(items): if i > 0: mo = self.dom.createElement('mml:mo') mo.appendChild(self.dom.createTextNode(" ")) mrow.appendChild(mo) mi = self.dom.createElement('mml:mi') mi.appendChild(self.dom.createTextNode(item)) mrow.appendChild(mi) return mrow else: mi = self.dom.createElement('mml:mi') mi.appendChild(self.dom.createTextNode(items[0])) return mi # translate name, supers and subs to unicode characters greek_letters = set(greeks) # make a copy def translate(s): if s in greek_unicode: return greek_unicode.get(s) else: return s name, supers, subs = split_super_sub(sym.name) name = translate(name) supers = [translate(sup) for sup in supers] subs = [translate(sub) for sub in subs] mname = self.dom.createElement('mml:mi') mname.appendChild(self.dom.createTextNode(name)) if len(supers) == 0: if len(subs) == 0: ci.appendChild(self.dom.createTextNode(name)) else: msub = self.dom.createElement('mml:msub') msub.appendChild(mname) msub.appendChild(join(subs)) ci.appendChild(msub) else: if len(subs) == 0: msup = self.dom.createElement('mml:msup') msup.appendChild(mname) msup.appendChild(join(supers)) ci.appendChild(msup) else: msubsup = self.dom.createElement('mml:msubsup') msubsup.appendChild(mname) msubsup.appendChild(join(subs)) msubsup.appendChild(join(supers)) ci.appendChild(msubsup) return ci def _print_Pow(self, e): #Here we use root instead of power if the exponent is the reciprocal of an integer if e.exp.is_Rational and e.exp.p == 1: x = self.dom.createElement('apply') x.appendChild(self.dom.createElement('root')) if e.exp.q != 2: xmldeg = self.dom.createElement('degree') xmlci = self.dom.createElement('ci') xmlci.appendChild(self.dom.createTextNode(str(e.exp.q))) xmldeg.appendChild(xmlci) x.appendChild(xmldeg) x.appendChild(self._print(e.base)) return x x = self.dom.createElement('apply') x_1 = self.dom.createElement(self.mathml_tag(e)) x.appendChild(x_1) x.appendChild(self._print(e.base)) x.appendChild(self._print(e.exp)) return x def _print_Number(self, e): x = self.dom.createElement(self.mathml_tag(e)) x.appendChild(self.dom.createTextNode(str(e))) return x def _print_Derivative(self, e): x = self.dom.createElement('apply') diff_symbol = self.mathml_tag(e) if requires_partial(e): diff_symbol = 'partialdiff' x.appendChild(self.dom.createElement(diff_symbol)) x_1 = self.dom.createElement('bvar') for sym in e.variables: x_1.appendChild(self._print(sym)) x.appendChild(x_1) x.appendChild(self._print(e.expr)) return x def _print_Function(self, e): x = self.dom.createElement("apply") x.appendChild(self.dom.createElement(self.mathml_tag(e))) for arg in e.args: x.appendChild(self._print(arg)) return x def _print_Basic(self, e): x = self.dom.createElement(self.mathml_tag(e)) for arg in e: x.appendChild(self._print(arg)) return x def _print_AssocOp(self, e): x = self.dom.createElement('apply') x_1 = self.dom.createElement(self.mathml_tag(e)) x.appendChild(x_1) for arg in e.args: x.appendChild(self._print(arg)) return x def _print_Relational(self, e): x = self.dom.createElement('apply') x.appendChild(self.dom.createElement(self.mathml_tag(e))) x.appendChild(self._print(e.lhs)) x.appendChild(self._print(e.rhs)) return x def _print_list(self, seq): """MathML reference for the <list> element: http://www.w3.org/TR/MathML2/chapter4.html#contm.list""" dom_element = self.dom.createElement('list') for item in seq: dom_element.appendChild(self._print(item)) return dom_element def _print_int(self, p): dom_element = self.dom.createElement(self.mathml_tag(p)) dom_element.appendChild(self.dom.createTextNode(str(p))) return dom_element def apply_patch(self): # Applying the patch of xml.dom.minidom bug # Date: 2011-11-18 # Description: http://ronrothman.com/public/leftbraned/xml-dom-minidom-\ # toprettyxml-and-silly-whitespace/#best-solution # Issue: http://bugs.python.org/issue4147 # Patch: http://hg.python.org/cpython/rev/7262f8f276ff/ from xml.dom.minidom import Element, Text, Node, _write_data def writexml(self, writer, indent="", addindent="", newl=""): # indent = current indentation # addindent = indentation to add to higher levels # newl = newline string writer.write(indent + "<" + self.tagName) attrs = self._get_attributes() a_names = list(attrs.keys()) a_names.sort() for a_name in a_names: writer.write(" %s=\"" % a_name) _write_data(writer, attrs[a_name].value) writer.write("\"") if self.childNodes: writer.write(">") if (len(self.childNodes) == 1 and self.childNodes[0].nodeType == Node.TEXT_NODE): self.childNodes[0].writexml(writer, '', '', '') else: writer.write(newl) for node in self.childNodes: node.writexml(writer, indent + addindent, addindent, newl) writer.write(indent) writer.write("</%s>%s" % (self.tagName, newl)) else: writer.write("/>%s" % (newl)) self._Element_writexml_old = Element.writexml Element.writexml = writexml def writexml(self, writer, indent="", addindent="", newl=""): _write_data(writer, "%s%s%s" % (indent, self.data, newl)) self._Text_writexml_old = Text.writexml Text.writexml = writexml def restore_patch(self): from xml.dom.minidom import Element, Text Element.writexml = self._Element_writexml_old Text.writexml = self._Text_writexml_old