def req_session_create(self, request): environment = Platform.DomainUlteo() try: document = minidom.parseString(request["data"]) sessionNode = document.documentElement if sessionNode.nodeName != "session": raise Exception("invalid root node") if not sessionNode.hasAttribute("id"): raise Exception("invalid root node") if not sessionNode.hasAttribute("mode"): raise Exception("invalid root node") session = {} session["id"] = sessionNode.getAttribute("id") session["mode"] = sessionNode.getAttribute("mode") external_apps_token = None if sessionNode.hasAttribute("external_apps_token"): external_apps_token = sessionNode.getAttribute("external_apps_token") if len(session["id"])==0: raise Exception("Missing attribute id") if session["mode"] == "desktop": session["mode"] = Platform.Session.MODE_DESKTOP elif session["mode"] == "applications": session["mode"] = Platform.Session.MODE_APPLICATIONS else: raise Exception("Missing attribute id") nodes = sessionNode.getElementsByTagName("environment") if len(nodes)>0: environmentNode = nodes[0] name = environmentNode.getAttribute("id") if name == "Microsoft": environment = Platform.DomainMicrosoft() elif name == "Novell": environment = Platform.DomainNovell() else: raise Exception("unknown environment '%s'"%(name)) ret = environment.parse(environmentNode) if ret is False: raise Exception("invalid environment schema") userNode = sessionNode.getElementsByTagName("user")[0] for attr in ["login", "password", "displayName"]: if not userNode.hasAttribute(attr): raise Exception("invalid child node: missing attribute "+attr) session[attr] = userNode.getAttribute(attr) applications = {} self.role_instance.applications_mutex.acquire() applicationNodes = sessionNode.getElementsByTagName("application") for node in applicationNodes: if node.parentNode != sessionNode: continue app_id = node.getAttribute("id") if self.role_instance.applications_id_SM.has_key(app_id): applications[app_id] = self.role_instance.applications_id_SM[app_id] elif self.role_instance.static_apps.applications.has_key(app_id): applications[app_id] = self.role_instance.static_apps.applications[app_id] else: self.role_instance.applications_mutex.release() Logger.warn("Unknown application id %s"%(app_id)) raise Exception("Unknown application id %s"%(app_id)) self.role_instance.applications_mutex.release() application_to_start = [] startNodes = sessionNode.getElementsByTagName("start") if len(startNodes)>0: startNodes = startNodes[0] applicationNodes = startNodes.getElementsByTagName("application") for node in applicationNodes: application = {} application["id"] = node.getAttribute("id") if application["id"] not in applications.keys(): Logger.warn("Cannot start unknown application %s"%(application["id"])) continue if node.hasAttribute("arg"): application["arg"] = node.getAttribute("arg") application_to_start.append(application) session["parameters"] = {} for node in sessionNode.getElementsByTagName("parameter"): session["parameters"][node.getAttribute("name")] = node.getAttribute("value") nodes = sessionNode.getElementsByTagName("profile") if len(nodes)>0: profileNode = nodes[0] for attribute in ["server", "dir", "login", "password"]: if len(profileNode.getAttribute(attribute)) == 0: raise Exception("Empty attribute "+attribute) else: profileNode = None sharedfolderNodes = sessionNode.getElementsByTagName("sharedfolder") for node in sharedfolderNodes: for attribute in ["server", "dir", "login", "password", "name"]: if len(node.getAttribute(attribute)) == 0: raise Exception("Empty attribute "+attribute) except Exception, err: Logger.warn("Invalid xml input: "+str(err)) doc = Document() rootNode = doc.createElement('error') rootNode.setAttribute("id", "usage") doc.appendChild(rootNode) return self.req_answer(doc)
def create(): for walk in os.walk(labels): for each in walk[2]: #print(each) fidin=open(walk[0] + '/'+ each,'r') objIndex = 0 for data in fidin.readlines(): objIndex += 1 data=data.strip('\n') datas = data.split(' ') print(len(datas)) if 6 != len(datas): print ('bounding box information error') continue pictureName = each.replace('.txt', '') imageFile = imgpath + pictureName imageFile = imageFile+".jpg" 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
# coding=utf-8 from idaapi import * from idc import * import idautils import sys as sys from xml.dom.minidom import Document ''' 获得可能存在的整型溢出漏洞函数以及堆栈变量类型信息 根据一个给定的XML Schema,使用DOM树的形式从空白文件生成一个XML ''' doc = Document() #创建DOM文档对象 bookstore = doc.createElement('bookstore') #创建根元素 bookstore.setAttribute('xmlns:xsi', "https://www.w3.org/2001/XMLSchema-instance") #设置命名空间 bookstore.setAttribute('xsi:noNamespaceSchemaLocation', 'bookstore.xsd') #引用本地XML Schema doc.appendChild(bookstore) book = doc.createElement('book') book.setAttribute('genre', 'XML') bookstore.appendChild(book) title = doc.createElement('title') title_text = doc.createTextNode('IDAPython整型溢出漏洞检测') #元素内容写入 title.appendChild(title_text) book.appendChild(title) var_size_threshold = 16 #阈值默认为16 current_addr = ScreenEA()
def main(): commandsusage = """Supported commands:\r\n - list:\tlists all scans\r\n - export:\texports the given scan to a given format\r\n - delete:\tdeletes the scan\r\n - stats:\tprint out some statistics of interest\r\n - search:\tsearch for a specific string in the user agent (svmap)\r\n """ commandsusage += "examples:\r\n\r\n" commandsusage += " %s.py list\r\n\r\n" % __prog__ commandsusage += " %s.py export -f pdf -o scan1.pdf -s scan1\r\n\r\n" % __prog__ commandsusage += " %s.py delete -s scan1\r\n\r\n" % __prog__ usage = "%prog [command] [options]\r\n\r\n" usage += commandsusage parser = OptionParser(usage=usage,version="%prog v"+str(__version__)+__GPL__) parser.add_option('-v', '--verbose', dest="verbose", action="count", help="Increase verbosity") parser.add_option('-q', '--quiet', dest="quiet", action="store_true", default=False, help="Quiet mode") parser.add_option("-t", "--type", dest="sessiontype", help="Type of session. This is usually either svmap, svwar or svcrack. If not set I will try to find the best match") parser.add_option("-s", "--session", dest="session", help="Name of the session") parser.add_option("-f", "--format", dest="format", help="Format type. Can be stdout, pdf, xml, csv or txt") parser.add_option("-o", "--output", dest="outputfile", help="Output filename") parser.add_option("-n", dest="resolve", default=True, action="store_false", help="Do not resolve the ip address") parser.add_option("-c", "--count", dest="count", default=False, action="store_true", help="Used togather with 'list' command to count the number of entries") (options,args) = parser.parse_args() if len(args) < 1: parser.error("Please specify a command.\r\n") exit(1) command = args[0] from libs.svhelper import listsessions,deletesessions,createReverseLookup, dbexists from libs.svhelper import getsessionpath,getasciitable,outputtoxml,outputtopdf, calcloglevel validcommands = ['list','export','delete','stats','search'] if command not in validcommands: parser.error('%s is not a supported command' % command) exit(1) logging.basicConfig(level=calcloglevel(options)) sessiontypes = ['svmap','svwar','svcrack'] logging.debug('started logging') if command == 'list': listsessions(options.sessiontype,count=options.count) if command == 'delete': if options.session is None: parser.error("Please specify a valid session.") exit(1) sessionpath = deletesessions(options.session,options.sessiontype) if sessionpath is None: parser.error('Session could not be found. Make sure it exists by making use of %s.py list' % __prog__) exit(1) elif command == 'export': from datetime import datetime start_time = datetime.now() if options.session is None: parser.error("Please specify a valid session") exit(1) if options.outputfile is None and options.format not in [None,'stdout']: parser.error("Please specify an output file") exit(1) tmp = getsessionpath(options.session,options.sessiontype) if tmp is None: parser.error('Session could not be found. Make sure it exists by making use of %s list' % __prog__) exit(1) sessionpath,sessiontype = tmp resolve = False resdb = None if sessiontype == 'svmap': dbloc = os.path.join(sessionpath,'resultua') labels = ['Host','User Agent'] elif sessiontype == 'svwar': dbloc = os.path.join(sessionpath,'resultauth') labels = ['Extension','Authentication'] elif sessiontype == 'svcrack': dbloc = os.path.join(sessionpath,'resultpasswd') labels = ['Extension','Password'] if not dbexists(dbloc): logging.error('The database could not be found: %s'%dbloc) exit(1) db = anydbm.open(dbloc,'r') if options.resolve and sessiontype == 'svmap': resolve = True labels.append('Resolved') resdbloc = os.path.join(sessionpath,'resolved') if not dbexists(resdbloc): logging.info('Performing DNS reverse lookup') resdb = anydbm.open(resdbloc,'c') createReverseLookup(db,resdb) else: logging.info('Not Performing DNS lookup') resdb = anydbm.open(resdbloc,'r') if options.outputfile is not None: if options.outputfile.find('.') < 0: if options.format is None: options.format = 'txt' options.outputfile += '.%s' % options.format if options.format in [None,'stdout','txt']: o = getasciitable(labels,db,resdb) if options.outputfile is None: print o else: open(options.outputfile,'w').write(o) elif options.format == 'xml': from xml.dom.minidom import Document doc = Document() node = doc.createElement(sessiontype) o = outputtoxml('%s report' % sessiontype,labels,db,resdb) open(options.outputfile,'w').write(o) elif options.format == 'pdf': outputtopdf(options.outputfile,'%s report' % sessiontype,labels,db,resdb) elif options.format == 'csv': import csv writer = csv.writer(open(options.outputfile,"w")) for k in db.keys(): row = [k,db[k]] if resdb is not None: if resdb.has_key(k): row.append(resdb[k]) else: row.append('N/A') writer.writerow(row) logging.info( "That took %s" % (datetime.now() - start_time)) elif command == 'stats': from operator import itemgetter import re if options.session is None: parser.error("Please specify a valid session") exit(1) if options.outputfile is None and options.format not in [None,'stdout']: parser.error("Please specify an output file") exit(1) tmp = getsessionpath(options.session,options.sessiontype) if tmp is None: parser.error('Session could not be found. Make sure it exists by making use of %s list' % __prog__) exit(1) sessionpath,sessiontype = tmp if sessiontype != 'svmap': parser.error('Only takes svmap sessions for now') exit(1) dbloc = os.path.join(sessionpath,'resultua') if not dbexists(dbloc): logging.error('The database could not be found: %s'%dbloc) exit(1) db = anydbm.open(dbloc,'r') useragents = dict() useragentconames = dict() for k in db.keys(): v = db[k] if not useragents.has_key(v): useragents[v] = 0 useragents[v]+=1 useragentconame = re.split('[ /]',v)[0] if not useragentconames.has_key(useragentconame): useragentconames[useragentconame] = 0 useragentconames[useragentconame] += 1 _useragents = sorted(useragents.iteritems(), key=itemgetter(1), reverse=True) suseragents = map(lambda x: '\t- %s (%s)' % (x[0],x[1]), _useragents) _useragentsnames = sorted(useragentconames.iteritems(), key=itemgetter(1), reverse=True) suseragentsnames = map(lambda x: '\t- %s (%s)' % (x[0],x[1]), _useragentsnames) print "Total number of SIP devices found: %s" % len(db.keys()) print "Total number of useragents: %s\r\n" % len(suseragents) print "Total number of useragent names: %s\r\n" % len(suseragentsnames) print "Most popular top 30 useragents:\r\n" print '\r\n'.join(suseragents[:30]) print '\r\n\r\n' print "Most unpopular top 30 useragents:\r\n\t" print '\r\n'.join(suseragents[-30:]) print "\r\n\r\n" print "Most popular top 30 useragent names:\r\n" print '\r\n'.join(suseragentsnames[:30]) print '\r\n\r\n' print "Most unpopular top 30 useragent names:\r\n\t" print '\r\n'.join(suseragentsnames[-30:]) print "\r\n\r\n" elif command == 'search': if options.session is None: parser.error("Please specify a valid session") exit(1) if len(args) < 2: parser.error('You need to specify a search string') searchstring = args[1] tmp = getsessionpath(options.session,options.sessiontype) if tmp is None: parser.error('Session could not be found. Make sure it exists by making use of %s list' % __prog__) exit(1) sessionpath,sessiontype = tmp if sessiontype != 'svmap': parser.error('Only takes svmap sessions for now') exit(1) dbloc = os.path.join(sessionpath,'resultua') if not dbexists(dbloc): logging.error('The database could not be found: %s'%dbloc) exit(1) db = anydbm.open(dbloc,'r') useragents = dict() useragentconames = dict() labels = ['Host','User Agent'] for k in db.keys(): v = db[k] if searchstring.lower() in v.lower(): print k+'\t'+v
def _prepare_response(): doc = Document() resp = doc.createElementNS('http://www.yale.edu/tp/cas', 'cas:serviceResponse') resp.setAttribute('xmlns:cas', 'http://www.yale.edu/tp/cas') # seriously minidom? doc.appendChild(resp) return (doc, resp)
def create_data_xml( cls, sheetname, data ): #数据文件Excel filename = cls.get_excel_file( sheetname ) #待生成数据文件 dataname = os.path.join( cls.ConfigRead( CONFIG_FILE, "MBKAUTOTEST", "data_path" ), sheetname+"Data.dat" ) try: xmlDoc = Document() document = xmlDoc.createElement("DataFileEntity") xmlDoc.appendChild( document ) node = xmlDoc.createElement("description") document.appendChild( node ) node = xmlDoc.createElement( "name" ) nodeText = xmlDoc.createTextNode(sheetname+"Data") node.appendChild(nodeText) document.appendChild( node ) node = xmlDoc.createElement("tag") document.appendChild( node ) node = xmlDoc.createElement("containsHeaders" ) nodeText = xmlDoc.createTextNode("true") node.appendChild(nodeText) document.appendChild(node) node = xmlDoc.createElement( "csvSeperator" ) document.appendChild(node) node = xmlDoc.createElement("dataFile" ) nodeText=xmlDoc.createTextNode(str(uuid.uuid1())) node.appendChild( nodeText ) document.appendChild( node ) node = xmlDoc.createElement("dataSourceUrl") nodeText = xmlDoc.createTextNode( filename ) node.appendChild( nodeText ) document.appendChild( node ) node = xmlDoc.createElement( "driver" ) nodeText = xmlDoc.createTextNode("ExcelFile") node.appendChild( nodeText ) document.appendChild( node ) node = xmlDoc.createElement( "isInternalPath" ) nodeText = xmlDoc.createTextNode( "false" ) node.appendChild( nodeText ) document.appendChild( node ) node = xmlDoc.createElement( "query" ) document.appendChild( node ) node = xmlDoc.createElement("secureUserAccount") nodeText = xmlDoc.createTextNode( "false" ) node.appendChild( nodeText ) document.appendChild( node ) node = xmlDoc.createElement("sheetName" ) nodeText = xmlDoc.createTextNode( sheetname ) node.appendChild( nodeText ) document.appendChild( node ) node = xmlDoc.createElement("usingGlobalDBSetting") nodeText = xmlDoc.createTextNode( "false" ) node.appendChild( nodeText ) document.appendChild( node ) with open( dataname, "w" ) as fp: xmlDoc.writexml( fp, indent="\t", newl="\n", encoding="utf-8" ) except Exception as e: logging.error(u"写文件[%s]失败!"%(dataname) )
def __init__(self, rootname): self.doc = Document() self.rootName = rootname
def writeXml(tmp, imgname, w, h, d, bboxes): doc = Document() # owner annotation = doc.createElement('annotation') doc.appendChild(annotation) # owner folder = doc.createElement('folder') annotation.appendChild(folder) folder_txt = doc.createTextNode("VOC2007") folder.appendChild(folder_txt) filename = doc.createElement('filename') annotation.appendChild(filename) filename_txt = doc.createTextNode(imgname) filename.appendChild(filename_txt) # ones# source = doc.createElement('source') annotation.appendChild(source) database = doc.createElement('database') source.appendChild(database) database_txt = doc.createTextNode("My Database") database.appendChild(database_txt) annotation_new = doc.createElement('annotation') source.appendChild(annotation_new) annotation_new_txt = doc.createTextNode("VOC2007") annotation_new.appendChild(annotation_new_txt) image = doc.createElement('image') source.appendChild(image) image_txt = doc.createTextNode("flickr") image.appendChild(image_txt) # owner owner = doc.createElement('owner') annotation.appendChild(owner) flickrid = doc.createElement('flickrid') owner.appendChild(flickrid) flickrid_txt = doc.createTextNode("NULL") flickrid.appendChild(flickrid_txt) ow_name = doc.createElement('name') owner.appendChild(ow_name) ow_name_txt = doc.createTextNode("idannel") ow_name.appendChild(ow_name_txt) # onee# # twos# size = doc.createElement('size') annotation.appendChild(size) width = doc.createElement('width') size.appendChild(width) width_txt = doc.createTextNode(str(w)) width.appendChild(width_txt) height = doc.createElement('height') size.appendChild(height) height_txt = doc.createTextNode(str(h)) height.appendChild(height_txt) depth = doc.createElement('depth') size.appendChild(depth) depth_txt = doc.createTextNode(str(d)) depth.appendChild(depth_txt) # twoe# segmented = doc.createElement('segmented') annotation.appendChild(segmented) segmented_txt = doc.createTextNode("0") segmented.appendChild(segmented_txt) for bbox in bboxes: # threes# object_new = doc.createElement("object") annotation.appendChild(object_new) name = doc.createElement('name') object_new.appendChild(name) name_txt = doc.createTextNode(str(bbox[4])) name.appendChild(name_txt) pose = doc.createElement('pose') object_new.appendChild(pose) pose_txt = doc.createTextNode("Unspecified") pose.appendChild(pose_txt) truncated = doc.createElement('truncated') object_new.appendChild(truncated) truncated_txt = doc.createTextNode("0") truncated.appendChild(truncated_txt) difficult = doc.createElement('difficult') object_new.appendChild(difficult) difficult_txt = doc.createTextNode("0") difficult.appendChild(difficult_txt) # threes-1# bndbox = doc.createElement('bndbox') object_new.appendChild(bndbox) xmin = doc.createElement('xmin') bndbox.appendChild(xmin) xmin_txt = doc.createTextNode(str(bbox[0])) xmin.appendChild(xmin_txt) ymin = doc.createElement('ymin') bndbox.appendChild(ymin) ymin_txt = doc.createTextNode(str(bbox[1])) ymin.appendChild(ymin_txt) xmax = doc.createElement('xmax') bndbox.appendChild(xmax) xmax_txt = doc.createTextNode(str(bbox[2])) xmax.appendChild(xmax_txt) ymax = doc.createElement('ymax') bndbox.appendChild(ymax) ymax_txt = doc.createTextNode(str(bbox[3])) ymax.appendChild(ymax_txt) print(bbox[0], bbox[1], bbox[2], bbox[3], bbox[4]) xmlname = os.path.splitext(imgname)[0] tempfile = tmp + "/%s.xml" % xmlname with open(tempfile, 'wb') as f: f.write(doc.toprettyxml(indent='\t', encoding='utf-8')) return
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()
def main(): wrong_list = [] for walk in os.walk(labels): # os.walk遍历文件夹所有的文件 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:{}'.format(datas[0]) # # 检验一行,是否为5个元素,不是则报错 # continue if 5 != len(datas): wrong_list.append(each) # 检验一行,是否为5个元素,不是则跳过 pass 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('matt')) 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) try: annotation.appendChild(insertObject(doc, datas)) except: wrong_list.append(each) else: try: annotation.appendChild(insertObject(doc, datas)) except: wrong_list.append(each) try: f.write(doc.toprettyxml(indent = ' ')) f.close() fidin.close() except: pass return wrong_list
def __init__(self): """Creates a new instance. """ self._xml_doc = Document() self._current_context = None
@login_required def google_map_ajax(request): """ Возвращает данные для построения карты. Список маркеров загоняется в ХМЛ и отдается клиенту. """ xml_doc = Document() xml_markers = xml_doc.createElement("markers") xml_doc.appendChild(xml_markers) account = request.user.gts_user.account if request.method == "POST": map_form = MapParametersForm(account, request.POST) if map_form.is_valid(): interval_begin = map_form.clean_data['interval_begin'] interval_end = map_form.clean_data['interval_end'] device = map_form.clean_data['device'] # if time not specified, we will assign time = end of day if (interval_end.hour == 0) and (interval_end.minute == 0) and (interval_end.second == 0): interval_end = interval_end.replace(hour = 23, minute = 59, second = 59) begin_timestamp = time.mktime(interval_begin.timetuple()) end_timestamp = time.mktime(interval_end.timetuple()) polyline_points = [] # первая точка - для центрирования карты first_point = "" #----------------------------------------------------------------- events = EventData.objects.filter(device__exact=device, timestamp__gte=begin_timestamp, timestamp__lte=end_timestamp).order_by('timestamp') # "предыдущее" событие di_event = None for event in events: icon = 'blue' if first_point=="": first_point = "new GLatLng(%.6f, %.6f)" % (event.latitude, event.longitude) icon = 'green' info_date = str(datetime.datetime.fromtimestamp(event.timestamp)) #info = '<b>Дата/Время: %s </b> <br>Скор: %.2f <br>Коорд: %s, %s ' % (info_date, event.speed_kph, event.lat(), event.lon()) info = '<b>Date/time: %s </b> <br>Speed: %.2f <br>GPS: %s, %s ' % (info_date, event.speed_kph, event.lat(), event.lon()) new_di_event = DriveIdleEvent("", info, icon, event.latitude, event.longitude) new_di_event.stop_begin_timestamp = event.timestamp new_di_event.stop_end_timestamp = event.timestamp # точку с одинаковыми координатами дважды не заносим # if (di_event) and (new_di_event.latitude_rounded == di_event.latitude_rounded) and (new_di_event.longitude_rounded == di_event.longitude_rounded): if (di_event) and IsSimilar(new_di_event.latitude, di_event.latitude) and IsSimilar(new_di_event.longitude, di_event.longitude): #if (di_event) and (new_di_event.lat == di_event.lat) and (new_di_event.lon == di_event.lon): di_event.stop_end_timestamp = new_di_event.stop_end_timestamp new_di_event = None else: if di_event: # из предыдущей точки берем окончание стоянки а из текущей - дату/время начала стоянки #new_di_event.motion_begin_timestamp = di_event.stop_end_timestamp #new_di_event.motion_end_timestamp = new_di_event.stop_begin_timestamp # добавляем новую точку в список polyline_points.append(di_event) di_event = None di_event = new_di_event else: di_event = new_di_event if di_event: di_event.icon = 'red' polyline_points.append(di_event) for point in polyline_points: xml_marker = xml_doc.createElement("marker") xml_marker.setAttribute("lat", '%.6f' % point.latitude) xml_marker.setAttribute("lng", '%.6f' % point.longitude) xml_marker.setAttribute("info", point.tooltip()) xml_marker.setAttribute("icon", point.actual_icon()) xml_markers.appendChild(xml_marker) # filename = "%smarkers.xml" % settings.MEDIA_ROOT # thefile=open(filename,"w") # xml_doc.writexml(thefile) # thefile.close()
def __init__(self, tag=None, **kwargs): self.doc = Document() XMLElement.__init__(self, self.doc, self.doc) if tag: self.el = self.add(tag, **kwargs).el
class Dialog(AbstractDialog): def __init__(self, role_instance): self.role_instance = role_instance @staticmethod def getName(): return "aps" def process(self, request): path = request["path"] if request["method"] == "GET": Logger.debug("do_GET "+path) if path == "/applications": return self.req_applications(request) elif path.startswith("/application/icon/"): app_id = path[len("/application/icon/"):] return self.req_icon(app_id) elif path == "/applications/static/sync": return self.req_sync_static_applications(request) elif path.startswith("/session/status/"): buf = path[len("/session/status/"):] return self.req_session_status(buf) elif path.startswith("/session/destroy/"): buf = path[len("/session/destroy/"):] return self.req_session_destroy(buf) elif path.startswith("/debian/") and self.role_instance.canManageApplications(): buf = path[len("/debian/"):] return self.req_debian_id(buf) return None elif request["method"] == "POST": Logger.debug("do_POST "+path) if path == "/session/create": return self.req_session_create(request) elif path == "/user/loggedin": return self.req_user_loggedin(request) elif path == "/user/logout": return self.req_user_logout(request) elif path == "/debian" and self.role_instance.canManageApplications(): return self.req_debian(request) elif path == "/applications/ids": return self.req_applications_matching(request) return None return None @staticmethod def session2xmlstatus(session): doc = Document() rootNode = doc.createElement('session') rootNode.setAttribute("id", session.id) rootNode.setAttribute("status", session.status) if session.status == Session.SESSION_STATUS_DESTROYED and session.end_status is not None: rootNode.setAttribute("reason", session.end_status) doc.appendChild(rootNode) return doc def req_applications(self, request): doc = Document() rootNode = doc.createElement('applications') doc.appendChild(rootNode) self.role_instance.applications_mutex.acquire() for application in self.role_instance.applications.values(): appNode = doc.createElement("application") appNode.setAttribute("id", application["local_id"]) appNode.setAttribute("name", application["name"]) appNode.setAttribute("desktopfile", application["filename"]) if application.has_key("description"): appNode.setAttribute("description", application["description"]) if application.has_key("package"): appNode.setAttribute("package", application["package"]) exeNode = doc.createElement("executable") exeNode.setAttribute("command", application["command"]) #if application.has_key("icon"): # exeNode.setAttribute("icon", application["icon"]) for mime in application["mimetypes"]: mimeNode = doc.createElement("mime") mimeNode.setAttribute("type", mime) appNode.appendChild(mimeNode) appNode.appendChild(exeNode) rootNode.appendChild(appNode) self.role_instance.applications_mutex.release() return self.req_answer(doc) def req_applications_matching(self, request): try: document = minidom.parseString(request["data"]) rootNode = document.documentElement if rootNode.nodeName != "applications": raise Exception("invalid root node") matching = [] applicationNodes = rootNode.getElementsByTagName("application") for node in applicationNodes: matching.append((node.getAttribute("id"), node.getAttribute("local_id"))) except Exception, err: Logger.warn("Invalid xml input: "+str(err)) doc = Document() rootNode = doc.createElement('error') rootNode.setAttribute("id", "usage") doc.appendChild(rootNode) return self.req_answer(doc) self.role_instance.applications_mutex.acquire() self.role_instance.applications_id_SM = {} for (sm_id, local_id) in matching: if not self.role_instance.applications.has_key(local_id): continue self.role_instance.applications[local_id]["id"] = sm_id self.role_instance.applications_id_SM[sm_id] = self.role_instance.applications[local_id] self.role_instance.applications_mutex.release() doc = Document() rootNode = doc.createElement('applications') rootNode.setAttribute("matching", "ok") doc.appendChild(rootNode) return self.req_answer(doc)
def testElement(): dom = Document() dom.appendChild(dom.createElement("abc")) confirm(dom.documentElement) dom.unlink()
def writeInfoToXml(directorList, actorList, picList, filename): # 创建dom文档 doc = Document() # 创建根节点 movie = doc.createElement('movie') # 根节点插入dom树 doc.appendChild(movie) dl = doc.createElement('directorList') movie.appendChild(dl) for v in directorList: (name, img, role, identity) = (v['name'], v['img'], v['role'], v['identity']) di = doc.createElement('director') dl.appendChild(di) namee = doc.createElement('name') name_text = doc.createTextNode(name) namee.appendChild(name_text) di.appendChild(namee) idenn = doc.createElement('identity') iden_text = doc.createTextNode(identity) idenn.appendChild(iden_text) di.appendChild(idenn) imgg = doc.createElement('img') img_text = doc.createTextNode(img) imgg.appendChild(img_text) di.appendChild(imgg) rolee = doc.createElement('role') role_text = doc.createTextNode(role) rolee.appendChild(role_text) di.appendChild(rolee) dl = doc.createElement('actorList') movie.appendChild(dl) for v in actorList: (name, img, role, identity) = (v['name'], v['img'], v['role'], v['identity']) di = doc.createElement('actor') dl.appendChild(di) namee = doc.createElement('name') name_text = doc.createTextNode(name) namee.appendChild(name_text) di.appendChild(namee) idenn = doc.createElement('identity') iden_text = doc.createTextNode(identity) idenn.appendChild(iden_text) di.appendChild(idenn) imgg = doc.createElement('img') img_text = doc.createTextNode(img) imgg.appendChild(img_text) di.appendChild(imgg) rolee = doc.createElement('role') role_text = doc.createTextNode(role) rolee.appendChild(role_text) di.appendChild(rolee) dl = doc.createElement('picList') movie.appendChild(dl) for v in picList: di = doc.createElement('pic') dl.appendChild(di) imgg = doc.createElement('img') img_text = doc.createTextNode(v) imgg.appendChild(img_text) di.appendChild(imgg) try: with open(filename, 'w', encoding='UTF-8') as fh: # 4.writexml()第一个参数是目标文件对象,第二个参数是根节点的缩进格式,第三个参数是其他子节点的缩进格式, # 第四个参数制定了换行格式,第五个参数制定了xml内容的编码。 doc.writexml(fh, indent='', addindent='\t', newl='\n', encoding='UTF-8') print('写入xml OK!') except Exception as err: print('错误信息:{0}'.format(err))
def testAttributeRepr(): dom = Document() el = dom.appendChild(dom.createElement(u"abc")) node = el.setAttribute("abc", "def") confirm(str(node) == repr(node)) dom.unlink()
version=__version__, help='Display version information, then exit.') args0 = parser.parse_args() return args0 ########################################################################### # args = processOptions() warn(0, "Patching extensions onto xml.dom.minidom.Node...") Node.__getitem__ = PyNode.__getitem__ Node._getChildNodesByName_ = PyNode._getChildNodesByName_ Node._getListItemsByName_ = PyNode._getListItemsByName_ theDocEl = Document() theDocEl.nodeName = "book" print("Document nodeName is '%s'." % (theDocEl.nodeName)) body = theDocEl.createElement("body") theDocEl.appendChild(body) print("body element nodeName is '%s'." % (body.nodeName)) nParas = 10 for i in range(nParas): e = theDocEl.createElement("p") e.setAttribute("id", "myId_%2d" % (i)) e.appendChild(theDocEl.createTextNode("This is a some text.")) body.appendChild(e) print("Sample document created.")
def dump_xml(self): """Dump scenario information in XML format, something like: <$job_partition> <nodes> <node>node1</node> <node>node2</node> </nodes> <config> <node>$firstnode</node> <ipaddress>$iprin</ipaddress> <session>$rfbport</session> <password>$mdp</password> </config> <enddatetime>$daylimit</enddatetime> <pid>$jobid</pid> </$job_partition> """ doc = Document() root = doc.createElement(self.conf.cluster_partition) doc.appendChild(root) nodes = doc.createElement('nodes') root.appendChild(nodes) for nodename in self.job.nodes: node = doc.createElement('node') node.appendChild(doc.createTextNode(nodename)) nodes.appendChild(node) config = doc.createElement('config') config_elmt = doc.createElement('node') config_elmt.appendChild(doc.createTextNode(self.job.firstnode)) config.appendChild(config_elmt) config_elmt = doc.createElement('ipaddress') config_elmt.appendChild(doc.createTextNode(self.rinip)) config.appendChild(config_elmt) config_elmt = doc.createElement('fqdn') try: # try to find a string representing the canonical name of the firstnode fqdn = socket.getaddrinfo(self.job.firstnode, 0, 0, 0, 0, socket.AI_CANONNAME)[0][3] except: fqdn = self.job.firstnode config_elmt.appendChild(doc.createTextNode(fqdn)) config.appendChild(config_elmt) config_elmt = doc.createElement('session') config_elmt.appendChild(doc.createTextNode(str(self.rfbport))) config.appendChild(config_elmt) config_elmt = doc.createElement('password') config_elmt.appendChild(doc.createTextNode(self.password)) config.appendChild(config_elmt) root.appendChild(config) enddatetime = doc.createElement('enddatetime') enddatetime.appendChild(doc.createTextNode(self.job.end.isoformat())) root.appendChild(enddatetime) pid = doc.createElement('pid') pid.appendChild(doc.createTextNode(str(self.job.jobid))) root.appendChild(pid) print doc.toprettyxml() sys.stdout.flush() # force flush to avoid buffering
def __init__(self, path): ReportWriter.__init__(self, path) self.doc = Document() self.activity_list = self.doc.createElement("activities")
def rss(): """RSS feed for the blog.""" doc = Document() rss = doc.createElement("rss") rss.setAttribute("version", "2.0") rss.setAttribute("xmlns:blogChannel", "http://backend.userland.com/blogChannelModule") doc.appendChild(rss) channel = doc.createElement("channel") rss.appendChild(channel) rss_time = "%a, %d %b %Y %H:%M:%S GMT" ###### ## Channel Information ###### today = time.strftime(rss_time, time.gmtime()) xml_add_text_tags(doc, channel, [ ["title", Config.blog.title], ["link", Config.blog.link], ["description", Config.blog.description], ["language", Config.blog.language], ["copyright", Config.blog.copyright], ["pubDate", today], ["lastBuildDate", today], ["webmaster", Config.blog.webmaster], ]) ###### ## Image Information ###### image = doc.createElement("image") channel.appendChild(image) xml_add_text_tags(doc, image, [ ["title", Config.blog.image_title], ["url", Config.blog.image_url], ["link", Config.blog.link], ["width", Config.blog.image_width], ["height", Config.blog.image_height], ["description", Config.blog.image_description], ]) ###### ## Add the blog posts ###### index = Blog.get_index() posts = get_index_posts(index) for post_id in posts[:int(Config.blog.entries_per_feed)]: post = Blog.get_entry(post_id) item = doc.createElement("item") channel.appendChild(item) # Render the body. if post["format"] == "markdown": post["rendered_body"] = render_markdown(post["body"]) else: post["rendered_body"] = post["body"] # Render emoticons. if post["emoticons"]: post["rendered_body"] = Emoticons.render(post["rendered_body"]) xml_add_text_tags(doc, item, [ ["title", post["subject"]], ["link", url_for("blog.entry", fid=post["fid"], _external=True)], ["description", post["rendered_body"]], ["pubDate", time.strftime(rss_time, time.gmtime(post["time"]))], ]) resp = make_response(doc.toprettyxml(encoding="utf-8")) resp.headers["Content-Type"] = "application/rss+xml; charset=utf-8" return resp
def test_minimal(self): coord = iris.coords.DimCoord(np.arange(10, dtype=np.int32)) element = coord.xml_element(Document()) self.assertXMLElement(coord, ('coord_api', 'minimal.xml'))
def __init__(self, settings=None): Printer.__init__(self, settings) from xml.dom.minidom import Document self.dom = Document()
def submissions(request, format): """ Use this view to make arbitrary queries on submissions. If the user is a logged in staff member, ignore submission.is_public, question.answer_is_public, and survey.can_have_public_submissions. Use the query string to pass keys and values. For example, /crowdsourcing/submissions/?survey=my-survey will return all submissions for the survey with slug my-survey. survey - the slug for the survey user - the username of the submittor. Leave blank for submissions without a logged in user. submitted_from and submitted_to - strings in the format YYYY-mm-ddThh:mm:ss For example, 2010-04-05T13:02:03 featured - A blank value, 'f', 'false', 0, 'n', and 'no' all mean ignore the featured flag. Everything else means display only featured. You can also use filters in the survey report sense. Rather than document exactly what parameters you would pass, follow these steps to figure it out: 1. Enable filters on your survey and the questions you want to filter on. 2. Go to the report page and fill out the filters you want. 3. Click Submit. 4. Examine the query string of the page you end up on and note which parameters are filled out. Use those same parameters here. """ format = format.lower() if format not in FORMAT_CHOICES: msg = ("%s is an unrecognized format. Crowdsourcing recognizes " "these: %s") % (format, ", ".join(FORMAT_CHOICES)) return HttpResponse(msg) is_staff = request.user.is_authenticated() and request.user.is_staff if is_staff: results = Submission.objects.all() else: # survey.can_have_public_submissions is complicated enough that # we'll check it in Python, not the database. results = Submission.objects.filter(is_public=True) results = results.select_related("survey", "user") get = request.GET.copy() limit = int(get.pop("limit", [0])[0]) keys = get.keys() basic_filters = ('survey', 'user', 'submitted_from', 'submitted_to', 'featured', 'is_public') if is_staff: basic_filters += BALLOT_STUFFING_FIELDS survey_slug = "" for field in [f for f in keys if f in basic_filters]: value = get[field] search_field = field if 'survey' == field: search_field = 'survey__slug' survey_slug = value elif 'user' == field: if '' == value: value = None else: search_field = 'user__username' elif field in ('submitted_from', 'submitted_to'): date_format = "%Y-%m-%dT%H:%M:%S" try: value = datetime.strptime(value, date_format) except ValueError: return HttpResponse(("Invalid %s format. Try, for example, " "%s") % ( field, datetime.now().strftime(date_format), )) if 'submitted_from' == field: search_field = 'submitted_at__gte' else: search_field = 'submitted_at__lte' elif field in ( 'featured', 'is_public', ): falses = ( 'f', 'false', 'no', 'n', '0', ) value = len(value) and not value.lower() in falses # search_field is unicode but needs to be ascii. results = results.filter(**{str(search_field): value}) get.pop(field) def get_survey(): survey = Survey.objects.get(slug=survey_slug) get_survey = lambda: survey return survey if get: if survey_slug: results = extra_from_filters(results, "crowdsourcing_submission.id", get_survey(), get) else: message = ( "You've got a couple of extra filters here, and we " "aren't sure what to do with them. You may have just " "misspelled one of the basic filters (%s). You may have a " "filter from a particular survey in mind. In that case, just " "include survey=my-survey-slug in the query string. You may " "also be trying to pull some hotshot move like, \"Get me all " "submissions that belong to a survey with a filter named '%s' " "that match '%s'.\" Crowdsourcing could support this, but it " "would be pretty inefficient and, we're guessing, pretty " "rare. If that's what you're trying to do I'm afraid you'll " "have to do something more complicated like iterating through " "all your surveys.") item = get.items()[0] message = message % (", ".join(basic_filters), item[0], item[1]) return HttpResponse(message) if not is_staff: if survey_slug: if not get_survey().can_have_public_submissions(): results = [] else: rs = [r for r in results if r.survey.can_have_public_submissions()] results = rs if limit: results = results[:limit] answer_lookup = get_all_answers(results, include_private_questions=is_staff) result_data = [] for r in results: data = r.to_jsondata(answer_lookup, include_private_questions=is_staff) result_data.append(data) for data in result_data: data.update(data["data"]) data.pop("data") def get_keys(): key_lookup = {} for data in result_data: for key in data.keys(): key_lookup[key] = True return sorted(key_lookup.keys()) if format == 'json': response = HttpResponse(mimetype='application/json') dump(result_data, response) elif format == 'csv': response = HttpResponse(mimetype='text/csv') writer = csv.writer(response) keys = get_keys() writer.writerow(keys) for data in result_data: row = [] for k in keys: row.append((u"%s" % _encode(data.get(k, ""))).encode("utf-8")) writer.writerow(row) elif format == 'xml': doc = Document() submissions = doc.createElement("submissions") doc.appendChild(submissions) for data in result_data: submission = doc.createElement("submission") submissions.appendChild(submission) for key, value in data.items(): if value: cell = doc.createElement(key) submission.appendChild(cell) cell.appendChild(doc.createTextNode(u"%s" % value)) response = HttpResponse(doc.toxml(), mimetype='text/xml') elif format == 'html': # mostly for debugging. keys = get_keys() results = [ "<html><body><table>", "<tr>%s</tr>" % "".join(["<th>%s</th>" % k for k in keys]) ] for data in result_data: cell = "<td>%s</td>" cells = [cell % _encode(data.get(key, "")) for key in keys] results.append("<tr>%s</tr>" % "".join(cells)) results.append("</table></body></html>") response = HttpResponse("\n".join(results)) else: return HttpResponse("Unsure how to handle %s format" % format) return response
this_siv_buf_size = genome[4]; # check to see if result's in history; if it is, use that! #for line in open("result_output.txt"): # if str(this_strp_fac) + ", " + str(this_strp_unt) + ", " + str(this_cb_nds) + str(this_cb_buf_size) + ", " + str(this_align) + ", " + str(this_siv_buf_size) in line: # return float(line.split(", ")[6]) print "Evaluate Parameters config (%d, %d, %d, %d, %d, %d): " % (this_strp_fac, this_strp_unt, this_cb_nds, this_align_thresh, this_align, this_siv_buf_size) #sys.stdout.flush() #################################################################### # # Create the minidom document for tunable parameters # #################################################################### doc = Document() # Create the <Parameters> base element params = doc.createElement("Parameters") doc.appendChild(params) high = doc.createElement("High_Level_IO_Library"); params.appendChild(high) algn = doc.createElement("alignment"); high.appendChild(algn); algn_txt_tmp = str(this_align_thresh) + ',' + str(this_align); algn_txt = doc.createTextNode(algn_txt_tmp); algn.appendChild(algn_txt); siv_buf = doc.createElement("sieve_buf_size"); high.appendChild(siv_buf);
# but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with PIConGPU. # If not, see <http://www.gnu.org/licenses/>. # import sys import glob import argparse from xml.dom.minidom import Document import splash2xdmf doc = Document() grid_doc = Document() poly_doc = Document() time_series = False # identifiers for vector components VECTOR_IDENTS = ["x", "y", "z", "w"] NAME_DELIM = "/" # PIC-specific names NAME_GLOBALCELLIDX = "globalCellIdx" NAME_POSITION = "position" def get_vector_basename(vector_name): """
def get_scheme(self): """ Get the scheme of the inputs parameters and return as a string. """ # Create the XML document doc = Document() # Create the <scheme> base element element_scheme = doc.createElement("scheme") doc.appendChild(element_scheme) # Create the title element element_title = doc.createElement("title") element_scheme.appendChild(element_title) element_title_text = doc.createTextNode(self.title) element_title.appendChild(element_title_text) # Create the description element element_desc = doc.createElement("description") element_scheme.appendChild(element_desc) element_desc_text = doc.createTextNode(self.description) element_desc.appendChild(element_desc_text) # Create the use_external_validation element element_external_validation = doc.createElement( "use_external_validation") element_scheme.appendChild(element_external_validation) element_external_validation_text = doc.createTextNode( self.use_external_validation) element_external_validation.appendChild( element_external_validation_text) # Create the streaming_mode element element_streaming_mode = doc.createElement("streaming_mode") element_scheme.appendChild(element_streaming_mode) element_streaming_mode_text = doc.createTextNode(self.streaming_mode) element_streaming_mode.appendChild(element_streaming_mode_text) # Create the use_single_instance element element_use_single_instance = doc.createElement("use_single_instance") element_scheme.appendChild(element_use_single_instance) element_use_single_instance_text = doc.createTextNode( self.use_single_instance) element_use_single_instance.appendChild( element_use_single_instance_text) # Create the elements to stored args element element_endpoint = doc.createElement("endpoint") element_scheme.appendChild(element_endpoint) element_args = doc.createElement("args") element_endpoint.appendChild(element_args) # Create the argument elements self.add_xml_args(doc, element_args) # Return the content as a string return doc.toxml()
def read_xml(img_path, xmlpath, outputpath, delta_w, delta_h, num_begin): xml = objectify.parse(open(xmlpath)) img = 0 root = xml.getroot() imagename = 0 wsize = 0 hsize = 0 label = [] x1 = [] y1 = [] x2 = [] y2 = [] img_newname = [] xml_newname = [] re_w = 0 # 余数 re_h = 0 # 余数 h_index = 0 w_index = 0 # 解析xml得到原始的label和BBox坐标 for tmp in root.getchildren(): if tmp.tag == 'filename': imagename = tmp.pyval img = plt.imread(img_path) hsize = img.shape[0] wsize = img.shape[1] h_index, w_index, re_h, re_w = calculate_images( hsize, wsize, delta_w, delta_h) img_newname, xml_newname = getname(num_begin, img_num=int((w_index + 1) * (h_index + 1))) # 得到原始的label和BBox坐标 if tmp.tag == 'object': obj = tmp for child in obj.getchildren(): if child.tag == 'name': label.append(child.pyval) if child.tag == 'bndbox': for coordinate in child.getchildren(): if coordinate.tag == 'xmin': x1.append(coordinate.pyval) if coordinate.tag == 'ymin': y1.append(coordinate.pyval) if coordinate.tag == 'xmax': x2.append(coordinate.pyval) if coordinate.tag == 'ymax': y2.append(coordinate.pyval) # 生成新的img和annotation文件 index = 0 #计数一张图片生成了的小图的数量 imagenewnames = [] for x_i in range(int(w_index + 1)): tmp_ox = x_i * delta_w x_boundary = tmp_ox + delta_w if x_i == w_index: x_boundary = tmp_ox + re_w for y_i in range(int(h_index + 1)): tmp_oy = y_i * delta_h y_boundary = tmp_oy + delta_h if y_i == h_index: y_boundary = tmp_oy + re_h ss_img = img[tmp_oy:y_boundary, tmp_ox:x_boundary, :] s_img = ss_img.copy() # 生成img tmp_path = os.path.join(outputpath, 'JPEGImages') save_path = os.path.join(tmp_path, img_newname[index]) # 获取新的label 和 BBox坐标 imagenewname = img_newname[index] new_label = [] x_min = [] y_min = [] x_max = [] y_max = [] img_w = 0 img_h = 0 for j in range(len(x1)): # 最理想的情况:边界框不被切割线穿过 if (x1[j] >= tmp_ox) & (y1[j] >= tmp_oy) & ( x2[j] <= x_boundary) & (y2[j] <= y_boundary): x_min.append(x1[j] - tmp_ox) x_max.append(x2[j] - tmp_ox) y_min.append(y1[j] - tmp_oy) y_max.append(y2[j] - tmp_oy) # 边界框被横着的切割线穿过 elif (x1[j] >= tmp_ox) & (y1[j] >= tmp_oy) & (x2[j] <= x_boundary) & ( y2[j] > y_boundary) & (y1[j] < y_boundary): if (y2[j] - y_boundary) <= (y_boundary - y1[j]): y_min.append(y1[j] - tmp_oy) y_max.append(y_boundary - tmp_oy) x_min.append(x1[j] - tmp_ox) x_max.append(x2[j] - tmp_ox) elif (x1[j] >= tmp_ox) & (y1[j] < tmp_oy) & ( x2[j] <= x_boundary) & (y2[j] <= y_boundary): if (y2[j] - tmp_oy) > (tmp_oy - y1[j]): y_min.append(1) y_max.append(y2[j] - tmp_oy) x_min.append(x1[j] - tmp_ox) x_max.append(x2[j] - tmp_ox) # 边界框被竖着的切割线切割 elif (x1[j] >= tmp_ox) & (y1[j] >= tmp_oy) & (x2[j] > x_boundary) & ( y2[j] <= y_boundary) & (x1[j] < x_boundary): if (x2[j] - x_boundary) <= (x_boundary - x1[j]): x_min.append(x1[j] - tmp_ox) x_max.append(x_boundary - tmp_ox) y_min.append(y1[j] - tmp_oy) y_max.append(y2[j] - tmp_oy) elif (x1[j] <= tmp_ox) & (y1[j] >= tmp_oy) & ( x2[j] <= x_boundary) & (y2[j] <= y_boundary): if (x2[j] - tmp_ox) > (tmp_ox - x1[j]): x_min.append(1) x_max.append(x2[j] - tmp_ox) y_min.append(y1[j] - tmp_oy) y_max.append(y2[j] - tmp_oy) # 最不理想的情况,边界框被两条切割线穿过 elif (x1[j] >= tmp_ox) & (y1[j] >= tmp_oy) & ( x2[j] >= x_boundary) & (y2[j] >= y_boundary) & ( x1[j] < x_boundary) & (y1[j] < y_boundary): if ((x2[j] - x_boundary) <= (x_boundary - x1[j])) & ((y2[j] - y_boundary) <= (y_boundary - y1[j])): x_min.append(x1[j] - tmp_ox) x_max.append(x_boundary - tmp_ox) y_min.append(y1[j] - tmp_oy) y_max.append(y_boundary - tmp_oy) elif (x1[j] < tmp_ox) & (y1[j] >= tmp_oy) & (x2[j] < x_boundary) & ( y2[j] >= y_boundary) & (y1[j] < y_boundary): if ((x2[j] - tmp_ox) >= (tmp_ox - x1[j])) & ((y2[j] - y_boundary) <= (y_boundary - y1[j])): x_min.append(1) x_max.append(x2[j] - tmp_ox) y_min.append(y1[j] - tmp_oy) y_max.append(y_boundary - tmp_oy) elif (x1[j] >= tmp_ox) & (y1[j] < tmp_oy) & (x2[j] > x_boundary) & ( y2[j] < y_boundary) & (x1[j] < x_boundary): if ((x2[j] - x_boundary) <= (x_boundary - x1[j])) & ((y2[j] - tmp_oy) >= (tmp_oy - y1[j])): x_min.append(x1[j] - tmp_ox) x_max.append(x_boundary - tmp_ox) y_min.append(1) y_max.append(y2[j] - tmp_oy) elif (x1[j] < tmp_ox) & (y1[j] < tmp_oy) & ( x2[j] < x_boundary) & (y2[j] < y_boundary): if ((x2[j] - tmp_ox) >= (tmp_ox - x1[j])) & ((y2[j] - tmp_oy) >= (tmp_oy - y1[j])): x_min.append(1) x_max.append(x2[j] - tmp_ox) y_min.append(1) y_max.append(y2[j] - tmp_oy) new_label.append(label[j]) img_h = y_boundary - tmp_oy img_w = x_boundary - tmp_ox # 生成xml if (len(x_min) != 0): doc = Document() annotation = doc.createElement('annotation') doc.appendChild(annotation) folder = doc.createElement('folder') folder_text = doc.createTextNode('VOC2007') folder.appendChild(folder_text) annotation.appendChild(folder) filename = doc.createElement('filename') filename_text = doc.createTextNode(imagenewname) filename.appendChild(filename_text) annotation.appendChild(filename) source = doc.createElement('source') annotation.appendChild(source) database = doc.createElement('database') database_text = doc.createTextNode('My Database') database.appendChild(database_text) source.appendChild(database) annotation1 = doc.createElement('annotation') annotation1_text = doc.createTextNode('VOC2007') annotation1.appendChild(annotation1_text) source.appendChild(annotation1) image = doc.createElement('image') image_text = doc.createTextNode('flickr') image.appendChild(image_text) source.appendChild(image) flickrid = doc.createElement('flickrid') flickrid_text = doc.createTextNode('NULL') flickrid.appendChild(flickrid_text) source.appendChild(flickrid) owner = doc.createElement('owner') annotation.appendChild(owner) flickrid = doc.createElement('flickrid') flickrid_text = doc.createTextNode('NULL') flickrid.appendChild(flickrid_text) owner.appendChild(flickrid) name = doc.createElement('name') name_text = doc.createTextNode('lijiangtao') name.appendChild(name_text) owner.appendChild(name) size = doc.createElement('size') annotation.appendChild(size) width = doc.createElement('width') width_text = doc.createTextNode(str(img_w)) width.appendChild(width_text) size.appendChild(width) height = doc.createElement('height') height_text = doc.createTextNode(str(img_h)) height.appendChild(height_text) size.appendChild(height) depth = doc.createElement('depth') depth_text = doc.createTextNode('3') depth.appendChild(depth_text) size.appendChild(depth) segmented = doc.createElement('segmented') segmented_text = doc.createTextNode('0') segmented.appendChild(segmented_text) annotation.appendChild(segmented) for i in range(len(x_min)): object = doc.createElement('object') annotation.appendChild(object) name1 = doc.createElement('name') name1_text = doc.createTextNode(str(new_label[i])) name1.appendChild(name1_text) object.appendChild(name1) pose = doc.createElement('pose') pose_text = doc.createTextNode('Unspecified') pose.appendChild(pose_text) object.appendChild(pose) truncated = doc.createElement('truncated') truncated_text = doc.createTextNode('0') truncated.appendChild(truncated_text) object.appendChild(truncated) difficult = doc.createElement('difficult') difficult_text = doc.createTextNode('0') difficult.appendChild(difficult_text) object.appendChild(difficult) bndbox = doc.createElement('bndbox') object.appendChild(bndbox) xmin = doc.createElement('xmin') xmin_text = doc.createTextNode(str(x_min[i])) xmin.appendChild(xmin_text) bndbox.appendChild(xmin) ymin = doc.createElement('ymin') ymin_text = doc.createTextNode(str(y_min[i])) ymin.appendChild(ymin_text) bndbox.appendChild(ymin) xmax = doc.createElement('xmax') xmax_text = doc.createTextNode(str(x_max[i])) xmax.appendChild(xmax_text) bndbox.appendChild(xmax) ymax = doc.createElement('ymax') ymax_text = doc.createTextNode(str(y_max[i])) ymax.appendChild(ymax_text) bndbox.appendChild(ymax) f = open(outputpath + '/Annotations/' + xml_newname[index], 'w') f.write(doc.toprettyxml(indent='')) f.close() plt.imsave(save_path, s_img) print(xml_newname[index]) imagenewnames.append(xml_newname[index]) index = 1 + index return index, imagenewnames
def saveXML(self): if self.rawdata: root = self.rawdata.getElementsByTagName('ComicInfo')[0] for row in (['Series', self.data['Series']], ['Volume', self.data['Volume']], ['Number', self.data['Number'] ], ['Writer', ', '.join(self.data['Writers'])], ['Penciller', ', '.join(self.data['Pencillers']) ], ['Inker', ', '.join(self.data['Inkers'])], ['Colorist', ', '.join(self.data['Colorists']) ], ['Summary', self.data['Summary']], [ 'ScanInformation', 'MCD(' + self.data['MUid'] + ')' if self.data['MUid'] else '' ]): if self.rawdata.getElementsByTagName(row[0]): node = self.rawdata.getElementsByTagName(row[0])[0] if row[1]: node.firstChild.replaceWholeText(row[1]) else: root.removeChild(node) elif row[1]: main = self.rawdata.createElement(row[0]) root.appendChild(main) text = self.rawdata.createTextNode(row[1]) main.appendChild(text) else: doc = Document() root = doc.createElement('ComicInfo') root.setAttribute('xmlns:xsd', 'http://www.w3.org/2001/XMLSchema') root.setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance') doc.appendChild(root) for row in (['Series', self.data['Series']], ['Volume', self.data['Volume']], ['Number', self.data['Number'] ], ['Writer', ', '.join(self.data['Writers'])], ['Penciller', ', '.join(self.data['Pencillers']) ], ['Inker', ', '.join(self.data['Inkers'])], ['Colorist', ', '.join(self.data['Colorists']) ], ['Summary', self.data['Summary']], [ 'ScanInformation', 'MCD(' + self.data['MUid'] + ')' if self.data['MUid'] else '' ]): if row[1]: main = doc.createElement(row[0]) root.appendChild(main) text = doc.createTextNode(row[1]) main.appendChild(text) self.rawdata = doc if self.source.endswith('.xml'): with open(self.source, 'w', encoding='utf-8') as f: self.rawdata.writexml(f, encoding='utf-8') else: workdir = mkdtemp('', 'KCC-') tmpXML = os.path.join(workdir, 'ComicInfo.xml') with open(tmpXML, 'w', encoding='utf-8') as f: self.rawdata.writexml(f, encoding='utf-8') if is_zipfile(self.source): removeFromZIP(self.source, 'ComicInfo.xml') with ZipFile(self.source, mode='a', compression=ZIP_DEFLATED) as zip_file: zip_file.write(tmpXML, arcname=tmpXML.split(os.sep)[-1]) elif rarfile.is_rarfile(self.source): raise NotImplementedError elif is_7zfile(self.source): output = Popen('7za a "' + self.source + '" "' + tmpXML + '"', stdout=PIPE, stderr=STDOUT, stdin=PIPE, shell=True) extracted = False for line in output.stdout: if b"Everything is Ok" in line: extracted = True if not extracted: rmtree(workdir) raise OSError('Failed to modify 7ZIP file.') rmtree(workdir)
def writexml(filename,saveimg,bboxes,xmlpath,img_set): ''' 本函数将用于打包xml格式的文件,是一种标准的打包xml文件打包的方法。 对于这个函数只需要传入以下几个参数就能够写入一个xml文件到指定的’xmlpath‘文件路径下 :param filename:图片名 :param saveimg:图片信息 :param bboxes:bounding box信息 :param xmlpath:xml文件存储信息 :param img_set:数据集类型:训练数据或者测试数据 :return: ''' doc = Document() annotation = doc.createElement('annotation') doc.appendChild(annotation) folder = doc.createElement('folder') if img_set == 'train': data_name = 'VOC2007' elif img_set =='val': data_name = 'VOC2012' elif img_set == 'eval': data_name = 'VOC2012' folder_name = doc.createTextNode(data_name) 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('The ' + data_name + ' Database')) source.appendChild(database) annotation_s = doc.createElement('annotation') annotation_s.appendChild(doc.createTextNode('PASCAL ' + data_name)) 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) 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] # class_name_mask = ['汽车', '卡车', '行人', '自行车', '交通灯'] # class_name = str(class_name_mask[bbox[0] - 1]) class_name = str(bbox[0]) objects = doc.createElement('object') annotation.appendChild(objects) object_name = doc.createElement('name') object_name.appendChild(doc.createTextNode(class_name)) objects.appendChild(object_name) pose = doc.createElement('pose') pose.appendChild(doc.createTextNode('Unspecified')) objects.appendChild(pose) truncated = doc.createElement('truncated') truncated.appendChild(doc.createTextNode('1')) objects.appendChild(truncated) difficult = doc.createElement('difficult') difficult.appendChild(doc.createTextNode('0')) objects.appendChild(difficult) bndbox = doc.createElement('bndbox') objects.appendChild(bndbox) xmin = doc.createElement('xmin') xmin.appendChild(doc.createTextNode(str(bbox[1]))) bndbox.appendChild(xmin) ymin = doc.createElement('ymin') ymin.appendChild(doc.createTextNode(str(bbox[2]))) bndbox.appendChild(ymin) xmax = doc.createElement('xmax') xmax.appendChild(doc.createTextNode(str(bbox[3]))) bndbox.appendChild(xmax) ymax = doc.createElement('ymax') ymax.appendChild(doc.createTextNode(str(bbox[4]))) bndbox.appendChild(ymax) f = open(xmlpath, "w") f.write(doc.toprettyxml(indent='')) f.close()