item['summary'], refs, nl ]) elif htmlOutput: print("<h2>" + item['id'] + "<br></h2>CVSS score: " + item['cvss'] + "<br>" + "<b>" + item['Published'] + "<b><br>" + item['summary'] + "<br>") print("References:<br>") for entry in item['references']: print(entry + "<br>") print("<hr><hr>") # bson straight from the MongoDB db - converted to JSON default # representation elif jsonOutput: printCVE(item) elif xmlOutput: c = SubElement(r, 'id') c.text = item['id'] c = SubElement(r, 'Published') c.text = item['Published'] c = SubElement(r, 'cvss') c.text = str(item['cvss']) c = SubElement(r, 'summary') c.text = SaxEscape(item['summary']) for e in item['references']: c = SubElement(r, 'references') c.text = SaxEscape(e) for e in item['vulnerable_configuration']: c = SubElement(r, 'vulnerable_configuration') c.text = SaxEscape(e) elif cveidOutput: print(item['id'])
async def _send( self, notification: Notification, notification_to_replace: Optional[Notification], ) -> str: """ Asynchronously sends a notification. :param notification: Notification to send. :param notification_to_replace: Notification to replace, if any. """ if notification_to_replace: platform_nid = cast(str, notification_to_replace.identifier) else: platform_nid = str(uuid.uuid4()) await self._request_background_task_access() # Create notification XML. toast_xml = Element("toast", {"launch": "default"}) visual_xml = SubElement(toast_xml, "visual") actions_xml = SubElement(toast_xml, "actions") if notification.thread: SubElement( toast_xml, "header", { "id": notification.thread, "title": notification.thread }, ) binding = SubElement(visual_xml, "binding", {"template": "ToastGeneric"}) title_xml = SubElement(binding, "text") title_xml.text = notification.title message_xml = SubElement(binding, "text") message_xml.text = notification.message if notification.icon: SubElement( binding, "image", { "placement": "appLogoOverride", "src": notification.icon }, ) if notification.attachment: SubElement(binding, "image", { "placement": "hero", "src": notification.attachment }) if notification.reply_field: SubElement(actions_xml, "input", {"id": "textBox", "type": "text"}) reply_button_xml = SubElement( actions_xml, "action", { "content": notification.reply_field.button_title, "activationType": "background", "arguments": "action=reply&", }, ) # If there are no other buttons, show the # reply buttons next to the text field. if not notification.buttons: reply_button_xml.set("hint-inputId", "textBox") for n, button in enumerate(notification.buttons): SubElement( actions_xml, "action", { "content": button.title, "activationType": "background", "arguments": str(n), }, ) if notification.sound: SubElement(toast_xml, "audio", {"src": "ms-winsoundevent:Notification.Default"}) else: SubElement(toast_xml, "audio", {"silent": "true"}) xml_document = dom.XmlDocument() xml_document.load_xml(tostring(toast_xml, encoding="unicode")) native = ToastNotification(xml_document) native.tag = platform_nid native.priority = self._to_native_urgency[notification.urgency] if notification.thread: native.group = notification.thread else: native.group = "default" def on_activated(sender, boxed_activated_args): activated_args = ToastActivatedEventArgs._from( boxed_activated_args) action_id = activated_args.arguments if action_id == "default": if notification.on_clicked: notification.on_clicked() elif action_id == "action=reply&": if notification.reply_field.on_replied: boxed_text = activated_args.user_input["textBox"] text = _unbox_winrt(boxed_text) notification.reply_field.on_replied(text) else: action_number = int(action_id) notification.buttons[action_number].on_pressed() def on_dismissed(sender, dismissed_args): self._clear_notification_from_cache(notification) if dismissed_args.reason == ToastDismissalReason.USER_CANCELED: if notification.on_dismissed: notification.on_dismissed() def on_failed(sender, failed_args): logger.warning( f"Notification failed (error code {failed_args.error_code.value})" ) native.add_activated(on_activated) native.add_dismissed(on_dismissed) native.add_failed(on_failed) self.notifier.show(native) return platform_nid
'year': 2007, }, '0132356139': { 'title': 'Python Web Development with Django', 'authors': ['Jeff Forcier', 'Paul Bissex', 'Wesley Chun'], 'year': 2009 }, '0.17143419': { 'title': 'Python Fundamentals', 'year': 2009 }, } books = Element('books') for isbn, infos in BOOKs.items(): book = SubElement(books, 'book') for key, val in infos.items(): SubElement(book, key).text = ', '.join(str(val).split(":")) xml = tostring(books) print('*** RAW XML') print(xml) print('***PRETTY PRINT XML') dom = parseString(xml) print(dom.toprettyxml(' ')) print('*** FLAT STRUCTURE') for ele in books.iter(): print(ele.tag, '-', ele.text) print('***TITLES ONLY') for book in books.findall('.//title'): print(book.text)
def batch_generate_xml(): #txt_path = '/home/yanglu/Database/MSCOCO/MSCOCO_API/ym/coco/PythonAPI/scripts/code/coco_81_seg/coco_81_train.txt' txt_path = '/home/yanglu/Database/MSCOCO/MSCOCO_API/ym/coco/PythonAPI/scripts/code/coco_81_seg/coco_81_val.txt' coco_train_list = [] f = open(txt_path, 'r') for i in f: coco_train_list.append(i.strip()) for idx in coco_train_list: print idx annotation_file = dataDir + idx + '.json' annotation_set = json.load(open(annotation_file, 'r')) img_annotation = annotation_set['annotation'] img_info = annotation_set['image'] img_height = img_info['height'] img_width = img_info['width'] labels = [] points = [] category_id = [] list = [] bbox = [] boxlist = [] for ann in img_annotation: if 'segmentation' in ann: if ann['iscrowd'] == 0: if len(ann['segmentation']) == 1: for seg in ann['segmentation']: points.append(seg) category_id.append(ann['category_id']) bbox.append(ann['bbox']) else: for seg in ann['segmentation']: list = list + seg points.append(list) category_id.append(ann['category_id']) bbox.append(ann['bbox']) top = Element('annotation') folder = SubElement(top, 'folder') folder.text = 'COCO2014' filename = SubElement(top, 'filename') filename.text = idx + '.jpg' source = SubElement(top, 'source') database = SubElement(source, 'database') database.text = 'COCO_VAL2014' #修改 anntation = SubElement(source, 'annotation') anntation.text = 'MSCOCO' image = SubElement(source, 'image') image.text = 'flickr' size_part = SubElement(top, 'size') width = SubElement(size_part, 'width') height = SubElement(size_part, 'height') depth = SubElement(size_part, 'depth') width.text = str(img_width) height.text = str(img_height) depth.text = '3' segmented = SubElement(top, 'segmented') segmented.text = '0' i = 0 for point in points: label = coco_label_map[(category_id[i])] x1 = bbox[i][0] y1 = bbox[i][1] x2 = bbox[i][0] + bbox[i][2] y2 = bbox[i][1] + bbox[i][3] bndbox = { 'xmin': int(x1), 'ymin': int(y1), 'xmax': int(x2), 'ymax': int(y2) } bndbox['name'] = label boxlist.append(bndbox) i += 1 for each_object in boxlist: object_item = SubElement(top, 'object') name = SubElement(object_item, 'name') name.text = str(each_object['name']) pose = SubElement(object_item, 'pose') pose.text = "Unspecified" truncated = SubElement(object_item, 'truncated') truncated.text = "0" difficult = SubElement(object_item, 'difficult') difficult.text = "0" bndbox = SubElement(object_item, 'bndbox') xmin = SubElement(bndbox, 'xmin') xmin.text = str(each_object['xmin']) ymin = SubElement(bndbox, 'ymin') ymin.text = str(each_object['ymin']) xmax = SubElement(bndbox, 'xmax') xmax.text = str(each_object['xmax']) ymax = SubElement(bndbox, 'ymax') ymax.text = str(each_object['ymax']) save_file = open('./coco_81_det/val2014/' + idx + '.xml', 'w') rough_string = ElementTree.tostring(top, 'utf8') reparsed = minidom.parseString(rough_string) save_file.write(reparsed.toprettyxml(indent="\t"))
def moveMeasureContents(measure: Element, otherMeasure: Element, staffNumber: int): # noinspection PyShadowingNames ''' Move the child elements of `measure` into `otherMeasure`; create voice numbers if needed; bump voice numbers if they conflict; account for <backup> and <forward> tags; skip <print> tags; set "number" on midmeasure clef changes; replace existing <barline> tags. >>> from xml.etree.ElementTree import fromstring as El >>> measure = El('<measure><note /></measure>') >>> otherMeasure = El('<measure><note /></measure>') >>> SX = musicxml.m21ToXml.ScoreExporter >>> SX.moveMeasureContents(measure, otherMeasure, 2) >>> SX().dump(otherMeasure) <measure> <note> <voice>1</voice> </note> <note> <voice>2</voice> </note> </measure> >>> SX.moveMeasureContents(El('<junk />'), otherMeasure, 2) Traceback (most recent call last): music21.musicxml.xmlObjects.MusicXMLExportException: moveMeasureContents() called on <Element 'junk'... Only one <barline> should be exported per merged measure: >>> from music21.musicxml import testPrimitive >>> s = converter.parse(testPrimitive.mixedVoices1a) >>> SX = musicxml.m21ToXml.ScoreExporter(s) >>> root = SX.parse() >>> root.findall('part/measure/barline') [<Element 'barline' at 0x...] ''' if measure.tag != 'measure' or otherMeasure.tag != 'measure': raise MusicXMLExportException( f'moveMeasureContents() called on {measure} and {otherMeasure} (not measures).') maxVoices: int = 0 otherMeasureLackedVoice: bool = False for other_voice in otherMeasure.findall('*/voice'): otherVoiceText = other_voice.text if otherVoiceText is not None: maxVoices = max(maxVoices, int(otherVoiceText)) if maxVoices == 0: otherMeasureLackedVoice = True for elem in otherMeasure.findall('note'): new_voice = Element('voice') new_voice.text = '1' helpers.insertBeforeElements( elem, new_voice, tagList=[ 'type', 'dot', 'accidental', 'time-modification', 'stem', 'notehead', 'notehead-text', 'staff', ] ) maxVoices = 1 # Create <backup> amountToBackup: int = 0 for dur in otherMeasure.findall('note/duration'): backupDurText = dur.text if backupDurText is not None: amountToBackup += int(backupDurText) for dur in otherMeasure.findall('forward/duration'): forwardDurText = dur.text if forwardDurText is not None: amountToBackup += int(forwardDurText) for backupDur in otherMeasure.findall('backup/duration'): backupDurText = backupDur.text if backupDurText is not None: amountToBackup -= int(backupDurText) if amountToBackup: mxBackup = Element('backup') mxDuration = SubElement(mxBackup, 'duration') mxDuration.text = str(amountToBackup) otherMeasure.append(mxBackup) # Move elements for elem in measure.findall('*'): # Skip elements that already exist in otherMeasure if elem.tag == 'print': continue if elem.tag == 'attributes': if elem.findall('divisions'): # This is likely the initial mxAttributes continue for midMeasureClef in elem.findall('clef'): midMeasureClef.set('number', str(staffNumber)) if elem.tag == 'barline': # Remove existing <barline>, if any for existingBarline in otherMeasure.findall('barline'): otherMeasure.remove(existingBarline) if elem.tag == 'note': voice = elem.find('voice') if voice is not None: if otherMeasureLackedVoice and voice.text: # otherMeasure assigned voice 1; Bump voice number here voice.text = str(int(voice.text) + 1) else: pass # No need to alter existing voice numbers else: voice = Element('voice') voice.text = str(maxVoices + 1) helpers.insertBeforeElements( elem, voice, tagList=[ 'type', 'dot', 'accidental', 'time-modification', 'stem', 'notehead', 'notehead-text', 'staff' ] ) # Append to otherMeasure otherMeasure.append(elem)
z = r0[2] + grid_z[:, 2] x[nmin[0] - 1:nmax[0]:nstep[0]].astype('float64').tofile(xgridfile) y[nmin[1] - 1:nmax[1]:nstep[1]].astype('float64').tofile(ygridfile) z[nmin[2] - 1:nmax[2]:nstep[2]].astype('float64').tofile(zgridfile) # # write xml file # from xml.etree import ElementTree from xml.dom import minidom from xml.etree.ElementTree import Element, SubElement, Comment Xdmf = Element("Xdmf", attrib={ "xmlns:xi": "http://www.w3.org/2001/XInclude", "Version": "2.0" }) domain = SubElement(Xdmf, "Domain") topology = SubElement(domain, "Topology", attrib={ "name": "TOPO", "TopologyType": "3DRectMesh", "Dimensions": "{} {} {}".format(n[2], n[1], n[0]) }) geometry = SubElement(domain, "Geometry", attrib={ "name": "GEO", "GeometryType": "VXVYVZ" }) dataitem = SubElement(geometry, "DataItem",
def run(self, vcloud="default", data=None): contenttype = "application/vnd.vmware.vcloud.composeVAppParams+xml" self.set_connection(vcloud) self.get_sessionid() post = {} all_orgs = self.get_orgs() for org in data: for vdc in data[org]['vdcs']: if org not in all_orgs.keys(): post["%s" % (org.lower())] = "Org does not exist" continue org_details = self.get_org(all_orgs[org]['id']) # Check VDC exists if vdc not in org_details['vdcs'].keys(): post["%s (%s)" % (vdc.lower(), org.lower())] =\ "VDC does not exist" continue # Check VAPP defined in input data if "vapps" not in data[org]['vdcs'][vdc].keys(): post["%s (%s)" % (vdc.lower(), org.lower())] =\ "No VAPPS defined" continue vdcid = org_details['vdcs'][vdc]['id'] endpoint = "vdc/%s/action/composeVApp" % vdcid cpureq = 0 memreq = 0 storagereq = {} storagestats = {} # CHECK RESOURCES AVAILABLE for svapp in data[org]['vdcs'][vdc]['vapps']: for svm in data[org]['vdcs'][vdc]['vapps'][ svapp]['vms']: svmdata = data[org]['vdcs'][vdc]['vapps'][ svapp]['vms'][svm] cpureq += svmdata['cpu'] memreq += svmdata['memory'] if svmdata['Storage_Profile'] in storagereq.keys(): storagereq[svmdata['Storage_Profile']] += \ svmdata['hdd'] else: storagereq[svmdata['Storage_Profile']] = 0 storagereq[svmdata['Storage_Profile']] += \ svmdata['hdd'] compute = org_details['vdcs'][vdc]['computecapacity'] if compute['Cpu']['Units'] == "MHz": cpuused = int(compute['Cpu']['Used']) / 1000 cpulimit = int(compute['Cpu']['Limit']) / 1000 if compute['Memory']['Units'] == "MB": memused = int(compute['Memory']['Used']) / 1024 memlimit = int(compute['Memory']['Limit']) / 1024 for sp in org_details['vdcs'][vdc]['storageprofiles']: spdetails = org_details['vdcs'][vdc]['storageprofiles'][sp] if sp not in storagestats.keys(): storagestats[sp] = {} if spdetails['unit'] == 'MB': storagestats[sp]['used'] = \ int(spdetails['used']) / 1024 storagestats[sp]['limit'] = \ int(spdetails['limit']) / 1024 else: storagestats[sp]['used'] = int(spdetails['used']) storagestats[sp]['limit'] = int(spdetails['limit']) notenough = False if (cpureq + cpuused) > cpulimit: post["%s - %s - CPU" % (org.lower(), vdc.lower())] = \ "Insufficant Allocation" notenough = True if (memreq + memused) > memlimit: post["%s - Memory" % (vdc.lower())] = \ "Insufficant Allocation" notenough = True for profile in storagereq: if (storagereq[profile] + storagestats[profile]['used'])\ > storagestats[profile]['limit']: post["%s - %s" % (vdc.lower(), profile.lower())] = \ "Insufficant Storage" notenough = True if notenough: continue for vapp in data[org]['vdcs'][vdc]['vapps']: # Skip if VAPP already exists if vapp in org_details['vdcs'][vdc]['vapps'].keys(): post["%s - %s" % (vdc.lower(), vapp.lower())] = \ "VAPP Already exists" continue vappdata = data[org]['vdcs'][vdc]['vapps'][vapp] composevapp = Element('ComposeVAppParams') composevapp.set('name', vapp) composevapp.set('xmlns', 'http://www.vmware.com/vcloud/v1.5') composevapp.set('xmlns:ovf', 'http://schemas.dmtf.org/ovf/envelope/1') vappdescription = SubElement(composevapp, 'Description') vappdescription.text = vappdata['Description'] composevapp.extend(vappdescription) vappinstantiate = SubElement(composevapp, 'InstantiationParams') vappnetconfsec = SubElement(vappinstantiate, 'NetworkConfigSection') ovfinfo = SubElement(vappnetconfsec, 'ovf:Info') ovfinfo.text = "Configuration parameters for "\ "logical networks" # Check Network exists if "availablenetworks" in org_details['vdcs'][vdc].keys(): vappnetcon = SubElement(vappnetconfsec, 'NetworkConfig') vappnetcon.set('networkName', vappdata['Network']) networkhref = org_details['vdcs'][vdc][ 'availablenetworks'][vappdata['Network']]['href'] configuration = SubElement(vappnetcon, 'Configuration') parentnetwork = SubElement(configuration, 'ParentNetwork') parentnetwork.set('href', networkhref) fencemode = SubElement(configuration, 'FenceMode') fencemode.text = 'bridged' isdeployed = SubElement(vappnetcon, 'IsDeployed') isdeployed.text = "True" else: post["%s - %s" % (vdc.lower(), vapp.lower())] =\ "No Network Found" for vm in data[org]['vdcs'][vdc]['vapps'][vapp]['vms']: # Map vm data to easier variable vmdata = data[org]['vdcs'][vdc][ 'vapps'][vapp]['vms'][vm] # check template catalog for this Organisation if vmdata['Catalog'] not in \ org_details['catalogs'].keys(): post["%s - %s" % (vmdata['Catalog'].lower(), vm.lower())] = "Catalog does not exist" continue # Check vapp template exists for this Organisation if vmdata['Template'] not in org_details['catalogs'][ vmdata['Catalog']]['templates'].keys(): post["%s - %s" % (vmdata['Template'].lower(), vm.lower())] = "Template does not exist" continue # Check VM template exist for Organisation if vmdata['Templatevm'] in org_details['catalogs'][ vmdata['Catalog']]['templates'][ vmdata['Template']]['vms'].keys(): vmref = org_details['catalogs'][vmdata['Catalog']][ 'templates'][vmdata['Template']]['vms'][ vmdata['Templatevm']]['href'] else: post["%s - %s" % (vmdata['Templatevm'].lower(), vm.lower())] = "VM does not exist" continue vmsourceditem = SubElement(composevapp, 'SourcedItem') vmsource = SubElement(vmsourceditem, 'Source') vmsource.set('href', vmref) vmgeneralparams = SubElement(vmsourceditem, 'VmGeneralParams') vmsourceditem.extend(vmgeneralparams) vmname = SubElement(vmgeneralparams, 'Name') vmname.text = vm vmdescription = SubElement(vmgeneralparams, 'Description') vmdescription.text = vmdata['Description'] vmneedcust = SubElement(vmgeneralparams, 'NeedsCustomization') vmneedcust.text = "true" vminstantiateparams = SubElement(vmsourceditem, 'InstantiationParams') # GUEST OS CUSTOMIZATION vmguestcust = SubElement(vminstantiateparams, 'GuestCustomizationSection') vmguestcust.set('xmlns', 'http://www.vmware.' 'com/vcloud/v1.5') vmguestcust.set('xmlns:ovf', 'http://schemas.dmtf.org/' 'ovf/envelope/1') vmguestcust.set('ovf:required', 'false') vmguestcustinfo = SubElement(vmguestcust, 'ovf:Info') vmguestcustinfo.text = "Guest OS Customization" vmguestcustenable = SubElement(vmguestcust, 'Enabled') vmguestcustenable.text = "True" vmguestcustsid = SubElement(vmguestcust, 'ChangeSid') vmguestcustsid.text = "True" vmcompname = SubElement(vmguestcust, 'ComputerName') vmcompname.text = vmdata['Hostname'] # NETWORK ADAPTER CUSTOMIZATION vmnetconsec = SubElement(vminstantiateparams, 'NetworkConnectionSection') vmnetconsec.set('xmlns', 'http://www.vmware.com/vcloud/v1.5') vmnetconsec.set('xmlns:ovf', 'http://schemas.dmtf.org/' 'ovf/envelope/1') vmnetconsec.set('type', 'application/vnd.vmware.vcloud.' 'networkConnectionSection+xml') vmnetconsec.set('href', vmref + '/networkConnectionSection/') vmnetconsec.set('ovf:required', 'false') # vmnetconsecinfo = SubElement(vmnetconsec, 'ovf:Info') vmnetconindex = SubElement(vmnetconsec, 'PrimaryNetworkConn' 'ectionIndex') vmnetconindex.text = str(0) vmnetcon = SubElement(vmnetconsec, 'NetworkConnection') vmnetcon.set('network', vmdata['Network']['Name']) vmnetindx = SubElement(vmnetcon, 'NetworkConnectionIndex') vmnetindx.text = str(0) # SET IP if provided otherwise set to DHCP if "IP" in vmdata['Network'].keys(): vmnetip = SubElement(vmnetcon, 'IpAddress') vmnetip.text = vmdata['Network']['IP'] vmnetisconnected = SubElement(vmnetcon, 'IsConnected') vmnetisconnected.text = "True" vmnetipmode = SubElement(vmnetcon, 'IpAddressAllocationMode') vmnetipmode.text = "MANUAL" else: vmnetisconnected = SubElement(vmnetcon, 'IsConnected') vmnetisconnected.text = "True" vmnetipmode = SubElement(vmnetcon, 'IpAddressAllocationMode') vmnetipmode.text = "DHCP" # Setup Hardware customisation section vmhardwaresection = SubElement(vminstantiateparams, 'ovf:VirtualHardware' 'Section') vmhardwaresection.set('xmlns:ovf', 'http://schemas.dmtf.org/' 'ovf/envelope/1') vmhardwaresection.set('xmlns:rasd', 'http://schemas.dmtf.org/wbem/w' 'scim/1/cim-schema/2/CIM_Resourc' 'eAllocationSettingData') vmhardwaresection.set('xmlns:vmw', 'http://www.vmware.' 'com/schema/ovf') vmhardwaresection.set('xmlns:vcloud', 'http://www.vmware.' 'com/vcloud/v1.5') vmhardwaresection.set('vcloud:href', vmref + # noqa: W504 '/virtualHardwareSection/') vmhardwaresection.set('vcloud:type', 'application/vnd.vmware.vcloud.' 'virtualHardwareSection+xml') vmhwinfo = SubElement(vmhardwaresection, 'ovf:Info') vmhwinfo.text = "Hardware Requirements" # Build CPU customisation setcpu = SubElement(vmhardwaresection, 'ovf:Item') cpuallocationunits = SubElement(setcpu, 'rasd:AllocationUnits') cpuallocationunits.text = "hertz * 10^6" cpudescription = SubElement(setcpu, 'rasd:Description') cpudescription.text = "Number of Virtual CPUs" cpuelementname = SubElement(setcpu, 'rasd:ElementName') cpuelementname.text = "1 Virtual CPU(s)" cpuinstanceid = SubElement(setcpu, 'rasd:InstanceID') cpuinstanceid.text = str(40) cpureservation = SubElement(setcpu, 'rasd:Reservation') cpureservation.text = str(0) cpuresourcetype = SubElement(setcpu, 'rasd:ResourceType') cpuresourcetype.text = str(3) cpuvirtualquantity = SubElement(setcpu, 'rasd:VirtualQuantity') cpuvirtualquantity.text = str(vmdata['cpu']) cpuweight = SubElement(setcpu, 'rasd:Weight') cpuweight.text = str(0) # Build Memory customisation setmemory = SubElement(vmhardwaresection, 'ovf:Item') memallocationUnits = SubElement(setmemory, 'rasd:AllocationUnits') memallocationUnits.text = "byte * 2^20" memdescription = SubElement(setmemory, 'rasd:Description') memdescription.text = "Memory Size" memelementname = SubElement(setmemory, 'rasd:ElementName') memelementname.text = "1024 MB of memory" meminstanceid = SubElement(setmemory, 'rasd:InstanceID') meminstanceid.text = str(50) memreservation = SubElement(setmemory, 'rasd:Reservation') memreservation.text = str(0) memresourcetype = SubElement(setmemory, 'rasd:ResourceType') memresourcetype.text = str(4) memvirtualquantity = SubElement(setmemory, 'rasd:VirtualQuantity') memvirtualquantity.text = str(vmdata['memory'] * 1024) memweight = SubElement(setmemory, 'rasd:Weight') memweight.text = str(0) # post to API endpoint post["%s (%s)" % (vm.lower(), vapp.lower())] =\ self.vcd_post(endpoint, composevapp, contenttype) return post
def _format_item_set(self, article, item_set, item_type): """ Construct the item element (newsItem or packageItem) and append the item_meta and contentMeta entities :param dict article: :param element item_set: :param str item_type: """ item = SubElement(item_set, item_type, attrib={ 'standard': 'NewsML-G2', 'standardversion': '2.18', 'guid': article['guid'], 'version': str(article[superdesk.config.VERSION]), 'xml:lang': article.get('language', 'en'), 'conformance': 'power' }) SubElement( item, 'catalogRef', attrib={ 'href': 'http://www.iptc.org/std/catalog/catalog.IPTC-G2-Standards_25.xml' }) self._format_rights(item, article) item_meta = SubElement(item, 'itemMeta') self._format_itemClass(article, item_meta) self._format_provider(item_meta) self._format_versioncreated(article, item_meta) self._format_firstcreated(article, item_meta) self._format_pubstatus(article, item_meta) if article.get(EMBARGO): SubElement(item_meta, 'embargoed').text = \ get_utc_schedule(article, EMBARGO).isoformat() # optional properties self._format_ednote(article, item_meta) self._format_signal(article, item_meta) content_meta = SubElement(item, 'contentMeta') SubElement(content_meta, 'urgency').text = str(article.get('urgency', 5)) self._format_timestamps(article, content_meta) self._format_creator(article, content_meta) self._format_located(article, content_meta) self._format_subject(article, content_meta) self._format_genre(article, content_meta) self._format_slugline(article, content_meta) self._format_headline(article, content_meta) self._format_place(article, content_meta) self._format_category(article, content_meta) self._format_company_codes(article, content_meta, item) if article[ITEM_TYPE] in { CONTENT_TYPE.PICTURE, CONTENT_TYPE.AUDIO, CONTENT_TYPE.VIDEO }: self._format_description(article, content_meta) self._format_creditline(article, content_meta) return item
def export(self): ''' exporting data to the vFeed XML format Output : CVE_xxxx_xxx_.xml file ''' # define id self.vfeedid = self.cveID.replace('self.cveID', 'vFeed') self.vfeedfile = self.cveID.replace('-', '_') + '.xml' # define generation time self.generated_on = strftime("%a, %d %b %Y %H:%M:%S", gmtime()) # define the vFeed XML attributes self.root = Element('vFeed') self.root.set('xmlns:xsi', "http://www.w3.org/2001/XMLSchema-instance") self.root.set('xmlns:meta', "http://www.toolswatch.org/vfeed/") self.root.set('xmlns', "http://www.toolswatch.org/vfeed/") self.root.set( 'xsi:schemaLocation', "http://www.toolswatch.org/vfeed/ http://www.toolswatch.org/vfeed/vFeed.xsd" ) self.root.append(Comment('#####################################')) self.root.append(Comment(config.product['__title__'])) self.root.append(Comment('Generated by vFeedApi.py')) self.head = SubElement(self.root, 'release') self.project_name = SubElement(self.head, 'name') self.project_name.text = 'vFeed XML for %s' % self.cveID self.project_version = SubElement(self.head, 'version') self.project_version.text = config.product['__build__'] self.project_author = SubElement(self.head, 'author') self.project_author.text = config.author['__name__'] self.project_url = SubElement(self.head, 'url') self.project_url.text = config.author['__website__'] self.date_generated = SubElement(self.head, 'date_generated') self.date_generated.text = self.generated_on # Exporting Vulnerability Summary self.root.append(Comment('#####################################')) self.root.append(Comment('Entry ID')) self.entry_head = SubElement(self.root, 'entry', { 'exported': self.vfeedfile, 'id': self.vfeedid, }) self.vul_summary_date = SubElement( self.entry_head, 'date', { 'published': self.cveInfo['published'], 'modified': self.cveInfo['modified'], }) self.vul_summary = SubElement(self.entry_head, 'summary') self.vul_summary.text = self.cveInfo['summary'] self.vul_summary_ref = SubElement(self.entry_head, 'cve_ref') self.vul_summary_ref.text = self.cve_url + self.cveID # Exporting references as they come from NVD XML self.entry_head.append( Comment('#####################################')) self.entry_head.append(Comment('Official References')) self.references_head = SubElement(self.entry_head, 'references') for i in range(0, len(self.cveRef)): self.source_head = SubElement(self.references_head, 'ref', { 'url': self.cveRef[i]['link'], 'source': self.cveRef[i]['id'], }) self.entry_head.append( Comment('#####################################')) self.entry_head.append(Comment('vFeed Mapped References')) self.mappedrefs_head = SubElement(self.entry_head, 'crossReferences') # Exporting extra SCIP ref from Mapping for i in range(0, len(self.SCIP_id)): self.source_head = SubElement( self.mappedrefs_head, 'ref', { 'url': self.SCIP_id[i]['link'], 'id': self.SCIP_id[i]['id'], 'source': "SCIP", }) # Exporting extra CERT VN ref from Mapping for i in range(0, len(self.CERTVN_id)): self.source_head = SubElement( self.mappedrefs_head, 'ref', { 'url': self.CERTVN_id[i]['link'], 'id': self.CERTVN_id[i]['id'], 'source': "CERT-VN", }) # Exporting IAVM ref from Mapping for i in range(0, len(self.IAVM_id)): self.source_head = SubElement( self.mappedrefs_head, 'ref', { 'vmskey': self.IAVM_id[i]['key'], 'id': self.IAVM_id[i]['id'], 'title': self.IAVM_id[i]['title'], 'source': "DISA/IAVM", }) # Exporting BID ref from Mapping for i in range(0, len(self.cveBID)): self.source_head = SubElement( self.mappedrefs_head, 'ref', { 'id': self.cveBID[i]['id'], 'url': self.cveBID[i]['link'], 'source': "SecurityFocus", }) # Exporting OSVDB ref from Mapping for i in range(0, len(self.OSVDB_id)): self.source_head = SubElement( self.mappedrefs_head, 'ref', { 'id': self.OSVDB_id[i]['id'], 'url': self.osvdb_url + self.OSVDB_id[i]['id'], 'source': "OSVDB", }) # Exporting Targets CPEs ids if self.CPE_id: self.entry_head.append( Comment('#####################################')) self.entry_head.append( Comment('Vulnerable Targets according to CPE')) self.vulnerabletargets_head = SubElement(self.entry_head, 'vulnerableTargets') for i in range(0, len(self.CPE_id)): self.cpe_head = SubElement(self.vulnerabletargets_head, 'cpe', { 'id': self.CPE_id[i]['id'], }) # Exporting Risk Scoring self.entry_head.append( Comment('#####################################')) self.entry_head.append(Comment('Risk Scoring Evaluation')) self.riskscoring_head = SubElement(self.entry_head, 'riskScoring') self.risk_head = SubElement(self.riskscoring_head, 'severityLevel', { 'status': self.Risk['severitylevel'], }) self.risk_head = SubElement( self.riskscoring_head, 'cvss', { 'base': self.cvssScore['base'], 'impact': self.cvssScore['impact'], 'exploit': self.cvssScore['exploit'], }) self.risk_head = SubElement( self.riskscoring_head, 'cvssVector', { 'AV': self.cvssScore['access_vector'], 'AC': self.cvssScore['access_complexity'], 'Au': self.cvssScore['authentication'], 'C': self.cvssScore['confidentiality_impact'], 'I': self.cvssScore['integrity_impact'], 'A': self.cvssScore['availability_impact'], }) self.risk_head = SubElement(self.riskscoring_head, 'topVulnerable', { 'status': str(self.Risk['topvulnerable']), }) self.risk_head = SubElement(self.riskscoring_head, 'topAlert', { 'status': str(self.Risk['topAlert']), }) self.risk_head = SubElement(self.riskscoring_head, 'pciCompliance', { 'status': self.Risk['pciCompliance'], }) # Exporting Patch Management self.entry_head.append( Comment('#####################################')) self.entry_head.append(Comment('Patch Management')) self.patchmanagement_head = SubElement(self.entry_head, 'patchManagement') ## Exporting Microsoft MS Patches for i in range(0, len(self.MS_id)): self.patch_head = SubElement( self.patchmanagement_head, 'patch', { 'id': self.MS_id[i]['id'], 'title': self.MS_id[i]['title'], 'source': 'microsoft', 'url': self.ms_bulletin_url + self.MS_id[i]['id'], }) ## Exporting Microsoft KB Patches for i in range(0, len(self.KB_id)): self.patch_head = SubElement( self.patchmanagement_head, 'patch', { 'id': self.KB_id[i]['id'], 'title': self.KB_id[i]['title'], 'source': 'microsoft KB', 'url': self.ms_kb_url + self.KB_id[i]['id'], }) ## Exporting IBM AIXAPAR Patches for i in range(0, len(self.AIXAPAR_id)): self.patch_head = SubElement(self.patchmanagement_head, 'patch', { 'id': self.AIXAPAR_id[i]['id'], 'source': 'IBM', }) ## Exporting REDHAT Patches for i in range(0, len(self.REDHAT_id)): self.patch_head = SubElement( self.patchmanagement_head, 'patch', { 'id': self.REDHAT_id[i]['id'], 'title': self.REDHAT_id[i]['title'], 'source': 'REDHAT', }) for i in range(0, len(self.BUGZILLA_id)): self.patch_head = SubElement( self.patchmanagement_head, 'patch', { 'date_issue': self.BUGZILLA_id[i]['date_issue'], 'id': self.BUGZILLA_id[i]['id'], 'title': self.BUGZILLA_id[i]['title'], 'source': 'BUGZILLA', }) ## Exporting SUSE Patches for i in range(0, len(self.SUSE_id)): self.patch_head = SubElement(self.patchmanagement_head, 'patch', { 'id': self.SUSE_id[i]['id'], 'source': 'SUSE', }) ## Exporting DEBIAN Patches for i in range(0, len(self.DEBIAN_id)): self.patch_head = SubElement(self.patchmanagement_head, 'patch', { 'id': self.DEBIAN_id[i]['id'], 'source': 'DEBIAN', }) ## Exporting MANDRIVA Patches for i in range(0, len(self.MANDRIVA_id)): self.patch_head = SubElement(self.patchmanagement_head, 'patch', { 'id': self.MANDRIVA_id[i]['id'], 'source': 'MANDRIVA', }) ## Exporting VMWARE Patches for i in range(0, len(self.VMWARE_id)): self.patch_head = SubElement(self.patchmanagement_head, 'patch', { 'id': self.VMWARE_id[i]['id'], 'source': 'VMWARE', }) ## Exporting CISCO Patches for i in range(0, len(self.CISCO_id)): self.patch_head = SubElement(self.patchmanagement_head, 'patch', { 'id': self.CISCO_id[i]['id'], 'source': 'CISCO', }) ## Exporting UBUNTU Patches for i in range(0, len(self.UBUNTU_id)): self.patch_head = SubElement(self.patchmanagement_head, 'patch', { 'id': self.UBUNTU_id[i]['id'], 'source': 'UBUNTU', }) ## Exporting GENTOO Patches for i in range(0, len(self.GENTOO_id)): self.patch_head = SubElement(self.patchmanagement_head, 'patch', { 'id': self.GENTOO_id[i]['id'], 'source': 'GENTOO', }) ## Exporting FEDORA Patches for i in range(0, len(self.FEDORA_id)): self.patch_head = SubElement(self.patchmanagement_head, 'patch', { 'id': self.FEDORA_id[i]['id'], 'source': 'FEDORA', }) ## Exporting HP Patches for i in range(0, len(self.HP_id)): self.patch_head = SubElement( self.patchmanagement_head, 'patch', { 'id': self.HP_id[i]['id'], 'link': self.HP_id[i]['link'], 'source': 'Hewlett-Packard', }) # Attack and Weaknesses Patterns if self.CWE_id: self.entry_head.append( Comment('#####################################')) self.entry_head.append( Comment( 'Attack and Weaknesses Categories. Useful when performing classification of threats' )) self.attackclassification_head = SubElement( self.entry_head, 'attackPattern') for i in range(0, len(self.CWE_id)): self.cwe_id_url = self.CWE_id[i]['id'].split("CWE-") self.attackPattern_head = SubElement( self.attackclassification_head, 'cwe', { 'standard': 'CWE - Common Weakness Enumeration', 'id': self.CWE_id[i]['id'], 'title': self.CWE_id[i]['title'], 'url': self.cwe_url + self.cwe_id_url[1] }) for i in range(len(self.CWE_id), len(self.CAPEC_id) + len(self.CWE_id)): self.attackPattern_head = SubElement( self.attackclassification_head, 'capec', { 'standard': 'CAPEC - Common Attack Pattern Enumeration and Classification', 'relatedCWE': self.CAPEC_id[i]['cwe'], 'id': self.CAPEC_id[i]['id'], 'url': self.capec_url + self.CAPEC_id[i]['id'] }) # Exporting Assessment, security tests and exploitation self.entry_head.append( Comment('#####################################')) self.entry_head.append( Comment( 'Assessment and security Tests. The IDs and source could be leveraged to test the vulnerability' )) self.securitytest_head = SubElement(self.entry_head, 'assessment') ## Exporting OVAL ids for i in range(0, len(self.OVAL_id)): self.ovalChecks_head = SubElement( self.securitytest_head, 'check', { 'type': 'Local Security Testing', 'id': self.OVAL_id[i]['id'], 'class': self.OVAL_id[i]['class'], 'title': self.OVAL_id[i]['title'], 'utility': "OVAL Interpreter", 'file': self.OVAL_id[i]['file'], }) for i in range(0, len(self.REDHAT_id)): try: self.ovalChecks_head = SubElement( self.securitytest_head, 'check', { 'type': 'Local Security Testing', 'id': self.REDHAT_id[i]['oval'], 'utility': "OVAL Interpreter", 'file': self.redhat_oval_url + self.REDHAT_id[i]['oval'].split( 'oval:com.redhat.rhsa:def:')[1] + '.xml', }) except: pass ## Exporting Nessus attributes for i in range(0, len(self.NESSUS_id)): self.nessusChecks_head = SubElement( self.securitytest_head, 'check', { 'type': 'Remote Security Testing', 'id': self.NESSUS_id[i]['id'], 'name': self.NESSUS_id[i]['name'], 'family': self.NESSUS_id[i]['family'], 'file': self.NESSUS_id[i]['file'], 'utility': "Nessus Vulnerability Scanner", }) ## Exporting OpenVAS attributes for i in range(0, len(self.OPENVAS_id)): self.openvasChecks_head = SubElement( self.securitytest_head, 'check', { 'type': 'Remote Security Testing', 'id': self.OPENVAS_id[i]['id'], 'name': self.OPENVAS_id[i]['name'], 'family': self.OPENVAS_id[i]['family'], 'file': self.OPENVAS_id[i]['file'], 'utility': "OpenVAS Vulnerability Scanner", }) ## Exporting Nmap attributes for i in range(0, len(self.NMAP_id)): self.nmapChecks_head = SubElement( self.securitytest_head, 'check', { 'type': 'Remote Security Testing', 'family': self.NMAP_id[i]['family'].replace('"', ''), 'file': self.NMAP_id[i]['file'], 'utility': "Nmap Network Mapper", }) ## Exporting EDB ids for i in range(0, len(self.EDB_id)): self.exploitChecks_head = SubElement( self.securitytest_head, 'check', { 'type': 'Exploitation', 'utility': "exploit-db", 'id': self.EDB_id[i]['id'], 'file': self.EDB_id[i]['file'], 'link': self.EDB_id[i]['link'], }) ## Exporting Milw0rm ids for i in range(0, len(self.MILWORM_id)): self.exploitChecks_head = SubElement( self.securitytest_head, 'check', { 'type': 'Exploitation', 'utility': "milw0rm", 'id': self.MILWORM_id[i]['id'], 'file': self.milw0rm_url + self.MILWORM_id[i]['id'], }) ## Exporting SAINT ids for i in range(0, len(self.SAINT_id)): self.exploitChecks_head = SubElement( self.securitytest_head, 'check', { 'type': 'Exploitation', 'utility': "saintExploit", 'id': self.SAINT_id[i]['id'], 'title': self.SAINT_id[i]['title'], 'file': self.SAINT_id[i]['file'], }) ## Exporting MSF - Metasploit ids for i in range(0, len(self.MSF_id)): self.exploitChecks_head = SubElement( self.securitytest_head, 'check', { 'type': 'Exploitation', 'utility': "Metasploit", 'id': self.MSF_id[i]['id'], 'title': self.MSF_id[i]['title'], 'script': self.MSF_id[i]['file'], }) ## Exporting D2 Elliot Framework for i in range(0, len(self.D2_id)): self.exploitChecks_head = SubElement( self.securitytest_head, 'check', { 'type': 'Exploitation', 'utility': "D2 Elliot", 'title': self.D2_id[i]['title'], 'script': self.D2_id[i]['file'], }) # Exporting Defense rules self.entry_head.append( Comment('#####################################')) self.entry_head.append( Comment( 'Defense and IDS rules. The IDs and source could be leveraged to deploy effective rules' )) self.defense_head = SubElement(self.entry_head, 'defense') ## Exporting Snort Rules for i in range(0, len(self.SNORT_id)): self.idsRules_head = SubElement( self.defense_head, 'rule', { 'type': 'Defense', 'utility': "Snort", 'id': self.SNORT_id[i]['id'], 'signature': self.SNORT_id[i]['signature'], 'classtype': self.SNORT_id[i]['classtype'], }) ## Exporting Suricata Rules for i in range(0, len(self.SURICATA_id)): self.idsRules_head = SubElement( self.defense_head, 'rule', { 'type': 'Defense', 'utility': "Suricata", 'id': self.SURICATA_id[i]['id'], 'signature': self.SURICATA_id[i]['signature'], 'classtype': self.SURICATA_id[i]['classtype'], }) self.xmlfile = open(self.vfeedfile, 'w+') print '[info] vFeed xml file %s exported for %s' % (self.vfeedfile, self.cveID) print >> self.xmlfile, self.prettify(self.root)
def make_event(parent, eid): event = SubElement(parent, "EVENT") event.set("eid", eid)
def _format_item(self, news_message): return SubElement(news_message, 'itemSet')
def annotate_timeml_events(parent, nodes): text = SubElement(parent, "TEXT") for tid in nodes: make_event(text, tid)
def create_instance(parent, nodes): for node in nodes: instance = SubElement(parent, "MAKEINSTANCE") instance.set("eiid", "instance_" + node) instance.set("eid", node)
def createLayout(self, elem): # We use an internal property to handle margins which will use separate # left, top, right and bottom margins if they are found to be # different. The following will select, in order of preference, # separate margins, the same margin in all directions, and the default # margin. margin = -1 if self.stack.topIsLayout() else self.defaults['margin'] margin = self.wprops.getProperty(elem, 'margin', margin) left = self.wprops.getProperty(elem, 'leftMargin', margin) top = self.wprops.getProperty(elem, 'topMargin', margin) right = self.wprops.getProperty(elem, 'rightMargin', margin) bottom = self.wprops.getProperty(elem, 'bottomMargin', margin) # A layout widget should, by default, have no margins. if self.stack.topIsLayoutWidget(): if left < 0: left = 0 if top < 0: top = 0 if right < 0: right = 0 if bottom < 0: bottom = 0 if left >= 0 or top >= 0 or right >= 0 or bottom >= 0: # We inject the new internal property. cme = SubElement(elem, 'property', name='pyuicMargins') SubElement(cme, 'number').text = str(left) SubElement(cme, 'number').text = str(top) SubElement(cme, 'number').text = str(right) SubElement(cme, 'number').text = str(bottom) # We use an internal property to handle spacing which will use separate # horizontal and vertical spacing if they are found to be different. # The following will select, in order of preference, separate # horizontal and vertical spacing, the same spacing in both directions, # and the default spacing. spacing = self.wprops.getProperty(elem, 'spacing', self.defaults['spacing']) horiz = self.wprops.getProperty(elem, 'horizontalSpacing', spacing) vert = self.wprops.getProperty(elem, 'verticalSpacing', spacing) if horiz >= 0 or vert >= 0: # We inject the new internal property. cme = SubElement(elem, 'property', name='pyuicSpacing') SubElement(cme, 'number').text = str(horiz) SubElement(cme, 'number').text = str(vert) classname = elem.attrib["class"] if self.stack.topIsLayout(): parent = None else: parent = self.stack.topwidget if "name" not in elem.attrib: elem.attrib["name"] = classname[1:].lower() self.stack.push(self.setupObject(classname, parent, elem)) self.traverseWidgetTree(elem) layout = self.stack.popLayout() self.configureLayout(elem, layout) if self.stack.topIsLayout(): top_layout = self.stack.peek() lp = elem.attrib['layout-position'] if isinstance(top_layout, QtWidgets.QFormLayout): top_layout.setLayout(lp[0], self._form_layout_role(lp), layout) else: top_layout.addLayout(layout, *lp)
def etree(cls): root = Element("PMTMODRQ") SubElement(root, "SRVRTID").text = "DEADBEEF" root.append(bp_common.PmtinfoTestCase.etree) return root
def make(n, y, add="", type="Car"): x = [] idx = [] i = 0 while i < len(y): j = i w = [] while j < len(y) and (abs(y[j][0]) > 1.0e-6 or abs(y[j][1]) > 1.0e-6): w.append(y[j]) j = j+1 if len(w) > 0: x.append(w) idx.append(i) i = j + 1 path = "./" + add +".xml" top = Element('boost_serialization', { "signature" : "serialization::archive", "version":'9'}) childTop = SubElement(top, 'tracklets', {"class_id":'0', "tracking_level":"0", "version":"0"}) tmp = SubElement(childTop, 'count') tmp.text = str(len(x)) tmp = SubElement(childTop, 'item_version') for elem, k in zip(x, idx): tmp.text = "1" child = SubElement(childTop, 'item', {"class_id":"1", "tracking_level":"0", "version":"1"}) subchild = SubElement(child, 'objectType') subchild.text = type if type == "Car": subchild = SubElement(child, 'h') subchild.text = "1.824000" subchild = SubElement(child, 'w') subchild.text = "2.074800" subchild = SubElement(child, 'l') subchild.text = "4.191000" else: subchild = SubElement(child, 'h') subchild.text = "1.824000" subchild = SubElement(child, 'w') subchild.text = "1.04800" subchild = SubElement(child, 'l') subchild.text = "3.191000" subchild = SubElement(child, 'first_frame') subchild.text = str(k) child2 = child child = SubElement(child, 'poses', {"class_id":"2", "tracking_level":"0", "version":"0"}) subchild = SubElement(child, 'count') subchild.text = str(len(elem)) subchild = SubElement(child, 'item_version') subchild.text = "2" for i in range(0, len(elem)): if (i == 0): subchild = SubElement(child, 'item', {"class_id":"3", "tracking_level":"0", "version":"2"}) else: subchild = SubElement(child, 'item') if i < len(y): tmp = SubElement(subchild, "tx") tmp.text = str(elem[i][0]) tmp = SubElement(subchild, "ty") tmp.text = str(elem[i][1]) tmp = SubElement(subchild, "tz") tmp.text = str(elem[i][2]) else: tmp = SubElement(subchild, "tx") tmp.text = "0.0" tmp = SubElement(subchild, "ty") tmp.text = "0.0" tmp = SubElement(subchild, "tz") tmp.text = "0.0" tmp = SubElement(subchild, "rx") tmp.text = "0.0" tmp = SubElement(subchild, "ry") tmp.text = "0.0" tmp = SubElement(subchild, "rz") tmp.text = "3.14" tmp = SubElement(subchild, "state") tmp.text = "1" tmp = SubElement(subchild, "occlusion") tmp.text = "-1" tmp = SubElement(subchild, "occlusion_kf") tmp.text = "-1" tmp = SubElement(subchild, "truncation") tmp.text = "-1" tmp = SubElement(subchild, "amt_occlusion") tmp.text = "0.0" tmp = SubElement(subchild, "amt_occlusion_kf") tmp.text = "-1" tmp = SubElement(subchild, "amt_border_l") tmp.text = "0.0" tmp = SubElement(subchild, "amt_border_r") tmp.text = "0.0" tmp = SubElement(subchild, "amt_border_kf") tmp.text = "-1" child = SubElement(child2, 'finished') child.text = "1" xml2 = xml.dom.minidom.parseString(tostring(top)) pretty_xml_as_string = xml2.toprettyxml() file = open(path, "w") #file .write(tostring(top)) file .write(pretty_xml_as_string) file.close()
from xml.etree.ElementTree import Element, dump, SubElement note = Element('note') to = Element('to') to.text = "Tove" note.append(to) SubElement(note, "from").text = 'Jani' note.attrib["date"] = "20120104" # 속성(attribuete)추가 # 부모Element.attrib["속성명"] = "속성값" dump(note)
print ## 7.6.8 Building Documents wih Element Nodes # ElementTree is also capable of creating well-formed XML documents from Element objects # The Element class can produce a serialized form of its contents which can then be stored # There are three helper functions useful when creating a hierarchy of Element nodes # Element() creates a standard node # SubElement() attached a new node to a parent # Comment() creates a node that serializes using XML's comment syntax top = Element('top') comment = Comment("Generated for pystl") top.append(comment) child = SubElement(top, 'child') child.text = "This child contains text." child_with_tail = SubElement(top, 'child_with_tail') child_with_tail.text = "This child has regular text." child_with_tail.tail = "And 'tail' text." child_with_entity_ref = SubElement(top, 'child_with_entity_ref') child_with_entity_ref.text = "This & That" print "An XML string has been built:" print ElementTree.tostring(top) print ## 7.6.9 Pretty-Printing XML # ElementTree doesn't add any whitespace, which is useful, but quite ugly
def SubElementUnique(parent, child): if parent.find(child) == None: return SubElement(parent, child) else: return parent.find(child)
candidate_vases = [] affect_differences = [] for i in range(1000): candidate_vases.append(WebEvolveVase.mutate(vase)) #print i,candidate_vases[i].DNA for i in range(1000): AffectAnalysis.analyzeAffectOfVase(candidate_vases[i]) affect_differences.append(Utilities.affectDifferenceFromValues(candidate_vases[i], activity, weight, warmth, hardness)) sorted_indices = np.argsort(affect_differences) #print sorted_indices[0] newVase = Utilities.splineVase(candidate_vases[sorted_indices[0]]) newvase_id = CreateVase.createAndSaveWebVase(newVase, image_id) #print "newvase id: ", newvase_id #print newVase.basePts, newVase.initHeight, newVase.leftedgeX, newVase.rightedgeX, newVase.numVertices, newVase.DNA #print newVase.affectValues["activity"], newVase.affectValues["heaviness"], newVase.affectValues["warmth"], newVase.affectValues["hardness"] #vase = Utilities.splineVase(vase) #CreateVase.draw(vase.finalLeft, vase.finalRight) #CreateVase.draw(newVase.finalLeft, newVase.finalRight) returndata = Element('returndata') vase_id_xml = SubElement(returndata, 'vaseid') image_id_xml = SubElement(returndata, 'imageid') vase_id_xml.text = str(newvase_id) image_id_xml.text = str(image_id) print tostring(returndata)
def data_to_xml_tree(data, parent=None, child_tag=None, child_attrib=None): """Converts a Python structure in an ElementTree structure. The key concept when using this function is that for generating a valid XML ElementData, three information should be available: tag, attributes, and value/children. Some of such information can be omitted if it's optional (so, not specified) or if it can be already extracted by the context. Of course, at least the tag information should be provided in some way. Usually a tuple of three elements is passed to fully qualify all three required data. For example, passing ('foo', {'bar': '123'}, 'spam'), as the data parameter, generates an ElementTree structure which, once converted to string, looks like: <foo bar="123">spam</foo> To generate only a node with the tag, it's enough to call the function with only a string as parameter (the tag). For example, 'foo' gives back: <foo/> That's because no tuple was provided, but only a basic primitive (a string), and since the tag is mandatory, it's automatically assumed that the string has to be used as the tag. Instead, passing the tuple ('foo', 'bar') generates: <foo>bar</foo> In this case the second element should contain either the attributes or the value(s) of the tag, but since it's not a dictionary (the only data type which can used to specify the list of attributes with their values), it's automatically assumed to be used as the value. Finally, passing ('foo', {'bar': '123'}) generates: <foo bar="123"/> That's because the two elements tuple has not enough information, but the second element is a dictionary, so it's automatically used for the tag's attributes. A list or tuple can also be passed as the tag's value, and in this case a deeper XML structure is generated. For example, passing ('foo', ['bar', 'spam']) generates: <foo> <bar/> <spam/> </foo> To each list's element is applied the same logic defined before, so a tuple/list can be passed as well, to better qualify each sub-tag. For example, passing ('foo', ['bar', ('spam', 123), ('droogs', {'milk': 'plus'})]) generates: <foo> <bar/> <spam>123</spam> <droogs milk="plus"/> </foo> Sometimes the sub-tags share the same tag name, so a mechanism is defined in order to avoid to specify it for all of them. In this case, a special key in the main tag's attributes can be used: '$tag'. For example, ('foo', {'$tag': 'bar'}, [1, 2, 3]) generates: <foo> <bar>1</bar> <bar>2</bar> <bar>3</bar> </foo> So, the application can focus on providing only the concrete data that should be generated. Similarly, if the sub-tags use the same attributes sets, a special key in the main tag's attributes can be used: '$attr'. For example, ('foo', {'$attr': {'bar': 'spam'}}, ['droogs', 'milk', 'plus']) generates: <foo> <droogs bar="spam"/> <milk bar="spam"/> <plus bar="spam"/> </foo> A combination of $tag and $attr can be used as well, so passing ('foo', {'$tag': 'bar', '$attr': {'milk': 'plus'}}, [1, 2, 3]) generates: <foo> <bar milk="plus">1</bar> <bar milk="plus">2</bar> <bar milk="plus">3</bar> </foo> Finally, it has to be noted that if the value information isn't a list or tuple, it'll be automatically converted to a string. For example, ('foo', datetime.datetime.now()) generates: <foo>2017-02-20 09:20:12.746000</foo> Args: data(list, tuple, dict, or any type): the Python data structure. See above for more details. parent(Element): the parent node (if available). child_tag(str, None): the tag to be used for direct children (if any). child_attrib(dict, None): the attributes to to be used for direct children (if any). """ # print('data_to_xml_tree: data={}, parent={}, child_tag={}, child_attrib={}'.format( # data, parent, child_tag, child_attrib), file=out) attrib, value = {}, None if child_tag: # Have: tag. Miss: attrib, value tag, child_tag = child_tag, None if child_attrib is not None: # Have: tag, attrib. Miss: value attrib, child_attrib, value = child_attrib, {}, data else: # Have: tag, Miss: attrib, value if isinstance(data, dict): attrib = data elif isinstance(data, (tuple, list)): if len(data) == 2: attrib, value = data else: tag, attrib, value = data[:3] else: value = data else: # Miss: tag, attrib, value if child_attrib is not None: # Have: attrib. Miss: tag, value attrib, child_attrib = child_attrib, {} if isinstance(data, (tuple, list)): if len(data) == 2: tag, value = data else: tag, attrib, value = data[:3] else: tag = data else: # Miss: tag, attrib, value if isinstance(data, (tuple, list)): if len(data) == 2: tag, data = data if isinstance(data, dict): attrib = data else: value = data else: tag, attrib, value = data[:3] else: tag = data if attrib: # The original attribute dictionary should be preserved, because it might be used by other # tags. That's because we'll remove some keys, if they are present. See below. attrib = attrib.copy() new_child_tag = attrib.pop('$tag', None) if new_child_tag is not None: child_tag = new_child_tag new_child_attrib = attrib.pop('$attr', None) if new_child_attrib is not None: child_attrib = new_child_attrib text, children = (None, value) if isinstance(value, (tuple, list)) \ else (str(value) if value is not None else None, ()) node = Element(tag, attrib) if parent is None else SubElement(parent, tag, attrib) if text is not None: node.text = text for child in children: data_to_xml_tree(child, node, child_tag, child_attrib) return node
import xml.etree.ElementTree as ET import datetime from xml.dom import minidom tree_origin = ET.parse("19a.eaf") tree_new_bee = Element("ANNOTATION_DOCUMENT") tree_new_bee.set("xmlns:xsi", "http://www.w3.orgn/2001/XMLSchema-instance") tree_new_bee.set("AUTHOR", " ") tree_new_bee.set("DATE", str(datetime.date.today())) tree_new_bee.set("FORMAT", "3.0") tree_new_bee.set("VERSION", "3.0") tree_new_bee.set("xsi:noNamespaceSchemaLocation", "http://www.mpi.nl/tools/elan/EAFv3.0.xsd") header = SubElement(tree_new_bee, "HEADER") header.set("MEDIA_FILE", "") header.set("TIME_UNITS", "milliseconds") prop = SubElement(header, "PROPERTY") prop1 = SubElement(header, "PROPERTY") prop.set("NAME", "URN") prop.text = tree_origin.findtext(".//HEADER/PROPERTY[@NAME ='URN']") prop1.set("NAME", "lastUsedAnnotationId") prop1.text = tree_origin.findtext( ".//HEADER/PROPERTY[@NAME ='lastUsedAnnotationId']") time_order = SubElement(tree_new_bee, "TIME_ORDER") temps = [] temps_value = [] for a in tree_origin.findall(".//TIME_ORDER/TIME_SLOT"): temps.append(a.attrib['TIME_SLOT_ID'])
def setEarliestAttributesAndClefsPartStaff(self, group: StaffGroup): ''' Set the <staff>, <key>, <time>, and <clef> information on the earliest measure <attributes> tag in the <part> representing the joined PartStaffs. Need the earliest <attributes> tag, which may not exist in the merged <part> until moved there by movePartStaffMeasureContents() -- e.g. RH of piano doesn't appear until m. 40, and earlier music for LH needs to be merged first in order to find earliest <attributes>. Called by :meth:`joinPartStaffs` Multiple keys: >>> from music21.musicxml import testPrimitive >>> xmlDir = common.getSourceFilePath() / 'musicxml' / 'lilypondTestSuite' >>> s = converter.parse(xmlDir / '43b-MultiStaff-DifferentKeys.xml') >>> SX = musicxml.m21ToXml.ScoreExporter(s) >>> root = SX.parse() >>> m1 = root.find('part/measure') >>> SX.dump(m1) <measure number="1"> <attributes> <divisions>10080</divisions> <key number="1"> <fifths>0</fifths> </key> <key number="2"> <fifths>2</fifths> </key> <time> <beats>4</beats> <beat-type>4</beat-type> </time> <staves>2</staves> <clef number="1"> <sign>G</sign> <line>2</line> </clef> <clef number="2"> <sign>F</sign> <line>4</line> </clef> </attributes> ... </measure> Multiple meters (not very well supported by MusicXML readers): >>> from music21.musicxml import testPrimitive >>> s = converter.parse(testPrimitive.pianoStaffPolymeter) >>> SX = musicxml.m21ToXml.ScoreExporter(s) >>> root = SX.parse() >>> m1 = root.find('part/measure') >>> SX.dump(m1) <measure number="1"> <attributes> <divisions>10080</divisions> <key> <fifths>0</fifths> </key> <time number="1"> <beats>4</beats> <beat-type>4</beat-type> </time> <time number="2"> <beats>2</beats> <beat-type>2</beat-type> </time> <staves>2</staves> <clef number="1"> <sign>G</sign> <line>2</line> </clef> <clef number="2"> <sign>F</sign> <line>4</line> </clef> </attributes> ... </measure> ''' def isMultiAttribute(m21Class: t.Type[M21ObjType], comparison: str = '__eq__') -> bool: ''' Return True if any first instance of m21Class in any subsequent staff in this StaffGroup does not compare to the first instance of that class in the earliest staff where found (not necessarily the first) using `comparison`. ''' initialM21Instance: t.Optional[M21ObjType] = None # noinspection PyShadowingNames for ps in group: # ps okay to reuse. if initialM21Instance is None: initialM21Instance = ps.recurse().getElementsByClass(m21Class).first() else: firstInstanceSubsequentStaff = ps.recurse().getElementsByClass(m21Class).first() if firstInstanceSubsequentStaff is not None: comparisonWrapper = getattr(firstInstanceSubsequentStaff, comparison) if not comparisonWrapper(initialM21Instance): return True # else, keep looking: 3+ staves # else, keep looking: 3+ staves return False multiKey: bool = isMultiAttribute(KeySignature) multiMeter: bool = isMultiAttribute(TimeSignature, comparison='ratioEqual') initialPartStaffRoot: t.Optional[Element] = None mxAttributes: t.Optional[Element] = None for i, ps in enumerate(group): staffNumber: int = i + 1 # 1-indexed # Initial PartStaff in group: find earliest mxAttributes, set clef #1 and <staves> if initialPartStaffRoot is None: initialPartStaffRoot = self.getRootForPartStaff(ps) mxAttributes = initialPartStaffRoot.find('measure/attributes') clef1: t.Optional[Element] = mxAttributes.find('clef') if mxAttributes else None if clef1 is not None: clef1.set('number', '1') mxStaves = Element('staves') mxStaves.text = str(len(group)) helpers.insertBeforeElements( mxAttributes, mxStaves, tagList=['part-symbol', 'instruments', 'clef', 'staff-details', 'transpose', 'directive', 'measure-style'] ) if multiKey and mxAttributes is not None: key1 = mxAttributes.find('key') if key1: key1.set('number', '1') if multiMeter and mxAttributes is not None: meter1 = mxAttributes.find('time') if meter1: meter1.set('number', '1') # Subsequent PartStaffs in group: set additional clefs on mxAttributes else: thisPartStaffRoot: Element = self.getRootForPartStaff(ps) oldClef: t.Optional[Element] = thisPartStaffRoot.find('measure/attributes/clef') if oldClef is not None and mxAttributes is not None: clefsInMxAttributesAlready = mxAttributes.findall('clef') if len(clefsInMxAttributesAlready) >= staffNumber: e = MusicXMLExportException('Attempted to add more clefs than staffs') e.partName = ps.partName raise e # Set initial clef for this staff newClef = Element('clef') newClef.set('number', str(staffNumber)) newSign = SubElement(newClef, 'sign') newSign.text = (oldClefSign.text if (oldClefSign := oldClef.find('sign')) is not None else None) newLine = SubElement(newClef, 'line') newLine.text = (foundLine.text if (foundLine := oldClef.find('line')) is not None else '') helpers.insertBeforeElements( mxAttributes, newClef, tagList=['staff-details', 'transpose', 'directive', 'measure-style'] )
#! /usr/bin/env/python # -*- coding:utf-8 -*- from xml.etree.ElementTree import ( Element, SubElement, Comment, ) from ElementTree_14 import prettify top = Element('top') comment = Comment('Generated for PyMOTW') top.append(comment) child = SubElement(top, 'child') child.text = 'This child contains text.' child_with_tail = SubElement(top, 'child_with_tail') child_with_tail.text = 'This child has regular text.' child_with_tail.tail = 'And "tail" text.' child_with_entity_ref = SubElement(top, 'child_with_entity_ref') child_with_entity_ref.text = 'This & that' print prettify(top)
<TrainingCenterDatabase xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://www.garmin.com/xmlschemas/ActivityExtension/v2 http://www.garmin.com/xmlschemas/ActivityExtensionv2.xsd http://www.garmin.com/xmlschemas/TrainingCenterDat abase/v2 http://www.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd"> ''' footer = '''</TrainingCenterDatabase>''' Activities = Element('Activities') for item in cur.fetchall(): act_type = "Other" if item[1] == 1 or item[1] == 14: act_type = "Running" if item[1] == 2 or item[1] == 4 or item[1] == 12: act_type = "Biking" Activity = SubElement(Activities, "Activity", {'Sport': act_type}) stime = datetime.datetime(item[7], item[6], item[5], item[4], item[3], item[2]) n = SubElement(Activity, "Id") n.text = stime.strftime("%Y-%m-%dT%H:%M:%SZ") lap = SubElement(Activity, "Lap", {'StartTime': stime.strftime("%Y-%m-%dT%H:%M:%SZ")}) secs = int(item[8]) + 60 * (int(item[9]) + 60 * int(item[10])) n = SubElement(lap, "TotalTimeSeconds") n.text = str(secs) n = SubElement(lap, "DistanceMeters") n.text = str(item[12]) n = SubElement(lap, "MaximumSpeed") n.text = str(item[14])
def etree(cls): root = Element("PMTINQRQ") SubElement(root, "SRVRTID").text = "DEADBEEF" return root
resFileToParse = int(input("Saisir le numero du pdf a parser : ")) data['fileName'] = fileListKey[resFileToParse] print("Parsing du document :", fileListKey[resFileToParse]) fileToParse = fileListDic[fileListKey[resFileToParse]] print("Chemin du document :", fileToParse) # Convert pdf os.system("pdftotext -nopgbrk -raw " + fileToParse) #os.system("pdftotext -nopgbrk "+fileToParse) pdf = open(os.path.splitext(fileToParse)[0] + '.txt', "r") lines = pdf.readlines() # genereration template xml root = Element('opml') root.set('version', '1.0') head = SubElement(root, 'article') preamble = SubElement(head, 'preamble') title = SubElement(head, 'title') auteur = SubElement(head, 'auteur') abstract = SubElement(head, 'abstract') introduction = SubElement(head, 'introduction') corps = SubElement(head, 'corps') discusion = SubElement(head, 'discussion') conclusion = SubElement(head, 'conclusion') biblio = SubElement(head, 'biblio') #print (prettify(root)) # recuperer le pdf source if (exportFormat == "txt"): sortie = open(fileToParse + "_parser.txt", "w") # delete le dernier fichier si il existe else:
def etree(cls): root = Element("PMTINQRS") SubElement(root, "SRVRTID").text = "DEADBEEF" root.append(bp_common.PmtprcstsTestCase.etree) SubElement(root, "CHECKNUM").text = "215" return root
from xml.etree.ElementTree import ElementTree, Element, dump, SubElement, Comment # indent를 모듈로 만들어서 사용 from FileIO.XML.indent import indent # xml 저장 root = Element("xml", kind="language") node1 = Element("first", id="01") node1.text = "안녕" root.append(node1) node2 = Element("second") node2.text = "Hello" node2.attrib["id"] = "02" # 또다른 속성추가 root.append(node2) # 속성과 값을 한번에 추가하기 SubElement(root, "append", id="03").text = "반갑습니다" # Comment () 함수는 xml 파일에 주석을 삽입하는 데 사용됩니다. comment1 = Comment("XML comment") root.append(comment1) comment2 = Comment("XML 한글 comment") root.append(comment2) # 저장하기 ElementTree(root).write("test1.xml") # 한글깨짐(encoding default 값이 'us-ascii') # xml_declaration=True : XML문서 선언 포함 ElementTree(root).write("test2.xml", encoding='utf8', xml_declaration=True) # 한글 정상 # 들여쓰기 저장
from xml.etree.ElementTree import Element, dump, SubElement, ElementTree note = Element("note") note.attrib["date"] = "20120104" to = Element("to") to.text = "Tove" note.append(to) SubElement(note, "from").text = "Jani" SubElement(note, "heading").text = "Reminder" SubElement(note, "body").text = "Don't forget me this weekend!" dump(note) ElementTree(note).write("note.xml") SubElement(note, "from").text = "Min" ElementTree(note).write("note.xml")