def build_xml(self, parent_node): node = etree.SubElement(parent_node, "char") node.text = self.value node.attrib['position'] = self.position if self.modifier is not None: node.attrib['modifier'] = str(self.modifier)
def _add_filter(self, enum_elem): filter_elem = ElementTree.SubElement(enum_elem, '{%s}Filter' % NS_WSMAN) filter_elem.set('Dialect', self.filter_dialect) filter_elem.text = self.filter_query
name_elements = codelist.getroot().xpath('/codelist/metadata/name[{}@xml:lang="{}"]'.format('not(@xml:lang) or ' if language==default_lang else '',language)) description_elements = codelist.getroot().xpath('/codelist/metadata/description[{}@xml:lang="{}"]'.format('not(@xml:lang) or ' if language==default_lang else '',language)) url_elements = codelist.getroot().xpath('/codelist/metadata/url') ## JSON json.dump( { 'attributes': { 'name': attrib['name'], 'complete': attrib.get('complete'), 'category-codelist': attrib.get('category-codelist'), }, 'metadata': { 'name': name_elements[0].text if name_elements else '', 'description': description_elements[0].text if description_elements else '', 'url': url_elements[0].text if url_elements else '' }, 'data': codelist_dicts }, open(os.path.join(OUTPUTDIR, 'json', language, attrib['name']+'.json'), 'w') ) codelists_list.append(attrib['name']) ET.SubElement(codelists, 'codelist').attrib['ref'] = attrib['name'] tree = ET.ElementTree(codelists) tree.write(os.path.join(OUTPUTDIR, 'codelists.xml'), pretty_print=True) json.dump(codelists_list, open(os.path.join(OUTPUTDIR, 'codelists.json'), 'w'))
def getContent(self): if self._caller != "databrowse": return None else: if self._content_mode == "full": try: st = os.stat(self._fullpath) except IOError: return "Failed To Get File Information: %s" % (self._fullpath) else: file_size = st[ST_SIZE] file_mtime = time.asctime(time.localtime(st[ST_MTIME])) file_ctime = time.asctime(time.localtime(st[ST_CTIME])) file_atime = time.asctime(time.localtime(st[ST_ATIME])) magicstore = magic.open(magic.MAGIC_MIME) magicstore.load() contenttype = magicstore.file(self._fullpath) extension = os.path.splitext(self._fullpath)[1][1:] icon = self._handler_support.GetIcon(contenttype, extension) downlink = self.getURL(self._relpath, content_mode="raw", download="true") xmlroot = etree.Element('{%s}dbhdf' % self._namespace_uri, nsmap=self.nsmap, name=os.path.basename(self._relpath), resurl=self._web_support.resurl, downlink=downlink, icon=icon) xmlchild = etree.SubElement(xmlroot, "filename", nsmap=self.nsmap) xmlchild.text = os.path.basename(self._fullpath) xmlchild = etree.SubElement(xmlroot, "path", nsmap=self.nsmap) xmlchild.text = os.path.dirname(self._fullpath) xmlchild = etree.SubElement(xmlroot, "size", nsmap=self.nsmap) xmlchild.text = self.ConvertUserFriendlySize(file_size) xmlchild = etree.SubElement(xmlroot, "mtime", nsmap=self.nsmap) xmlchild.text = file_mtime xmlchild = etree.SubElement(xmlroot, "ctime", nsmap=self.nsmap) xmlchild.text = file_ctime xmlchild = etree.SubElement(xmlroot, "atime", nsmap=self.nsmap) xmlchild.text = file_atime # Content Type xmlchild = etree.SubElement(xmlroot, "contenttype", nsmap=self.nsmap) xmlchild.text = contenttype # File Permissions xmlchild = etree.SubElement(xmlroot, "permissions", nsmap=self.nsmap) xmlchild.text = self.ConvertUserFriendlyPermissions(st[ST_MODE]) # User and Group username = pwd.getpwuid(st[ST_UID])[0] groupname = grp.getgrgid(st[ST_GID])[0] xmlchild = etree.SubElement(xmlroot, "owner", nsmap=self.nsmap) xmlchild.text = "%s:%s" % (username, groupname) # Contents of File f = open(self._fullpath) xmlchild = etree.SubElement(xmlroot, "contents", nsmap=self.nsmap) output, error = subprocess.Popen(['/usr/bin/h5dump', '-x', '-H', self._fullpath], stdout=subprocess.PIPE).communicate() output = output.replace('xmlns:hdf5="http://hdfgroup.org/HDF5/XML/schema/HDF5-File.xsd"', 'xmlns:hdf5="http://hdfgroup.org/DTDs/HDF5-File"') xmlchild.append(etree.XML(output)) #xmlchild.text = f.read() return xmlroot elif self._content_mode == "raw" and self._web_support.req.form['getimage'].value == "true" and 'hdfloc' in self._web_support.req.form: hdfpath = self._web_support.req.form['hdfloc'].value tagname = base64.urlsafe_b64encode(hdfpath) ext='png' if self.CacheFileExists(tagname, extension=ext): size = os.path.getsize(self.getCacheFileName(tagname, extension=ext)) f = self.getCacheFileHandler('rb', tagname, extension=ext) self._web_support.req.response_headers['Content-Type'] = 'image/png' self._web_support.req.response_headers['Content-Length'] = str(size) self._web_support.req.start_response(self._web_support.req.status, self._web_support.req.response_headers.items()) self._web_support.req.output_done = True if 'wsgi.file_wrapper' in self._web_support.req.environ: return self._web_support.req.environ['wsgi.file_wrapper'](f, 1024) else: return iter(lambda: f.read(1024)) else: print(self._fullpath) f = h5py.File(self._fullpath, 'r') data = f.get(self._web_support.req.form['hdfloc'].value) if len(data.value.shape) == 1: pylab.figure() pylab.plot(data.value) imgf = self.getCacheFileHandler('w', tagname, 'png') pylab.savefig(imgf) imgf.close() pylab.clf() elif len(data.value.shape) == 2: pylab.figure() pylab.imshow(data.value, origin='lower') imgf = self.getCacheFileHandler('w', tagname, 'png') pylab.savefig(imgf) imgf.close() pylab.clf() f.close() size = os.path.getsize(self.getCacheFileName(tagname, extension=ext)) f = self.getCacheFileHandler('rb', tagname, extension=ext) self._web_support.req.response_headers['Content-Type'] = 'image/png' self._web_support.req.response_headers['Content-Length'] = str(size) self._web_support.req.start_response(self._web_support.req.status, self._web_support.req.response_headers.items()) self._web_support.req.output_done = True if 'wsgi.file_wrapper' in self._web_support.req.environ: return self._web_support.req.environ['wsgi.file_wrapper'](f, 1024) else: return iter(lambda: f.read(1024)) elif self._content_mode == "raw": size = os.path.getsize(self._fullpath) magicstore = magic.open(magic.MAGIC_MIME) magicstore.load() contenttype = magicstore.file(self._fullpath) f = open(self._fullpath, "rb") self._web_support.req.response_headers['Content-Type'] = contenttype self._web_support.req.response_headers['Content-Length'] = str(size) self._web_support.req.response_headers['Content-Disposition'] = "attachment; filename=" + os.path.basename(self._fullpath) self._web_support.req.start_response(self._web_support.req.status, self._web_support.req.response_headers.items()) self._web_support.req.output_done = True if 'wsgi.file_wrapper' in self._web_support.req.environ: return self._web_support.req.environ['wsgi.file_wrapper'](f, 1024) else: return iter(lambda: f.read(1024)) else: raise self.RendererException("Invalid Content Mode") pass
# 将 第 0014 题中的 student.xls 文件中的内容写到 student.xml 文件中,如 import xlrd from lxml import etree root = etree.Element('network') root.set('name', 'Network') tree = etree.ElementTree(root) name = etree.Element('nodes') root.append(name) wb = xlrd.open_workbook("emme_nodes1.xls") sh = wb.sheet_by_index(0) for row in range(1, sh.nrows): val = sh.row_values(row) element = etree.SubElement(name, 'node') element.set('id', str(int(val[0]))) element.set('x', str(val[1])) element.set('y', str(val[2])) print etree.tostring(root,pretty_print=True)
def encode_polyline_object(cls, obj, categories): ann_elem = cls.encode_annotation_base(obj) ET.SubElement(ann_elem, 'label').text = \ str(cls._get_label(obj.label, categories)) ET.SubElement(ann_elem, 'label_id').text = str(obj.label) x, y, w, h = obj.get_bbox() area = w * h bbox_elem = ET.SubElement(ann_elem, 'bbox') ET.SubElement(bbox_elem, 'x').text = str(x) ET.SubElement(bbox_elem, 'y').text = str(y) ET.SubElement(bbox_elem, 'w').text = str(w) ET.SubElement(bbox_elem, 'h').text = str(h) ET.SubElement(bbox_elem, 'area').text = str(area) points = obj.points for i in range(0, len(points), 2): point_elem = ET.SubElement(ann_elem, 'point') ET.SubElement(point_elem, 'x').text = str(points[i]) ET.SubElement(point_elem, 'y').text = str(points[i + 1]) return ann_elem
def _create_xml_content(self, export_data): """ Creates an xml content based on the input data and the EDI report template and puts it into the 'body' field of the 'self'. :param export_data: a list of dictionaries with element names and values This method is called explicitly when the EDI document is created. """ report_obj = self.env['report'] self.body = report_obj.get_html( self, 'eintegration_edi_manager.edi_document_report_template') root = etree.fromstring(self.body.lstrip()) message_body = root.find('.//MESSAGEBODY') element_list = [] for export_data_item in export_data: top = None for exported_field in export_data_item['names']: element_names = exported_field.split('/') field_element = None parent_element = top for element_name in element_names: if top is None: field_element = etree.Element(element_name) top = field_element else: field_element = parent_element.find(element_name) if field_element is None: field_element = parent_element if parent_element.tag == element_name else etree.SubElement( parent_element, element_name) parent_element = field_element data_value = export_data_item['data'].pop(0) if isinstance(data_value, (int, float)): field_element.text = CDATA(str(data_value)) else: field_element.text = CDATA(data_value) element_list.append(top) for element in element_list: message_body.append(element) self.body = "<?xml version='1.0' encoding='ISO-8859-1'?>\n" + etree.tostring( root, pretty_print=True)
def TC_pafHTR( root, relativeTime, HTRSEL, SET, PVALUE, IVALUE, DVALUE, Timeline_settings, comment="", ): if not (_Globals.latestRelativeTime <= relativeTime <= Timeline_settings["duration"]): Logger.error( "Invalid argument: negative relativeTime, decreasing relativeTime, exceeding timeline duration" ) raise ValueError if not ((1 <= HTRSEL <= 3 or 64 <= HTRSEL <= 67 or 128 <= HTRSEL <= 131 or 192 <= HTRSEL <= 195) and type(HTRSEL) == int): Logger.error("Invalid argument: HTRSEL") raise ValueError if not (86 <= SET <= 2285 and type(SET) == int): Logger.error("Invalid argument: 86 > SET or SET > 2285") raise ValueError if not (0 <= PVALUE <= 65536 and type(PVALUE) == int): Logger.error("Invalid argument: PVALUE") raise ValueError if not (0 <= IVALUE <= 65536 and type(IVALUE) == int): Logger.error("Invalid argument: IVALUE") raise ValueError if not (0 <= DVALUE <= 65536 and type(DVALUE) == int): Logger.error("Invalid argument: DVALUE") raise ValueError etree.SubElement(root[1], "command", mnemonic="TC_pafHTR") etree.SubElement(root[1][len(root[1]) - 1], "relativeTime") root[1][len(root[1]) - 1][0].text = str(int(relativeTime)) etree.SubElement(root[1][len(root[1]) - 1], "comment") root[1][len(root[1]) - 1][1].text = comment etree.SubElement(root[1][len(root[1]) - 1], "tcArguments") etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="HTRSEL") root[1][len(root[1]) - 1][2][0].text = str(HTRSEL) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="SET") root[1][len(root[1]) - 1][2][1].text = str(SET) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="PVALUE") root[1][len(root[1]) - 1][2][2].text = str(PVALUE) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="IVALUE") root[1][len(root[1]) - 1][2][3].text = str(IVALUE) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="DVALUE") root[1][len(root[1]) - 1][2][4].text = str(DVALUE) incremented_time = relativeTime + Timeline_settings["CMD_separation"] _Globals.latestRelativeTime = relativeTime return incremented_time
def TC_pafCCDMain( root, relativeTime, CCDSEL, PWR, TEXPMS, TEXPIMS, NRSKIP, NRBIN, NROW, NCBIN, NCOL, WDW, JPEGQ, SYNC, NCBINFPGA, SIGMODE, GAIN, NFLUSH, NCSKIP, Timeline_settings, comment="", ): if not (_Globals.latestRelativeTime <= relativeTime <= Timeline_settings["duration"]): Logger.error( "Invalid argument: negative relativeTime, decreasing relativeTime, exceeding timeline duration" ) raise ValueError if not (1 <= CCDSEL <= 127 and type(CCDSEL) == int): Logger.error("Invalid argument: CCDSEL") raise ValueError if not (0 <= PWR <= 255 and type(PWR) == int): Logger.error("Invalid argument: PWR") raise ValueError if not (0 <= NRSKIP <= 510 and type(NRSKIP) == int): Logger.error("Invalid argument: NRSKIP") raise ValueError if not (1 <= NRBIN <= 63 and type(NRBIN) == int): Logger.error("Invalid argument: NRBIN") raise ValueError if not (1 <= NROW <= 511 and type(NROW) == int): Logger.error("Invalid argument: NROW") raise ValueError if not (0 <= NCSKIP <= 2046 and type(NCSKIP) == int): Logger.error("Invalid argument: NCSKIP") raise ValueError if not (1 <= NCBIN <= 255 and type(NCBIN) == int): Logger.error("Invalid argument: NCBIN") raise ValueError if not (1 <= NCOL <= 2047 and type(NCOL) == int): Logger.error("Invalid argument: NCOL") raise ValueError if not (0 <= NCBINFPGA <= 7 and type(NCBINFPGA) == int): Logger.error( "Invalid argument: NCBINFPGA, if NCBINFPGA=8 then the CRB may stop working" ) raise ValueError if not (1 <= SIGMODE <= 255 and type(SIGMODE) == int): Logger.error( "Invalid argument: SIGMODE, if SIGMODE=0 then the CCDs may be unstable and stop working." ) raise ValueError if not ((0 <= WDW <= 7 or WDW == 128) and type(WDW) == int): Logger.error("Invalid argument: WDW") raise ValueError # if( WDW == 7 and JPEGQ <= 100): # Logger.error('Invalid argument: WDW == 7, but JPEGQ <= 100') # raise ValueError # if( 0 <= WDW <= 4 and JPEGQ >= 101): # Logger.error('Invalid argument: 0 <= WDW <= 4, but JPEGQ >= 101') # raise ValueError if not (0 <= JPEGQ <= 100 and type(JPEGQ) == int): Logger.error("Invalid argument: JPEGQ") raise ValueError if not (0 <= GAIN <= 7 and type(GAIN) == int): Logger.error("Invalid argument: GAIN") raise ValueError if not (0 <= NFLUSH <= 1023 and type(NFLUSH) == int): Logger.error("Invalid argument: NFLUSH") raise ValueError if not (NROW * NRBIN + NRSKIP <= 511): Logger.error("Invalid argument: NROW * NRBIN + NRSKIP exceeds 511") raise ValueError if not ((NCOL + 1) * NCBIN * 2**NCBINFPGA + NCSKIP <= 2048): Logger.error( "Invalid argument: (NCOL+1) * NCBIN * 2^NCBINFPGA + NCSKIP exceeds 2048" ) raise ValueError T_readout, T_delay, T_Extra = calculate_time_per_row( NCOL=NCOL, NCBIN=NCBIN, NCBINFPGA=NCBINFPGA, NRSKIP=NRSKIP, NROW=NROW, NRBIN=NRBIN, NFLUSH=NFLUSH, ) ReadOutTime = T_readout + T_delay + T_Extra # Logger.debug('ReadOutTime = '+str(ReadOutTime)) if not (0 <= TEXPMS <= 32000 and TEXPMS + ReadOutTime < TEXPIMS): Logger.error( "Invalid argument: 32000 < TEXPMS < 0 or TEXPMS + ReadOutTime > TEXPIMS" ) raise ValueError if not (type(TEXPMS) == int and type(TEXPIMS) == int): Logger.error("Invalid argument: TEXPMS or TEXPIMS is not an integer") raise TypeError etree.SubElement(root[1], "command", mnemonic="TC_pafCCD") etree.SubElement(root[1][len(root[1]) - 1], "relativeTime") root[1][len(root[1]) - 1][0].text = str(int(relativeTime)) etree.SubElement(root[1][len(root[1]) - 1], "comment") root[1][len(root[1]) - 1][1].text = comment etree.SubElement(root[1][len(root[1]) - 1], "tcArguments") etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="CCDSEL") root[1][len(root[1]) - 1][2][0].text = str(CCDSEL) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="PWR") root[1][len(root[1]) - 1][2][1].text = str(PWR) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="WDW") root[1][len(root[1]) - 1][2][2].text = str(WDW) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="JPEGQ") root[1][len(root[1]) - 1][2][3].text = str(JPEGQ) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="SYNC") root[1][len(root[1]) - 1][2][4].text = str(SYNC) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="TEXPIMS") root[1][len(root[1]) - 1][2][5].text = str(TEXPIMS) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="TEXPMS") root[1][len(root[1]) - 1][2][6].text = str(TEXPMS) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="GAIN") root[1][len(root[1]) - 1][2][7].text = str(GAIN) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="NFLUSH") root[1][len(root[1]) - 1][2][8].text = str(NFLUSH) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="NRSKIP") root[1][len(root[1]) - 1][2][9].text = str(NRSKIP) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="NRBIN") root[1][len(root[1]) - 1][2][10].text = str(NRBIN) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="NROW") root[1][len(root[1]) - 1][2][11].text = str(NROW) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="NCSKIP") root[1][len(root[1]) - 1][2][12].text = str(NCSKIP) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="NCBIN") root[1][len(root[1]) - 1][2][13].text = str(NCBIN) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="NCOL") root[1][len(root[1]) - 1][2][14].text = str(NCOL) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="NCBINFPGA") root[1][len(root[1]) - 1][2][15].text = str(NCBINFPGA) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="SIGMODE") root[1][len(root[1]) - 1][2][16].text = str(SIGMODE) incremented_time = relativeTime + Timeline_settings["CMD_separation"] _Globals.latestRelativeTime = relativeTime return incremented_time
def main(argv): if len(argv) < 3: print("usage: ./run.py </path/to/genre.csv> <output_dir>") sys.exit(1) genre_csv = argv[1] out_dir = argv[2] filter_dir = argv[3] if not os.path.isfile(genre_csv): print("Not a file: " + str(genre_csv)) sys.exit(1) if not os.path.isdir(out_dir): print("Not a directory: " + str(out_dir)) sys.exit(1) filter_books = os.listdir(filter_dir) genres = {} genre_id = 1 with open(genre_csv, newline='') as csvfile: csvreader = csv.reader(csvfile, delimiter=',', quotechar='"') for row in csvreader: genre = row[0] identifier = row[1] title = row[2] available = row[3] if genre is None or genre == "": continue if identifier not in filter_books: continue if genre not in genres: genres[genre] = {"id": str(genre_id), "books": {}} genre_id += 1 genres[genre]["books"][identifier] = { "title": title, "available": available } sorted_genres = sorted([genre for genre in genres], key=lambda genre: len(genres[genre]["books"]), reverse=True) genre_list_csv = os.path.join(out_dir, "genres.csv") with open(genre_list_csv, 'w', newline='') as csvfile: csvwriter = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL) for genre in sorted_genres: csvwriter.writerow([ genre, genres[genre]["id"] + ".xml", str(len(genres[genre]["books"])) ]) for genre in sorted_genres: genre_xml = os.path.join(out_dir, genres[genre]["id"] + ".xml") # <active_loans>...</active_loans> active_loans = ET.Element("active_loans") # <result><success>true</success><message/></result> result = ET.SubElement(active_loans, "result") success = ET.SubElement(result, "success") success.text = "true" message = ET.SubElement(result, "message") # <data><books>...</books></data> data = ET.SubElement(active_loans, "data") data.append(ET.Comment(" " + genre + " ")) books = ET.SubElement(data, "books") for identifier in genres[genre]["books"]: # <book><id>...</id><title>...</title></book> book = ET.SubElement(books, "book") id_element = ET.SubElement(book, "id") id_element.text = identifier title = ET.SubElement(book, "title") title.text = genres[genre]["books"][identifier]["title"] book.append( ET.Comment(" " + genres[genre]["books"][identifier]["available"] + " ")) tree = ET.ElementTree(active_loans) tree.write(genre_xml, xml_declaration=True, encoding='UTF-8', pretty_print=True)
def TC_pafUpload(root, relativeTime, PINDEX, PTOTAL, WFLASH, NIMG, IMG, Timeline_settings, comment=""): if not (_Globals.latestRelativeTime <= relativeTime <= Timeline_settings["duration"]): Logger.error( "Invalid argument: negative relativeTime, decreasing relativeTime, exceeding timeline duration" ) raise ValueError if not (0 <= PINDEX and type(PINDEX) == int): Logger.error("Invalid argument: PINDEX") raise ValueError if not (0 <= PTOTAL and type(PTOTAL) == int): Logger.error("Invalid argument: PTOTAL") raise ValueError if not (PINDEX <= (PTOTAL - 1)): Logger.error("Invalid argument: PINDEX or PTOTAL") raise ValueError if not (NIMG == len(IMG) and type(NIMG) == int): Logger.error("Invalid argument: NIMG") raise ValueError if not (0 <= WFLASH <= 1 and type(WFLASH) == int): Logger.error("Invalid argument: WFLASH") raise ValueError etree.SubElement(root[1], "command", mnemonic="TC_pafUPLOAD") etree.SubElement(root[1][len(root[1]) - 1], "relativeTime") root[1][len(root[1]) - 1][0].text = str(int(relativeTime)) etree.SubElement(root[1][len(root[1]) - 1], "comment") root[1][len(root[1]) - 1][1].text = comment etree.SubElement(root[1][len(root[1]) - 1], "tcArguments") etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="PINDEX") root[1][len(root[1]) - 1][2][0].text = str(PINDEX) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="PTOTAL") root[1][len(root[1]) - 1][2][1].text = str(PTOTAL) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="WFLASH") root[1][len(root[1]) - 1][2][2].text = str(WFLASH) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="NIMG") root[1][len(root[1]) - 1][2][3].text = str(NIMG) x = 4 for Image in IMG: if not (0 <= Image <= 255 and type(Image) == int): Logger.error("Invalid argument: Image") raise ValueError etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="IMG") root[1][len(root[1]) - 1][2][x].text = str(Image) x = x + 1 incremented_time = relativeTime + Timeline_settings["CMD_separation"] _Globals.latestRelativeTime = relativeTime return incremented_time
def effect(self): if len(self.options.ids) < 2: inkex.errormsg("This extension requires two selected paths.") exit() obj = self.svg.selected[self.options.ids[0]] envelope = self.svg.selected[self.options.ids[1]] if obj.get(inkex.addNS('type', 'sodipodi')): inkex.errormsg( "The first selected object is of type '%s'.\nTry using the procedure Path->Object to Path." % obj.get(inkex.addNS('type', 'sodipodi'))) exit() if obj.tag == inkex.addNS('path', 'svg') or obj.tag == inkex.addNS( 'g', 'svg'): if envelope.tag == inkex.addNS('path', 'svg'): mat = envelope.transform * Transform([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]) path = CubicSuperPath(envelope.get('d')) Path(path).transform(mat) absolute_envelope_path = envelope.get('d') # inkex.debug(absolute_envelope_path) coords_to_project = self.envelope2coords( absolute_envelope_path) if obj.tag == inkex.addNS('path', 'svg'): mat = obj.transform * Transform([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]) absolute_d = str(Path(obj.get('d'))) path = CubicSuperPath(absolute_d) Path(path).transform(mat) absolute_object_path = str(path) # inkex.debug(absolute_object_path) elif obj.tag == inkex.addNS('g', 'svg'): absolute_object_path = "" for p in obj.iterfind( ".//{http://www.w3.org/2000/svg}path"): absolute_d = str(Path(p.get('d'))) mat = p.transform * Transform([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]) path = CubicSuperPath(absolute_d) Path(path).transform(mat) absolute_object_path += str(Path(path)) # inkex.debug(absolute_object_path) new_path = projection(absolute_object_path, coords_to_project) attributes = {'d': new_path} new_element = etree.SubElement(self.svg.get_current_layer(), inkex.addNS('path', 'svg'), attributes) else: if envelope.tag == inkex.addNS('g', 'svg'): inkex.errormsg( "The second selected object is a group, not a path.\nTry using the procedure Object->Ungroup." ) else: inkex.errormsg( "The second selected object is not a path.\nTry using the procedure Path->Object to Path." ) exit() else: inkex.errormsg( "The first selected object is not a path.\nTry using the procedure Path->Object to Path." ) exit()
def parse_html(data, log=None, decoder=None, preprocessor=None, filename='<string>', non_html_file_tags=frozenset()): if log is None: from calibre.utils.logging import default_log log = default_log filename = force_unicode(filename, enc=filesystem_encoding) if not isinstance(data, unicode): if decoder is not None: data = decoder(data) else: data = xml_to_unicode(data)[0] data = strip_encoding_declarations(data) if preprocessor is not None: data = preprocessor(data) # There could be null bytes in data if it had � entities in it data = data.replace('\0', '') # Remove DOCTYPE declaration as it messes up parsing # In particular, it causes tostring to insert xmlns # declarations, which messes up the coercing logic idx = data.find('<html') if idx == -1: idx = data.find('<HTML') if idx > -1: pre = data[:idx] data = data[idx:] if '<!DOCTYPE' in pre: # Handle user defined entities user_entities = {} for match in re.finditer(r'<!ENTITY\s+(\S+)\s+([^>]+)', pre): val = match.group(2) if val.startswith('"') and val.endswith('"'): val = val[1:-1] user_entities[match.group(1)] = val if user_entities: pat = re.compile(r'&(%s);'%('|'.join(user_entities.keys()))) data = pat.sub(lambda m:user_entities[m.group(1)], data) data = clean_word_doc(data, log) # Setting huge_tree=True causes crashes in windows with large files parser = etree.XMLParser(no_network=True) # Try with more & more drastic measures to parse try: data = etree.fromstring(data, parser=parser) except etree.XMLSyntaxError: log.debug('Initial parse failed, using more' ' forgiving parsers') data = xml_replace_entities(data) try: data = etree.fromstring(data, parser=parser) except etree.XMLSyntaxError: log.debug('Parsing %s as HTML' % filename) try: data = html5_parse(data) except: log.exception( 'HTML 5 parsing failed, falling back to older parsers') data = _html4_parse(data) if data.tag == 'HTML': # Lower case all tag and attribute names data.tag = data.tag.lower() for x in data.iterdescendants(): try: x.tag = x.tag.lower() for key, val in list(x.attrib.iteritems()): del x.attrib[key] key = key.lower() x.attrib[key] = val except: pass if barename(data.tag) != 'html': if barename(data.tag) in non_html_file_tags: raise NotHTML(data.tag) log.warn('File %r does not appear to be (X)HTML'%filename) nroot = etree.fromstring('<html></html>') has_body = False for child in list(data): if isinstance(child.tag, (unicode, str)) and barename(child.tag) == 'body': has_body = True break parent = nroot if not has_body: log.warn('File %r appears to be a HTML fragment'%filename) nroot = etree.fromstring('<html><body/></html>') parent = nroot[0] for child in list(data.iter()): oparent = child.getparent() if oparent is not None: oparent.remove(child) parent.append(child) data = nroot # Force into the XHTML namespace if not namespace(data.tag): log.warn('Forcing', filename, 'into XHTML namespace') data.attrib['xmlns'] = XHTML_NS data = etree.tostring(data, encoding=unicode) try: data = etree.fromstring(data, parser=parser) except: data = data.replace(':=', '=').replace(':>', '>') data = data.replace('<http:/>', '') try: data = etree.fromstring(data, parser=parser) except etree.XMLSyntaxError: log.warn('Stripping comments from %s'% filename) data = re.compile(r'<!--.*?-->', re.DOTALL).sub('', data) data = data.replace( "<?xml version='1.0' encoding='utf-8'?><o:p></o:p>", '') data = data.replace("<?xml version='1.0' encoding='utf-8'??>", '') try: data = etree.fromstring(data, parser=RECOVER_PARSER) except etree.XMLSyntaxError: log.warn('Stripping meta tags from %s'% filename) data = re.sub(r'<meta\s+[^>]+?>', '', data) data = etree.fromstring(data, parser=RECOVER_PARSER) elif namespace(data.tag) != XHTML_NS: # OEB_DOC_NS, but possibly others ns = namespace(data.tag) attrib = dict(data.attrib) nroot = etree.Element(XHTML('html'), nsmap={None: XHTML_NS}, attrib=attrib) for elem in data.iterdescendants(): if isinstance(elem.tag, basestring) and \ namespace(elem.tag) == ns: elem.tag = XHTML(barename(elem.tag)) for elem in data: nroot.append(elem) data = nroot data = merge_multiple_html_heads_and_bodies(data, log) # Ensure has a <head/> head = xpath(data, '/h:html/h:head') head = head[0] if head else None if head is None: log.warn('File %s missing <head/> element' % filename) head = etree.Element(XHTML('head')) data.insert(0, head) title = etree.SubElement(head, XHTML('title')) title.text = _('Unknown') elif not xpath(data, '/h:html/h:head/h:title'): title = etree.SubElement(head, XHTML('title')) title.text = _('Unknown') # Ensure <title> is not empty title = xpath(data, '/h:html/h:head/h:title')[0] if not title.text or not title.text.strip(): title.text = _('Unknown') # Remove any encoding-specifying <meta/> elements for meta in META_XP(data): meta.getparent().remove(meta) meta = etree.SubElement(head, XHTML('meta'), attrib={'http-equiv': 'Content-Type'}) meta.set('content', 'text/html; charset=utf-8') # Ensure content is second # attribute # Ensure has a <body/> if not xpath(data, '/h:html/h:body'): body = xpath(data, '//h:body') if body: body = body[0] body.getparent().remove(body) data.append(body) else: log.warn('File %s missing <body/> element' % filename) etree.SubElement(data, XHTML('body')) # Remove microsoft office markup r = [x for x in data.iterdescendants(etree.Element) if 'microsoft-com' in x.tag] for x in r: x.tag = XHTML('span') # Remove lang redefinition inserted by the amazing Microsoft Word! body = xpath(data, '/h:html/h:body')[0] for key in list(body.attrib.keys()): if key == 'lang' or key.endswith('}lang'): body.attrib.pop(key) def remove_elem(a): p = a.getparent() idx = p.index(a) -1 p.remove(a) if a.tail: if idx <= 0: if p.text is None: p.text = '' p.text += a.tail else: if p[idx].tail is None: p[idx].tail = '' p[idx].tail += a.tail # Remove hyperlinks with no content as they cause rendering # artifacts in browser based renderers # Also remove empty <b>, <u> and <i> tags for a in xpath(data, '//h:a[@href]|//h:i|//h:b|//h:u'): if a.get('id', None) is None and a.get('name', None) is None \ and len(a) == 0 and not a.text: remove_elem(a) # Convert <br>s with content into paragraphs as ADE can't handle # them for br in xpath(data, '//h:br'): if len(br) > 0 or br.text: br.tag = XHTML('div') # Remove any stray text in the <head> section and format it nicely data.text = '\n ' head = xpath(data, '//h:head') if head: head = head[0] head.text = '\n ' head.tail = '\n ' for child in head: child.tail = '\n ' child.tail = '\n ' return data
def embed_all_fonts(container, stats, report): all_font_rules = tuple(itervalues(stats.all_font_rules)) warned = set() rules, nrules = [], {} modified = set() for path in container.spine_items: name = container.abspath_to_name(path) fu = stats.font_usage_map.get(name, None) fs = stats.font_spec_map.get(name, None) fr = stats.font_rule_map.get(name, None) if None in (fs, fu, fr): continue fs = {icu_lower(x) for x in fs} for font in itervalues(fu): if icu_lower(font['font-family']) not in fs: continue rule = matching_rule(font, fr) if rule is None: # This font was not already embedded in this HTML file, before # processing started key = font_key(font) rule = nrules.get(key) if rule is None: rule = embed_font(container, font, all_font_rules, report, warned) if rule is not None: rules.append(rule) nrules[key] = rule modified.add(name) stats.font_stats[rule['name']] = font['text'] else: # This font was previously embedded by this code, update its stats stats.font_stats[rule['name']] |= font['text'] modified.add(name) if not rules: report(_('No embeddable fonts found')) return False # Write out CSS rules = [ ';\n\t'.join( '%s: %s' % (k, '"%s"' % v if k == 'font-family' else v) for k, v in iteritems(rulel) if (k in props and props[k] != v and v != '400') or k == 'src') for rulel in rules ] css = '\n\n'.join(['@font-face {\n\t%s\n}' % r for r in rules]) item = container.generate_item('fonts.css', id_prefix='font_embed') name = container.href_to_name(item.get('href'), container.opf_name) with container.open(name, 'wb') as out: out.write(css.encode('utf-8')) # Add link to CSS in all files that need it for spine_name in modified: root = container.parsed(spine_name) head = root.xpath('//*[local-name()="head"][1]')[0] href = container.name_to_href(name, spine_name) etree.SubElement(head, XHTML('link'), rel='stylesheet', type='text/css', href=href).tail = '\n' container.dirty(spine_name) return True
def write_into(self, elem): ec = ET.SubElement(elem, 'Clan', {'name': self.name, 'id': self.id})
def TC_acfLimbPointingAltitudeOffset(root, relativeTime, Initial, Final, Rate, Timeline_settings, comment=""): """Schedules Pointing Command If the desired pointing altitude is already set, the maximum TEXPMS last set with a CCD_macro is added to *relativeTime*. Otherwise if Rate == 0, a delay equal to *Timeline_settings['pointing_stabilization']* is added to *relativeTime*. If Rate != 0, only *Timeline_settings['CMD_separation']* is added to *relativeTime* to allow the payload to enter operation mode during the sweep. """ if not (_Globals.latestRelativeTime <= relativeTime <= Timeline_settings["duration"]): Logger.error( "Invalid argument: negative relativeTime, decreasing relativeTime, exceeding timeline duration" ) raise ValueError if not (-60000 <= Initial <= 230000 and type(Initial) == int): Logger.error("Invalid argument: Initial") raise ValueError if not (-60000 <= Final <= 230000 and type(Final) == int): Logger.error("Invalid argument: Final") raise ValueError if not (-5000 <= Rate <= 5000 and (type(Rate) == int or type(Rate) == float)): Logger.error("Invalid argument: Rate") raise ValueError if Rate != 0 and sign(Final - Initial) != sign(Rate): Logger.error("Invalid argument: sign(Final - Initial) != sign(Rate)") raise ValueError current_pointing = _Globals.current_pointing Logger.debug("current_pointing: " + str(current_pointing)) Logger.debug("Initial: " + str(Initial) + ", Final: " + str(Final) + ", Rate: " + str(Rate)) # if(current_pointing != Final or current_pointing != Initial ): # Logger.debug('Scheduling pointing command') etree.SubElement(root[1], "command", mnemonic="TC_acfLimbPointingAltitudeOffset") etree.SubElement(root[1][len(root[1]) - 1], "relativeTime") root[1][len(root[1]) - 1][0].text = str(int(relativeTime)) etree.SubElement(root[1][len(root[1]) - 1], "comment") root[1][len(root[1]) - 1][1].text = comment etree.SubElement(root[1][len(root[1]) - 1], "tcArguments") etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="Initial") root[1][len(root[1]) - 1][2][0].text = str(Initial) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="Final") root[1][len(root[1]) - 1][2][1].text = str(Final) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="Rate") root[1][len(root[1]) - 1][2][2].text = str(Rate) if Rate != 0: incremented_time = relativeTime + Timeline_settings["CMD_separation"] _Globals.current_pointing = None Logger.debug( 'Rate != 0, meaning a sweep is scheduled. Next CMD is only staggered by Timeline_settings["CMD_separation"]' ) elif current_pointing == Final and current_pointing == Initial: _Globals.current_pointing = Final incremented_time = (relativeTime + int(ceil(_Globals.LargestSetTEXPMS / 1000)) + Timeline_settings["CMD_separation"]) Logger.debug( 'Satellite is already orientated the right way. Next CMD is staggered by Timeline_settings["CMD_separation"]+_Globals.LargestSetTEXPMS/1000, rounded up.' ) Logger.debug("_Globals.LargestSetTEXPMS: " + str(_Globals.LargestSetTEXPMS)) elif Rate == 0: _Globals.current_pointing = Final incremented_time = relativeTime + Timeline_settings[ "pointing_stabilization"] Logger.debug( 'Satellite is not orientated the right way. Next CMD is staggered by Timeline_settings["pointing_stabilization"]' ) # else: # Logger.debug('Skipping pointing command as satellite is already oriented the desired way') # incremented_time = relativeTime # incremented_time = relativeTime+Timeline_settings['CMD_separation'] _Globals.latestRelativeTime = relativeTime return incremented_time
for key, value in read_dictionary.items(): urls.append(key) for k,v in value.items(): for k in v: urls.append(k) #Delete duplicates. print (len(urls)) urls = f7(urls) print(len(urls)) #print (urls) #Translate to xml. #We can add speak context later root = ET.Element('AIMind') rroot = ET.SubElement(root, 'Root') rroot.attrib["id"] = "0" features = ET.SubElement(root, 'Features') #Fill in the features for url in urls: feature = ET.SubElement(features, 'Feature') feature.attrib["data"] = url feature.attrib["zh-data"] = "" feature.attrib["id"] = str(urls.index(url)+1) feature.attrib["uri"] = "" neighbors = ET.SubElement(feature, 'neighbors') #Find the neighbor in the dict. if url in read_dictionary.keys(): for tags,sequence in read_dictionary[url].items():
def TC_pafCCDSYNCHRONIZE(root, relativeTime, CCDSEL, NCCD, TEXPIOFS, Timeline_settings, comment=""): "Note: CCDSYNCHRONIZE takes one TEXPIMS cycle to execute." if not (_Globals.latestRelativeTime <= relativeTime <= Timeline_settings["duration"]): Logger.error( "Invalid argument: negative relativeTime, decreasing relativeTime, exceeding timeline duration" ) raise ValueError if not (3 <= CCDSEL <= 127 and CCDSEL not in [4, 8, 16, 32, 64] and type(CCDSEL) == int): Logger.error("Invalid argument: CCDSEL") raise ValueError if not (2 <= NCCD <= 7 and NCCD == bin(CCDSEL).count("1") and type(NCCD) == int): Logger.error( "Invalid argument: More than 7 CCDs chosen (or less than 2) or NCCD does not coincide with CCDSEL" ) raise ValueError if not (len(TEXPIOFS) == NCCD): Logger.error( "Invalid argument: Number of CCDs (NCCD) does not coincide with the number of time-offsets (TEXPIOFS)." ) raise ValueError for TEXPIOFS_value in TEXPIOFS: if not (0 <= TEXPIOFS_value <= 12000): Logger.error( "Invalid argument: 0 > TEXPIOFS_value, TEXPIOFS_value > 12000") raise ValueError Logger.debug("CCDSEL: " + str(CCDSEL)) Logger.debug("NCCD: " + str(NCCD)) Logger.debug("TEXPIOFS: " + str(TEXPIOFS)) etree.SubElement(root[1], "command", mnemonic="TC_pafCCDSYNCHRONIZE") etree.SubElement(root[1][len(root[1]) - 1], "relativeTime") root[1][len(root[1]) - 1][0].text = str(int(relativeTime)) etree.SubElement(root[1][len(root[1]) - 1], "comment") root[1][len(root[1]) - 1][1].text = comment etree.SubElement(root[1][len(root[1]) - 1], "tcArguments") etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="CCDSEL") root[1][len(root[1]) - 1][2][0].text = str(CCDSEL) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="NCCD") root[1][len(root[1]) - 1][2][1].text = str(NCCD) Leading_CCD_selected = False x = 2 for TimeOffset in TEXPIOFS: if not (0 <= TimeOffset <= 12000 and TimeOffset / 10 == round( TimeOffset / 10, 0) and type(TimeOffset) == int): Logger.error("Invalid argument: TEXPIOFS") raise ValueError if TimeOffset == 0: Leading_CCD_selected = True etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="TEXPIOFS") root[1][len(root[1]) - 1][2][x].text = str(TimeOffset) x = x + 1 if not (Leading_CCD_selected == True): Logger.error( "Invalid argument: Any TEXPIOFS not set to 0 -> No leading CCD selected" ) raise ValueError incremented_time = (relativeTime + Timeline_settings["CMD_separation"] + Timeline_settings["CCDSYNC_Waittime"]) _Globals.latestRelativeTime = relativeTime return incremented_time
def encode_caption_object(cls, obj): ann_elem = cls.encode_annotation_base(obj) ET.SubElement(ann_elem, 'caption').text = str(obj.caption) return ann_elem
def TC_pafCCDBadColumn(root, relativeTime, CCDSEL, NBC, BC, Timeline_settings, comment=""): if not (_Globals.latestRelativeTime <= relativeTime <= Timeline_settings["duration"]): Logger.error( "Invalid argument: negative relativeTime, decreasing relativeTime, exceeding timeline duration" ) raise ValueError if not (1 <= CCDSEL <= 127 and type(CCDSEL) == int): Logger.error("Invalid argument: CCDSEL") raise ValueError if not (0 <= NBC <= 63 and type(NBC) == int): Logger.error( "Invalid argument: More than 63 BadColumns chosen (or less than 0)" ) raise ValueError if not (len(BC) == NBC): Logger.error( "Invalid argument: Number of BadColumns (NBC) does not coincide with Bad Column (BC)." ) raise ValueError etree.SubElement(root[1], "command", mnemonic="TC_pafCCDBadColumn") etree.SubElement(root[1][len(root[1]) - 1], "relativeTime") root[1][len(root[1]) - 1][0].text = str(int(relativeTime)) etree.SubElement(root[1][len(root[1]) - 1], "comment") root[1][len(root[1]) - 1][1].text = comment etree.SubElement(root[1][len(root[1]) - 1], "tcArguments") etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="CCDSEL") root[1][len(root[1]) - 1][2][0].text = str(CCDSEL) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="NBC") root[1][len(root[1]) - 1][2][1].text = str(NBC) x = 2 for BadColumn in BC: if not (4 <= BadColumn <= 2047 and type(BadColumn) == int): Logger.error("Invalid argument: BC") raise ValueError etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="BC") root[1][len(root[1]) - 1][2][x].text = str(BadColumn) x = x + 1 incremented_time = relativeTime + Timeline_settings["CMD_separation"] _Globals.latestRelativeTime = relativeTime return incremented_time
def main(): if len(sys.argv) != 4: print( 'usage: python gaff2xml.py [path_to_smarts_defs] [path_to_gaff_dat_file] [path_to_output_xml_file]' ) exit(1) else: smarts_def_path = sys.argv[1] gaff_parm_path = sys.argv[2] xml_out_path = sys.argv[3] start_mass = 1 end_mass = 84 start_lj = 7119 end_lj = 7202 start_bond = 86 end_bond = 1014 start_angle = 1015 end_angle = 6330 start_torsion = 6331 end_torsion = 7075 start_improper = 7076 end_improper = 7114 data = [] with open(gaff_parm_path) as f: for line in f: data.append(line.strip()) # Create dict with smarts definitions smarts = {} smarts_tree = ET.parse(smarts_def_path) defs = smarts_tree.getroot() for atype in defs.findall('Definition'): name = atype.get('name') iclass = atype.get('class') idef = atype.get('def') over = atype.get('overrides') desc = atype.get('desc') doi = atype.get('doi') smarts.update({name: [iclass, idef, over, desc, doi]}) # Extract gaff parameters mass_parms = { x.split()[0]: x.split()[1] for x in data[start_mass:end_mass] } lj_parms = {x.split()[0]: (x.split()[1:3]) for x in data[start_lj:end_lj]} bond_parms = [x for x in data[start_bond:end_bond]] angle_parms = [x for x in data[start_angle:end_angle]] torsion_parms = [x for x in data[start_torsion:end_torsion]] improper_parms = [x for x in data[start_improper:end_improper]] # Create our force field root = ET.Element("ForceField") atomtypes = ET.SubElement(root, 'AtomTypes') nonbonded = ET.SubElement(root, 'NonbondedForce') nonbonded.set('coulomb14scale', '0.833333333') nonbonded.set('lj14scale', '0.5') for atype in smarts.keys(): atomtype = ET.SubElement(atomtypes, 'Type') nb_force = ET.SubElement(nonbonded, 'Atom') atomtype.set('name', atype) iclass = smarts[atype][0] atomtype.set('class', iclass) atomtype.set('element', determine_element(mass_parms[iclass])) atomtype.set('mass', mass_parms[iclass]) if smarts[atype][1] is not None: atomtype.set('def', smarts[atype][1]) if smarts[atype][2] is not None: atomtype.set('overrides', smarts[atype][2]) atomtype.set('desc', smarts[atype][3]) atomtype.set('doi', smarts[atype][4]) nb_force.set('type', atype) nb_force.set('charge', '0.0') nb_force.set('sigma', convert_sigma(lj_parms[iclass][0])) nb_force.set('epsilon', convert_epsilon(lj_parms[iclass][1])) # Bonds bond_forces = ET.SubElement(root, 'HarmonicBondForce') for bond in bond_parms: bond_force = ET.SubElement(bond_forces, 'Bond') classes = re.split('\s+-|-|\s+', bond[0:5]) parms = bond[5:24].split() bond_force.set('class1', classes[0]) bond_force.set('class2', classes[1]) bond_force.set('length', convert_bondlength(parms[1])) bond_force.set('k', convert_bondk(parms[0])) # Angles angle_forces = ET.SubElement(root, 'HarmonicAngleForce') for angle in angle_parms: angle_force = ET.SubElement(angle_forces, 'Angle') classes = re.split('\s+-|-|\s+', angle[0:8]) parms = angle[8:30].split() angle_force.set('class1', classes[0]) angle_force.set('class2', classes[1]) angle_force.set('class3', classes[2]) angle_force.set('angle', convert_theta(parms[1])) angle_force.set('k', convert_anglek(parms[0])) # Proper dihedrals torsion_forces = ET.SubElement(root, 'PeriodicTorsionForce') continue_reading = False for torsion in torsion_parms: # Amber leap convention...if periodicity < 0 it means it is # there will follow additional torsional terms for the same set of atoms # See http://ambermd.org/FileFormats.php#parm.dat classes = re.split('\s+-|-|\s+', torsion[0:11]) parms = torsion[11:54].split() if continue_reading == False: torsion_force = ET.SubElement(torsion_forces, 'Proper') torsion_ctr = 1 if classes[0].upper() == 'X': torsion_force.set('class1', '') else: torsion_force.set('class1', classes[0]) if classes[1].upper() == 'X': torsion_force.set('class2', '') else: torsion_force.set('class2', classes[1]) if classes[2].upper() == 'X': torsion_force.set('class3', '') else: torsion_force.set('class3', classes[2]) if classes[3].upper() == 'X': torsion_force.set('class4', '') else: torsion_force.set('class4', classes[3]) else: torsion_ctr += 1 if float(parms[3]) < 0.0: continue_reading = True else: continue_reading = False name = 'periodicity' + str(torsion_ctr) torsion_force.set(name, str(int(abs(float(parms[3]))))) name = 'k' + str(torsion_ctr) torsion_force.set(name, convert_torsionk(parms[1], parms[0])) name = 'phase' + str(torsion_ctr) torsion_force.set(name, convert_theta(parms[2])) # Improper dihedrals continue_reading = False for torsion in improper_parms: classes = re.split('\s+-|-|\s+', torsion[0:11]) parms = torsion[11:54].split() if continue_reading == False: torsion_force = ET.SubElement(torsion_forces, 'Improper') torsion_ctr = 1 if classes[2].upper() == 'X': torsion_force.set('class1', '') else: torsion_force.set('class1', classes[2]) if classes[0].upper() == 'X': torsion_force.set('class2', '') else: torsion_force.set('class2', classes[0]) if classes[1].upper() == 'X': torsion_force.set('class3', '') else: torsion_force.set('class3', classes[1]) if classes[3].upper() == 'X': torsion_force.set('class4', '') else: torsion_force.set('class4', classes[3]) else: torsion_ctr += 1 if float(parms[2]) < 0.0: continue_reading = True else: continue_reading = False name = 'periodicity' + str(torsion_ctr) torsion_force.set(name, str(int(abs(float(parms[2]))))) name = 'k' + str(torsion_ctr) torsion_force.set(name, convert_improperk(parms[0])) name = 'phase' + str(torsion_ctr) torsion_force.set(name, convert_theta(parms[1])) # Write XML with SMARTS defs ET.ElementTree(root).write(xml_out_path, pretty_print=True)
def TC_pafCCDBIAS(root, relativeTime, CCDSEL, VGATE, VSUBST, VRD, VOD, Timeline_settings, comment=""): if not (_Globals.latestRelativeTime <= relativeTime <= Timeline_settings["duration"]): Logger.error( "Invalid argument: negative relativeTime, decreasing relativeTime, exceeding timeline duration" ) raise ValueError if not (1 <= CCDSEL <= 127 and type(CCDSEL) == int): Logger.error("Invalid argument: CCDSEL") raise ValueError if not (0 <= VGATE <= 255 and 0 <= VSUBST <= 255 and 0 <= VRD <= 255 and 0 <= VOD <= 255 and type(VGATE) == int and type(VSUBST) == int and type(VRD) == int and type(VOD) == int): Logger.error( "Invalid argument: CCDBIAS values are not set as integers, or too high or low" ) raise ValueError etree.SubElement(root[1], "command", mnemonic="TC_pafCCDBIAS") etree.SubElement(root[1][len(root[1]) - 1], "relativeTime") root[1][len(root[1]) - 1][0].text = str(int(relativeTime)) etree.SubElement(root[1][len(root[1]) - 1], "comment") root[1][len(root[1]) - 1][1].text = comment etree.SubElement(root[1][len(root[1]) - 1], "tcArguments") etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="CCDSEL") root[1][len(root[1]) - 1][2][0].text = str(CCDSEL) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="VGATE") root[1][len(root[1]) - 1][2][1].text = str(VGATE) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="VSUBST") root[1][len(root[1]) - 1][2][2].text = str(VSUBST) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="VRD") root[1][len(root[1]) - 1][2][3].text = str(VRD) etree.SubElement(root[1][len(root[1]) - 1][2], "tcArgument", mnemonic="VOD") root[1][len(root[1]) - 1][2][4].text = str(VOD) incremented_time = relativeTime + Timeline_settings["CMD_separation"] _Globals.latestRelativeTime = relativeTime return incremented_time
def parse_inline_content(self, tree, item): assert tree.get('contenttype') == TTNITF body_elt = tree.find('BODY') abstracts = [] for elt in body_elt.xpath('descendant::INGRESS/*'): abstracts.append(etree.tostring(elt, encoding='unicode', method='text')) item['abstract'] = '\n'.join(abstracts) for byline_elt in body_elt.xpath('.//BYLINE'): epost = byline_elt.get('EPOST', '') text = byline_elt.text parent = byline_elt.getparent() p_elt = etree.Element('p') a_elt = etree.SubElement(p_elt, 'a', {'href': epost}) a_elt.text = text parent.replace(byline_elt, p_elt) for dat_elt in body_elt.xpath('.//DAT'): try: ort = dat_elt.xpath('ORT/text()')[0] except IndexError: ort = '' try: source = dat_elt.xpath('SOURCE/text()')[0] except IndexError: source = '' parent = dat_elt.getparent() p_elt = etree.Element('p') p_elt.text = ort + ' / ' + source parent.replace(dat_elt, p_elt) etree.strip_tags(body_elt, 'TEXT', 'FAKTATEXT', 'BAKGRUNDTEXT', 'INGRESS', 'BRODTEXT') etree.strip_elements(body_elt, 'BILD', with_tail=False) for elt in body_elt.iter(tag=etree.Element): elt.attrib.clear() tag = elt.tag if tag in ('BODY', 'p', 'a'): continue elif tag == 'RUBRIK': elt.tag = 'h1' elif tag == 'P': elt.tag = 'p' elif tag == 'CITAT': elt.tag = 'blockquote' elif tag == 'MELLIS': elt.tag = 'h2' elif tag == 'FRAGA': elt.tag = 'p' elif tag == 'SVAR': elt.tag = 'p' elif tag == 'UL': elt.tag = 'ul' elif tag == 'LI': elt.tag = 'li' elif tag == 'TABELL': elt.tag = 'table' elif tag == 'TH': elt.tag = 'th' elif tag == 'TR': elt.tag = 'tr' elif tag == 'TD': elt.tag = 'td' else: logger.warning('unknown tag: {tag}'.format(tag=tag)) elt.tag = 'p' div_elt = etree.Element('div') div_elt[:] = body_elt[:] contents = [etree.tostring(e, encoding='unicode', method='html') for e in div_elt] return {'content': '\n'.join(contents)}
if __name__ == '__main__': pass import sys from sys import exit try: from lxml import etree print("running with lxml.etree") except ImportError: print "error loading library lxml" root = etree.Element("Project") email = etree.SubElement(root, "email") email.text = ("*****@*****.**") password = etree.SubElement(root, "password") password.text = ("SillyPassword") locales = etree.SubElement(root, "locales") language1 = etree.SubElement(locales,'Language',ID="1") picture = etree.SubElement(language1, "picture") picture.text = ("C:\Mixed\Upload\BF3.png") et = etree.ElementTree(root) et.write(sys.stdout, pretty_print=True)
def _add_body(self, envelope): return ElementTree.SubElement(envelope, '{%s}Body' % NS_SOAP_ENV)
def _append_node(name, text): n = etree.SubElement(config, name) n.text = text
def _add_enum_optimization(self, pull_elem): max_elem_elem = ElementTree.SubElement(pull_elem, '{%s}MaxElements' % NS_WSMAN) max_elem_elem.text = str(self.max_elems)
def create_tree(thing, parent=None, object_names=False): """Create an XML etree from a thing. Arguments: thing thing to interpret parent etree.Element to put subtree into default: create a new Element tree object_names experimental feature: if True tag names are object names instead of types Returns: etree.Element instance created """ try: grammar = type(thing).grammar except AttributeError: if isinstance(thing, list): grammar = pypeg2.csl(pypeg2.name()) else: grammar = pypeg2.word name = type(thing).__name__ if object_names: try: name = str(thing.name) name = name.replace(" ", "_") except AttributeError: pass if parent is None: me = etree.Element(name) else: me = etree.SubElement(parent, name) for e in pypeg2.attributes(grammar): if object_names and e.name == "name": if name != type(thing).__name__: continue key, value = e.name, getattr(thing, e.name, None) if value is not None: if pypeg2._issubclass(e.thing, (str, int, pypeg2.Literal)) \ or type(e.thing) == pypeg2._RegEx: me.set(key, str(value)) else: create_tree(value, me, object_names) if isinstance(thing, list): things = thing elif isinstance(thing, pypeg2.Namespace): things = thing.values() else: things = [] last = None for t in things: if type(t) == str: if last is not None: last.tail = str(t) else: me.text = str(t) else: last = create_tree(t, me, object_names) if isinstance(thing, str): me.text = str(thing) return me
def entry_xml_startdate(entry, entryxml): '''Convert the entry startDate to XML.''' if entry.not_empty('startDate'): sub_element = etree.SubElement(entryxml, 'startDate') sub_element.text = entry['startDate']
def _sign_file(cert, password, request): minimum = 1 maximum = 99999 signature_id = 'Signature%05d' % random.randint(minimum, maximum) signed_properties_id = (signature_id + '-SignedProperties%05d') % ( random.randint(minimum, maximum)) key_info_id = 'KeyInfo%05d' % random.randint(minimum, maximum) reference_id = 'Reference%05d' % random.randint(minimum, maximum) object_id = 'Object%05d' % random.randint(minimum, maximum) etsi = 'http://uri.etsi.org/01903/v1.3.2#' sig_policy_identifier = 'http://www.facturae.es/' \ 'politica_de_firma_formato_facturae/' \ 'politica_de_firma_formato_facturae_v3_1' \ '.pdf' sig_policy_hash_value = 'Ohixl6upD6av8N7pEvDABhEL6hM=' root = etree.fromstring(request) sign = xmlsig.template.create( c14n_method=xmlsig.constants.TransformInclC14N, sign_method=xmlsig.constants.TransformRsaSha1, name=signature_id, ns="ds") key_info = xmlsig.template.ensure_key_info(sign, name=key_info_id) x509_data = xmlsig.template.add_x509_data(key_info) xmlsig.template.x509_data_add_certificate(x509_data) xmlsig.template.add_key_value(key_info) certificate = crypto.load_pkcs12(base64.b64decode(cert), password) xmlsig.template.add_reference( sign, xmlsig.constants.TransformSha1, uri='#' + signed_properties_id, uri_type='http://uri.etsi.org/01903#SignedProperties') xmlsig.template.add_reference(sign, xmlsig.constants.TransformSha1, uri='#' + key_info_id) ref = xmlsig.template.add_reference(sign, xmlsig.constants.TransformSha1, name=reference_id, uri="") xmlsig.template.add_transform(ref, xmlsig.constants.TransformEnveloped) object_node = etree.SubElement( sign, etree.QName(xmlsig.constants.DSigNs, 'Object'), nsmap={'etsi': etsi}, attrib={xmlsig.constants.ID_ATTR: object_id}) qualifying_properties = etree.SubElement( object_node, etree.QName(etsi, 'QualifyingProperties'), attrib={'Target': '#' + signature_id}) signed_properties = etree.SubElement( qualifying_properties, etree.QName(etsi, 'SignedProperties'), attrib={xmlsig.constants.ID_ATTR: signed_properties_id}) signed_signature_properties = etree.SubElement( signed_properties, etree.QName(etsi, 'SignedSignatureProperties')) now = datetime.now().replace(microsecond=0, tzinfo=pytz.utc) etree.SubElement(signed_signature_properties, etree.QName( etsi, 'SigningTime')).text = now.isoformat() signing_certificate = etree.SubElement( signed_signature_properties, etree.QName(etsi, 'SigningCertificate')) signing_certificate_cert = etree.SubElement( signing_certificate, etree.QName(etsi, 'Cert')) cert_digest = etree.SubElement(signing_certificate_cert, etree.QName(etsi, 'CertDigest')) etree.SubElement( cert_digest, etree.QName(xmlsig.constants.DSigNs, 'DigestMethod'), attrib={'Algorithm': 'http://www.w3.org/2000/09/xmldsig#sha1'}) hash_cert = hashlib.sha1( crypto.dump_certificate(crypto.FILETYPE_ASN1, certificate.get_certificate())) etree.SubElement( cert_digest, etree.QName( xmlsig.constants.DSigNs, 'DigestValue')).text = base64.b64encode(hash_cert.digest()) issuer_serial = etree.SubElement(signing_certificate_cert, etree.QName(etsi, 'IssuerSerial')) etree.SubElement( issuer_serial, etree.QName(xmlsig.constants.DSigNs, 'X509IssuerName') ).text = xmlsig.utils.get_rdns_name( certificate.get_certificate().to_cryptography().issuer.rdns) etree.SubElement( issuer_serial, etree.QName( xmlsig.constants.DSigNs, 'X509SerialNumber')).text = str( certificate.get_certificate().get_serial_number()) signature_policy_identifier = etree.SubElement( signed_signature_properties, etree.QName(etsi, 'SignaturePolicyIdentifier')) signature_policy_id = etree.SubElement( signature_policy_identifier, etree.QName(etsi, 'SignaturePolicyId')) sig_policy_id = etree.SubElement(signature_policy_id, etree.QName(etsi, 'SigPolicyId')) etree.SubElement(sig_policy_id, etree.QName( etsi, 'Identifier')).text = sig_policy_identifier etree.SubElement(sig_policy_id, etree.QName( etsi, 'Description')).text = "Política de Firma FacturaE v3.1" sig_policy_hash = etree.SubElement( signature_policy_id, etree.QName(etsi, 'SigPolicyHash')) etree.SubElement( sig_policy_hash, etree.QName(xmlsig.constants.DSigNs, 'DigestMethod'), attrib={'Algorithm': 'http://www.w3.org/2000/09/xmldsig#sha1'}) hash_value = sig_policy_hash_value etree.SubElement( sig_policy_hash, etree.QName(xmlsig.constants.DSigNs, 'DigestValue')).text = hash_value signer_role = etree.SubElement(signed_signature_properties, etree.QName(etsi, 'SignerRole')) claimed_roles = etree.SubElement(signer_role, etree.QName(etsi, 'ClaimedRoles')) etree.SubElement(claimed_roles, etree.QName(etsi, 'ClaimedRole')).text = 'supplier' signed_data_object_properties = etree.SubElement( signed_properties, etree.QName(etsi, 'SignedDataObjectProperties')) data_object_format = etree.SubElement( signed_data_object_properties, etree.QName(etsi, 'DataObjectFormat'), attrib={'ObjectReference': '#' + reference_id}) etree.SubElement(data_object_format, etree.QName(etsi, 'Description')).text = 'Factura' etree.SubElement(data_object_format, etree.QName(etsi, 'MimeType')).text = 'text/xml' ctx = xmlsig.SignatureContext() key = crypto.load_pkcs12(base64.b64decode(cert), password) ctx.x509 = key.get_certificate().to_cryptography() ctx.public_key = ctx.x509.public_key() ctx.private_key = key.get_privatekey().to_cryptography_key() root.append(sign) ctx.sign(sign) return etree.tostring(root, xml_declaration=True, encoding='UTF-8')