step_xml_ids = {} options.directory = os.path.expanduser(options.directory) # Extract scenario scenario_obj = connection.env['scanner.scenario'] scenario = scenario_obj.browse(options.scenario_id).with_context( # Force the en_US language to export translateable values active_test=False, lang='en_US') scenario_xml_id = scenario.get_metadata()[0]['xmlid'] if not scenario_xml_id: scenario_xml_id = 'scanner_scenario_%s' % normalize_name(scenario.name) # Add the scenario values in the XML structure root = Element('scenario') new_node('id', scenario_xml_id), new_node('active', str(scenario.active)), new_node('sequence', str(scenario.sequence)), new_node('name', scenario.name), new_node('type', scenario.type), new_node('notes', scenario.notes or ''), if scenario.model_id: new_node('model_id', scenario.model_id.model), if scenario.company_id: new_node('company_id', scenario.company_id.name), if scenario.parent_id: parent_value = scenario.parent_id.get_metadata()[0]['xmlid']
def parameter(dico, resource, special=None): """ Convert value to a parameter for SOAP query @type dico: dict @param dico: Contain parameter starts with OERP_ @type resource: dict @param resource: Contain parameter starts with WIZARD_ @rtype: xmlstring @return: XML String representation """ res = '' for key in resource: _logger.debug(' PARAMETER -> RESOURCE: %s' % key) if key in 'xml_data': continue e = Element('parameter') e.set('name', 'OERP_%s' % key.upper()) e.text = ustr(resource[key]) res += tostring(e) + '\n' for key in dico: _logger.debug(' PARAMETER -> DICO: %s' % key) if key in 'params': continue val = dico[key] e = Element('parameter') e.set('name', 'WIZARD_%s' % key.upper()) if isinstance(val, list): if isinstance(val[0], tuple): e.text = ','.join(map(str, val[0][2])) else: e.text = ','.join(map(str, val)) else: e.text = val and ustr(val) or '' res += tostring(e) + '\n' # Duplicate WIZARD parameters with prefix OERP e = Element('parameter') e.set('name', 'OERP_%s' % key.upper()) if isinstance(val, list): if isinstance(val[0], tuple): e.text = ','.join(map(str, val[0][2])) else: e.text = ','.join(map(str, val)) else: e.text = val and ustr(val) or '' res += tostring(e) + '\n' if special is None: special = {} for key in special: _logger.debug(' PARAMETER -> SPECIAL: %s' % key) e = Element('parameter') e.set('name', key) e.text = ustr(special[key]) res += tostring(e) + '\n' res = entities(res) if resource.get('xml_data'): res += '<parameter class="java.lang.String"' \ 'name="XML_DATA">' res += '<![CDATA["%s"]]></parameter>' % \ resource['xml_data'] return res
def TransformDataset(anno_path='/media/lih/LiH/LRDataset/annotations', img_path='/media/lih/LiH/LRDataset/images', anno_save_path='/media/lih/LiH/LRDataset/lrannotations', img_save_path='/media/lih/LiH/LRDataset/lrimages'): ''' :param anno_path: annotations path :param img_path: images path :param anno_save_path: anmnotations save path, chang xml params to match resized imgs :param img_save_path: images save path, resized image to [0.5h 0.5w] :return: ''' anno_name = [] for root, subdirs, files in os.walk(anno_path): for filepath in files: anno_name.append(filepath) img_name = [] for root, subdirs, files in os.walk(img_path): for filepath in files: img_name.append(filepath) # read img, resize and save for tmp in img_name: curr_img = cv2.imread(osp.join(img_path, tmp)) print("Process {}".format(tmp)) assert curr_img.shape == (720, 1280, 3) curr_img = cv2.resize(curr_img, (640, 360)) # cv2.imshow('debug', curr_img) # cv2.waitKey(1) assert curr_img.shape == (360, 640, 3) cv2.imwrite(osp.join(img_save_path, tmp), curr_img) # process xml for tmp in anno_name: xml_f = osp.join(anno_path, tmp) tree = parse(xml_f) node_root = Element('annotation') node_folder = SubElement(node_root, 'folder') node_folder.text = 'images' node_filename = SubElement(node_root, 'filename') node_filename.text = tree.xpath('//filename/text()')[0] node_path = SubElement(node_root, 'path') node_path.text = tree.xpath('//path/text()')[0] node_source = SubElement(node_root, 'source') node_database = SubElement(node_source, 'database') node_database.text = 'Unknown' node_size = SubElement(node_root, 'size') node_width = SubElement(node_size, 'width') node_width.text = str(int(int(tree.xpath('//width/text()')[0]) / 2)) node_height = SubElement(node_size, 'height') node_height.text = str(int(int(tree.xpath('//height/text()')[0]) / 2)) node_depth = SubElement(node_size, 'depth') node_depth.text = tree.xpath('//depth/text()')[0] node_segmented = SubElement(node_root, 'segmented') node_segmented.text = '0' for num in range(len(tree.xpath('//name/text()'))): xmin = tree.xpath('//xmin/text()') ymin = tree.xpath('//ymin/text()') xmax = tree.xpath('//xmax/text()') ymax = tree.xpath('//ymax/text()') node_object = SubElement(node_root, 'object') node_name = SubElement(node_object, 'name') node_name.text = 'marker' node_pose = SubElement(node_object, 'pose') node_pose.text = 'Unspecified' node_truncated = SubElement(node_object, 'truncated') node_truncated.text = '0' node_difficult = SubElement(node_object, 'difficult') node_difficult.text = '0' node_bndbox = SubElement(node_object, 'bndbox') node_xmin = SubElement(node_bndbox, 'xmin') node_xmin.text = str(ceil(0.5 * int(xmin[num]))) node_ymin = SubElement(node_bndbox, 'ymin') node_ymin.text = str(ceil(0.5 * int(ymin[num]))) node_xmax = SubElement(node_bndbox, 'xmax') node_xmax.text = str(ceil(0.5 * int(xmax[num]))) node_ymax = SubElement(node_bndbox, 'ymax') node_ymax.text = str(ceil(0.5 * int(ymax[num]))) xml = tostring(node_root, pretty_print=True) # 格式化显示,该换行的换行 xml_file_path = os.path.join(anno_save_path, tmp) print(xml_file_path) with open(xml_file_path, 'wb') as file: file.write(xml)
def test_parse_xml(self): expected = Element("test", {"foo": "bar"}) actual = parse_xml("""<test foo="bar"/>""", ignore_ns=True) self.assertEqual(expected.tag, actual.tag) self.assertEqual(expected.attrib, actual.attrib)
def rpc(self, rpc): return exec_rpc(self.module, tostring(Element(rpc)))
def zeroize(ele): return send_request(ele, Element('request-system-zeroize'))
from lxml.etree import Element, SubElement, ElementTree # Testprogram container RavenRevA = Element('Testprogram', name='RavenRevA') # Tests and Flows RavenTests = SubElement(RavenRevA, 'TestRef', name='RavenTests') Gpio18_PinCont = SubElement(RavenTests, 'TestFlow', name='Gpio18_PinCont') Gpio18_Opens = SubElement(Gpio18_PinCont, 'Test', name='Gpio18_Opens', testtype='BASIC', pinref='GpioPins', force='100e-6', lolim='200e-3', hilim='1.5', delay='2e-3') Gpio18_Shorts = SubElement(Gpio18_PinCont, 'Test', name='Gpio18_Shorts', testtype='BASIC', pinref='JtagPins', force='100e-6', lolim='200e-3', hilim='1.5', delay='2e-3') # Pins and Pingroups RavenPins = SubElement(RavenRevA, 'PinRef', name='RavenPins') # Gpio18_Opens_Pins = SubElement(RavenPins, 'PinGroup', name='Gpio18_Opens_Pins', pintype='IO') GpioPins = SubElement(RavenPins, 'PinGroup', name='GpioPins', pintype='IO') SubElement(GpioPins, 'Pin', name='PWROK') SubElement(GpioPins, 'Pin', name='RST_L')
def _generate_xml(self): """ This were all is put together into an xml. """ namespaces = { # Default None: 'urn:iso:std:iso:20022:tech:xsd:pain.001.001.03', 'xsi': 'http://www.w3.org/2001/XMLSchema-instance' } document = Element('Document', nsmap=namespaces) if self._type is 'CT': main = SubElement(document, 'CstmrCdtTrfInitn') elif self._type is 'DD': main = SubElement(document, 'CstmrDrctDbtInitn') else: raise Exception('Unknown SepaDocument type, or type not set.') # Group Header grp_hdr = SubElement(main, 'GrpHdr') SubElement(grp_hdr, 'MsgId').text = str(self.message_identification) SubElement(grp_hdr, 'CreDtTm').text = datetime.strftime(datetime.now(), '%Y-%m-%dT%H:%I:%S') SubElement(grp_hdr, 'NbOfTxs').text = str(len(self._credit_transfers)) SubElement(grp_hdr, 'CtrlSum').text = str(self._header_control_sum) assert self.initiating_party initg_pty = SubElement(grp_hdr, 'InitgPty') SubElement(initg_pty, 'Nm').text = self.initiating_party.name # Credit Transfer Transactions Information # Rabobank wants only one transaction per payment info so we create multiple payment infos here. for transfer in self._credit_transfers: # PaymentInformation pmt_inf = SubElement(main, 'PmtInf') # PaymentInformationIdentification # PmtInfId SubElement(pmt_inf, 'PmtInfId').text = transfer.creditor_payment_id if self.category_purpose_code: cd = SubElement(pmt_inf, 'Cd') SubElement(cd, 'CtgyPurp').text = self.category_purpose_code # PaymentMethod SubElement(pmt_inf, 'PmtMtd').text = self._payment_method # BatchBooking [optional] # BtchBookg # NumberofTransactions SubElement(pmt_inf, 'NbOfTxs').text = "1" # ControlSum [optional] # CtrlSum # PaymentTypeInformation pmt_tp_inf = SubElement(pmt_inf, 'PmtTpInf') # InstructionPriority [optional] # InstrPrty # ServiceLevel svc_lvl = SubElement(pmt_tp_inf, 'SvcLvl') # Code SubElement(svc_lvl, 'Cd').text = 'SEPA' if self._local_instrument_code: # LocalInstrument lcl_instr = SubElement(pmt_inf, 'LclInstr') # Code SubElement(lcl_instr, 'Cd').text = self._local_instrument_code # Proprietary [otional] # Prtry # CategoryPurpose [optional # CtgyPurp # # - Cd Code # - Prtry Proprietary # RequestedExecutionDate SubElement(pmt_inf, 'ReqdExctnDt').text = datetime.strftime( datetime.now(), '%Y-%m-%d') # Debtor dbtr = SubElement(pmt_inf, 'Dbtr') # Name SubElement(dbtr, 'Nm').text = self.debtor.name # PostalAddress [optional] # PstlAdr # # - Country [optional] # - Ctry # # - AddressLine [optional] # - AdrLine # Identification [optional] # Id # DebtorAccount dbtr_acct = SubElement(pmt_inf, 'DbtrAcct') # Identification dbtr_id = SubElement(dbtr_acct, 'Id') # IBAN SubElement(dbtr_id, 'IBAN').text = self.debtor.iban # Currency SubElement(dbtr_acct, 'Ccy').text = self.currency # DebtorAgent dbtr_agt = SubElement(pmt_inf, 'DbtrAgt') # FinancialInstitutionIdentification fin_isnstn_id = SubElement(dbtr_agt, 'FinInstnId') # BIC SubElement(fin_isnstn_id, 'BIC').text = self.debtor.bic # UltimateDebtor [optional] # UltmtDbtr # - Name # - Nm # # - Identification # - Id # ChargeBearer SubElement(pmt_inf, 'ChrgBr').text = 'SLEV' # CTTransactionInformation cd_trf_tx_inf = SubElement(pmt_inf, 'CdtTrfTxInf') # PaymentIdentification pmt_id = SubElement(cd_trf_tx_inf, 'PmtId') # InstructionIdentification # InstrId [optional] # End to End Identification SubElement(pmt_id, 'EndToEndId').text = transfer.end_to_end_id # PaymentTypeInformation [optional] # PmtTpInf # ServiceLevel # SvcLvl [optional] # # - Code # - Cd # LocalInstrument [optional] # LclInstrm # # - Code # - Cd # # - Proprietary # - Prtry # CategoryPurpose [optional] # CtgyPurp # # - Code # - Cd # Amount amt = SubElement(cd_trf_tx_inf, 'Amt') # InstructedAmount instd_amt = SubElement(amt, 'InstdAmt', {'Ccy': transfer.currency}) instd_amt.text = str(transfer.amount) # Charge Bearer [optional] # ChrgBr # UltimateDebtor [optional] # UltmtDbtr # - Name # - Nm # # - Identification # - Id # Creditor Agent cdtr_agt = SubElement(cd_trf_tx_inf, 'CdtrAgt') # FinancialInstitutionIdentification fin_inst_id = SubElement(cdtr_agt, 'FinInstnId') # BIC bic = SubElement(fin_inst_id, 'BIC') bic.text = transfer.creditor.bic # Creditor cdrt = SubElement(cd_trf_tx_inf, 'Cdtr') # Name SubElement(cdrt, 'Nm').text = transfer.creditor.name # PostalAddress [optional] # PstlAdr # # - Country [optional] # - Ctry # # - AddressLine [optional] # - AdrLine # Identification [optional] # Id # Creditor Account cdtr_acct = SubElement(cd_trf_tx_inf, 'CdtrAcct') # Id cdtr_id = SubElement(cdtr_acct, 'Id') # IBAN SubElement(cdtr_id, 'IBAN').text = transfer.creditor.iban # Currency [optional] # Ccy # Name [optional] # Nm # UltimateDebtor [optional] # UltmtDbtr # - Name # - Nm # # - Identification # - Id # Purpose [optional] # Purp # # - Code # - Cd # RemittanceInformation rmt_inf = SubElement(cd_trf_tx_inf, 'RmtInf') # Unstructured if transfer.remittance_information: SubElement(rmt_inf, 'Ustrd').text = transfer.remittance_information # Structured (optional) # # - CreditorReferenceInformation (optional) # # - - Type # - - Tp # # - - - CodeOrProprietary # - - - CdOrPrtry # - - - - Code # - - - Issuer # - - Reference return document
line = text_lines[i] box = [int(line[0]), int(line[1]), int(line[2]), int(line[3])] cropImage = inputImage.crop((box[0],box[1], box[2], box[3])) cropImage.save('../textResults/'+'text'+ str(i) + name[index:]) eng.GetMC('../textResults/'+'text'+ str(i)+ name[index:],'../textResults/'+'saliency'+ str(i) + name[index:]) i = len(text_lines)-1 #字体边界refine #text_lines = extraction_function.text_segmentation('../textResults/'+name[0:index],name[index:], i,text_lines) #分析文字色彩,找到tag,action等信息 text_difColor,text_lines = extraction_function.color_analysis('../textResults/',name[index:], i,text_lines) backgroundMask = extraction_function.backgroundMask(imgSize.shape[1],imgSize.shape[0],text_difColor,text_lines,masks,className,name[0:index],name[index:]) ''' node_root = Element('annotation') node_folder = SubElement(node_root, 'folder') node_folder.text = 'GTSDB' node_filename = SubElement(node_root, 'filename') node_filename.text = name node_size = SubElement(node_root, 'size') node_width = SubElement(node_size, 'width') node_width.text = str(imgSize.shape[1]) node_height = SubElement(node_size, 'height') node_height.text = str(imgSize.shape[0]) node_depth = SubElement(node_size, 'depth')
def writexml(fname, objects, savepath): name, posetfix = fname.split('.') node_root = Element('annotation') node_folder = SubElement(node_root, 'folder') node_folder.text = 'VOC2007' node_filename = SubElement(node_root, 'filename') #node_filename.text = '010001.jpg' node_filename.text = name + '.jpg' node_source = SubElement(node_root, 'source') node_database = SubElement(node_source, 'database') node_database.text = 'ISLAB' node_anno = SubElement(node_source, 'annotation') node_anno.text = 'VOC2007' node_image = SubElement(node_source, 'image') node_image.text = 'flickr' node_fli = SubElement(node_source, 'flickrid') node_fli.text = 'NULL' node_owner = SubElement(node_root, 'owner') node_fli2 = SubElement(node_owner, 'flickrid') node_fli2.text = 'NULL' node_name = SubElement(node_owner, 'name') node_name.text = 'zhanghan' node_size = SubElement(node_root, 'size') node_width = SubElement(node_size, 'width') node_height = SubElement(node_size, 'height') node_depth = SubElement(node_size, 'depth') node_width.text = '640' node_height.text = '360' node_depth.text = '3' node_object = SubElement(node_root, 'object') for i in range(len(objects)): node_name = SubElement(node_object, 'name') node_name.text = 'person' node_pose = SubElement(node_object, 'pose') node_pose.text = 'Unspecified' node_truncated = SubElement(node_object, 'truncated') node_truncated.text = '0' node_diff = SubElement(node_object, 'difficult') node_diff.text = '0' node_bbox = SubElement(node_object, 'bndbox') node_xmin = SubElement(node_bbox, 'xmin') node_ymin = SubElement(node_bbox, 'ymin') node_xmax = SubElement(node_bbox, 'xmax') node_ymax = SubElement(node_bbox, 'ymax') node_xmin.text = objects[i][3] node_ymin.text = objects[i][4] node_xmax.text = objects[i][5] node_ymax.text = objects[i][6] tree = ElementTree(node_root) #tree.write('010001.xml',pretty_print=True,xml_declaration=False) tree.write(savepath + name + '.xml', pretty_print=True, xml_declaration=False)
def create_xml(output_path, filename, path, size, obj_info): utils.io_utils.mkdir(output_path) node_root = Element('annotation') node_folder = SubElement(node_root, 'folder') node_folder.text = 'JPEGImages' node_filename = SubElement(node_root, 'filename') node_filename.text = filename + ".jpg" node_path = SubElement(node_root, 'path') node_path.text = path node_size = SubElement(node_root, 'size') node_width = SubElement(node_size, 'width') node_width.text = str(size[0]) node_height = SubElement(node_size, 'height') node_height.text = str(size[1]) node_depth = SubElement(node_size, 'depth') node_depth.text = str(3) node_segmented = SubElement(node_root, 'segmented') node_segmented.text = '0' for k in list(obj_info.keys()): node_object = SubElement(node_root, 'object') node_name = SubElement(node_object, 'name') caption = "{}".format(k) node_name.text = caption node_pose = SubElement(node_object, 'pose') node_pose.text = 'Unspecified' node_truncated = SubElement(node_object, 'truncated') node_truncated.text = '0' node_difficult = SubElement(node_object, 'difficult') node_difficult.text = '0' #check_border(b, Image_info.im_info.width, Image_info.im_info.height) node_bndbox = SubElement(node_object, 'bndbox') node_xmin = SubElement(node_bndbox, 'xmin') node_xmin.text = str(obj_info[k][0]) node_ymin = SubElement(node_bndbox, 'ymin') node_ymin.text = str(obj_info[k][1]) node_xmax = SubElement(node_bndbox, 'xmax') node_xmax.text = str(obj_info[k][2]) node_ymax = SubElement(node_bndbox, 'ymax') node_ymax.text = str(obj_info[k][3]) xml = tostring(node_root, pretty_print=True) dom = parseString(xml) xml_path = os.path.join(output_path, filename + '.xml') print(xml_path) with open(xml_path, 'w+') as f: dom.writexml(f, addindent='', newl='', encoding='utf-8')
def save_citygml(self): fn = str( pg.QtGui.QFileDialog.getSaveFileName(self, "Save State..", "untitled.gml", "Citygml (*.gml)")) if fn == '': return shptype = self.objectGroup.param('Shapefile').param("Type").value() filename = self.objectGroup.param('Shapefile').param( "Shapefile Loaded").value() schemaLocation = "http://www.opengis.net/citygml/landuse/1.0\ http://schemas.opengis.net/citygml/landuse/1.0/landUse.xsd http://www.opengis.net/citygml/cityfurniture/1.0\ http://schemas.opengis.net/citygml/cityfurniture/1.0/cityFurniture.xsd http://www.opengis.net/citygml/appearance/1.0\ http://schemas.opengis.net/citygml/appearance/1.0/appearance.xsd http://www.opengis.net/citygml/texturedsurface/1.0\ http://schemas.opengis.net/citygml/texturedsurface/1.0/texturedSurface.xsd http://www.opengis.net/citygml/transportation/1.0\ http://schemas.opengis.net/citygml/transportation/1.0/transportation.xsd http://www.opengis.net/citygml/waterbody/1.0\ http://schemas.opengis.net/citygml/waterbody/1.0/waterBody.xsd http://www.opengis.net/citygml/building/1.0\ http://schemas.opengis.net/citygml/building/1.0/building.xsd http://www.opengis.net/citygml/relief/1.0\ http://schemas.opengis.net/citygml/relief/1.0/relief.xsd http://www.opengis.net/citygml/vegetation/1.0\ http://schemas.opengis.net/citygml/vegetation/1.0/vegetation.xsd http://www.opengis.net/citygml/cityobjectgroup/1.0\ http://schemas.opengis.net/citygml/cityobjectgroup/1.0/cityObjectGroup.xsd http://www.opengis.net/citygml/generics/1.0\ http://schemas.opengis.net/citygml/generics/1.0/generics.xsd" if shptype == 'Plots': root = Element("CityModel", attrib={ "{" + XMLNamespaces.xsi + "}schemaLocation": schemaLocation }, nsmap={ None: "http://www.opengis.net/citygml/1.0", 'xsi': XMLNamespaces.xsi, 'trans': XMLNamespaces.trans, 'wtr': XMLNamespaces.wtr, 'gml': XMLNamespaces.gml, 'smil20lang': XMLNamespaces.smil20lang, 'xlink': XMLNamespaces.xlink, 'grp': XMLNamespaces.grp, 'luse': XMLNamespaces.luse, 'frn': XMLNamespaces.frn, 'app': XMLNamespaces.app, 'tex': XMLNamespaces.tex, 'smil20': XMLNamespaces.smil20, 'xAL': XMLNamespaces.xAL, 'bldg': XMLNamespaces.bldg, 'dem': XMLNamespaces.dem, 'veg': XMLNamespaces.veg, 'gen': XMLNamespaces.gen }) et = ElementTree(root) cityObjectMember = SubElement(root, 'cityObjectMember') sf = shapefile.Reader(filename) shapeRecs = sf.shapeRecords() shapetype = shapeRecs[0].shape.shapeType shapeRecs = sf.shapeRecords() shapetype = shapeRecs[0].shape.shapeType #shapetype 3 is polyline, shapetype 5 is polygon count_shapes = 0 for rec in shapeRecs: shape = rec.shape if shapetype == 5: #A ring is a connected sequence of four or more #points that form a closed, non-self-intersecting loop #The rings of a polygon are referred to as its parts parts = shape.parts num_parts = len(parts) points = shape.points luse = SubElement( cityObjectMember, "{" + XMLNamespaces.luse + "}" + 'LandUse') luse.attrib["{" + XMLNamespaces.gml + "}" + 'id'] = 'plot' + str(count_shapes) gml_name = SubElement( luse, "{" + XMLNamespaces.gml + "}" + 'name') gml_name.text = 'simpleplot' + str(count_shapes) gml_boundedBy = SubElement( luse, "{" + XMLNamespaces.gml + "}" + 'boundedBy') gml_Envelope = SubElement( gml_boundedBy, "{" + XMLNamespaces.gml + "}" + 'Envelope') #gml_Envelope.attrib['srsDimension'] = '3' #gml_Envelope.attrib['srsName'] = 'urn:ogc:def:crs,crs:EPSG:6.12:3068,crs:EPSG:6.12:5783' gml_lowerCorner = SubElement( gml_Envelope, "{" + XMLNamespaces.gml + "}" + 'lowerCorner') gml_upperCorner = SubElement( gml_Envelope, "{" + XMLNamespaces.gml + "}" + 'upperCorner') creationDate = SubElement(luse, 'creationDate') creationDate.text = str(datetime.datetime.now()) luse_lod1MultiSurface = SubElement( luse, "{" + XMLNamespaces.luse + "}" + 'lod1MultiSurface') gml_MultiSurface = SubElement( luse_lod1MultiSurface, "{" + XMLNamespaces.gml + "}" + 'MultiSurface') gml_MultiSurface.attrib["{" + XMLNamespaces.gml + "}" + 'id'] = 'UUID_' + str(uuid.uuid1()) #======================================================================================================= #attrib of each land parcel #======================================================================================================= poly_attribs = rec.record plot_ratio = poly_attribs[1] landusefunction = poly_attribs[2] luse_function = SubElement( luse, "{" + XMLNamespaces.luse + "}" + 'function') if landusefunction == "residential": luse_function.text = "1010" else: luse_function.text = "1020" gen_plot_ratio = SubElement( luse, "{" + XMLNamespaces.gen + "}" + 'doubleAttribute') gen_plot_ratio.attrib['name'] = 'plot ratio' gen_value = SubElement( gen_plot_ratio, "{" + XMLNamespaces.gen + "}" + 'value') gen_value.text = str(plot_ratio) #======================================================================================================= #part is a surface in each landuse #======================================================================================================= count_parts = 0 for part in parts: part_s = parts[count_parts] if count_parts == num_parts - 1: part_e = len(points) else: part_e = parts[count_parts + 1] part_points = points[part_s:part_e] #======================================================================================================= gml_surfaceMember = SubElement( gml_MultiSurface, "{" + XMLNamespaces.gml + "}" + 'surfaceMember') gml_Polygon = SubElement( gml_surfaceMember, "{" + XMLNamespaces.gml + "}" + 'Polygon') gml_Polygon.attrib["{" + XMLNamespaces.gml + "}" + 'id'] = 'UUID_' + str(uuid.uuid1()) gml_exterior = SubElement( gml_Polygon, "{" + XMLNamespaces.gml + "}" + 'exterior') gml_LinearRing = SubElement( gml_exterior, "{" + XMLNamespaces.gml + "}" + 'LinearRing') gml_LinearRing.attrib["{" + XMLNamespaces.gml + "}" + 'id'] = 'UUID_' + str( uuid.uuid1()) gml_posList = SubElement( gml_LinearRing, "{" + XMLNamespaces.gml + "}" + 'posList') gml_posList.attrib['srsDimension'] = '3' position = "" count_points = 0 for point in part_points: if count_points == 0: position = position + str( point[0]) + " " + str(point[1]) + " 0" else: position = position + " " + str( point[0]) + " " + str(point[1]) + " 0" count_points += 1 gml_posList.text = position count_parts += 1 #======================================================================================================= count_shapes += 1 outFile = open(fn, 'w') et.write(outFile, pretty_print=True, xml_declaration=True, encoding="UTF-8", standalone="yes") outFile.close() print "done"
colorQuant = 128 csd = calcCSD.compCSD(img, colorQuant) csdFlag = False for descriptor in doc.iterfind( 'Description/MultimediaContent/Image/VisualDescriptor'): Dtype = descriptor.get( '{http://www.w3.org/2001/XMLSchema-instance}type') if Dtype == 'ColorStructureType': descriptor.set('colorQuant', colorQuantDic[str(colorQuant)]) HValues = descriptor.find('Values') HValues.text = " ".join(str(x) for x in np.uint8(csd)) csdFlag = True if not csdFlag: imageLevel = doc.find('Description/MultimediaContent/Image') newVisualDescriptor = Element('VisualDescriptor') newVisualDescriptor.set( '{http://www.w3.org/2001/XMLSchema-instance}type', 'ColorStructureType') newVisualDescriptor.set('colorQuant', colorQuantDic[str(colorQuant)]) newVal = Element('Values') newVal.text = " ".join(str(x) for x in np.uint8(csd)) newVisualDescriptor.append(newVal) imageLevel.append(newVisualDescriptor) fig1 = plt.figure() plt.bar(range(colorQuant), csd) plt.show() doc.write(mpeg7FileDir + imageFileName + '.mpeg7.xml')
def main(): """ main entry point for module execution """ neighbors_spec = dict(host=dict(), port=dict()) element_spec = dict( name=dict(), description=dict(), enabled=dict(default=True, type="bool"), speed=dict(type="str"), mtu=dict(type="int"), duplex=dict(choices=["full", "half", "auto"]), tx_rate=dict(), rx_rate=dict(), neighbors=dict(type="list", elements="dict", options=neighbors_spec), delay=dict(default=10, type="int"), state=dict(default="present", choices=["present", "absent", "up", "down"]), active=dict(default=True, type="bool"), ) aggregate_spec = deepcopy(element_spec) aggregate_spec["name"] = dict(required=True) # remove default in aggregate spec, to handle common arguments remove_default_spec(aggregate_spec) argument_spec = dict( aggregate=dict(type="list", elements="dict", options=aggregate_spec)) argument_spec.update(element_spec) argument_spec.update(junos_argument_spec) required_one_of = [["name", "aggregate"]] mutually_exclusive = [["name", "aggregate"]] module = AnsibleModule( argument_spec=argument_spec, required_one_of=required_one_of, mutually_exclusive=mutually_exclusive, supports_check_mode=True, ) warnings = list() result = {"changed": False} if warnings: result["warnings"] = warnings top = "interfaces/interface" param_to_xpath_map = collections.OrderedDict() param_to_xpath_map.update([ ("name", { "xpath": "name", "is_key": True }), ("description", "description"), ("speed", "speed"), ("mtu", "mtu"), ("duplex", "link-mode"), ("disable", { "xpath": "disable", "tag_only": True }), ]) choice_to_value_map = { "link-mode": { "full": "full-duplex", "half": "half-duplex", "auto": "automatic", } } params = to_param_list(module) requests = list() for param in params: # if key doesn't exist in the item, get it from module.params for key in param: if param.get(key) is None: param[key] = module.params[key] item = param.copy() state = item.get("state") item["disable"] = True if not item.get("enabled") else False if state in ("present", "up", "down"): item["state"] = "present" validate_param_values(module, param_to_xpath_map, param=item) want = map_params_to_obj(module, param_to_xpath_map, param=item) requests.append( map_obj_to_ele(module, want, top, value_map=choice_to_value_map, param=item)) diff = None with locked_config(module): for req in requests: diff = load_config(module, tostring(req), warnings, action="merge") # issue commit after last configuration change is done commit = not module.check_mode if diff: if commit: commit_configuration(module) else: discard_changes(module) result["changed"] = True if module._diff: result["diff"] = {"prepared": diff} failed_conditions = [] neighbors = None for item in params: state = item.get("state") tx_rate = item.get("tx_rate") rx_rate = item.get("rx_rate") want_neighbors = item.get("neighbors") if (state not in ("up", "down") and tx_rate is None and rx_rate is None and want_neighbors is None): continue element = Element("get-interface-information") intf_name = SubElement(element, "interface-name") intf_name.text = item.get("name") if result["changed"]: sleep(item.get("delay")) reply = exec_rpc(module, tostring(element), ignore_warning=False) if state in ("up", "down"): admin_status = reply.xpath( "interface-information/physical-interface/admin-status") if not admin_status or not conditional( state, admin_status[0].text.strip()): failed_conditions.append("state " + "eq(%s)" % state) if tx_rate: output_bps = reply.xpath( "interface-information/physical-interface/traffic-statistics/output-bps" ) if not output_bps or not conditional( tx_rate, output_bps[0].text.strip(), cast=int): failed_conditions.append("tx_rate " + tx_rate) if rx_rate: input_bps = reply.xpath( "interface-information/physical-interface/traffic-statistics/input-bps" ) if not input_bps or not conditional( rx_rate, input_bps[0].text.strip(), cast=int): failed_conditions.append("rx_rate " + rx_rate) if want_neighbors: if neighbors is None: element = Element("get-lldp-interface-neighbors") intf_name = SubElement(element, "interface-device") intf_name.text = item.get("name") reply = exec_rpc(module, tostring(element), ignore_warning=False) have_host = [ item.text for item in reply.xpath( "lldp-neighbors-information/lldp-neighbor-information/lldp-remote-system-name" ) ] have_port = [ item.text for item in reply.xpath( "lldp-neighbors-information/lldp-neighbor-information/lldp-remote-port-id" ) ] for neighbor in want_neighbors: host = neighbor.get("host") port = neighbor.get("port") if host and host not in have_host: failed_conditions.append("host " + host) if port and port not in have_port: failed_conditions.append("port " + port) if failed_conditions: msg = "One or more conditional statements have not been satisfied" module.fail_json(msg=msg, failed_conditions=failed_conditions) module.exit_json(**result)
0: (6.5, -1.0), 1: (-4.0, 1.5), 2: (3, -0.5), 3: (2.5, 0.5) } targetPosition = { 0: (9.0, -2.0), 1: (-1.0, 1.5), 2: (-2.5, -0.5), 3: (-3, 0.5) } speedOfActor = {0: 0.6, 1: 0.9, 2: 1.1, 3: 1.2} actor_list = [] for item in range(int(actor_number)): actor = Element("actor", name="actor" + str(item)) pose = Element("pose") #randomly generate position to pose text x = str(startingPosition[item][0]) y = str(startingPosition[item][1]) pose.text = x + " " + y + " " + "1.02 0 0 0" actor.append(pose) skin = Element("skin") skin_fn = Element("filename") skin_fn.text = random.choice(skin_list) skin_scale = Element("scale") skin_scale.text = "1" skin.append(skin_fn) skin.append(skin_scale)
def rpc(self, rpc): return send_request(self.module, Element(rpc))
def map_obj_to_ele(module, want, top, value_map=None, param=None): if not HAS_LXML: module.fail_json(msg='lxml is not installed.') if not param: param = module.params root = Element('root') top_ele = top.split('/') ele = SubElement(root, top_ele[0]) if len(top_ele) > 1: for item in top_ele[1:-1]: ele = SubElement(ele, item) container = ele state = param.get('state') active = param.get('active') if active: oper = 'active' else: oper = 'inactive' # build xml subtree if container.tag != top_ele[-1]: node = SubElement(container, top_ele[-1]) else: node = container for fxpath, attributes in want.items(): for attr in attributes: tag_only = attr.get('tag_only', False) leaf_only = attr.get('leaf_only', False) value_req = attr.get('value_req', False) is_key = attr.get('is_key', False) parent_attrib = attr.get('parent_attrib', True) value = attr.get('value') field_top = attr.get('top') # operation 'delete' is added as element attribute # only if it is key or leaf only node if state == 'absent' and not (is_key or leaf_only): continue # convert param value to device specific value if value_map and fxpath in value_map: value = value_map[fxpath].get(value) if (value is not None) or tag_only or leaf_only: ele = node if field_top: # eg: top = 'system/syslog/file' # field_top = 'system/syslog/file/contents' # <file> # <name>test</name> # <contents> # </contents> # </file> ele_list = root.xpath(top + '/' + field_top) if not len(ele_list): fields = field_top.split('/') ele = node for item in fields: inner_ele = root.xpath(top + '/' + item) if len(inner_ele): ele = inner_ele[0] else: ele = SubElement(ele, item) else: ele = ele_list[0] if value is not None and not isinstance(value, bool): value = to_text(value, errors='surrogate_then_replace') if fxpath: tags = fxpath.split('/') for item in tags: ele = SubElement(ele, item) if tag_only: if state == 'present': if not value: # if value of tag_only node is false, delete the node ele.set('delete', 'delete') elif leaf_only: if state == 'present': ele.set(oper, oper) ele.text = value else: ele.set('delete', 'delete') # Add value of leaf node if required while deleting. # in some cases if value is present while deleting, it # can result in error, hence the check if value_req: ele.text = value if is_key: par = ele.getparent() par.set('delete', 'delete') else: ele.text = value par = ele.getparent() if parent_attrib: if state == 'present': # set replace attribute at parent node if not par.attrib.get('replace'): par.set('replace', 'replace') # set active/inactive at parent node if not par.attrib.get(oper): par.set(oper, oper) else: par.set('delete', 'delete') return root.getchildren()[0]
def to_etree(self, data, options=None, name=None, depth=0): """ Given some data, converts that data to an ``etree.Element`` suitable for use in the XML output. """ if isinstance(data, (list, tuple)): new_name = None if name: element = Element(name) new_name = self.objNames.get(name) if self.objNames else name else: element = Element('objects') for item in data: element.append( self.to_etree(item, options, name=new_name, depth=depth + 1)) element[:] = sorted(element, key=lambda x: x.tag) elif isinstance(data, dict): if depth == 0: element = Element(name or 'response') else: element = Element(name or self.objName) for (key, value) in data.items(): element.append( self.to_etree(value, options, name=key, depth=depth + 1)) element[:] = sorted(element, key=lambda x: x.tag) elif isinstance(data, Bundle): element = Element(name or self.objName) for field_name, field_object in data.data.items(): element.append( self.to_etree(field_object, options, name=field_name, depth=depth + 1)) element[:] = sorted(element, key=lambda x: x.tag) elif hasattr(data, 'dehydrated_type'): if getattr(data, 'dehydrated_type', None) == 'related' and data.is_m2m == False: if data.full: return self.to_etree(data.fk_resource, options, name, depth + 1) else: return self.to_etree(data.value, options, name, depth + 1) elif getattr(data, 'dehydrated_type', None) == 'related' and data.is_m2m == True: if data.full: element = Element(name or 'objects') for bundle in data.m2m_bundles: element.append( self.to_etree(bundle, options, bundle.resource_name, depth + 1)) else: element = Element(name or 'objects') for value in data.value: element.append( self.to_etree(value, options, name, depth=depth + 1)) else: return self.to_etree(data.value, options, name) else: element = Element(name or 'value') simple_data = self.to_simple(data, options) if simple_data: try: element.text = unicode(simple_data) except ValueError as e: self.log.error('Not valid XML character detected in ', exc_info=True, extra={ 'data': simple_data, 'original_exception': e }) cleaned_string = ''.join(c for c in simple_data if valid_xml_char_ordinal(c)) element.text = unicode(cleaned_string) return element
if i == 10: break # create rss rss = PyRSS2Gen.RSS2( title='GeoAdmin - RSS Feed', link=api_url + 'releasenotes/', description= "The latest news about GeoAdmin application's changes, new and updated data available on map.geo.admin.ch", lastBuildDate=time.strftime('%a, %d %b %Y %H:%M:%S %z'), items=items) # Make the feed validate (https://validator.w3.org/feed/check.cgi?) rss = rss.to_xml('utf-8') root = etree.fromstring(rss) new_root = Element('rss', nsmap={'atom': XMLNamespaces.atom}) new_root.attrib['version'] = '2.0' channel = root[0] atom_link = Element(QName(XMLNamespaces.atom, 'link')) # Ugly hack to force closing open/close tag style atom_link.text = '' atom_link.attrib['type'] = "application/rss+xml" atom_link.attrib['rel'] = "self" atom_link.attrib['href'] = api_url + 'releasenotes/rss2.xml' link = channel.find("link") link.addnext(atom_link) new_root.append(channel) with open('chsdi/static/doc/build/releasenotes/rss2.xml', 'w') as xml: xml.write(etree.tostring(new_root, pretty_print=True))
def convert(src, outmml, outconfig, opts): if os.path.exists(src): # local file # using 'file:' enables support on win32 # for opening local files with urllib.urlopen # Note: this must only be used with abs paths to local files # otherwise urllib will think they are absolute, # therefore in the future it will likely be # wiser to just open local files with open() if os.path.isabs(src) and sys.platform == "win32": src = 'file:%s' % src doc = ElementTree.parse(urllib.urlopen(src)) map = doc.getroot() defaults = standard_projections sources = {} all_srs = dict([(v, k) for k, v in standard_projections.items()]) name_filter = re.compile("\W") for layer in map.findall("Layer"): if not opts.extract_all and layer.attrib.get('status', "on").lower() == "off": map.remove(layer) continue srs = layer.attrib['srs'] srs_name = all_srs.get(srs) if not srs_name: srs_name = "srs%d" % len(all_srs) defaults[srs_name] = srs all_srs[srs] = srs_name id = layer.attrib.get('id') classes = layer.attrib.get('class') keys = [] if id: keys.append("%s_" % id) if classes: keys.extend(classes.split(" ")) ds_name = name_filter.sub("_", " ".join(keys)) params = {} for param in layer.find("Datasource").findall("Parameter"): params[param.attrib['name']] = param.text params.update(layer.find("Datasource").attrib) params['source_srs'] = "%%(%s)s" % srs_name ds_name = add_source(sources, ds_name, params) layer.attrib['source_name'] = ds_name del layer.attrib['srs'] layer.remove(layer.find("Datasource")) # now generate unique bases g_params = {} for name, params in sources.items(): gp = {} name_base = None if params.get('type') == 'postgis': param_set = ("port", "host", "user", "source_srs", "password", "type", "dbname", "estimate_extent", "extent") name_base = "postgis_conn_%d" # elif params.get('type') == 'shape': # param_set = ("type","file","source_srs") # name_base = "shapefile_%d" else: continue for p in param_set: if p in params: gp[p] = params[p] del params[p] gp_name, gp_data = g_params.get(repr(gp), (None, None)) if not gp_name: gp_name = name_base % len(g_params) g_params[repr(gp)] = gp_name, gp params['base'] = gp_name config = MyConfigParser(defaults) for name, params in itertools.chain(g_params.values(), sources.items()): config.add_section(name) for pn, pv in params.items(): if pn == 'table': pv = pv.strip() config.set(name, pn, pv) with codecs.open(outconfig, "w", "utf-8") as oc: config.write(oc) map.insert(0, Element("DataSourcesConfig", src=outconfig)) doc.write(outmml, "utf8")
def rpc(module, items): responses = list() for item in items: name = item['name'] xattrs = item['xattrs'] fetch_config = False args = item.get('args') text = item.get('text') name = str(name).replace('_', '-') if all((module.check_mode, not name.startswith('get'))): module.fail_json(msg='invalid rpc for running in check_mode') if name == 'command' and text.startswith( 'show configuration') or name == 'get-configuration': fetch_config = True element = Element(name, xattrs) if text: element.text = text elif args: for key, value in iteritems(args): key = str(key).replace('_', '-') if isinstance(value, list): for item in value: child = SubElement(element, key) if item is not True: child.text = item else: child = SubElement(element, key) if value is not True: child.text = value if fetch_config: reply = get_configuration(module, format=xattrs['format']) else: reply = exec_rpc(module, tostring(element), ignore_warning=False) if xattrs['format'] == 'text': if fetch_config: data = reply.find('.//configuration-text') else: data = reply.find('.//output') if data is None: module.fail_json(msg=tostring(reply)) responses.append(data.text.strip()) elif xattrs['format'] == 'json': responses.append(module.from_json(reply.text.strip())) elif xattrs['format'] == 'set': data = reply.find('.//configuration-set') if data is None: module.fail_json( msg= "Display format 'set' is not supported by remote device.") responses.append(data.text.strip()) else: responses.append(tostring(reply)) return responses
unquoted = unquote(quoted) return unquoted.decode('utf-8') else: quote_base_url = quote unquote_base_url = unquote NEWLINE = u'\n' EMPTY = u'' SPACE = u' ' # we do not want to flatten etree elements _do_not_iter_append(_Element) UNPARSEABLE = Element('unparseable') base_href = XPath('//base[@href]/@href | //x:base[@href]/@href', namespaces={'x': XHTML_NAMESPACE}) default_namespaces = {'re': 'http://exslt.org/regular-expressions'} # see http://lxml.de/extensions.html#the-functionnamespace function_namespace = FunctionNamespace(None) _html_text_nodes = XPath( 'descendant-or-self::node()' + '[not(local-name()) or not(text())]' + '[not(ancestor::script or ancestor::style or ancestor::noscript)]') def _wex_html_text(context, arg=None):
def test_parse_xml_ns(self): expected = Element("{foo:bar}test", {"foo": "bar"}) actual = parse_xml("""<h:test foo="bar" xmlns:h="foo:bar"/>""") self.assertEqual(expected.tag, actual.tag) self.assertEqual(expected.attrib, actual.attrib)
def zeroize(module): return exec_rpc(module, tostring(Element('request-system-zeroize')), ignore_warning=False)
def create_widget(self): self.widget = Element(self.declaration.tag)
def main(): """main entry point for Ansible module """ argument_spec = dict( rpc=dict(required=True), args=dict(type='dict'), attrs=dict(type='dict'), output=dict(default='xml', choices=['xml', 'json', 'text']), ) argument_spec.update(junos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False) warnings = list() result = {'changed': False, 'warnings': warnings} rpc = str(module.params['rpc']).replace('_', '-') if all((module.check_mode, not rpc.startswith('get'))): module.fail_json(msg='invalid rpc for running in check_mode') args = module.params['args'] or {} attrs = module.params['attrs'] or {} xattrs = {'format': module.params['output']} for key, value in iteritems(attrs): xattrs.update({key: value}) element = Element(module.params['rpc'], xattrs) for key, value in iteritems(args): key = str(key).replace('_', '-') if isinstance(value, list): for item in value: child = SubElement(element, key) if item is not True: child.text = item else: child = SubElement(element, key) if value is not True: child.text = value reply = exec_rpc(module, tostring(element), ignore_warning=False) result['xml'] = str(tostring(reply)) if module.params['output'] == 'text': data = reply.find('.//output') result['output'] = data.text.strip() result['output_lines'] = result['output'].split('\n') elif module.params['output'] == 'json': result['output'] = module.from_json(reply.text.strip()) else: result['output'] = str(tostring(reply)).split('\n') module.exit_json(**result)
def to_etree(self, data, options=None, name=None, depth=0): """ Given some data, converts that data to an ``etree.Element`` suitable for use in the XML output. """ if isinstance(data, (list, tuple)): element = Element(name or 'objects') if name: element = Element(name) element.set('type', 'list') else: element = Element('objects') for item in data: element.append(self.to_etree(item, options, depth=depth + 1)) elif isinstance(data, dict): if depth == 0: element = Element(name or 'response') else: element = Element(name or 'object') element.set('type', 'hash') for (key, value) in data.iteritems(): element.append( self.to_etree(value, options, name=key, depth=depth + 1)) elif isinstance(data, Bundle): element = Element(name or 'object') for field_name, field_object in data.data.items(): element.append( self.to_etree(field_object, options, name=field_name, depth=depth + 1)) elif hasattr(data, 'dehydrated_type'): if getattr(data, 'dehydrated_type', None) == 'related' and data.is_m2m == False: if data.full: return self.to_etree(data.fk_resource, options, name, depth + 1) else: return self.to_etree(data.value, options, name, depth + 1) elif getattr(data, 'dehydrated_type', None) == 'related' and data.is_m2m == True: if data.full: element = Element(name or 'objects') for bundle in data.m2m_bundles: element.append( self.to_etree(bundle, options, bundle.resource_name, depth + 1)) else: element = Element(name or 'objects') for value in data.value: element.append( self.to_etree(value, options, name, depth=depth + 1)) else: return self.to_etree(data.value, options, name) else: element = Element(name or 'value') simple_data = self.to_simple(data, options) data_type = get_type_string(simple_data) if data_type != 'string': element.set('type', get_type_string(simple_data)) if data_type != 'null': if isinstance(simple_data, unicode): element.text = simple_data else: element.text = force_unicode(simple_data) return element
except LangDetectException as e: print(arg, "->", e) return ret def process_text(arg): if arg is None: return arg ret = unescape(arg) ret = escape_apostrophe.sub(r"\'", ret) return ret zh_cn = Element('resources') zh_tw = Element('resources') used_names = {} for _, _, files in os.walk(translations_dir + dir_name): arrays = ElementTree.Element("resources") for file_name in files: locale_code = file_name[:-4] if not locale_code in source_locales: continue
def generate(self): if not self.host: conn = cups.Connection() else: if not self.port: self.port = 631 conn = cups.Connection(self.host, self.port) printers = conn.getPrinters() for p, v in printers.items(): if v['printer-is-shared']: attrs = conn.getPrinterAttributes(p) uri = urlparse.urlparse(v['printer-uri-supported']) tree = ElementTree() tree.parse(StringIO(XML_TEMPLATE.replace('\n', '').replace('\r', '').replace('\t', ''))) name = tree.find('name') name.text = 'AirPrint %s @ %%h' % (p) service = tree.find('service') port = service.find('port') port_no = None if hasattr(uri, 'port'): port_no = uri.port if not port_no: port_no = self.port if not port_no: port_no = cups.getPort() port.text = '%d' % port_no if hasattr(uri, 'path'): rp = uri.path else: rp = uri[2] re_match = re.match(r'^//(.*):(\d+)(/.*)', rp) if re_match: rp = re_match.group(3) #Remove leading slashes from path #TODO XXX FIXME I'm worried this will match broken urlparse #results as well (for instance if they don't include a port) #the xml would be malform'd either way rp = re.sub(r'^/+', '', rp) path = Element('txt-record') path.text = 'rp=%s' % (rp) service.append(path) desc = Element('txt-record') desc.text = 'note=%s' % (v['printer-info']) service.append(desc) product = Element('txt-record') product.text = 'product=(GPL Ghostscript)' service.append(product) state = Element('txt-record') state.text = 'printer-state=%s' % (v['printer-state']) service.append(state) ptype = Element('txt-record') ptype.text = 'printer-type=%s' % (hex(v['printer-type'])) service.append(ptype) pdl = Element('txt-record') fmts = [] defer = [] for a in attrs['document-format-supported']: if a in DOCUMENT_TYPES: if DOCUMENT_TYPES[a]: fmts.append(a) else: defer.append(a) if 'image/urf' not in fmts: sys.stderr.write('image/urf is not in mime types, %s may not be available on ios6 (see https://github.com/tjfontaine/airprint-generate/issues/5)%s' % (p, os.linesep)) fmts = ','.join(fmts+defer) dropped = [] # TODO XXX FIXME all fields should be checked for 255 limit while len('pdl=%s' % (fmts)) >= 255: (fmts, drop) = fmts.rsplit(',', 1) dropped.append(drop) if len(dropped) and self.verbose: sys.stderr.write('%s Losing support for: %s%s' % (p, ','.join(dropped), os.linesep)) pdl.text = 'pdl=%s' % (fmts) service.append(pdl) if self.adminurl: admin = Element('txt-record') admin.text = 'adminurl=%s' % (v['printer-uri-supported']) service.append(admin) fname = '%s%s.service' % (self.prefix, p) if self.directory: fname = os.path.join(self.directory, fname) f = open(fname, 'w') if etree: tree.write(f, pretty_print=True, xml_declaration=True, encoding="UTF-8") else: xmlstr = tostring(tree.getroot()) doc = parseString(xmlstr) dt= minidom.getDOMImplementation('').createDocumentType('service-group', None, 'avahi-service.dtd') doc.insertBefore(dt, doc.documentElement) doc.writexml(f) f.close() if self.verbose: sys.stderr.write('Created: %s%s' % (fname, os.linesep))
def trans(data, set_name): curr_dir = os.path.dirname(os.path.abspath(__file__)) os.mkdir(os.path.join(curr_dir, set_name)) Annotations_dir = os.path.join(curr_dir, set_name, 'Annotations') JPEGImages_dir = os.path.join(curr_dir, set_name, 'JPEGImages') os.mkdir(Annotations_dir) os.mkdir(JPEGImages_dir) for each_pic_data in tqdm.tqdm(data): # for each_pic_data in data: data_list = each_pic_data.strip().split() video_id = data_list[0] frame_num = str(data_list[1]).zfill(4) new_img_name = '{}_{}'.format(video_id, frame_num) frame_num = 'frame_{}.jpg'.format(frame_num) im_path = os.path.join(data_root, video_id, frame_num) boxes = np.reshape(np.array(map(int, data_list[2:])), (-1, 4)) node_root = Element('annotation') node_folder = SubElement(node_root, 'folder') node_folder.text = 'egohands' node_filename = SubElement(node_root, 'filename') node_filename.text = new_img_name # node_size = SubElement(node_root, 'size') node_segmented = SubElement(node_root, 'segmented') node_segmented.text = '0' node_width = SubElement(node_size, 'width') im_height, im_width, channel = cv2.imread(im_path).shape node_width.text = str(im_width) # node_height = SubElement(node_size, 'height') node_height.text = str(im_height) # node_depth = SubElement(node_size, 'depth') node_depth.text = str(channel) # # im = cv2.imread(im_path) # for index in range(boxes.shape[0]): # minx, miny, w, h = boxes[index] # cv2.namedWindow("", 0) # cv2.resizeWindow('', 300, 300) # cv2.rectangle(im, (minx, miny), (minx+w-1, miny+h-1), (0, 255, 0), thickness=2) # print(w, h) # cv2.imshow('', im) # cv2.waitKey(0) effective_hands = 0 for index in range(boxes.shape[0]): minx, miny, w, h = boxes[index] maxx = minx + w - 1 maxy = miny + h - 1 maxx = im_width if maxx > im_width else maxx maxy = im_height if maxy > im_height else maxy minx = 0 if minx < 0 else minx miny = 0 if miny < 0 else miny w = maxx - minx + 1 h = maxy - miny + 1 if min(w, h) < 40: continue if maxx <= minx or maxy <= miny: print(minx, miny) effective_hands = effective_hands + 1 node_object = SubElement(node_root, 'object') node_name = SubElement(node_object, 'name') node_name.text = 'hand' node_difficult = SubElement(node_object, 'difficult') node_difficult.text = '0' node_bndbox = SubElement(node_object, 'bndbox') node_xmin = SubElement(node_bndbox, 'xmin') node_xmin.text = str(minx) node_ymin = SubElement(node_bndbox, 'ymin') node_ymin.text = str(miny) node_xmax = SubElement(node_bndbox, 'xmax') node_xmax.text = str(maxx) node_ymax = SubElement(node_bndbox, 'ymax') node_ymax.text = str(maxy) xml = tostring(node_root, pretty_print=True) # if effective_hands == 0: # print(im_path) if effective_hands != 0: # print(im_path) with open(Annotations_dir + "/" + new_img_name + '.xml', 'w') as f: f.write(xml) shutil.copy(im_path, JPEGImages_dir + '/' + new_img_name + '.jpg')