def createDoc(path): file = open(path, "w") doc = Document() wml = doc.createElement("data") doc.appendChild(wml) file.write(doc.toprettyxml(indent=" ")) file.close()
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','')
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 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 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 send_server_status(self, status): doc = Document() rootNode = doc.createElement('server') rootNode.setAttribute("status", status) doc.appendChild(rootNode) response = self.send_packet("/server/status", doc) if response is False: Logger.warn("SMRequest::send_server_status Unable to send packet") return False document = self.get_response_xml(response) if document is None: Logger.warn("SMRequest::send_server_status response not XML") return False rootNode = document.documentElement if rootNode.nodeName != "server": Logger.error("SMRequest::send_server_status response not valid %s"%(rootNode.toxml())) return False if not rootNode.hasAttribute("name") or rootNode.getAttribute("name") != self.name: Logger.error("SMRequest::send_server_status response invalid name") return False if not rootNode.hasAttribute("status") or rootNode.getAttribute("status") != status: Logger.error("SMRequest::send_server_status response invalid status") return False return True
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 createBalanceSheetPDF(self): out = open("/tmp/balancesheet_"+str(self.id)+".xml","w") doc = Document() main = doc.createElement("koalixaccountingbalacesheet") calculationUnitName = doc.createElement("calculationUnitName") calculationUnitName.appendChild(doc.createTextNode(self.__unicode__())) main.appendChild(calculationUnitName) calculationUnitTo = doc.createElement("calculationUnitTo") calculationUnitTo.appendChild(doc.createTextNode(self.end.year.__str__())) main.appendChild(calculationUnitTo) calculationUnitFrom = doc.createElement("calculationUnitFrom") calculationUnitFrom.appendChild(doc.createTextNode(self.begin.year.__str__())) main.appendChild(calculationUnitFrom) accountNumber = doc.createElement("AccountNumber") accounts = Account.objects.all() for account in list(accounts) : currentValue = account.valueNow(self) if (currentValue != 0): currentAccountElement = doc.createElement("Account") accountNumber = doc.createElement("AccountNumber") accountNumber.appendChild(doc.createTextNode(account.accountNumber.__str__())) currentValueElement = doc.createElement("currentValue") currentValueElement.appendChild(doc.createTextNode(currentValue.__str__())) accountNameElement = doc.createElement("accountName") accountNameElement.appendChild(doc.createTextNode(account.title)) currentAccountElement.setAttribute("accountType", account.accountType.__str__()) currentAccountElement.appendChild(accountNumber) currentAccountElement.appendChild(accountNameElement) currentAccountElement.appendChild(currentValueElement) main.appendChild(currentAccountElement) doc.appendChild(main) out.write(doc.toprettyxml(indent=" ")) system("fop -c /var/www/koalixcrm/verasans.xml -xml /tmp/balancesheet_"+str(self.id)+".xml -xsl /var/www/koalixcrm/balancesheet.xsl -pdf /tmp/balancesheet_"+str(self.id)+".pdf") return "/tmp/balancesheet_"+str(self.id)+".pdf"
def testTaxes7(self): doc = Document() parent_node = doc.createElement('parent_node') doc.appendChild(parent_node) data = { 'default-tax-table': { 'tax-rules': [ { 'shipping-taxed': True, 'rate': 0.175, 'tax-area': { 'postal-area': [ {'country-code': 'DE'}, {'country-code': 'ES'}, {'country-code': 'GB'}, ], }, }, ] }, } self.gc._taxes(doc, parent_node, data) xml1 = "<parent_node><tax-tables>\ <default-tax-table><tax-rules><default-tax-rule>\ <shipping-taxed>true</shipping-taxed><rate>0.175</rate>\ <tax-areas><postal-area><country-code>DE</country-code>\ </postal-area><postal-area><country-code>ES</country-code>\ </postal-area><postal-area><country-code>GB</country-code>\ </postal-area></tax-areas></default-tax-rule></tax-rules>\ </default-tax-table></tax-tables></parent_node>" doc_good = parseString(xml1) self.assertEquals(doc.toxml(), doc_good.toxml())
def testShippingExclude(self): doc = Document() parent_node = doc.createElement('parent_node') doc.appendChild(parent_node) data = { 'us-state-area': ['AK','HI'], 'us-zip-area': ['90210', '04005', '04092'], 'us-country-area': 'CONTINENTAL_48', 'world-area': True, 'postal-area': [{ 'country-code': 'US', 'postal-code-pattern': ['94043', '90211'], }, ], } self.gc._shipping_allowed_excluded(doc, parent_node, data) xml1 = "<parent_node><us-state-area><state>AK</state></us-state-area>\ <us-state-area><state>HI</state></us-state-area>\ <us-zip-area><zip-pattern>90210</zip-pattern></us-zip-area>\ <us-zip-area><zip-pattern>04005</zip-pattern></us-zip-area>\ <us-zip-area><zip-pattern>04092</zip-pattern></us-zip-area>\ <us-country-area country-area='CONTINENTAL_48'/>\ <world-area/>\ <postal-area><country-code>US</country-code>\ <postal-code-pattern>94043</postal-code-pattern>\ <postal-code-pattern>90211</postal-code-pattern></postal-area>\ </parent_node>" doc_good = parseString(xml1) self.assertEquals(doc.toxml(), doc_good.toxml())
def testTaxes2(self): doc = Document() parent_node = doc.createElement('parent_node') doc.appendChild(parent_node) data = { 'default-tax-table': { 'tax-rules': [ { 'shipping-taxed': True, 'rate': 0.06, 'tax-area': { 'us-state-area': ['CT'], } }, { 'rate': 0.05, 'tax-area': { 'us-state-area': ['MD'], } } ] } } self.gc._taxes(doc, parent_node, data) xml1 = "<parent_node><tax-tables><default-tax-table><tax-rules>\ <default-tax-rule><shipping-taxed>true</shipping-taxed><rate>0.06</rate>\ <tax-area><us-state-area><state>CT</state></us-state-area></tax-area>\ </default-tax-rule><default-tax-rule><shipping-taxed>false</shipping-taxed>\ <rate>0.05</rate><tax-area><us-state-area><state>MD</state></us-state-area>\ </tax-area></default-tax-rule></tax-rules></default-tax-table></tax-tables>\ </parent_node>" doc_good = parseString(xml1) self.assertEquals(doc.toxml(), doc_good.toxml())
def testTaxes3(self): doc = Document() parent_node = doc.createElement('parent_node') doc.appendChild(parent_node) data = { 'default-tax-table': { 'tax-rules': [ { 'shipping-taxed': False, 'rate': 0.08375, 'tax-area': { 'us-zip-area': ['100*'], } }, { 'shipping-taxed': True, 'rate': 0.04, 'tax-area': { 'us-state-area': ['NY'], } } ] } } self.gc._taxes(doc, parent_node, data) xml1 = "<parent_node><tax-tables><default-tax-table>\ <tax-rules><default-tax-rule><shipping-taxed>false</shipping-taxed>\ <rate>0.08375</rate><tax-area><us-zip-area><zip-pattern>100*</zip-pattern>\ </us-zip-area></tax-area></default-tax-rule>\ <default-tax-rule><shipping-taxed>true</shipping-taxed>\ <rate>0.04</rate><tax-area><us-state-area><state>NY</state></us-state-area>\ </tax-area></default-tax-rule>\ </tax-rules></default-tax-table></tax-tables></parent_node>" doc_good = parseString(xml1) self.assertEquals(doc.toxml(), doc_good.toxml())
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 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 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 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 createGeneralUnknownResponse(self, message): doc = Document() root_response = doc.createElement('Response') root_response.setAttribute("creationSuccessful", str(False)) root_response.setAttribute("reason", message) doc.appendChild(root_response) return doc.toxml(encoding='utf-8')
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')
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 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 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 impl_create_pydevproject(self, system_path, user_path): # create a pydevproject file doc = Document() doc.appendChild(doc.createProcessingInstruction('eclipse-pydev', 'version="1.0"')) pydevproject = doc.createElement('pydev_project') prop = self.add(doc, pydevproject, 'pydev_property', 'python %d.%d'%(sys.version_info[0], sys.version_info[1])) prop.setAttribute('name', 'org.python.pydev.PYTHON_PROJECT_VERSION') prop = self.add(doc, pydevproject, 'pydev_property', 'Default') prop.setAttribute('name', 'org.python.pydev.PYTHON_PROJECT_INTERPRETER') # add waf's paths wafadmin = [p for p in system_path if p.find('wafadmin') != -1] if wafadmin: prop = self.add(doc, pydevproject, 'pydev_pathproperty', {'name':'org.python.pydev.PROJECT_EXTERNAL_SOURCE_PATH'}) for i in wafadmin: self.add(doc, prop, 'path', i) if user_path: prop = self.add(doc, pydevproject, 'pydev_pathproperty', {'name':'org.python.pydev.PROJECT_SOURCE_PATH'}) for i in user_path: self.add(doc, prop, 'path', '/${PROJECT_DIR_NAME}/'+i) doc.appendChild(pydevproject) return doc
def testLegalChildren(): dom = Document() elem = dom.createElement('element') text = dom.createTextNode('text') try: dom.appendChild(text) except HierarchyRequestErr: pass else: print "dom.appendChild didn't raise HierarchyRequestErr" dom.appendChild(elem) try: dom.insertBefore(text, elem) except HierarchyRequestErr: pass else: print "dom.appendChild didn't raise HierarchyRequestErr" try: dom.replaceChild(text, elem) except HierarchyRequestErr: pass else: print "dom.appendChild didn't raise HierarchyRequestErr" nodemap = elem.attributes try: nodemap.setNamedItem(text) except HierarchyRequestErr: pass else: print "NamedNodeMap.setNamedItem didn't raise HierarchyRequestErr" try: nodemap.setNamedItemNS(text) except HierarchyRequestErr: pass else: print "NamedNodeMap.setNamedItemNS didn't raise HierarchyRequestErr" elem.appendChild(text) dom.unlink()
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 do_call_exit(self): if d.sessionProperties["persistent"]: mode = "suspend" else: mode = "logout" document = Document() rootNode = document.createElement("logout") rootNode.setAttribute("mode", mode) document.appendChild(rootNode) url = "%s/logout.php" % (self.base_url) request = urllib2.Request(url) request.add_header("Content-type", "text/xml; charset=UTF-8") request.add_data(document.toxml()) try: url = self.urlOpener.open(request) except urllib2.HTTPError, exc: if exc.code == 500: Logger.warn("Service failurek") return False Logger.debug("HTTP request return code %d (%s)" % (exc.code, exc.msg)) Logger.debug(" * return: %s" % (str(exc.read()))) return False
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 _create_Address_Validation_Request_xml( self, data_for_Address_Validation_Request, data_for_Access_Request): """Creates and returns the xml request to be sent address validation API """ doc1 = Document() AccessRequest = doc1.createElement("AccessRequestxml") AccessRequest.setAttribute("xml:lang", "en-US") doc1.appendChild(AccessRequest) AccessLicenseNumber = doc1.createElement("AccessLicenseNumber") ptext = doc1.createTextNode( data_for_Access_Request["AccessLicenseNumber"]) AccessLicenseNumber.appendChild(ptext) AccessRequest.appendChild(AccessLicenseNumber) UserId = doc1.createElement("UserId") ptext = doc1.createTextNode(data_for_Access_Request["UserId"]) UserId.appendChild(ptext) AccessRequest.appendChild(UserId) Password = doc1.createElement("Password") ptext = doc1.createTextNode(data_for_Access_Request["Password"]) Password.appendChild(ptext) AccessRequest.appendChild(Password) doc = Document() #creating AddressValidationRequest tag AddressValidationRequest = doc.createElement( "AddressValidationRequest") AddressValidationRequest.setAttribute("xml:lang", "en-US") doc.appendChild(AddressValidationRequest) #creating Request tag XMLpath=/AddressValidationRequest/Request Request = doc.createElement("Request") AddressValidationRequest.appendChild(Request) #creating TransactionReference tag XMLpath=AddressValidationRequest/Request/TransactionReference TransactionReference = doc.createElement("TransactionReference") Request.appendChild(TransactionReference) #creating CustomerContext tag XMLpath=/AddressValidationRequest/Request/TransactionReference/CustomerContext CustomerContext = doc.createElement("CustomerContext") ptext = doc.createTextNode( data_for_Address_Validation_Request['Request'] ['Transaction Reference']["CustomerContext"]) CustomerContext.appendChild(ptext) TransactionReference.appendChild(CustomerContext) #creating XpciVersion tag XMLpath=AddressValidationRequest/Request/TransactionReference/XpciVersion XpciVersion = doc.createElement("XpciVersion") ptext = doc.createTextNode( data_for_Address_Validation_Request['Request'] ['Transaction Reference']["XpciVersion"]) XpciVersion.appendChild(ptext) TransactionReference.appendChild(XpciVersion) #creating ToolVersion tag XMLpath=AddressValidationRequest/Request/TransactionReference/ToolVersion ToolVersion = doc.createElement("ToolVersion") ptext = doc.createTextNode( data_for_Address_Validation_Request['Request'] ['Transaction Reference']["ToolVersion"]) ToolVersion.appendChild(ptext) TransactionReference.appendChild(ToolVersion) #creating RequestAction tag XMLpath=AddressValidationRequest/Request/RequestAction RequestAction = doc.createElement("RequestAction") ptext = doc.createTextNode( data_for_Address_Validation_Request['Request']["RequestAction"]) RequestAction.appendChild(ptext) Request.appendChild(RequestAction) #creating RequestOption tag XMLpath=AddressValidationRequest/Request/RequestOption RequestOption = doc.createElement("RequestOption") ptext = doc.createTextNode( data_for_Address_Validation_Request['Request']["RequestOption"]) RequestOption.appendChild(ptext) Request.appendChild(RequestOption) #creating RequestOption tag XMLpath=AddressValidationRequest/MaximumListSize MaximumListSize = doc.createElement("MaximumListSize") ptext = doc.createTextNode( data_for_Address_Validation_Request["MaximumListSize"]) MaximumListSize.appendChild(ptext) AddressValidationRequest.appendChild(MaximumListSize) #creating AddressKeyFormat tag XMLpath=AddressValidationRequest/AddressKeyFormat AddressKeyFormat = doc.createElement("AddressKeyFormat") AddressValidationRequest.appendChild(AddressKeyFormat) #creating ConsigneeName tag XMLpath=AddressValidationRequest/AddressKeyFormat/ConsigneeName ConsigneeName = doc.createElement("ConsigneeName") ptext = doc.createTextNode( data_for_Address_Validation_Request['AddressKeyFormat'] ["ConsigneeName"]) ConsigneeName.appendChild(ptext) AddressKeyFormat.appendChild(ConsigneeName) #creating BuildingName tag XMLpath=AddressValidationRequest/AddressKeyFormat/BuildingName BuildingName = doc.createElement("BuildingName") ptext = doc.createTextNode( data_for_Address_Validation_Request['AddressKeyFormat'] ["BuildingName"]) BuildingName.appendChild(ptext) BuildingName.appendChild(BuildingName) #creating AddressLine tag XMLpath=AddressValidationRequest/AddressKeyFormat/AddressLine AddressLine = doc.createElement("AddressLine") ptext = doc.createTextNode( data_for_Address_Validation_Request['AddressKeyFormat'] ["AddressLine1"]) AddressLine.appendChild(ptext) AddressKeyFormat.appendChild(AddressLine) AddressLine = doc.createElement("AddressLine") ptext = doc.createTextNode( data_for_Address_Validation_Request['AddressKeyFormat'] ["AddressLine2"]) AddressLine.appendChild(ptext) AddressKeyFormat.appendChild(AddressLine) #creating Region tag XMLpath=AddressValidationRequest/AddressKeyFormat/Region Region = doc.createElement("Region") ptext = doc.createTextNode( data_for_Address_Validation_Request['AddressKeyFormat']["Region"]) Region.appendChild(ptext) AddressKeyFormat.appendChild(Region) #creating PoliticalDivision2 tag XMLpath=AddressValidationRequest/AddressKeyFormat/PoliticalDivision2 PoliticalDivision2 = doc.createElement("PoliticalDivision2") ptext = doc.createTextNode( data_for_Address_Validation_Request['AddressKeyFormat'] ["PoliticalDivision2"]) PoliticalDivision2.appendChild(ptext) AddressKeyFormat.appendChild(PoliticalDivision2) #creating PoliticalDivision1 tag XMLpath=AddressValidationRequest/AddressKeyFormat/PoliticalDivision1 PoliticalDivision1 = doc.createElement("PoliticalDivision1") ptext = doc.createTextNode( data_for_Address_Validation_Request['AddressKeyFormat'] ["PoliticalDivision1"]) PoliticalDivision1.appendChild(ptext) AddressKeyFormat.appendChild(PoliticalDivision1) #creating PostcodePrimaryLow tag XMLpath=AddressValidationRequest/AddressKeyFormat/PostcodePrimaryLow PostcodePrimaryLow = doc.createElement("PostcodePrimaryLow") ptext = doc.createTextNode( data_for_Address_Validation_Request['AddressKeyFormat'] ["PostcodePrimaryLow"]) PostcodePrimaryLow.appendChild(ptext) AddressKeyFormat.appendChild(PostcodePrimaryLow) #creating PostcodeExtendedLow tag XMLpath=AddressValidationRequest/AddressKeyFormat/PostcodeExtendedLow PostcodeExtendedLow = doc.createElement("PostcodeExtendedLow") ptext = doc.createTextNode( data_for_Address_Validation_Request['AddressKeyFormat'] ["PostcodeExtendedLow"]) PostcodeExtendedLow.appendChild(ptext) AddressKeyFormat.appendChild(PostcodeExtendedLow) #creating PostcodeExtendedLow tag XMLpath=AddressValidationRequest/AddressKeyFormat/Urbanization Urbanization = doc.createElement("Urbanization") ptext = doc.createTextNode( data_for_Address_Validation_Request['AddressKeyFormat'] ["Urbanization"]) Urbanization.appendChild(ptext) AddressKeyFormat.appendChild(Urbanization) #creating CountryCode tag XMLpath=AddressValidationRequest/AddressKeyFormat/CountryCode CountryCode = doc.createElement("CountryCode") ptext = doc.createTextNode( data_for_Address_Validation_Request['AddressKeyFormat'] ["CountryCode"]) CountryCode.appendChild(ptext) AddressKeyFormat.appendChild(CountryCode) Request_string1 = doc1.toprettyxml() Request_string2 = doc.toprettyxml() Request_string = Request_string1 + Request_string2 return Request_string
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('yang')) 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()
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.2"/> <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 = {} # 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, 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. """ vuln_dict = { "method": request.method, "path": request.file_path, "info": info, "level": level, "parameter": parameter, "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, 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. """ anom_dict = { "method": request.method, "path": request.file_path, "info": info, "level": level, "parameter": parameter, "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) 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") # 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 # 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) 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) with open(output_path, "w") as fd: self._xml_doc.writexml(fd, addindent=" ", newl="\n")
index_str = doc.createTextNode(str(i)) index.appendChild(index_str) fileName = doc.createElement("Name") fileName_str = doc.createTextNode(file[:-4]) fileName.appendChild(fileName_str) plate.appendChild(index) plate.appendChild(fileName) for pstr, confidence, rect in model.SimpleRecognizePlateByE2E(grr): platestr = doc.createElement("pstr") if confidence > 0.8: print("file name:", file[:-4]) print("plate_str:", pstr) print("plate_confidence:", confidence) if (file[:-4] == pstr): pstr_str = doc.createTextNode(pstr) platestr.appendChild(pstr_str) else: pstr_str = doc.createTextNode("Error " + pstr) platestr.appendChild(pstr_str) else: pstr_str = doc.createTextNode("Error " + pstr + "confidence: " + str(confidence)) platestr.appendChild(pstr_str) plate.appendChild(platestr) print('\n') root.appendChild(plate) doc.appendChild(root) f = codecs.open("data.xml", 'w', 'utf-8') f.write(doc.toprettyxml(indent=' ')) f.close()
def _build_xml_shopping_cart(request): doc = Document() if request.POST: postdata = request.POST.copy() card_num = postdata.get('credit_card_number', '') exp_month = postdata.get('credit_card_expire_month', '') exp_year = postdata.get('credit_card_expire_year', '') exp_date = exp_year + '-' + exp_month cvv = postdata.get('credit_card_cvv', '') emailaddr = postdata.get('email', '') # billing info bname = postdata.get('billing_name', '') fullname = bname.split(' ') fname = fullname[0] if len(fullname) > 1: lname = fullname[len(fullname) - 1] bcity = postdata.get('shipping_city', '') bstate = postdata.get('shipping_state', '') bzip = postdata.get('shipping_zip', '') bcountry = postdata.get('shipping_country', '') baddress1 = postdata.get('billing_address_1', '') baddress2 = postdata.get('billing_address_2', '') baddress = str(baddress1) + ' ' + str(baddress2) # shipping info sname = postdata.get('shipping_name', '') sfullname = sname.split(' ') sfname = sfullname[0] if len(sfullname) > 1: slname = sfullname[len(sfullname) - 1] scity = postdata.get('billing_city', '') sstate = postdata.get('billing_state', '') szip = postdata.get('billing_zip', '') scountry = postdata.get('billing_country', '') saddress1 = postdata.get('billing_address_1', '') saddress2 = postdata.get('billing_address_2', '') saddress = str(saddress1) + ' ' + str(saddress2) root = doc.createElement('createTransactionRequest') root.setAttribute('xmlns', 'AnetApi/xml/v1/schema/AnetApiSchema.xsd') doc.appendChild(root) merchantauthentication = doc.createElement('merchantAuthentication') root.appendChild(merchantauthentication) name = doc.createElement('name') name_text = doc.createTextNode(str(settings.AUTHNET_LOGIN)) name.appendChild(name_text) merchantauthentication.appendChild(name) transactionkey = doc.createElement('transactionKey') transactionkey_text = doc.createTextNode(str(settings.AUTHNET_KEY)) transactionkey.appendChild(transactionkey_text) merchantauthentication.appendChild(transactionkey) transactionrequest = doc.createElement('transactionRequest') root.appendChild(transactionrequest) transactiontype = doc.createElement('transactionType') transactiontype_text = doc.createTextNode(str(settings.AUTHNET_TTYPE)) transactiontype.appendChild(transactiontype_text) transactionrequest.appendChild(transactiontype) amount = doc.createElement('amount') amount_text = doc.createTextNode(str(cart.cart_subtotal(request) / USD)) amount.appendChild(amount_text) transactionrequest.appendChild(amount) payment = doc.createElement('payment') transactionrequest.appendChild(payment) creditcard = doc.createElement('creditCard') payment.appendChild(creditcard) cardnumber = doc.createElement('cardNumber') cardnumber_text = doc.createTextNode(str(card_num)) cardnumber.appendChild(cardnumber_text) creditcard.appendChild(cardnumber) expirationdate = doc.createElement('expirationDate') expirationdate_text = doc.createTextNode(str(exp_date)) expirationdate.appendChild(expirationdate_text) creditcard.appendChild(expirationdate) cardcode = doc.createElement('cardCode') cardcode_text = doc.createTextNode(cvv) cardcode.appendChild(cardcode_text) creditcard.appendChild(cardcode) order = doc.createElement('order') transactionrequest.appendChild(order) invoicenumber = doc.createElement('invoiceNumber') invoicenumber_text = doc.createTextNode(str(InvNo)) invoicenumber.appendChild(invoicenumber_text) order.appendChild(invoicenumber) description = doc.createElement('description') description_text = doc.createTextNode('Modern Musician Invoice') description.appendChild(description_text) order.appendChild(description) lineitems = doc.createElement('lineItems') transactionrequest.appendChild(lineitems) cart_items = cart.get_cart_items(request) for cart_item in cart_items: lineitem = doc.createElement('lineItem') lineitems.appendChild(lineitem) itemid = doc.createElement('itemId') itemid_text = doc.createTextNode(str(cart_item.product.id)) itemid.appendChild(itemid_text) lineitem.appendChild(itemid) name = doc.createElement('name') name_text = doc.createTextNode(str(cart_item.name)) name.appendChild(name_text) lineitem.appendChild(name) description = doc.createElement('description') description_text = doc.createTextNode(str(cart_item.description)) description.appendChild(description_text) lineitem.appendChild(description) quantity = doc.createElement('quantity') quantity_text = doc.createTextNode(str(cart_item.quantity)) quantity.appendChild(quantity_text) lineitem.appendChild(quantity) price = doc.createElement('unitPrice') price_text = doc.createTextNode(str(cart_item.price / USD)) price.appendChild(price_text) lineitem.appendChild(price) customer = doc.createElement('customer') transactionrequest.appendChild(customer) id = doc.createElement('id') id_text = doc.createTextNode(str(request.user.id)) id.appendChild(id_text) customer.appendChild(id) email = doc.createElement('email') email_text = doc.createTextNode(str(emailaddr)) email.appendChild(email_text) customer.appendChild(email) billto = doc.createElement('billTo') transactionrequest.appendChild(billto) firstname = doc.createElement('firstName') firstname_text = doc.createTextNode(str(fname)) firstname.appendChild(firstname_text) billto.appendChild(firstname) lastname = doc.createElement('lastName') lastnmae_text = doc.createTextNode(str(lname)) lastname.appendChild(lastnmae_text) billto.appendChild(lastname) address = doc.createElement('address') address_text = doc.createTextNode(baddress) address.appendChild(address_text) billto.appendChild(address) city = doc.createElement('city') city_text = doc.createTextNode(str(bcity)) city.appendChild(city_text) billto.appendChild(city) state = doc.createElement('state') state_text = doc.createTextNode(str(bstate)) state.appendChild(state_text) billto.appendChild(state) zip = doc.createElement('zip') zip_text = doc.createTextNode(str(bzip)) zip.appendChild(zip_text) billto.appendChild(zip) country = doc.createElement('country') country_text = doc.createTextNode(str(bcountry)) country.appendChild(country_text) billto.appendChild(country) shipto = doc.createElement('shipTo') transactionrequest.appendChild(shipto) firstname = doc.createElement('firstName') firstname_text = doc.createTextNode(str(sfname)) firstname.appendChild(firstname_text) shipto.appendChild(firstname) lastname = doc.createElement('lastName') lastnmae_text = doc.createTextNode(str(slname)) lastname.appendChild(lastnmae_text) shipto.appendChild(lastname) address = doc.createElement('address') address_text = doc.createTextNode(saddress) address.appendChild(address_text) shipto.appendChild(address) city = doc.createElement('city') city_text = doc.createTextNode(str(scity)) city.appendChild(city_text) shipto.appendChild(city) state = doc.createElement('state') state_text = doc.createTextNode(str(sstate)) state.appendChild(state_text) shipto.appendChild(state) zip = doc.createElement('zip') zip_text = doc.createTextNode(str(szip)) zip.appendChild(zip_text) shipto.appendChild(zip) country = doc.createElement('country') country_text = doc.createTextNode(str(scountry)) country.appendChild(country_text) shipto.appendChild(country) return doc.toxml(encoding='utf-8')
def txt2xml(img_folder_path, converted_xml_path, csv_path): """ :param img_folder_path: 图片文件夹路径 :param converted_xml_path: 转化成的xml文件路径 :param csv_path: csv文件的路径 :param dict_path: 生成的所有出现的字的字典 :return: """ chinese = set() for img_path in os.listdir(img_folder_path): if (img_path.endswith('.png') or img_path.endswith('.jpg')) and img_path != '1.png': doc = Document() annotation = doc.createElement('annotation') doc.appendChild(annotation) folder = doc.createElement('folder') annotation.appendChild(folder) folder_content = doc.createTextNode(img_folder_path) folder.appendChild(folder_content) filename = doc.createElement('filename') annotation.appendChild(filename) filename_content = doc.createTextNode(img_path) filename.appendChild(filename_content) """ read img file """ img = cv2.imread(os.path.join(img_folder_path, img_path)) h, w, c = img.shape size = doc.createElement('size') annotation.appendChild(size) width = doc.createElement('width') size.appendChild(width) width_content = doc.createTextNode(str(w)) width.appendChild(width_content) height = doc.createElement('height') size.appendChild(height) height_content = doc.createTextNode(str(h)) height.appendChild(height_content) channel = doc.createElement('depth') size.appendChild(channel) channel_txt = doc.createTextNode(str(c)) channel.appendChild(channel_txt) df = pd.read_csv(csv_path) res = df[df.FileName == img_path] # print(res) for i in res.index: # print('i=',i) object_new = doc.createElement('object') annotation.appendChild(object_new) name = doc.createElement('name') object_new.appendChild(name) name_txt = doc.createTextNode(res.loc[i, 'text']) name.appendChild(name_txt) for c in res.loc[i, 'text']: chinese.add(c) bndbox = doc.createElement('bndbox') object_new.appendChild(bndbox) """ 因为rpn是基于xmin xmax等四个坐标来做的,所以不得不把坐标位置修改成四元组 所以这只能基于长方形训练,对于普通四边形不可行 """ x = np.array([ res.loc[i, 'x1'], res.loc[i, 'x2'], res.loc[i, 'x3'], res.loc[i, 'x4'] ]) y = np.array([ res.loc[i, 'y1'], res.loc[i, 'y2'], res.loc[i, 'y3'], res.loc[i, 'y4'] ]) xmin_int = x.min() xmax_int = x.max() ymin_int = y.min() ymax_int = y.max() xmin = doc.createElement('xmin') bndbox.appendChild(xmin) xmin_text = doc.createTextNode(str(xmin_int)) xmin.appendChild(xmin_text) ymin = doc.createElement('ymin') bndbox.appendChild(ymin) ymin_text = doc.createTextNode(str(ymin_int)) ymin.appendChild(ymin_text) xmax = doc.createElement('xmax') bndbox.appendChild(xmax) xmax_text = doc.createTextNode(str(xmax_int)) xmax.appendChild(xmax_text) ymax = doc.createElement('ymax') bndbox.appendChild(ymax) ymax_text = doc.createTextNode(str(ymax_int)) ymax.appendChild(ymax_text) xml_path = os.path.join(converted_xml_path, img_path.strip('.jpg') + '.xml') # temp=doc.toprettyxml(indent='\t',encoding='utf-8') # print(temp) # print('++++++++++++') with open(xml_path, 'w', encoding='utf-8') as f: doc.writexml(f, indent='\t', encoding='utf-8', newl='\n', addindent='\t') print(xml_path + ' done!') with open('data/chinese/chinese.txt', 'w') as f: f.write(','.join(list(chinese))) print('all done!')
def txt2xml_single_thread(img_path): img_folder_path = 'D:\python_projects\ChineseCalligraphyDetection\data\\train_img' csv_path = 'D:\python_projects\ChineseCalligraphyDetection\data\original_csv\concat_train.csv' converted_xml_path = 'D:\python_projects\ChineseCalligraphyDetection\data\\annotation' # global chinese if (img_path.endswith('.png') or img_path.endswith('.jpg')) and img_path != '1.png': doc = Document() annotation = doc.createElement('annotation') doc.appendChild(annotation) folder = doc.createElement('folder') annotation.appendChild(folder) folder_content = doc.createTextNode(img_folder_path) folder.appendChild(folder_content) filename = doc.createElement('filename') annotation.appendChild(filename) filename_content = doc.createTextNode(img_path) filename.appendChild(filename_content) """ read img file """ img = cv2.imread(os.path.join(img_folder_path, img_path)) h, w, c = img.shape size = doc.createElement('size') annotation.appendChild(size) width = doc.createElement('width') size.appendChild(width) width_content = doc.createTextNode(str(w)) width.appendChild(width_content) height = doc.createElement('height') size.appendChild(height) height_content = doc.createTextNode(str(h)) height.appendChild(height_content) channel = doc.createElement('depth') size.appendChild(channel) channel_txt = doc.createTextNode(str(c)) channel.appendChild(channel_txt) df = pd.read_csv(csv_path) res = df[df.FileName == img_path] # print(res) for i in res.index: # print('i=',i) object_new = doc.createElement('object') annotation.appendChild(object_new) # name = doc.createElement('name') # object_new.appendChild(name) # name_txt = doc.createTextNode(res.loc[i, 'text']) # name.appendChild(name_txt) # for c in res.loc[i, 'text']: # chinese.add(c) bndbox = doc.createElement('bndbox') object_new.appendChild(bndbox) """ 因为rpn是基于xmin xmax等四个坐标来做的,所以不得不把坐标位置修改成四元组 所以这只能基于长方形训练,对于普通四边形不可行 """ x = np.array([ res.loc[i, 'x1'], res.loc[i, 'x2'], res.loc[i, 'x3'], res.loc[i, 'x4'] ]) y = np.array([ res.loc[i, 'y1'], res.loc[i, 'y2'], res.loc[i, 'y3'], res.loc[i, 'y4'] ]) xmin_int = x.min() xmax_int = x.max() ymin_int = y.min() ymax_int = y.max() xmin = doc.createElement('xmin') bndbox.appendChild(xmin) xmin_text = doc.createTextNode(str(xmin_int)) xmin.appendChild(xmin_text) ymin = doc.createElement('ymin') bndbox.appendChild(ymin) ymin_text = doc.createTextNode(str(ymin_int)) ymin.appendChild(ymin_text) xmax = doc.createElement('xmax') bndbox.appendChild(xmax) xmax_text = doc.createTextNode(str(xmax_int)) xmax.appendChild(xmax_text) ymax = doc.createElement('ymax') bndbox.appendChild(ymax) ymax_text = doc.createTextNode(str(ymax_int)) ymax.appendChild(ymax_text) xml_path = os.path.join(converted_xml_path, img_path.strip('.jpg') + 'g.xml') with open(xml_path, 'w', encoding='utf-8') as f: doc.writexml(f, indent='\t', encoding='utf-8', newl='\n', addindent='\t') print(xml_path + ' done!')
# Specifying where the output should be written... whand = open('comedians.gexf', 'wb') # Opening the database with the information to be written... con = lite.connect('comedians.db') cur = con.cursor() # # Creating the top GEFX element within the XML... gexfObject = doc.createElement("gexf") gexfObject.setAttribute("xmlns", "http://www.gexf.net/1.2draft") gexfObject.setAttribute("version", "1.2") doc.appendChild(gexfObject) # Creating metadata tags for the GEXF file... metaObject = doc.createElement("meta") metaObject.setAttribute("lastmodifieddate", "2012-02-16") gexfObject.appendChild(metaObject) creator = doc.createElement("project") metaObject.appendChild(creator) myName = doc.createTextNode("Stand-Up Comedian Genome Project") creator.appendChild(myName) description = doc.createElement("description") metaObject.appendChild(description) title = doc.createTextNode(
def __params2xml__(self, prefix, xmlfile, stim_file, resp_file, rfsize, bins, n_iter): """Write estimation parameters to XML file """ # Create document doc = Document() # Root element base = doc.createElement('Configuration') doc.appendChild(base) # Spike parameters params = doc.createElement('ParametersGroup') params.setAttribute('name', 'Spike Parameters') base.appendChild(params) cs = [] cs.append(doc.createElement('StringListParameter')) cs[-1].setAttribute('name', 'spike files') cs[-1].setAttribute('value', resp_file) cs.append(doc.createElement('IntegerParameter')) cs[-1].setAttribute('name', 'number of parts') cs[-1].setAttribute('value', '4') cs.append(doc.createElement('IntegerParameter')) cs[-1].setAttribute('name', 'number of trials') cs[-1].setAttribute('value', '%d' % 0) for c in cs: params.appendChild(c) # Annealing parameters anneal = doc.createElement('ParametersGroup') anneal.setAttribute('name', 'Annealing Parameters') base.appendChild(anneal) ca = [] ca.append(doc.createElement('IntegerParameter')) ca[-1].setAttribute('name', 'max annealing iterations') ca[-1].setAttribute('value', "1") ca.append(doc.createElement('DoubleParameter')) ca[-1].setAttribute('name', 'start temperature') ca[-1].setAttribute('value', "1") ca.append(doc.createElement('DoubleParameter')) ca[-1].setAttribute('name', 'stop temperature') ca[-1].setAttribute('value', "1.0e-5") ca.append(doc.createElement('DoubleParameter')) ca[-1].setAttribute('name', 'down temperature factor') ca[-1].setAttribute('value', "0.95") ca.append(doc.createElement('DoubleParameter')) ca[-1].setAttribute('name', 'up temperature factor') ca[-1].setAttribute('value', "10") ca.append(doc.createElement('DoubleParameter')) ca[-1].setAttribute('name', 'function tolerance') ca[-1].setAttribute('value', "5.0e-5") ca.append(doc.createElement('IntegerParameter')) ca[-1].setAttribute('name', 'updateFactor') ca[-1].setAttribute('value', "100") for c in ca: anneal.appendChild(c) # Movie parameters movie = doc.createElement('ParametersGroup') movie.setAttribute('name', 'Movie Parameters') base.appendChild(movie) cm = [] cm.append(doc.createElement('StringListParameter')) cm[-1].setAttribute('name', 'movie files') cm[-1].setAttribute('value', stim_file) cm.append(doc.createElement('EnumeratorParameter')) cm[-1].setAttribute('name', 'data type') cm[-1].setAttribute('value', "2") cm[-1].setAttribute('values', "byte:1:double:2") cm.append(doc.createElement('IntegerParameter')) cm[-1].setAttribute('name', 'width') cm[-1].setAttribute('value', "%d" % rfsize[1]) cm.append(doc.createElement('IntegerParameter')) cm[-1].setAttribute('name', 'x offset') cm[-1].setAttribute('value', "1") cm.append(doc.createElement('IntegerParameter')) cm[-1].setAttribute('name', 'sta width') cm[-1].setAttribute('value', "%d" % rfsize[1]) cm.append(doc.createElement('IntegerParameter')) cm[-1].setAttribute('name', 'x downsample') cm[-1].setAttribute('value', "1") cm.append(doc.createElement('IntegerParameter')) cm[-1].setAttribute('name', 'height') cm[-1].setAttribute('value', "%d" % rfsize[0]) cm.append(doc.createElement('IntegerParameter')) cm[-1].setAttribute('name', 'y offset') cm[-1].setAttribute('value', "1") cm.append(doc.createElement('IntegerParameter')) cm[-1].setAttribute('name', 'sta height') cm[-1].setAttribute('value', "%d" % rfsize[0]) cm.append(doc.createElement('IntegerParameter')) cm[-1].setAttribute('name', 'y downsample') cm[-1].setAttribute('value', "1") cm.append(doc.createElement('IntegerParameter')) cm[-1].setAttribute('name', 'sta duration') cm[-1].setAttribute('value', "1") cm.append(doc.createElement('IntegerParameter')) cm[-1].setAttribute('name', 'skipped sta frames') cm[-1].setAttribute('value', "0") cm.append(doc.createElement('IntegerListParameter')) cm[-1].setAttribute('name', 'number of bins') cm[-1].setAttribute('value', "%d" % bins) cm.append(doc.createElement('IntegerListParameter')) cm[-1].setAttribute('name', 'number of iterations') cm[-1].setAttribute('value', "%d" % n_iter) for c in cm: movie.appendChild(c) # Output parameters output = doc.createElement('ParametersGroup') output.setAttribute('name', 'Output Parameters') base.appendChild(output) co = doc.createElement('StringParameter') co.setAttribute('name', 'prefix') co.setAttribute('value', prefix) output.appendChild(co) # Finally, write document to formatted xml file with open(xmlfile, 'w') as f: doc.writexml(f, indent="", addindent=" ", newl="\n", encoding='UTF-8')
def writexml(filename, saveimg, bboxes, xmlpath): 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('muke')) owner.appendChild(flickrid_o) name_o = doc.createElement('name') name_o.appendChild(doc.createTextNode('muke')) 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('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') #+1 是因为faster rcnn坐标为0,0时 xmin.appendChild(doc.createTextNode(str(bbox[0] + 1))) bndbox.appendChild(xmin) ymin = doc.createElement('ymin') ymin.appendChild(doc.createTextNode(str(bbox[1] + 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()
def generate(self, pretty=False): logging.debug('AtomResponse.generate is called.') document = Document() feed = document.createElement('feed') for namespace in list(self.namespaces.keys()): namespaceAttr = 'xmlns' if namespace != '': namespaceAttr += ':' + namespace feed.setAttribute(namespaceAttr, self.namespaces[namespace]) document.appendChild(feed) title = document.createElement('title') feed.appendChild(title) title.appendChild( document.createTextNode(xml.sax.saxutils.escape(self.title))) ''' link = document.createElement('link') feed.appendChild(link) link.appendChild(document.createTextNode(xml.sax.saxutils.escape(self.link))) ''' updated = document.createElement('updated') feed.appendChild(updated) updated.appendChild( document.createTextNode(xml.sax.saxutils.escape(self.updated))) id = document.createElement('id') feed.appendChild(id) id.appendChild( document.createTextNode(xml.sax.saxutils.escape(self.id))) author = document.createElement('author') feed.appendChild(author) for authorName in self.authors: authorElement = document.createElement('name') author.appendChild(authorElement) authorElement.appendChild( document.createTextNode(xml.sax.saxutils.escape(authorName))) for variable in self.variables: ''' elementName = variable['name'] if 'namespace' in variable: elementName = variable['namespace']+':'+elementName variableElement = document.createElement(elementName) feed.appendChild(variableElement) variableElement.appendChild(document.createTextNode(xml.sax.saxutils.escape(str(variable['value'])))) ''' self._createNode(document, variable, feed) for item in self.items: itemElement = document.createElement('entry') feed.appendChild(itemElement) for itemEntry in item: self._createNode(document, itemEntry, itemElement) ''' elementName = itemEntry['name'] if 'namespace' in itemEntry: elementName = itemEntry['namespace']+':'+elementName variableElement = document.createElement(elementName) itemElement.appendChild(variableElement) if 'value' in itemEntry: value = itemEntry['value'] if isinstance(value, list): if len(value) > 1: for valueEntry in value: valueName = 'value' if 'namespace' in itemEntry: valueName = itemEntry['namespace']+':'+valueName valueElement = document.createElement(valueName) variableElement.appendChild(valueElement) valueElement.appendChild(document.createTextNode(xml.sax.saxutils.escape(str(valueEntry)))) else: variableElement.appendChild(document.createTextNode(xml.sax.saxutils.escape(str(value[0])))) elif isinstance(value, dict): for key in value.keys(): valueName = key if 'namespace' in itemEntry: valueName = itemEntry['namespace']+':'+valueName valueElement = document.createElement(valueName) variableElement.appendChild(valueElement) valueElement.appendChild(document.createTextNode(xml.sax.saxutils.escape(str(value[key])))) else: variableElement.appendChild(document.createTextNode(xml.sax.saxutils.escape(str(value)))) else: if 'attribute' in itemEntry: for attr in itemEntry['attribute'].keys(): variableElement.setAttribute(attr, itemEntry['attribute'][attr]) ''' return document.toprettyxml() if pretty else document.toxml('utf-8')
def Serialize(self): if not self._message: raise ICException( "iC message not sent, cannot serialize", '%s::%s' % (self.__class__.__name__, current_function())) # Create an XML document object doc = Document() # Root node cacheFidx = '%d' % self._message.GetFidx() root = doc.createElement(ICMESSAGE_ROOTNODE) root.setAttribute(ICMESSAGE_VERSIONNODE, ICMESSAGE_VERSION) root.setAttribute(ICMESSAGE_FRAMENODE, cacheFidx) doc.appendChild(root) # Classifiers for cname in self._message.classifiers.map: classifier = self._message.classifiers.Get(cname) cnode = doc.createElement(ICMESSAGE_CLASSISIFERNODE) cnode.setAttribute(ICMESSAGE_NAMENODE, classifier.GetChName()) cnode.setAttribute(ICMESSAGE_DESCNODE, classifier.GetChDescription()) root.appendChild(cnode) # Attribute: Value Type vtype = classifier.GetValueType() pvtype = ICTYPES_ENTRY_UNDEF if vtype == ICClassifier.ValueUndef: pvtype = ICTYPES_ENTRY_UNDEF elif vtype == ICClassifier.ValueProb: pvtype = ICTYPES_ENTRY_PROB elif vtype == ICClassifier.ValueDist: pvtype = ICTYPES_ENTRY_DIST elif vtype == ICClassifier.ValueCLbl: pvtype = ICTYPES_ENTRY_CLBL elif vtype == ICClassifier.ValueRCoe: pvtype = ICTYPES_ENTRY_RCOE cnode.setAttribute(ICMESSAGE_VTYPENODE, pvtype) # Attribute: Label Type ltype = classifier.GetLabelType() pltype = ICTYPES_LABEL_UNDEF if ltype == ICClassifier.LabelClass: pltype = ICTYPES_LABEL_CLASS elif ltype == ICClassifier.LabelUndef: pltype = ICTYPES_LABEL_UNDEF elif ltype == ICClassifier.LabelBiosig: pltype = ICTYPES_LABEL_BIOSIG elif ltype == ICClassifier.LabelCustom: pltype = ICTYPES_LABEL_CUSTOM cnode.setAttribute(ICMESSAGE_LTYPENODE, pltype) # Loop over classes for theclassname in classifier.classes.map: theclass = classifier.classes.Get(theclassname) knode = doc.createElement(ICMESSAGE_CLASSNODE) textnode = doc.createTextNode(theclass.GetChValue()) knode.appendChild(textnode) knode.setAttribute(ICMESSAGE_LABELNODE, theclass.GetChLabel()) cnode.appendChild(knode) return doc.toxml()
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.planet-lab.org/resources/vt_manager.communication.sfa.credential.xsd") signed_cred.setAttribute("xsi:schemaLocation", "http://www.planet-lab.org/resources/vt_manager.communication.sfa.ext/policy/1 http://www.planet-lab.org/resources/vt_manager.communication.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", "privilege") append_sub(doc, cred, "serial", "8") append_sub(doc, cred, "owner_gid", self.gidCaller.save_to_string()) append_sub(doc, cred, "owner_urn", self.gidCaller.get_urn()) append_sub(doc, cred, "target_gid", self.gidObject.save_to_string()) append_sub(doc, cred, "target_urn", self.gidObject.get_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) append_sub(doc, cred, "expires", self.expiration.isoformat()) privileges = doc.createElement("privileges") cred.appendChild(privileges) if self.privileges: rights = self.get_privileges() for right in rights.rights: priv = doc.createElement("privilege") append_sub(doc, priv, "name", right.kind) append_sub(doc, priv, "can_delegate", str(right.delegate).lower()) privileges.appendChild(priv) # Add the parent credential if it exists if self.parent: sdoc = parseString(self.parent.get_xml()) # If the root node is a signed-credential (it should be), then # get all its attributes and attach those to our signed_cred # node. # Specifically, PG and PLadd attributes for namespaces (which is reasonable), # and we need to include those again here or else their signature # no longer matches on the credential. # We expect three of these, but here we copy them all: # signed_cred.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") # and from PG (PL is equivalent, as shown above): # 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") # HOWEVER! # PL now also declares these, with different URLs, so # the code notices those attributes already existed with # different values, and complains. # This happens regularly on delegation now that PG and # PL both declare the namespace with different URLs. # If the content ever differs this is a problem, # but for now it works - different URLs (values in the attributes) # but the same actual schema, so using the PG schema # on delegated-to-PL credentials works fine. # Note: you could also not copy attributes # which already exist. It appears that both PG and PL # will actually validate a slicecred with a parent # signed using PG namespaces and a child signed with PL # namespaces over the whole thing. But I don't know # if that is a bug in xmlsec1, an accident since # the contents of the schemas are the same, # or something else, but it seems odd. And this works. parentRoot = sdoc.documentElement if parentRoot.tagName == "signed-credential" and parentRoot.hasAttributes(): for attrIx in range(0, parentRoot.attributes.length): attr = parentRoot.attributes.item(attrIx) # returns the old attribute of same name that was # on the credential # Below throws InUse exception if we forgot to clone the attribute first oldAttr = signed_cred.setAttributeNode(attr.cloneNode(True)) if oldAttr and oldAttr.value != attr.value: msg = "Delegating cred from owner %s to %s over %s replaced attribute %s value '%s' with '%s'" % (self.parent.gidCaller.get_urn(), self.gidCaller.get_urn(), self.gidObject.get_urn(), oldAttr.name, oldAttr.value, attr.value) #raise CredentialNotVerifiable("Can't encode new valid delegated credential: %s" % msg) p_cred = doc.importNode(sdoc.getElementsByTagName("credential")[0], True) p = doc.createElement("parent") p.appendChild(p_cred) cred.appendChild(p) # done handling parent credential # Create the <signatures> tag signatures = doc.createElement("signatures") signed_cred.appendChild(signatures) # Add any parent signatures if self.parent: for cur_cred in self.get_credential_list()[1:]: sdoc = parseString(cur_cred.get_signature().get_xml()) ele = doc.importNode(sdoc.getElementsByTagName("Signature")[0], True) signatures.appendChild(ele) # Get the finished product self.xml = doc.toxml()
raise Exception("The inference mode requre background images") # env = UnityEnvironment(file_name=None) env = UnityEnvironment(file_name=env_name) # is None if you use Unity Editor # Set the default brain to work with default_brain = env.brain_names[0] brain = env.brains[default_brain] distance_bias = 12.11 print("Begin generation") doc = Document() TrainingImages = doc.createElement('TrainingImages') TrainingImages.setAttribute("Version", "1.0") doc.appendChild(TrainingImages) Items = doc.createElement('Items') Items.setAttribute("number", "-") TrainingImages.appendChild(Items) def get_save_images_by_attributes(attribute_list, control_list, cam_id, dataset_size, output_dir): if not os.path.isdir(output_dir): os.mkdir(output_dir) z = 0 cnt = 0 angle = np.random.permutation( ancestral_sampler_fix_sigma(mu=attribute_list[:6], size=dataset_size * 3)) temp_intensity_list = np.random.normal(loc=attribute_list[6],
class finder(): def __init__(self): self.result = 0 self.tcIDList = [] self.xmlName = '' # xml 'testcase name' self.feature = "" self.subarea = "" self.directory = '' # py def reader 'path' self.file = '' # file name in full path self.classN = "" # xml 'class', test case - class name self.method = "" # test case def name self.doc = Document() self.testset = self.doc.createElement("testset") self.doc.appendChild(self.testset) def find(self, xls): self.xmlReader(xls) IdList = self.tcIDList # IdList = self.xmlReader(xls) for what in IdList: self.result = 0 iter = os.walk(os.getcwd()) for path, folder, file in iter: if len(file) != 0: for oneFile in file: self.fileReader(what, oneFile) self.directory = path if not self.result: print("sorry,can\'t find '%s' in '%s'" % (what, self.directory)) print("\n XML generated: \n" + self.doc.toprettyxml(indent=" ")) f = open('testset.xml', 'w') f.write(self.doc.toprettyxml(indent='')) f.close() def xmlReader(self, xls): app = Dispatch("Excel.Application") app.Visible = 0 doc = app.Workbooks.Open(xls) sheet = doc.Sheets(1) content = 1 # There's content in the first line i = 1 while content: if (sheet.Cells( i, 5).Value): # colum 5 is the value of tcID colum 'E' i = i + 1 self.tcIDList.append(sheet.Cells(i, 5).Value) else: content = 0 app.Quit() def fileReader(self, what, oneFile): if '.py' and not '.pyc' and not '.xls' and not '.testset' in oneFile: if oneFile != 'findTxt.py': #stupid way to ignore this file itself fReader = open(oneFile, 'rb') fString = fReader.read() if what in fString: self.searchLogCreater(what, oneFile) self.result = 1 fReader.close def searchLogCreater(self, what, oneFile): print("Find '%s'" % what + ' in: ' + self.directory + '\\' + oneFile) #full path need return in future self.xmlName = what self.file = oneFile.split(".")[0] patternFeature = re.compile( r'subarea\s*=\s*(.+)') #something wrong with re pattern match = patternFeature.match(oneFile) if match: self.teature = match.group().split('=')[1] # print "self.file = %s \n\ # self.xmlName = %s \n\ # self.directory = %s \n\ # self.feature = %s \n\ # \n"%(self.file,self.xmlName,self.directory,self.feature) #need to transfer to xml in future self.xmlTcCreater(self.file, self.xmlName, self.directory, self.feature) def xmlTcCreater(self, file, xmlName, directory, feature): testcase = self.doc.createElement("testcase") testcase.setAttribute("directory", directory) testcase.setAttribute("file", file) testcase.setAttribute("name", xmlName) self.testset.appendChild(testcase)
row, attr_title['Name'])] = attribute_sheet.cell_value( row, attr_title['Scale']) offset[attribute_sheet.cell_value( row, attr_title['Name'])] = attribute_sheet.cell_value( row, attr_title['Offset']) data_type[attribute_sheet.cell_value( row, attr_title['Name'])] = attribute_sheet.cell_value( row, attr_title['DataType']) # create document doc = Document() # create root element CutChart = doc.createElement('CutChart') CutChart.setAttribute('Vendor', vendor_name) CutChart.setAttribute('Version', cut_chart_version) doc.appendChild(CutChart) # create system config information for row in range(config_start_row, config_sheet.nrows): CutChartConfig = doc.createElement('CutChartConfig') CutChart.appendChild(CutChartConfig) for col in range(0, len(config_define[vendor_name])): CutChartConfig.setAttribute( config_define[vendor_name][col], "%s" % config_sheet.cell_value(row, col + 1)) # create current select element and attr element CurrentSelect = doc.createElement('CurrentSelectCutChartRecord') CutChartAttr = doc.createElement('CutChartAttr') CutChart.appendChild(CurrentSelect) CutChart.appendChild(CutChartAttr)
def generate_xml(name,split_lines,img_size,class_ind): doc = Document() # 创建DOM文档对象 annotation = doc.createElement('annotation') doc.appendChild(annotation) title = doc.createElement('folder') title_text = doc.createTextNode('KITTI') title.appendChild(title_text) annotation.appendChild(title) img_name=name+'.png' title = doc.createElement('filename') title_text = doc.createTextNode(img_name) title.appendChild(title_text) annotation.appendChild(title) source = doc.createElement('source') annotation.appendChild(source) title = doc.createElement('database') title_text = doc.createTextNode('The KITTI Database') title.appendChild(title_text) source.appendChild(title) title = doc.createElement('annotation') title_text = doc.createTextNode('KITTI') title.appendChild(title_text) source.appendChild(title) size = doc.createElement('size') annotation.appendChild(size) title = doc.createElement('width') title_text = doc.createTextNode(str(img_size[1])) title.appendChild(title_text) size.appendChild(title) title = doc.createElement('height') title_text = doc.createTextNode(str(img_size[0])) title.appendChild(title_text) size.appendChild(title) title = doc.createElement('depth') title_text = doc.createTextNode(str(img_size[2])) title.appendChild(title_text) size.appendChild(title) for split_line in split_lines: line=split_line.strip().split() if line[0] in class_ind: object = doc.createElement('object') annotation.appendChild(object) title = doc.createElement('name') title_text = doc.createTextNode(line[0]) title.appendChild(title_text) object.appendChild(title) bndbox = doc.createElement('bndbox') object.appendChild(bndbox) title = doc.createElement('xmin') title_text = doc.createTextNode(str(int(float(line[4])))) title.appendChild(title_text) bndbox.appendChild(title) title = doc.createElement('ymin') title_text = doc.createTextNode(str(int(float(line[5])))) title.appendChild(title_text) bndbox.appendChild(title) title = doc.createElement('xmax') title_text = doc.createTextNode(str(int(float(line[6])))) title.appendChild(title_text) bndbox.appendChild(title) title = doc.createElement('ymax') title_text = doc.createTextNode(str(int(float(line[7])))) title.appendChild(title_text) bndbox.appendChild(title) # 将DOM对象doc写入文件 f = open('Annotations/'+name+'.xml','w') f.write(doc.toprettyxml(indent = '')) f.close()
def save(self, filepath, pack_textures, exclude_materials=None): doc = Document() root = doc.createElement("tmesh") root.setAttribute("version", "0.4") doc.appendChild(root) path = os.path.dirname(filepath) current_vertex_id = 0 data_str_list = [] for material in self.materials.values(): if exclude_materials is not None: if material.name in exclude_materials: continue node = export_material.create_node( doc, material, path, os.path.basename(filepath) + "_tex", pack_textures) root.appendChild(node) for mesh in self.meshes.values(): if mesh.towerengine.vertices_only: for vertex in mesh.vertices: self.__append_vertex(data_str_list, vertex, (0.0, 0.0), vertex.normal) current_vertex_id += 1 else: face_vertex_id = dict() vertex_id = dict() #vertex_nodes = dict() vertex_uv = dict() vertex_normal = dict() mesh.update(False, True) if mesh.tessface_uv_textures.active is not None: uv_layer = mesh.tessface_uv_textures.active.data else: uv_layer = None for face in mesh.tessfaces: # Create Vertex Nodes from Face Vertices face_vertex_id[face] = dict() for i in range(len(face.vertices)): # All Vertices of Face vertex = face.vertices[i] vertex_found = False if vertex not in vertex_id: vertex_id[vertex] = list() #vertex_nodes[vertex] = list() vertex_uv[vertex] = list() vertex_normal[vertex] = list() if face.use_smooth: normal = mesh.vertices[vertex].normal else: normal = face.normal if uv_layer is not None: uv = uv_layer[face.index].uv[i] else: uv = [0.0, 0.0] for j in range(len(vertex_id[vertex])): if vertex_uv[vertex][j][0] == uv[0] and vertex_uv[ vertex][j][1] == uv[1] and vertex_normal[ vertex][j] == normal: # might be wrong face_vertex_id[face][i] = vertex_id[vertex][j] vertex_found = True break if vertex_found: continue vertex_id[vertex].append(current_vertex_id) #vertex_nodes[vertex].append(node) vertex_uv[vertex].append(uv) vertex_normal[vertex].append(normal) face_vertex_id[face][i] = current_vertex_id current_vertex_id += 1 vertex = mesh.vertices[vertex] self.__append_vertex(data_str_list, vertex, uv, normal) ''' node = doc.createElement("vertex") node.setAttribute("id", str(current_vertex_id)) node.setAttribute("x", str(vertex.co.x)) node.setAttribute("y", str(vertex.co.z)) node.setAttribute("z", str(-vertex.co.y)) node.setAttribute("u", str(uv[0])) node.setAttribute("v", str(uv[1])) node.setAttribute("nx", str(normal.x)) node.setAttribute("ny", str(normal.z)) node.setAttribute("nz", str(-normal.y)) root.appendChild(node) ''' for face in mesh.tessfaces: if len(face.vertices) >= 3: data_str_list.append("t ") for i in range(3): data_str_list.append(str(face_vertex_id[face][i])) data_str_list.append(" ") if len(mesh.materials) != 0: if mesh.materials[face.material_index] is not None: data_str_list.append( mesh.materials[face.material_index].name) data_str_list.append("\n") ''' node = doc.createElement("triangle") node.setAttribute("mat", "$NONE") if len(mesh.materials) != 0: if mesh.materials[face.material_index] != None: node.setAttribute("mat", mesh.materials[face.material_index].name) for i in range(3): node2 = doc.createElement("vertex"); node2.setAttribute("id", str(face_vertex_id[face][i])); node.appendChild(node2) root.appendChild(node) ''' if len(face.vertices) >= 4: data_str_list.append("t ") for i in [2, 3, 0]: data_str_list.append(str(face_vertex_id[face][i])) data_str_list.append(" ") if len(mesh.materials) != 0: if mesh.materials[face.material_index] is not None: data_str_list.append( mesh.materials[face.material_index].name) data_str_list.append("\n") ''' node = doc.createElement("triangle") node.setAttribute("mat", "$NONE") if len(mesh.materials) != 0: if mesh.materials[face.material_index] != None: node.setAttribute("mat", mesh.materials[face.material_index].name) for i in [2, 3, 0]: node2 = doc.createElement("vertex"); node2.setAttribute("id", str(face_vertex_id[face][i])); node.appendChild(node2) root.appendChild(node) ''' node = doc.createElement("mesh_data") node.appendChild(doc.createTextNode(''.join(data_str_list))) root.appendChild(node) f = open(filepath, 'w') f.write(doc.toprettyxml(indent=" ")) f.close()
def create_visual_xacro(): global robot """ Creates a <ROBOT>_visual_collision.xacro file with xacro macros for visual and collision tags of the urdf. This function creates xacro macros for visualisation and collisions. It checks if the meshes are on the computer(nao only) and set the 'meshes_installed' property accordingly """ global OUTPUT global MESH_VERSION global NAME global LINKS_DICO global VISU_DICO global OFFSETS_DICO global MESHPKG prefix = "xacro:insert_visu_" doc = Document() root = doc.createElement("robot") doc.appendChild(root) root.setAttribute("xmlns:xacro","http://www.ros.org/wiki/xacro") cmd = "rospack find "+ NAME + MESHPKG try: path_mesh_pkg = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)[:-1] except: print("unable to find "+ NAME + MESHPKG + " package") sys.exit(0) # Set Mesh path if NAME =='nao': node = ur.short(doc,'xacro:property','name','PI_2') node.setAttribute('value',str(math.pi/2.0)) root.appendChild(node) node = ur.short(doc,'xacro:property','name','meshes_installed') if os.path.isdir(os.path.join(path_mesh_pkg,'meshes',MESH_VERSION)): node.setAttribute('value',"true") else: node.setAttribute('value',"false") root.appendChild(node) #Insert xacro macro for link in robot.links: (tempVisu,tempCol) = adjustMeshPath(path_mesh_pkg,link) if robot.links[link].visual != None: robot.links[link].xacro = prefix + robot.links[link].name node = ur.short(doc,'xacro:macro','name',robot.links[link].xacro) if NAME=='nao': # add xacro condition macro to handle the absence of meshes on the system node2 = ur.short(doc,'xacro:unless','value',"${meshes_installed}") if tempVisu != None: node2.appendChild(tempVisu.to_xml(doc)) if tempCol != None: node2.appendChild(tempCol.to_xml(doc)) node.appendChild(node2) node3 = ur.short(doc,'xacro:if','value',"${meshes_installed}") node3.appendChild(robot.links[link].visual.to_xml(doc)) node3.appendChild(robot.links[link].collision.to_xml(doc)) node.appendChild(node3) else: node.appendChild(robot.links[link].visual.to_xml(doc)) node.appendChild(robot.links[link].collision.to_xml(doc)) root.appendChild(node) robot.links[link].visual = None robot.links[link].collision = None filename = OUTPUT[0:OUTPUT.find('.')] + '_visual_collisions.xacro' write_comments_in_xacro(doc, filename)
def CCPD_to_VOC(fileitem): # image info # img = cv2.imread(os.path.join(root_dir, fileitem)) # height, width, depth = img.shape height = 1160 width = 720 depth = 3 # check image size if height != 1160 or width != 720: print("wrong size") print(fileitem) # four corners four_corners = fileitem.split('-')[3] if len(four_corners.split('_')) != 4: print("wrong four corners") print(fileitem) carplate_x_bottom_right = int(four_corners.split('_')[0].split('&')[0]) + 1 carplate_y_bottom_right = int(four_corners.split('_')[0].split('&')[1]) + 1 carplate_x_bottom_left = int(four_corners.split('_')[1].split('&')[0]) + 1 carplate_y_bottom_left = int(four_corners.split('_')[1].split('&')[1]) + 1 carplate_x_top_left = int(four_corners.split('_')[2].split('&')[0]) + 1 carplate_y_top_left = int(four_corners.split('_')[2].split('&')[1]) + 1 carplate_x_top_right = int(four_corners.split('_')[3].split('&')[0]) + 1 carplate_y_top_right = int(four_corners.split('_')[3].split('&')[1]) + 1 # 将车牌四点顺序改为标准的左上右上右下左下 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) # bbox 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) # 官方自带的bbox,视觉效果很不准,还是通过四点得到bbox # # bbox # bbox = fileitem.split('-')[2] # if len(bbox.split('_')) != 2: # print("wrong bbox") # print(fileitem) # carplate_xmin = int(bbox.split('_')[0].split('&')[0]) # carplate_ymin = int(bbox.split('_')[0].split('&')[1]) # carplate_xmax = int(bbox.split('_')[1].split('&')[0]) # carplate_ymax = int(bbox.split('_')[1].split('&')[1]) # # 限制不超过图片上下界 # carplate_xmin = ex.limit_in_bounds(carplate_xmin, 1, width) # carplate_ymin = ex.limit_in_bounds(carplate_ymin, 1, height) # carplate_xmax = ex.limit_in_bounds(carplate_xmax, 1, width) # carplate_ymax = ex.limit_in_bounds(carplate_ymax, 1, height) # 创建dom文档 doc = Document() # 创建根节点 annotation = doc.createElement('annotation') # 根节点插入dom树 doc.appendChild(annotation) # folder folder = doc.createElement('folder') annotation.appendChild(folder) folder.appendChild(doc.createTextNode('carplate')) # filename filename = doc.createElement('filename') annotation.appendChild(filename) filename.appendChild(doc.createTextNode(str(fileitem))) # source source = doc.createElement('source') annotation.appendChild(source) database_ = doc.createElement('database') database_.appendChild(doc.createTextNode('carplate')) 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))) # carplate object = doc.createElement('object') annotation.appendChild(object) name = doc.createElement('name') name.appendChild(doc.createTextNode("carplate")) 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(0))) object.appendChild(occluded_) # truncated truncated_ = doc.createElement('truncated') truncated_.appendChild(doc.createTextNode(str(0))) object.appendChild(truncated_) # difficult difficult_ = doc.createElement('difficult') difficult_.appendChild(doc.createTextNode(str(0))) object.appendChild(difficult_) # the bndbox bndbox = doc.createElement('bndbox') object.appendChild(bndbox) xmin_ = doc.createElement('xmin') xmin_.appendChild(doc.createTextNode(str(carplate_xmin))) bndbox.appendChild(xmin_) ymin_ = doc.createElement('ymin') ymin_.appendChild(doc.createTextNode(str(carplate_ymin))) bndbox.appendChild(ymin_) xmax_ = doc.createElement('xmax') xmax_.appendChild(doc.createTextNode(str(carplate_xmax))) bndbox.appendChild(xmax_) ymax_ = doc.createElement('ymax') ymax_.appendChild(doc.createTextNode(str(carplate_ymax))) bndbox.appendChild(ymax_) x_top_left_ = doc.createElement('x_top_left') x_top_left_.appendChild(doc.createTextNode(str(carplate_x_top_left))) bndbox.appendChild(x_top_left_) y_top_left_ = doc.createElement('y_top_left') y_top_left_.appendChild(doc.createTextNode(str(carplate_y_top_left))) bndbox.appendChild(y_top_left_) x_top_right_ = doc.createElement('x_top_right') x_top_right_.appendChild(doc.createTextNode(str(carplate_x_top_right))) bndbox.appendChild(x_top_right_) y_top_right_ = doc.createElement('y_top_right') y_top_right_.appendChild(doc.createTextNode(str(carplate_y_top_right))) bndbox.appendChild(y_top_right_) x_bottom_right_ = doc.createElement('x_bottom_right') x_bottom_right_.appendChild( doc.createTextNode(str(carplate_x_bottom_right))) bndbox.appendChild(x_bottom_right_) y_bottom_right_ = doc.createElement('y_bottom_right') y_bottom_right_.appendChild( doc.createTextNode(str(carplate_y_bottom_right))) bndbox.appendChild(y_bottom_right_) x_bottom_left_ = doc.createElement('x_bottom_left') x_bottom_left_.appendChild(doc.createTextNode(str(carplate_x_bottom_left))) bndbox.appendChild(x_bottom_left_) y_bottom_left_ = doc.createElement('y_bottom_left') y_bottom_left_.appendChild(doc.createTextNode(str(carplate_y_bottom_left))) bndbox.appendChild(y_bottom_left_) # save xml fp = open(os.path.join(target_dir, fileitem[:-4] + ".xml"), 'w') doc.writexml(fp, indent='\t', addindent='\t', newl='\n', encoding="utf-8")
def createaimlfile(currentdb): # Create the minidom document doc = Document() # Create the aiml base element aiml = doc.createElement("aiml") doc.appendChild(aiml) currenthost = "" currentdb.sort_host() for show in currentdb.show_list: if show.host not in currenthost: strguests = "" currenthost = show.host for guest in show.musicalguest: if len(strguests) == 0: strguests = guest + " on show number " + str( show.epnumber).upper() else: strguests += ", " + guest + " on show number " + str( show.epnumber).upper() for ep in hostEPquestions: strqu = ep.replace('<ep>', str(show.epnumber).upper()) # Create category element maincard = doc.createElement("category") aiml.appendChild(maincard) # pattern pattern = doc.createElement("pattern") maincard.appendChild(pattern) # pattern text ptext = doc.createTextNode(strqu) pattern.appendChild(ptext) # template template = doc.createElement("template") maincard.appendChild(template) # template text strhost = show.host if "(none)" in strhost: strhost = "No one" ptext = doc.createTextNode(strhost) template.appendChild(ptext) for mg in hostMSquestions: strhost = str(show.host).upper() if "(none)" in strhost: strhost = "No one" strqu = mg.replace('<host>', strhost) # Create category element maincard = doc.createElement("category") aiml.appendChild(maincard) # pattern pattern = doc.createElement("pattern") maincard.appendChild(pattern) # pattern text ptext = doc.createTextNode(strqu) pattern.appendChild(ptext) # template template = doc.createElement("template") maincard.appendChild(template) # template text ptext = doc.createTextNode(strguests) template.appendChild(ptext) for da in hostdatequestions: strhost = str(show.host).upper() if "(none)" in strhost: strhost = "No one" strqu = da.replace('<host>', strhost) # Create category element maincard = doc.createElement("category") aiml.appendChild(maincard) # pattern pattern = doc.createElement("pattern") maincard.appendChild(pattern) # pattern text ptext = doc.createTextNode(strqu) pattern.appendChild(ptext) # template template = doc.createElement("template") maincard.appendChild(template) # template text stryear = str(show.airdate)[0:4] strmon = str(show.airdate)[4:6] strday = str(show.airdate)[6:8] strmon = structmonth[int(strmon) - 1] numlen = len(strmon) strmon = strmon[0:1].upper() + strmon[1:numlen] strairdate = "%s %s, %s" % (strmon, strday, stryear) ptext = doc.createTextNode(strairdate) template.appendChild(ptext) filename = 'C:\\Users\\Shaffer\\Desktop\\School\\A.I\\chatbot-ai\\trunk\\Chatbot\\aimlfiles\\snl.aiml' newfile = open(filename, 'w') newfile.write(doc.toprettyxml(indent=" ")) newfile.close()
def export_kinematic_chain_to_xacro(keyword,baseChain='base_link',tipRefChain='default'): """ This function allows to export a specific kinematic chain to a xacro file :param : keyword, string defining kinematic chains to export (legs,arms,head,torso) :param : baseChain, string representing the name of the link where the reference chain starts :param : tipRefChain, string representing the name of the link where the reference chain ends """ global robot, OUTPUT if tipRefChain == 'default': print("applying torso to end of ref chain") tipRefChain = XACRO_DICO['torso'] chainRef = robot.get_chain(baseChain,tipRefChain) print(chainRef) doc = Document() root = doc.createElement("robot") doc.appendChild(root) root.setAttribute("xmlns:xacro","http://www.ros.org/wiki/xacro") chainNb =0 try: chain1 = robot.get_chain(baseChain,'l_' + XACRO_DICO[keyword]) chain2 = robot.get_chain(baseChain,'r_' + XACRO_DICO[keyword]) chainNb=2 except KeyError: try: chain1 = robot.get_chain(baseChain,'L' + XACRO_DICO[keyword]) chain2 = robot.get_chain(baseChain,'R' + XACRO_DICO[keyword]) chainNb = 2 except KeyError: try: chain1 = robot.get_chain(baseChain, XACRO_DICO[keyword]) chainNb = 1 except KeyError: print("the chain " + keyword + " cannot be found") if chainNb != 0: duplicate = 0 for i in range(len(chain1)): for j in range(len(chainRef)): if chain1[i] == chainRef[j]: duplicate =1 if duplicate == 0 or keyword == 'torso': try: root.appendChild(robot.links[chain1[i]].to_xml(doc)) except KeyError: try: root.appendChild(robot.joints[chain1[i]].to_xml(doc)) except KeyError: print("unknown element" + chain1[i]) else: duplicate =0 if chainNb ==2: for i in range(len(chain2)): for j in range(len(chainRef)): if chain2[i] == chainRef[j]: duplicate =1 if duplicate == 0: try: root.appendChild(robot.links[chain2[i]].to_xml(doc)) except KeyError: try: root.appendChild(robot.joints[chain2[i]].to_xml(doc)) except KeyError: print("unknown element" + chain2[i]) else: duplicate =0 filename = OUTPUT[0:OUTPUT.find('.')] + '_' + keyword + str('.xacro') write_comments_in_xacro(doc, filename)
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 save_to_xml(save_path, im_name, im_width, im_height, im_depth, objects_axis, label_name, angles, folder_name='VOC self-defined'): object_num = len(objects_axis) doc = Document() annotation = doc.createElement('annotation') doc.appendChild(annotation) folder = doc.createElement('folder') folder_name = doc.createTextNode(folder_name) folder.appendChild(folder_name) annotation.appendChild(folder) filename = doc.createElement('filename') filename_name = doc.createTextNode(im_name) filename.appendChild(filename_name) annotation.appendChild(filename) source = doc.createElement('source') annotation.appendChild(source) database = doc.createElement('database') database.appendChild( doc.createTextNode('The custom Database with reference to VOC2007')) source.appendChild(database) annotation_s = doc.createElement('annotation') annotation_s.appendChild(doc.createTextNode('CUSTOM')) 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('yang')) 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)) objects.appendChild(object_name) pose = doc.createElement('pose') pose.appendChild(doc.createTextNode('Unspecified')) objects.appendChild(pose) # 为了训练回归的旋转角度,在xml中加入角度 angle = doc.createElement('angle') angle.appendChild(doc.createTextNode(angles[i])) objects.appendChild(angle) 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((objects_axis[i][0]))) bndbox.appendChild(xmin) ymin = doc.createElement('ymin') ymin.appendChild(doc.createTextNode((objects_axis[i][1]))) bndbox.appendChild(ymin) xmax = doc.createElement('xmax') xmax.appendChild(doc.createTextNode((objects_axis[i][2]))) bndbox.appendChild(xmax) ymax = doc.createElement('ymax') ymax.appendChild(doc.createTextNode((objects_axis[i][3]))) bndbox.appendChild(ymax) f = open(save_path, 'w') f.write(doc.toprettyxml(indent='\t')) f.close()
def createFeed(examples): doc = Document() atomuri = "http://www.w3.org/2005/Atom" feed = doc.createElementNS(atomuri, "feed") feed.setAttribute("xmlns", atomuri) title = doc.createElementNS(atomuri, "title") title.appendChild(doc.createTextNode("OpenLayers Examples")) feed.appendChild(title) link = doc.createElementNS(atomuri, "link") link.setAttribute("rel", "self") link.setAttribute("href", feedPath + feedName) modtime = time.strftime("%Y-%m-%dT%I:%M:%SZ", time.gmtime()) id = doc.createElementNS(atomuri, "id") id.appendChild( doc.createTextNode("%s%s#%s" % (feedPath, feedName, modtime))) feed.appendChild(id) updated = doc.createElementNS(atomuri, "updated") updated.appendChild(doc.createTextNode(modtime)) feed.appendChild(updated) examples.sort(key=lambda x: x["modified"]) for example in sorted(examples, key=lambda x: x["modified"], reverse=True): entry = doc.createElementNS(atomuri, "entry") title = doc.createElementNS(atomuri, "title") title.appendChild( doc.createTextNode(example["title"] or example["example"])) entry.appendChild(title) link = doc.createElementNS(atomuri, "link") link.setAttribute("href", "%s%s" % (feedPath, example["example"])) entry.appendChild(link) summary = doc.createElementNS(atomuri, "summary") summary.appendChild( doc.createTextNode(example["shortdesc"] or example["example"])) entry.appendChild(summary) updated = doc.createElementNS(atomuri, "updated") updated.appendChild(doc.createTextNode(example["modified"])) entry.appendChild(updated) author = doc.createElementNS(atomuri, "author") name = doc.createElementNS(atomuri, "name") name.appendChild(doc.createTextNode(example["author"])) author.appendChild(name) entry.appendChild(author) id = doc.createElementNS(atomuri, "id") id.appendChild( doc.createTextNode( "%s%s#%s" % (feedPath, example["example"], example["modified"]))) entry.appendChild(id) feed.appendChild(entry) doc.appendChild(feed) return doc
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 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.bool_to_str(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()
def main(): # Create a command-line argument parser parser = argparse.ArgumentParser(prog='ReaktoroParserSUPCRT') # Add the input argument parser.add_argument('input', type=str, \ help='the relative path to the SUPCRT database file') # Add the output argument parser.add_argument('output', type=str, \ help='the relative path of the output file') # Add the debug option (optional) parser.add_argument('-e', '--exclude', type=str, nargs='?', \ help='the relative path to a file containing the names of species \ to be excluded from the final database' ) # Parse the command-line arguments (remove the first argument, which is the name of this file args = parser.parse_args(sys.argv[1:]) # Extract the command-line arguments infile = args.input outfile = args.output # Create a list of species that should be excluded excluded = [] if args.exclude is not None: excluded = [line.strip() for line in open(args.exclude, 'r')] # Parse the SUPCRT92 database datalist = parseDatabase(infile) # Collect the aqueous, gaseous and mineral species data aqueous_datalist = [data for data in datalist if data.type == 'Aqueous'] gaseous_datalist = [data for data in datalist if data.type == 'Gaseous'] mineral_datalist = [data for data in datalist if data.type == 'Mineral'] if len(aqueous_datalist) + len(gaseous_datalist) + len( mineral_datalist) != len(datalist): raise RuntimeError( 'the number of aqueous, gaseous and mineral species does not match with the total number of species' ) # Remove from the excluded species from the lists above aqueous_datalist = [ x for x in aqueous_datalist if x.origname not in excluded ] gaseous_datalist = [ x for x in gaseous_datalist if x.origname not in excluded ] mineral_datalist = [ x for x in mineral_datalist if x.origname not in excluded ] # Create the minidom document doc = Document() # Create the <database> element db = doc.createElement("Database") doc.appendChild(db) # Create the xml elements for the chemical element for (name, value) in sorted(elements.items(), key=lambda x: x[1]): element_node = doc.createElement("Element") appendElement(doc, element_node, 'Name', name) appendElement(doc, element_node, 'MolarMass', str(value), ('units', 'g/mol')) db.appendChild(element_node) # Create the xml elements for the mineral species for data in mineral_datalist: writeMineralSpeciesXML(doc, db, data) # Create the xml elements for the gaseous species for data in gaseous_datalist: writeGaseousSpeciesXML(doc, db, data) # Create the xml elements for the aqueous species writeWaterSpeciesXML(doc, db) for data in aqueous_datalist: writeAqueousSpeciesXML(doc, db, data) # Output the database in XML format f = open(outfile, 'w') f.write(doc.toprettyxml(indent=" ")) print 'File', outfile, 'has been successfully written!'
class XMLExporter(object): '''EDSeg XML exporter''' ROOT_TAG = 'discourse' SDS_TAG = 'sds' EDS_TAG = 'eds' def __init__(self): self.root = None def on_start(self): '''Create the document root and initialize SDS and EDS counters. ''' self._doc = Document() self.root = self.make_root() self._doc.appendChild(self.root) self._sds_buff = [] self._sds_count = 0 self._eds_count = 0 def on_finish(self): '''Sort the SDS elements to reflect document order and add all SDS element to the root. ''' self._sds_buff.sort() for sds_no, sds in self._sds_buff: self.root.appendChild(sds) def on_sds(self, sent_no, sds): '''Create new SDS and EDS elements.''' sds_elem = self.make_sds(sds) for eds in sds: sds_elem.appendChild(self._traverse(eds)) self._sds_buff.append((sent_no, sds_elem)) def as_string(self, indent=' ', encoding='utf-8'): '''Return a string representation of the XML.''' return self._doc.toprettyxml(indent=indent, encoding=encoding) def write_to(self, path_or_file): '''Write the XML to a given file path or file handle.''' if hasattr(path_or_file, 'write'): fp = path_or_file else: fp = open(path_or_file, 'wb') fp.write(self.as_string()) if fp is not path_or_file: fp.close() return self def make_root(self): '''Create the document root (element). This method can be overwritten to customize the root element. ''' return self._doc.createElement(self.ROOT_TAG) def make_sds(self, sds): '''Create an SDS element. This method can be overwritten to customize SDS elements. ''' elem = self._doc.createElement(self.SDS_TAG) elem.setAttribute('id', 's{0}'.format(self.new_sds_id())) return elem def make_eds(self, eds): '''Create an EDS element. This method can be overwritten to customize EDS elements. ''' elem = self._doc.createElement(self.EDS_TAG) elem.setAttribute('id', 'e{0}'.format(self.new_eds_id())) elem.setAttribute('type', eds.get('type')) return elem def make_token(self, tok): '''Create a token element. This method can be overwritten to customize token elements. ''' return self._doc.createTextNode(tok['form']) def new_sds_id(self): '''Return the next free SDS ID.''' sds_id = self._sds_count self._sds_count += 1 return sds_id def new_eds_id(self): '''Return the next free EDS ID.''' eds_id = self._eds_count self._eds_count += 1 return eds_id def _traverse(self, node): if not hasattr(node, 'label'): return self.make_token(node) elem = self.make_eds(node) for child in node: elem.appendChild(self._traverse(child)) return elem
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", 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(): fdroid_signer = fdroid_signing_key_fingerprints.get( appid, {}).get('signer') fdroid_signed_apk = None name_match_apk = None for x in apksforver: if fdroid_signer and x.get('signer', None) == fdroid_signer: 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: _copy_to_local_copy_dir(repodir, os.path.join(repodir, jar_output)) # 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 "repo/icons/%s" does not exist, generating placeholder.' ) % repo_icon) os.makedirs(os.path.dirname(iconfilename), exist_ok=True) try: 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)