def add_wallpaper(wallpaper_file_path, xml_file_path, name_text, options_text, pcolor_text='#ffffff', scolor_text='#000000'): print('in add_wallpaper') tree = get_tree(xml_file_path) wallpapers = tree.getroot() if wallpaper_is_there(wallpaper_file_path, wallpapers) != []: print('not adding.') return wallpaper = SubElement(wallpapers, 'wallpaper', deleted="false") name = SubElement(wallpaper, 'name') name.text = name_text filename = SubElement(wallpaper, 'filename') filename.text = wallpaper_file_path options = SubElement(wallpaper, 'options') options.text = options_text pcolor = SubElement(wallpaper, 'pcolor') pcolor.text = pcolor_text scolor = SubElement(wallpaper, 'scolor') scolor.text = scolor_text write_xml(tree, xml_file_path)
def process(self, lxmlNode): def prefixForNamespace(ns): if ns in prefixes: return prefixes[ns] return self._namespaceEnum.get(ns) def filterOac(nodes): result = [] for node in nodes: namespace, name = splitType(getAttrib(node, 'rdf:resource')) if namespace == namespaces['oa']: result.append(node) return result descriptions = xpath(lxmlNode, "//rdf:Description[rdf:type]") for description in descriptions: nodes = xpath(description, "rdf:type") oacNodes = filterOac(nodes) typeNode = nodes[0] if len(oacNodes) > 0: typeNode = oacNodes[0] parent = description.getparent() namespace, name = splitType(getAttrib(typeNode, 'rdf:resource')) prefix = prefixForNamespace(namespace) newNode = SubElement( parent, "{%(ns)s}%(tag)s" % {'ns': namespace, 'tag': name}, attrib=description.attrib, nsmap={prefix: namespace}) for child in (child for child in description.getchildren() if child != typeNode): newNode.append(child) parent.remove(description) yield self.all.process(lxmlNode=lxmlNode)
def process_GET(self, request): # parse input arguments args = {} args['pid'] = request.args0.get('project_id', '') args['vid'] = request.args0.get('volcano_id', '') args['start'] = request.args0.get('start_datetime', 'NOW()') args['end'] = request.args0.get('end_datetime', 'NOW()') # generate XML result xml = Element("query") # build up and execute query query = sql.text(""" SELECT event_type, count(event_type) AS event_count FROM "/seismology/event" WHERE datetime >= :start AND datetime <= :end GROUP BY event_type; """) try: result = self.env.db.query(query, **args) except: return toString(xml) for res in result: if not res.event_type: continue s = Sub(xml, "eventcount", type=res.event_type) s.text = str(res.event_count) return toString(xml)
def outputFunc(element, sets, token_kw): for setSpec, setName, setDescription in sets: e_set = SubElement(e_listSets, nsoai('set')) e_setSpec = SubElement(e_set, nsoai('setSpec')) e_setSpec.text = setSpec e_setName = SubElement(e_set, nsoai('setName')) e_setName.text = setName
def to_xml(self): q = Element('dictionary') q.attrib["value"] = basename(dirname(self.dct)) r = SubElement(q, "revision", value=str(self._svn_revision(dirname(self.dct))), timestamp=datetime.utcnow().isoformat(), checksum=self._checksum(open(self.dct, 'rb').read())) s = SubElement(r, 'corpus') s.attrib["value"] = basename(self.fn) s.attrib["checksum"] = self._checksum(open(self.fn, 'rb').read()) SubElement(r, 'percent').text = "%.2f" % self.get_coverage() SubElement(r, 'total').text = str(len(self.get_words())) SubElement(r, 'known').text = str(len(self.get_known_words())) SubElement(r, 'unknown').text = str(len(self.get_unknown_words())) wrx = re.compile(r"\^(.*)/") s = SubElement(r, 'top') for word, count in self.get_top_unknown_words(): SubElement(s, 'word', count=str(count)).text = wrx.search(word).group(1) s = SubElement(r, 'system') SubElement(s, 'time').text = "%.4f" % self.timer return ("coverage", etree.tostring(q))
def create_container(self, container_name): if self.ex_location_name: root = Element('CreateBucketConfiguration') child = SubElement(root, 'LocationConstraint') child.text = self.ex_location_name data = tostring(root) else: data = '' response = self.connection.request('/%s' % (container_name), data=data, method='PUT') if response.status == httplib.OK: container = Container(name=container_name, extra=None, driver=self) return container elif response.status == httplib.CONFLICT: raise InvalidContainerNameError( value='Container with this name already exists. The name must ' 'be unique among all the containers in the system', container_name=container_name, driver=self) elif response.status == httplib.BAD_REQUEST: raise InvalidContainerNameError(value='Container name contains ' + 'invalid characters.', container_name=container_name, driver=self) raise LibcloudError('Unexpected status code: %s' % (response.status), driver=self)
def copyHtml(sourceXml, targetHtml): for sourceChild in sourceXml.iterchildren(): targetChild = SubElement(targetHtml, sourceChild.localName if sourceChild.namespaceURI == XbrlConst.xhtml else sourceChild.tag) for attrTag, attrValue in sourceChild.items(): targetChild.set(attrTag, attrValue) copyHtml(sourceChild, targetChild)
def _to_xsd_type(self, type_prefix): xsd_uri = self.nsmap["xs"] # Create an xs:complexType element # Note: This type-name will not be prefixed with type_prefix, as it is # computed from the object's type-name (and we want to re-use it). tname = self.typename e = Element(QName(xsd_uri, "complexType"), attrib={"name": tname}) e1 = SubElement(e, QName(xsd_uri, "all")) # Create an xs:element for each field ys_prefix = type_prefix + "_" + self.typename tdefs = {} for k, yf in self.obj.iter_fields(exclude_properties=True): yv = yf.get(self.obj) yf1 = yf.bind(FieldContext(key=k, value=yv)) ys = serializer_for_field(yf1) ys.target_namespace = self.target_namespace ye, ye_tdefs = ys.to_xsd(type_prefix=ys_prefix) e1.append(ye) tdefs.update(ye_tdefs) tdefs[tname] = e return ("target:" + tname, tdefs)
def add_custom_option(self, optionstr, content): """ Add custom configuration option optionstr -- XPath-like expression for specifying XMvn configuration option location with '/' used as delimiter example: buildSettings/compilerSource content -- XML content to be added to specified node. Can be just text, XML node or multiple nodes. examples: someText <someNode>someText</someNode><someOtherNode/> """ node_names = optionstr.split("/") confpath = self.__get_current_config() root = self.__init_xml() par = root for node in node_names: par = SubElement(par, node) try: # wrap content into something to allow text content inserted = "<root>{0}</root>".format(content) contentRoot = ET.fromstring(inserted) par.text = contentRoot.text for element in contentRoot: par.append(element) except lxml.etree.XMLSyntaxError: raise XMvnConfigException("content is not valid content for XML node") self.__write_xml(confpath, root)
def list_packages(cls): xmlns = "urn:nbn:se:uu:ub:epc-schema:rs-location-mapping" def locns(loc): return "{%s}%s" % (xmlns, loc) xsi = "http://www.w3.org/2001/XMLSchema-instance" schemaLocation = "urn:nbn:se:uu:ub:epc-schema:rs-location-mapping http://urn.kb.se/resolve?urn=urn:nbn:se:uu:ub:epc-schema:rs-location-mapping&godirectly" records = Element("{" + xmlns + "}records", attrib={"{" + xsi + "}schemaLocation": schemaLocation}, nsmap={'xsi': xsi, None: xmlns}) q = Session.query(Package) q = q.filter(Package.name.ilike('urn:nbn:fi:csc-kata%')) pkgs = q.all() prot = SubElement(records, locns('protocol-version')) prot.text = '3.0' datestmp = SubElement(records, locns('datestamp'), attrib={'type': 'modified'}) now = datetime.datetime.now().isoformat() datestmp.text = now for pkg in pkgs: record = SubElement(records, locns('record')) header = SubElement(record, locns('header')) datestmp = SubElement(header, locns('datestamp'), attrib={'type': 'modified'}) datestmp.text = now identifier = SubElement(header, locns('identifier')) identifier.text = pkg.name destinations = SubElement(header, locns('destinations')) destination = SubElement(destinations, locns('destination'), attrib={'status': 'activated'}) datestamp = SubElement(destination, locns('datestamp'), attrib={'type': 'activated'}) url = SubElement(destination, locns('url')) url.text = "%s%s" % (config.get('ckan.site_url', ''), helpers.url_for(controller='package', action='read', id=pkg.name)) return tostring(records)
def handler(self, request, q_der): try: from django.db import connection connection.cursor() # Reconnect to mysqld if necessary self.start_new_transaction() serverCA = rpki.irdb.models.ServerCA.objects.get() rpkid = serverCA.ee_certificates.get(purpose = "rpkid") irdbd = serverCA.ee_certificates.get(purpose = "irdbd") q_cms = rpki.left_right.cms_msg(DER = q_der) q_msg = q_cms.unwrap((serverCA.certificate, rpkid.certificate)) self.cms_timestamp = q_cms.check_replay(self.cms_timestamp, request.path) if self.debug: logger.debug("Received: %s", ElementToString(q_msg)) if q_msg.get("type") != "query": raise rpki.exceptions.BadQuery("Message type is {}, expected query".format( q_msg.get("type"))) r_msg = Element(rpki.left_right.tag_msg, nsmap = rpki.left_right.nsmap, type = "reply", version = rpki.left_right.version) try: for q_pdu in q_msg: getattr(self, "handle_" + q_pdu.tag[len(rpki.left_right.xmlns):])(q_pdu, r_msg) except Exception, e: logger.exception("Exception processing PDU %r", q_pdu) r_pdu = SubElement(r_msg, rpki.left_right.tag_report_error, error_code = e.__class__.__name__) r_pdu.text = str(e) if q_pdu.get("tag") is not None: r_pdu.set("tag", q_pdu.get("tag")) if self.debug: logger.debug("Sending: %s", ElementToString(r_msg)) request.send_cms_response(rpki.left_right.cms_msg().wrap( r_msg, irdbd.private_key, irdbd.certificate))
def add_segment(self, dim, lang=None, confidence=None): """ Marks the beginning of a new topographical segment in the current scope. Most often this correspond to a word recognized by an engine. Args: dim (tuple): A tuple containing the bounding box (x0, y0, x1, y1) lang (unicode): Optional identifier of the segment language. confidence (float): Optional confidence value between 0 and 100. """ zone = SubElement(self.line_scope, self.tei_ns + 'zone', ulx=str(dim[0]), uly=str(dim[1]), lrx=str(dim[2]), lry=str(dim[3]), type='segment') self.word_scope = zone self.seg_cnt += 1 self.word_scope.set(self.xml_ns + 'id', 'seg_' + str(self.seg_cnt)) if confidence: cert = SubElement(self.word_scope, self.tei_ns + 'certainty', degree=u'{0:.2f}'.format(confidence / 100.0), locus='value', target='#' + 'seg_' + str(self.seg_cnt)) if self.resp: cert.set('resp', '#' + self.resp) if self.resp: self.word_scope.set('resp', '#' + self.resp)
def write_cell(worksheet, cell): string_table = worksheet.parent.shared_strings coordinate = cell.coordinate attributes = {'r': coordinate} if cell.has_style: attributes['s'] = '%d' % cell._style if cell.data_type != 'f': attributes['t'] = cell.data_type value = cell.internal_value el = Element("c", attributes) if value in ('', None): return el if cell.data_type == 'f': shared_formula = worksheet.formula_attributes.get(coordinate, {}) if shared_formula is not None: if (shared_formula.get('t') == 'shared' and 'ref' not in shared_formula): value = None formula = SubElement(el, 'f', shared_formula) if value is not None: formula.text= value[1:] value = None if cell.data_type == 's': value = string_table.add(value) cell_content = SubElement(el, 'v') if value is not None: cell_content.text = safe_string(value) return el
def map_obj_to_ele(module, want): element = Element('system') login = SubElement(element, 'login') for item in want: if item['state'] != 'present': if item['name'] == 'root': module.fail_json(msg="cannot delete the 'root' account.") operation = 'delete' else: operation = 'merge' user = SubElement(login, 'user', {'operation': operation}) SubElement(user, 'name').text = item['name'] if operation == 'merge': if item['active']: user.set('active', 'active') else: user.set('inactive', 'inactive') if item['role']: SubElement(user, 'class').text = item['role'] if item.get('full_name'): SubElement(user, 'full-name').text = item['full_name'] if item.get('sshkey'): auth = SubElement(user, 'authentication') ssh_rsa = SubElement(auth, 'ssh-rsa') key = SubElement(ssh_rsa, 'name').text = item['sshkey'] return element
def on_createConfig_released(self): toolname = str(self.test_line[0].text()) if(len(toolname) < 1): self.test_line[0].setText('must specify configuration name') self.test_line[0].selectAll() self.test_line[0].setFocus() return newNode = Element('tool_config', {'name': toolname}) newChild = SubElement(newNode, 'tool_hook', {'hidden': 'True', }) newChild.text = str(self.typeSelection) # for key,val in self.vars.iteritems(): for x in xrange(1,len(self.test_text)): #self.vars[self.test_text[x].text()] = self.test_line[x].text() if type(self.test_line[x]) == QComboBox: key = self.test_text[x].text() val = self.test_line[x].currentText() else: key = self.test_text[x].text() val = self.test_line[x].text() typeVal = self.test_text_type[x].text().remove(QRegExp("[\(\)]")) SubElement(newNode, str(key)).text = str(val) self.callback(newNode) self.close()
def something(method, terrain, spots, encounters, rate, swarm=None): if not rate: return monsters = SubElement(parent, 'monsters', method=method, ) if not spots: monsters.set('rate', str(rate)) if terrain: monsters.set('terrain', terrain) if spots: monsters.set('spots', 'spots') for slot, (poke, levels) in enumerate(encounters): poke, form_index = poke & 0x7ff, poke >> 11 e = SubElement(monsters, 'pokemon', number=str(poke), levels=fmtrange(*levels), name=names[poke], #slot=str(slot), ) if poke in FORMS: form = FORMS[poke][form_index] e.set('form', str(form)) else: assert form_index == 0, (poke, form_index) if swarm: swarm_monsters = SubElement(monsters, 'monsters', swarm='swarm', ) SubElement(swarm_monsters, 'pokemon', number=str(swarm), name=names[swarm], levels=fmtrange(0xf, 0x37), )
def save(self): ''' Save the GUI configuration file to disk''' if self.xml_node is None: print('Warning -- Tried to save a GUI configuration that is not ' 'loaded') return # Update font settings font_settings_node = self.xml_node.find('font_settings') pairs = [('menu', 'menu_font_size'), ('tabs', 'main_tabs_font_size'), ('general', 'general_text_font_size')] for group, node_name in pairs: font_settings_node.find(node_name).text = str(self.fonts[group]) # Update latest project history proj_hist_node = self.xml_node.find('project_history') open_latest_node = proj_hist_node.find('open_latest_project_on_start') prevproj_node = proj_hist_node.find('previous_project') open_latest_tab_node = proj_hist_node.find('open_latest_tab_on_start') if open_latest_tab_node is None: open_latest_tab_node = SubElement(proj_hist_node, 'open_latest_tab_on_start') prevtab_node = proj_hist_node.find('previous_tab') if prevtab_node is None: prevtab_node = SubElement(proj_hist_node, 'previous_tab') # Ensure that the value is 'True' or 'False' open_latest_node.text = self.load_latest_on_start and 'True' or 'False' prevproj_node.text = self.latest_project_filename open_latest_tab_node.text = self.load_latest_tab_on_start and 'True' or 'False' prevtab_node.text = self.latest_tab_index # Write config to disk self.xml_node.write(self.xml_filename)
def _make_key_group(self, primary, secondary, cls=None): if cls is None: cls = "" trans = { "\u00A0": "NBSP", "\u200B": "ZWSP", "\u200C": "ZWNJ", "\u200D": "ZWJ", "\u2060": "WJ", } primary = trans.get(primary, primary) secondary = trans.get(secondary, secondary) g = Element("g", **{"class": ("key-group " + cls).strip()}) p = SubElement( g, "text", **{"dy": "1em", "y": "32", "x": "32", "class": "key-text-primary"} ) try: p.text = primary except Exception as e: logger.warning("For char 0x%04x: %s" % (ord(primary), e)) s = SubElement( g, "text", **{"dy": "-.4em", "y": "32", "x": "32", "class": "key-text-secondary"} ) try: s.text = secondary except Exception as e: logger.warning("For char 0x%04x: %s" % (ord(secondary), e)) return (g, p, s)
def _outputResuming(self, element, input_func, output_func, kw): if 'resumptionToken' in kw: resumptionToken = kw['resumptionToken'] result, token = input_func(resumptionToken=resumptionToken) # unpack keywords from resumption token #token_kw, dummy = decodeResumptionToken(resumptionToken) #HACK.. this should be fixed because the I don't like how the pyoai #handles resuptionToken decoding token_kw = {'metadataPrefix': resumptionToken.split(',')[0]} else: result, token = input_func(**kw) # if we don't get results for the first request, # then no records match # XXX this will also be triggered if there are no sets, # but input_func (listSets) should have already raised # NoSetHierarchyError in that case if not result: raise error.NoRecordsMatchError,\ "No records match for request." # without resumption token keys are fine token_kw = kw output_func(element, result, token_kw) if token is not None: e_resumptionToken = SubElement(element, nsoai('resumptionToken')) e_resumptionToken.text = token
def getSignupGuide(node): tagGuide = nextFreeName('Signup_Guide', node) attribsGuide = { RESERVED_XML_TYPE : TYPE_GUI_DATA, ATTRIB_F : FLAG_NOLABEL, ATTRIB_T : UI_TYPE_WEBVIEW, } tagMd = TAG_MARKDOWN textMd = '---\n' textMd += 'In order to sign up, please ensure:\n' textMd += '\n' textMd += '1. Your device is online\n' textMd += '2. FAIMS is connected to the correct server\n' textMd += '3. You have choosen a strong password\n' textMd += '\n' textMd += 'A strong password has at least 6 characters, 1 uppercase letter,' textMd += ' 1 lowercase letter, 1 digit and 1 symbol. Examples of strong ' textMd += 'passwords:\n' textMd += '\n' textMd += '* Bond@007\n' textMd += '* Dig.123\n' g = Element ( tagGuide, attribsGuide) m = SubElement(g, tagMd ); m.text = textMd return g,
def getSearch(node): search = Element( 'Search', { RESERVED_XML_TYPE : TYPE_TAB }, f='nodata noscroll' ) cols = SubElement(search, 'Colgroup_0', { RESERVED_XML_TYPE : TYPE_COLS }, t=UI_TYPE_GROUP, s='orientation') lCol = SubElement(cols, 'Col_0', { RESERVED_XML_TYPE : TYPE_COL }, t=UI_TYPE_GROUP, s='even') rCol = SubElement(cols, 'Col_1', { RESERVED_XML_TYPE : TYPE_COL }, t=UI_TYPE_GROUP, s='large') term = SubElement(lCol, 'Search_Term', t=UI_TYPE_INPUT) btn = SubElement(rCol, 'Search_Button', t=UI_TYPE_BUTTON) # Add 'Entity Types' dropdown if there's more than one entity to choose from isGuiAndData = lambda e: util.gui. isGuiNode (e) and \ util.data.isDataElement(e) nodes = getTabGroups(node, isGuiAndData, descendantOrSelf=False) if len(nodes) > 1: SubElement(search, 'Entity_Types', t=UI_TYPE_DROPDOWN) SubElement(search, 'Entity_List', t=UI_TYPE_LIST) for n in search: annotateWithXmlTypes(n) search.attrib[RESERVED_XML_TYPE] = TYPE_SEARCH btn.text = 'Search' return search,
def copyHtml(sourceXml, targetHtml): for sourceChild in sourceXml.iterchildren(): targetChild = SubElement(targetHtml, sourceChild.localName if sourceChild.namespaceURI == xhtml else sourceChild.tag) for attrTag, attrValue in sourceChild.items(): targetChild.set("lang" if attrTag == "{http://www.w3.org/XML/1998/namespace}lang" else attrTag, attrValue) copyHtml(sourceChild, targetChild)
def write_triangle(pyptlist): """ This function writes a GML triangle member. Parameters ---------- pyptlist : a list of tuples List of points to be converted. A pypt is a tuple that documents the xyz coordinates of a pt e.g. (x,y,z), thus a pyptlist is a list of tuples e.g. [(x1,y1,z1), (x2,y2,z2), ...] Returns ------- triangle member : lxml Element The lxml triangle member. """ gml_Triangle = Element("{" + XMLNamespaces.gml+ "}" + 'Triangle') #gml_Triangle.attrib["{" + write_gml.XMLNamespaces.gml+ "}" +'id'] = 'UUID_' + str(uuid.uuid1()) gml_exterior = SubElement(gml_Triangle, "{" + XMLNamespaces.gml+ "}" + 'exterior') gml_LinearRing = SubElement(gml_exterior, "{" + XMLNamespaces.gml+ "}" + 'LinearRing') gml_LinearRing.attrib["{" + XMLNamespaces.gml+ "}" +'id'] = 'UUID_' + str(uuid.uuid1()) gml_posList = SubElement(gml_LinearRing, "{" + XMLNamespaces.gml+ "}" + 'posList') gml_posList.attrib['srsDimension'] = '3' gml_posList.text = pos_list2text(pyptlist) return gml_Triangle
def _create_sub_with_text(parent,child,text): if text != "" : childEl = SubElement(parent,child) childEl.text = unicode(text) else : childEl = SubElement(parent,child) return childEl
def analyze(self, namespaces): element = "waypoints" sub_elements = "waypoint" if namespaces: namespace = "{https://github.com/StevenMohr/gpsies-xml/schema/database.xsd}" element = namespace + element sub_elements = namespace + sub_elements root = Element(element) data = etree.fromstring(self._raw_data) r = data.xpath('//kml:coordinates', namespaces={'kml':'http://www.opengis.net/kml/2.2'} ) waypoints = list() for element in r: text = element.text parts = [x.split(',') for x in text.split("\n")] old_geo = None for part in parts: waypoint = (part[0], part[1]) new_geo = geo.xyz(float(part[0]), float(part[1])) if old_geo is None or geo.distance(new_geo, old_geo) > 25: waypoints.append(waypoint) old_geo = new_geo for point in waypoints: waypoint = SubElement(root, sub_elements) waypoint.attrib['longitude'] = point[0] waypoint.attrib['latitude'] = point[1] return root
def _update_xml_from_dict(self, xml_node, viz_params): ''' convert the dictionary into XML structure and update/create child nodes of xml_node ''' # Update the XML node with new data viz_name = str(self.leVizName.text()).replace('DATASET', viz_params['dataset_name']) # Renaming must be passed through the models to enable check for # inheritance and to keep the integrity of internal representations xml_node.set('name', viz_name) xml_node.set('hidden', 'Children') # hide all the kids # Convert the dictionary to XML fixed_node_names = ['dataset_name', 'visualization_type', 'indicators', 'output_type'] for key, value in viz_params.items(): if key in fixed_node_names: node = xml_node.find(key) if node is None: node = SubElement(xml_node, key) else: settings_node = xml_node.find('settings') if settings_node is None: settings_node = SubElement(xml_node, 'settings') node = settings_node.find("setting[@name='%s']" % key) if node is None: node = SubElement(settings_node, 'setting', {'name': key}) node.text = str(value).strip()
def _item_comments(self, item, db): """Creates comments for an item.""" for key in db['postComment'].keys(): comment = SubElement(item,self.WP+'comment') #id SubElement(comment,self.WP+'comment_id').text= str(key) #author comment_author = SubElement(comment,self.WP+'comment_author') comment_author.text=CDATA(db['postComment'][key]['author']) #email SubElement(comment,self.WP+'comment_author_email').text=db['postComment'][key]['email'] #url #leave url blank since it may contain old info #ip SubElement(comment,self.WP+'comment_author_IP').text=db['postComment'][key]['ip'] #date SubElement(comment,self.WP+'comment_date').text=db['postComment'][key]['date'] SubElement(comment,self.WP+'comment_date_gmt').text=db['postComment'][key]['date'] #content SubElement(comment,self.WP+'comment_content').text=db['postComment'][key]['content'] #static info SubElement(comment,self.WP+'comment_approved').text='1' SubElement(comment,self.WP+'comment_type') #parent SubElement(comment,self.WP+'comment_parent').text=unicode(db['postComment'][key]['parent'])
def analysis_section(notice, child): # Create the section element section_elm = Element('analysisSection') # Add the title element title_elm = SubElement(section_elm, 'title') title_elm.text = child['title'] # Add paragraphs for paragraph in child['paragraphs']: paragraph_number = child['paragraphs'].index(paragraph) paragraph_footnotes = [ fn for fn in child['footnote_refs'] if fn['paragraph'] == paragraph_number] text = self.resolve_footnotes(notice, paragraph, paragraph_footnotes) paragraph_elm = fromstring( '<analysisParagraph>' + text + '</analysisParagraph>') # Make sure to strip out elements that don't belong strip_tags(paragraph_elm, 'EM') section_elm.append(paragraph_elm) # Construct an analysis section for any children. try: map(lambda c: section_elm.append(analysis_section(notice, c)), child['children']) except: print("Failed to write analysis for", child['title']) pass return section_elm
def serialize_task(self, task, skip_children=False): assert isinstance(task, Task) if isinstance(task.task_spec, SubWorkflow): raise TaskNotSupportedError( "Subworkflow tasks cannot be serialized (due to their use of" + " internal_data to store the subworkflow).") # We are not serializing task.workflow; the deserializer accepts # an already-deserialized Workflow instead. elem = etree.Element('task') if task.id is not None: SubElement(elem, 'id').text = str(task.id) if task.parent is not None: SubElement(elem, 'parent').text = str(task.parent.id) if not skip_children: children_elem = SubElement(elem, 'children') for child in task.children: child_elem = self.serialize_task(child) children_elem.append(child_elem) SubElement(elem, 'state').text = task.get_state_name() if task.triggered: SubElement(elem, 'triggered') SubElement(elem, 'spec').text = task.task_spec.name SubElement(elem, 'last-state-change').text = str( task.last_state_change) self.serialize_value_map(SubElement(elem, 'data'), task.data) internal_data_elem = SubElement(elem, 'internal-data') self.serialize_value_map(internal_data_elem, task.internal_data) return elem
def _item_categories(self, item, cata): """Links an item to categories.""" element = SubElement(item,'category') element.text = CDATA(cata) elNice = SubElement(item,'category',domain="category", nicename=self._to_pin_yin(cata))
def gen_xml_from_predict(root_path): for (root_path, dirnames, _) in walk(root_path): for dirname in dirnames: sub_path = os.path.join(root_path, dirname) for (_, _, filenames) in walk(sub_path): for filename in filenames: if filename[-4:] == '.png': pic_path = os.path.join(sub_path, filename) print(pic_path) mask = cv2.imdecode( np.fromfile(pic_path, dtype=np.uint8), -1) h = mask.shape[0] w = mask.shape[1] if len(mask.shape) == 3: mask = cv2.cvtColor(mask, cv2.COLOR_RGB2GRAY) dict = collections.Counter(list(np.ravel(mask))) dict = sorted(dict.items(), key=lambda d: d[1], reverse=True) if dict[0][0] == 0: cls = dict[1][0] else: cls = dict[0][0] _, mask_Bin = cv2.threshold(mask, 0, 255, cv2.THRESH_BINARY) contours, hierarchy = cv2.findContours( mask_Bin, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) node_root = Element('annotation') node_folder = SubElement(node_root, 'folder') node_folder.text = dirname node_filename = SubElement(node_root, 'filename') node_filename.text = filename[:-4] + '.jpg' node_path = SubElement(node_root, 'path') node_path.text = pic_path node_source = SubElement(node_root, 'source') node_database = SubElement(node_source, 'database') node_database.text = 'tdf' node_size = SubElement(node_root, 'size') node_width = SubElement(node_size, 'width') node_width.text = str(w) node_height = SubElement(node_size, 'height') node_height.text = str(h) node_depth = SubElement(node_size, 'depth') node_depth.text = '3' node_segmented = SubElement(node_root, 'segmented') node_segmented.text = '0' if len(contours) > 0: # max_cont_index = 0 # max_cont = 0 contours_OK = [] max_thresh = 0 #800 for j in range(len(contours)): arclen = cv2.arcLength(contours[j], True) if arclen > max_thresh: contours_OK.append(contours[j]) # max_cont = arclen # max_cont_index = j # contours = contours.tolist() # 得到最大轮廓 # max_length_cont = contours[max_cont_index] contours = contours_OK for h in range(len(contours)): # 多边形近似 approx = cv2.approxPolyDP( contours[h], 0.6, True) pt_x = "" pt_y = "" for k in range(len(approx)): # if k%8==0: #或者平均间隔 pt = approx[k] pt_x += str(pt[0][0]) pt_y += str(pt[0][1]) pt_x += "," pt_y += "," node_object = SubElement(node_root, 'object') node_name = SubElement(node_object, 'name') # node_name.text = dirname.split('.', 1)[0] # node_name.text = str(cls) node_name.text = str('huangdou') node_pose = SubElement(node_object, 'pose') node_pose.text = 'Unspecified' node_truncated = SubElement( node_object, 'truncated') node_truncated.text = '0' node_polygen = SubElement( node_object, 'polygen') node_points_x = SubElement( node_polygen, 'points_x') node_points_x.text = str(pt_x) node_points_y = SubElement( node_polygen, 'points_y') node_points_y.text = str(pt_y) xml = tostring(node_root, pretty_print=True) dom = parseString(xml) xml_path = os.path.join(pic_path[:-4] + '.xml') with open(xml_path, 'wb') as f: f.write( dom.toprettyxml(indent='\t', encoding='utf-8'))
def from_light_response(cls: Type[SAMLResponseType], light_response: LightResponse, audience: Optional[str], destination: Optional[str], issued: datetime, validity: timedelta) -> SAMLResponseType: """Convert light response to SAML response.""" light_response.validate() issue_instant = datetime_iso_format_milliseconds(issued) + 'Z' # UTC valid_until = datetime_iso_format_milliseconds(issued + validity) + 'Z' # UTC root_attributes = { 'ID': light_response.id, 'InResponseTo': light_response.in_response_to_id, 'Version': '2.0', 'IssueInstant': issue_instant, } confirmation_data = { 'InResponseTo': light_response.in_response_to_id, 'NotOnOrAfter': valid_until } conditions = { 'NotBefore': issue_instant, 'NotOnOrAfter': valid_until } if destination is not None: root_attributes['Destination'] = destination confirmation_data['Recipient'] = destination root = etree.Element(Q_NAMES['saml2p:Response'], attrib=root_attributes, nsmap=EIDAS_NAMESPACES) # 1. StatusResponseType <saml2:Issuer> optional if light_response.issuer is not None: SubElement(root, Q_NAMES['saml2:Issuer']).text = light_response.issuer # 2. StatusResponseType <ds:Signature> optional, skipped # 3. StatusResponseType <saml2p:Extensions> optional, skipped # 4. StatusResponseType <saml2p:Status> required status = light_response.status assert status is not None status_elm = SubElement(root, Q_NAMES['saml2p:Status']) # 4.1 <saml2p:Status> <saml2p:StatusCode> required status_code = status.status_code sub_status_code = status.sub_status_code if status_code is None: status_code = StatusCode.SUCCESS if not status.failure else StatusCode.RESPONDER # VERSION_MISMATCH is a status code in SAML 2 but a sub status code in Light response! if sub_status_code == SubStatusCode.VERSION_MISMATCH: status_code_value = SubStatusCode.VERSION_MISMATCH.value sub_status_code_value = None else: status_code_value = status_code.value sub_status_code_value = None if sub_status_code is None else sub_status_code.value status_code_elm = SubElement(status_elm, Q_NAMES['saml2p:StatusCode'], {'Value': status_code_value}) if sub_status_code_value is not None: SubElement(status_code_elm, Q_NAMES['saml2p:StatusCode'], {'Value': sub_status_code_value}) # 4.2 <saml2p:Status> <saml2p:StatusMessage> optional if status.status_message is not None: SubElement(status_elm, Q_NAMES['saml2p:StatusMessage']).text = status.status_message # 4.3 <saml2p:Status> <saml2p:StatusDetail> optional, skipped if not status.failure: # 5. AssertionType assertion_elm = SubElement(root, Q_NAMES['saml2:Assertion'], { 'ID': '_' + light_response.id, 'Version': '2.0', 'IssueInstant': issue_instant, }) # 5.1 AssertionType <saml2:Issuer> required SubElement(assertion_elm, Q_NAMES['saml2:Issuer']).text = light_response.issuer # 5.2 AssertionType <ds:Signature> optional, skipped # 5.3 AssertionType <saml2:Subject> optional subject_elm = SubElement(assertion_elm, Q_NAMES['saml2:Subject']) SubElement(subject_elm, Q_NAMES['saml2:NameID'], {'Format': light_response.subject_name_id_format.value}).text = light_response.subject confirmation_elm = SubElement(subject_elm, Q_NAMES['saml2:SubjectConfirmation'], {'Method': 'urn:oasis:names:tc:SAML:2.0:cm:bearer'}) SubElement(confirmation_elm, Q_NAMES['saml2:SubjectConfirmationData'], confirmation_data) # 5.4 AssertionType <saml2:Conditions> optional conditions_elm = SubElement(assertion_elm, Q_NAMES['saml2:Conditions'], conditions) if audience is not None: SubElement(SubElement(conditions_elm, Q_NAMES['saml2:AudienceRestriction']), Q_NAMES['saml2:Audience']).text = audience # 5.5 AssertionType <saml2:Advice> optional, skipped # 5.6 AssertionType <saml2:AttributeStatement> attributes_elm = SubElement(assertion_elm, Q_NAMES['saml2:AttributeStatement']) for name, values in light_response.attributes.items(): attribute = SubElement(attributes_elm, Q_NAMES['saml2:Attribute'], create_attribute_elm_attributes(name, None)) for value in values: SubElement(attribute, Q_NAMES['saml2:AttributeValue']).text = value # 5.7 AssertionType <saml2:AuthnStatement> statement_elm = SubElement(assertion_elm, Q_NAMES['saml2:AuthnStatement'], {'AuthnInstant': issue_instant}) if light_response.ip_address is not None: SubElement(statement_elm, Q_NAMES['saml2:SubjectLocality'], {'Address': light_response.ip_address}) SubElement(SubElement(statement_elm, Q_NAMES['saml2:AuthnContext']), Q_NAMES['saml2:AuthnContextClassRef']).text = light_response.level_of_assurance.value return cls(ElementTree(root), light_response.relay_state)
def get_atom(): def _format_date(date): iso8601 = date.isoformat() if date.tzinfo: return iso8601 return iso8601 + 'Z' def _format_update_date(date): DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S" return date.strftime(DATETIME_FORMAT) + 'Z' auth = app.auth if not auth.authorized([], None, flask.request.method): return auth.authenticate() XML_ROOT = '<?xml version="1.0" encoding="UTF-8"?>' _message_nsmap = { None: 'http://www.w3.org/2005/Atom', 'dcterms': 'http://purl.org/dc/terms/', 'media': 'http://search.yahoo.com/mrss/', 'mi': 'http://schemas.ingestion.microsoft.com/common/' } # feed = etree.Element('feed', attrib={'lang': 'en-us'}, nsmap=_message_nsmap) feed = etree.Element('feed', nsmap=_message_nsmap) SubElement(feed, 'title').text = etree.CDATA('{} Atom Feed'.format( app.config['SITE_NAME'])) SubElement(feed, 'updated').text = _format_update_date(utcnow()) SubElement(SubElement(feed, 'author'), 'name').text = app.config['SITE_NAME'] SubElement(feed, 'id').text = flask.url_for('atom.get_atom', _external=True) SubElement(feed, 'link', attrib={ 'href': flask.url_for('atom.get_atom', _external=True), 'rel': 'self' }) response = get_internal('news/search') # req = ParsedRequest() # req.args = {'include_fields': 'abstract'} # response = superdesk.get_resource_service('news/search').get(req=req, lookup=None) for item in response[0].get('_items'): try: complete_item = superdesk.get_resource_service('items').find_one( req=None, _id=item.get('_id')) # If featuremedia is not allowed for the company don't add the item if ((complete_item.get('associations') or {}).get('featuremedia') or {}).get('renditions'): if not check_association_permission(complete_item): continue entry = SubElement(feed, 'entry') # If the item has any parents we use the id of the first, this should be constant throught the update # history if complete_item.get('ancestors') and len( complete_item.get('ancestors')): SubElement(entry, 'id').text = complete_item.get('ancestors')[0] else: SubElement(entry, 'id').text = complete_item.get('_id') SubElement(entry, 'title').text = etree.CDATA( complete_item.get('headline')) SubElement(entry, 'published').text = _format_date( complete_item.get('firstpublished')) SubElement(entry, 'updated').text = _format_update_date( complete_item.get('versioncreated')) SubElement(entry, 'link', attrib={ 'rel': 'self', 'href': flask.url_for('news/item.get_item', item_id=item.get('_id'), format='TextFormatter', _external=True) }) if complete_item.get('byline'): SubElement(SubElement(entry, 'author'), 'name').text = complete_item.get('byline') if complete_item.get('pubstatus') == 'usable': SubElement(entry, etree.QName(_message_nsmap.get('dcterms'), 'valid')).text = \ 'start={}; end={}; scheme=W3C-DTF'.format(_format_date(utcnow()), _format_date(utcnow() + datetime.timedelta(days=30))) else: # in effect a kill set the end date into the past SubElement(entry, etree.QName(_message_nsmap.get('dcterms'), 'valid')).text = \ 'start={}; end={}; scheme=W3C-DTF'.format(_format_date(utcnow()), _format_date(utcnow() - datetime.timedelta(days=30))) categories = [{ 'name': s.get('name') } for s in complete_item.get('service', [])] for category in categories: SubElement(entry, 'category', attrib={'term': category.get('name')}) SubElement(entry, 'summary').text = etree.CDATA( complete_item.get('description_text', '')) # If there are any image embeds then reset the source to a Newshub asset html_updated = False regex = r' EMBED START Image {id: \"editor_([0-9]+)' root_elem = lxml_html.fromstring(complete_item.get( 'body_html', '')) comments = root_elem.xpath('//comment()') for comment in comments: if 'EMBED START Image' in comment.text: m = re.search(regex, comment.text) # Assumes the sibling of the Embed Image comment is the figure tag containing the image figure_elem = comment.getnext() if figure_elem is not None and figure_elem.tag == "figure": imgElem = figure_elem.find("./img") if imgElem is not None and m and m.group(1): embed_id = "editor_" + m.group(1) src = complete_item.get("associations").get( embed_id).get("renditions").get("16-9") if src: imgElem.attrib["src"] = flask.url_for( 'assets.get_item', asset_id=src.get('media'), _external=True) html_updated = True if html_updated: complete_item["body_html"] = to_string(root_elem, method="html") SubElement(entry, 'content', attrib={ 'type': 'html' }).text = etree.CDATA(complete_item.get('body_html', '')) if ((complete_item.get('associations') or {}).get('featuremedia') or {}).get('renditions'): image = ((complete_item.get('associations') or {}).get('featuremedia') or {}).get('renditions').get("16-9") metadata = ((complete_item.get('associations') or {}).get('featuremedia') or {}) url = flask.url_for('assets.get_item', _external=True, asset_id=image.get('media')) media = SubElement(entry, etree.QName(_message_nsmap.get('media'), 'content'), attrib={ 'url': url, 'type': image.get('mimetype'), 'medium': 'image' }) SubElement(media, etree.QName(_message_nsmap.get('media'), 'credit')).text = metadata.get('byline') SubElement( media, etree.QName( _message_nsmap.get('media'), 'title')).text = metadata.get('description_text') SubElement(media, etree.QName( _message_nsmap.get('media'), 'text')).text = metadata.get('body_text') focr = SubElement( media, etree.QName(_message_nsmap.get('mi'), 'focalRegion')) SubElement(focr, etree.QName(_message_nsmap.get('mi'), 'x1')).text = str( image.get('poi').get('x')) SubElement(focr, etree.QName(_message_nsmap.get('mi'), 'x2')).text = str( image.get('poi').get('x')) SubElement(focr, etree.QName(_message_nsmap.get('mi'), 'y1')).text = str( image.get('poi').get('y')) SubElement(focr, etree.QName(_message_nsmap.get('mi'), 'y2')).text = str( image.get('poi').get('y')) except Exception as ex: logger.exception('processing {} - {}'.format(item.get('_id'), ex)) return flask.Response( XML_ROOT + etree.tostring(feed, pretty_print=True).decode('utf-8'), mimetype='application/atom+xml')
def _to_xml(self, parent): SubElement(parent, "EPOCH").text = format_epoch(self.epoch) if self.frame: SubElement(parent, "COV_REF_FRAME").text = self.frame for key, index in COV_XML_ENTRY_MAP.items(): SubElement(parent, key).text = format_float(self.matrix[index])
def identify(self): envelope, e_identify = self._outputEnvelope(verb='Identify') identify = self._server.identify() e_repositoryName = SubElement(e_identify, nsoai('repositoryName')) e_repositoryName.text = identify.repositoryName() e_baseURL = SubElement(e_identify, nsoai('baseURL')) e_baseURL.text = identify.baseURL() e_protocolVersion = SubElement(e_identify, nsoai('protocolVersion')) e_protocolVersion.text = identify.protocolVersion() for adminEmail in identify.adminEmails(): e = SubElement(e_identify, nsoai('adminEmail')) e.text = adminEmail e_earliestDatestamp = SubElement(e_identify, nsoai('earliestDatestamp')) e_earliestDatestamp.text = datetime_to_datestamp( identify.earliestDatestamp()) e_deletedRecord = SubElement(e_identify, nsoai('deletedRecord')) e_deletedRecord.text = identify.deletedRecord() e_granularity = SubElement(e_identify, nsoai('granularity')) e_granularity.text = identify.granularity() compressions = identify.compression() if compressions != ['identity']: for compression in compressions: e_compression = SubElement(e_identify, nsoai('compression')) e_compression.text = compression for description in identify.descriptions(): e_description = SubElement(e_identify, nsoai('description')) e_description.append(etree.fromstring(description)) return envelope
def _outputHeader(self, element, header): e_header = SubElement(element, nsoai('header')) if header.isDeleted(): e_header.set('status', 'deleted') e_identifier = SubElement(e_header, nsoai('identifier')) e_identifier.text = header.identifier() e_datestamp = SubElement(e_header, nsoai('datestamp')) e_datestamp.text = datetime_to_datestamp(header.datestamp()) for set in header.setSpec(): e = SubElement(e_header, nsoai('setSpec')) e.text = set
def _outputEnvelope(self, **kw): e_tree, e_oaipmh = self._outputBasicEnvelope(**kw) e_element = SubElement(e_oaipmh, nsoai(kw['verb'])) return e_tree, e_element
def createxml(filename, imagePath, imageName, width, height, info): xmlfile = open(filename, "w") node_root = Element('annotation') node_folder = SubElement(node_root, 'folder') node_folder.text = imagePath node_filename = SubElement(node_root, 'filename') node_filename.text = imageName node_size = SubElement(node_root, 'size') node_width = SubElement(node_size, 'width') node_width.text = str(width) node_height = SubElement(node_size, 'height') node_height.text = str(height) node_depth = SubElement(node_size, 'depth') node_depth.text = '3' for i in info: node_object = SubElement(node_root, 'object') node_name = SubElement(node_object, 'name') node_name.text = i.name node_difficult = SubElement(node_object, 'difficult') node_difficult.text = '0' node_bndbox = SubElement(node_object, 'bndbox') node_xmin = SubElement(node_bndbox, 'xmin') node_xmin.text = str(i.xmin) node_ymin = SubElement(node_bndbox, 'ymin') node_ymin.text = str(i.ymin) node_xmax = SubElement(node_bndbox, 'xmax') node_xmax.text = str(i.xmax) node_ymax = SubElement(node_bndbox, 'ymax') node_ymax.text = str(i.ymax) xml = tostring(node_root, pretty_print=True) #格式化显示,该换行的换行 dom = parseString(xml) dom.writexml(xmlfile, encoding="utf-8") xmlfile.close()
def xml_generator(bndbox): # Root node_root = Element('annotation') ## Folder node_folder = SubElement(node_root, 'folder') node_folder.text = bndbox['folder'] ## Filename node_filename = SubElement(node_root, 'filename') node_filename.text = bndbox['filename'] ## Path node_path = SubElement(node_root, 'path') node_path.text = bndbox['path'] ## Source node_source = SubElement(node_root, 'source') node_database = SubElement(node_source, 'database') node_database.text = 'Unknown' ## Size node_size = SubElement(node_root, 'size') ### Width node_width = SubElement(node_size, 'width') node_width.text = str(bndbox['width']) ### Height node_height = SubElement(node_size, 'height') node_height.text = str(bndbox['height']) ### Depth node_depth = SubElement(node_size, 'depth') node_depth.text = str(bndbox['depth']) ## Segmented node_segmented = SubElement(node_root, 'segmented') node_segmented.text = '0' ## Object node_object = SubElement(node_root, 'object') ### Name node_name = SubElement(node_object, 'name') node_name.text = 'car_door' ### Pose node_pose = SubElement(node_object, 'pose') node_pose.text = 'Unspecified' ### Truncated node_truncated = SubElement(node_object, 'truncated') node_truncated.text = '0' ### Difficult node_difficult = SubElement(node_object, 'difficult') node_difficult.text = '0' ### Bounding box node_bndbox = SubElement(node_object, 'bndbox') #### x-y value node_xmin = SubElement(node_bndbox, 'xmin') node_xmin.text = str(bndbox['xmin']) node_ymin = SubElement(node_bndbox, 'ymin') node_ymin.text = str(bndbox['ymin']) node_xmax = SubElement(node_bndbox, 'xmax') node_xmax.text = str(bndbox['xmax']) node_ymax = SubElement(node_bndbox, 'ymax') node_ymax.text = str(bndbox['ymax']) # format display xml = tostring(node_root, pretty_print=True) xml_name = bndbox['filename'][:-4] fp = open('../data/annotations/xmls/{}.xml'.format(xml_name), 'w') fp.write(xml.decode()) fp.close()
def getKML(self, nolabels=False, **kwargs): """ Posts an event.getList() and returns the results as a KML file. For optional arguments, see documentation of :meth:`~obspy.seishub.client._EventMapperClient.getList()` :type nolabels: bool :param nolabels: Hide labels of events in KML. Can be useful with large data sets. :rtype: str :return: String containing KML information of all matching events. This string can be written to a file and loaded into e.g. Google Earth. """ events = self.getList(**kwargs) timestamp = datetime.now() # construct the KML file kml = Element("kml") kml.set("xmlns", "http://www.opengis.net/kml/2.2") document = SubElement(kml, "Document") SubElement(document, "name").text = "Seishub Event Locations" # style definitions for earthquakes style = SubElement(document, "Style") style.set("id", "earthquake") iconstyle = SubElement(style, "IconStyle") SubElement(iconstyle, "scale").text = "0.5" icon = SubElement(iconstyle, "Icon") SubElement(icon, "href").text = \ "http://maps.google.com/mapfiles/kml/shapes/earthquake.png" hotspot = SubElement(iconstyle, "hotSpot") hotspot.set("x", "0.5") hotspot.set("y", "0") hotspot.set("xunits", "fraction") hotspot.set("yunits", "fraction") labelstyle = SubElement(style, "LabelStyle") SubElement(labelstyle, "color").text = "ff0000ff" SubElement(labelstyle, "scale").text = "0.8" folder = SubElement(document, "Folder") SubElement(folder, "name").text = "SeisHub Events (%s)" % \ timestamp.date() SubElement(folder, "open").text = "1" # additional descriptions for the folder descrip_str = "Fetched from: %s" % self.client.base_url descrip_str += "\nFetched at: %s" % timestamp descrip_str += "\n\nSearch options:\n" descrip_str += "\n".join(["=".join((str(k), str(v))) \ for k, v in kwargs.items()]) SubElement(folder, "description").text = descrip_str style = SubElement(folder, "Style") liststyle = SubElement(style, "ListStyle") SubElement(liststyle, "listItemType").text = "check" SubElement(liststyle, "bgColor").text = "00ffffff" SubElement(liststyle, "maxSnippetLines").text = "5" # add one marker per event interesting_keys = ['resource_name', 'localisation_method', 'account', 'user', 'public', 'datetime', 'longitude', 'latitude', 'depth', 'magnitude', 'used_p', 'used_s'] for event_dict in events: placemark = SubElement(folder, "Placemark") date = str(event_dict['datetime']).split(" ")[0] mag = str(event_dict['magnitude']) # scale marker size to magnitude if this information is present if mag: mag = float(mag) label = "%s: %.1f" % (date, mag) try: icon_size = 1.2 * log(1.5 + mag) except ValueError: icon_size = 0.1 else: label = date icon_size = 0.5 if nolabels: SubElement(placemark, "name").text = "" else: SubElement(placemark, "name").text = label SubElement(placemark, "styleUrl").text = "#earthquake" style = SubElement(placemark, "Style") icon_style = SubElement(style, "IconStyle") liststyle = SubElement(style, "ListStyle") SubElement(liststyle, "maxSnippetLines").text = "5" SubElement(icon_style, "scale").text = str(icon_size) if event_dict['longitude'] and event_dict['latitude']: point = SubElement(placemark, "Point") SubElement(point, "coordinates").text = "%.10f,%.10f,0" % \ (event_dict['longitude'], event_dict['latitude']) # detailed information on the event for the description descrip_str = "" for key in interesting_keys: if not key in event_dict: continue descrip_str += "\n%s: %s" % (key, event_dict[key]) SubElement(placemark, "description").text = descrip_str # generate and return KML string return tostring(kml, pretty_print=True, xml_declaration=True)
def rpc(module, items): responses = list() for item in items: name = item["name"] xattrs = item["xattrs"] fetch_config = False args = item.get("args") text = item.get("text") name = str(name).replace("_", "-") if all((module.check_mode, not name.startswith("get"))): module.fail_json(msg="invalid rpc for running in check_mode") if ( name == "command" and text == "show configuration" or name == "get-configuration" ): fetch_config = True element = Element(name, xattrs) if text: element.text = text elif args: for key, value in iteritems(args): key = str(key).replace("_", "-") if isinstance(value, list): for item in value: child = SubElement(element, key) if item is not True: child.text = item else: child = SubElement(element, key) if value is not True: child.text = value if fetch_config: reply = get_configuration(module, format=xattrs["format"]) else: reply = exec_rpc(module, tostring(element), ignore_warning=False) if xattrs["format"] == "text": if len(reply) >= 1: if fetch_config: data = reply.find(".//configuration-text") else: if text and text.startswith("show configuration"): data = reply.find(".//configuration-output") else: data = reply.find(".//output") if data is None: module.fail_json(msg=tostring(reply)) responses.append(data.text.strip()) else: responses.append(reply.text.strip()) elif xattrs["format"] == "json": responses.append(module.from_json(reply.text.strip())) elif xattrs["format"] == "set": data = reply.find(".//configuration-set") if data is None: module.fail_json( msg="Display format 'set' is not supported by remote device." ) responses.append(data.text.strip()) else: responses.append(tostring(reply)) return responses
def makefeedentry(feedentry_data): """Create an individual Atom feed entry from a ready to be publish post.""" entry = Element('{http://www.w3.org/2005/Atom}entry', nsmap=NSMAP2) id_element = SubElement(entry, 'id') id_element.text = feedentry_data['tagid'] linkfeedentry = SubElement(entry, 'link') linkfeedentry.attrib["rel"] = "alternate" linkfeedentry.attrib["type"] = "text/html" linkfeedentry.attrib["href"] = feedentry_data['url'] title = SubElement(entry, 'title') title.text = feedentry_data['title'] published = SubElement(entry, 'published') published.text = feedentry_data['created'] updated = SubElement(entry, 'updated') updated.text = feedentry_data['modified'] content = SubElement(entry, 'content') content.attrib["type"] = "xhtml" # changing the namespace to HTML # so only the local root element (div) will get the namespace divcontent = SubElement(content, "{%s}div" % HTMLNS, nsmap=NSMAP) # Adding a full tree fragment. divcontent.append(feedentry_data['content']) linkselfatom = SubElement(entry, 'link', nsmap=NSMAP2) linkselfatom.attrib["rel"] = "license" linkselfatom.attrib["href"] = LICENSELIST['ccby'] entry_string = etree.tostring(entry, encoding='unicode') # Change the image links to absolute links # This will break one day. This is for Anthony Ricaud. normalized_entry = entry_string.replace( '<img src="/', '<img src="https://www.la-grange.net/') # Convert as an elementTree entry = etree.parse(StringIO(normalized_entry)) logging.info("makefeedentry: new entry created") return entry
def create_description(tool, model): if "description" in model.opt_attribs.keys( ) and model.opt_attribs["description"] is not None: description = SubElement(tool, "description") description.text = model.opt_attribs["description"]
def create_param_attribute_list(param_node, param, supported_file_formats): param_node.attrib["name"] = get_galaxy_parameter_name(param) param_type = TYPE_TO_GALAXY_TYPE[param.type] if param_type is None: raise ModelError( "Unrecognized parameter type %(type)s for parameter %(name)s" % { "type": param.type, "name": param.name }) if param.is_list: param_type = "text" if is_selection_parameter(param): param_type = "select" if len(param.restrictions.choices) < 5: param_node.attrib["display"] = "radio" if is_boolean_parameter(param): param_type = "boolean" if param.type is _InFile: # assume it's just text unless restrictions are provided param_format = "txt" if param.restrictions is not None: # join all formats of the file, take mapping from supported_file if available for an entry if type(param.restrictions) is _FileFormat: param_format = ",".join([ get_supported_file_type(i, supported_file_formats) if get_supported_file_type(i, supported_file_formats) else i for i in param.restrictions.formats ]) else: raise InvalidModelException( "Expected 'file type' restrictions for input file [%(name)s], " "but instead got [%(type)s]" % { "name": param.name, "type": type(param.restrictions) }) param_node.attrib["type"] = "data" param_node.attrib["format"] = param_format # in the case of multiple input set multiple flag if param.is_list: param_node.attrib["multiple"] = "true" else: param_node.attrib["type"] = param_type # check for parameters with restricted values (which will correspond to a "select" in galaxy) if param.restrictions is not None: # it could be either _Choices or _NumericRange, with special case for boolean types if param_type == "boolean": create_boolean_parameter(param_node, param) elif type(param.restrictions) is _Choices: # create as many <option> elements as restriction values for choice in param.restrictions.choices: option_node = add_child_node( param_node, "option", OrderedDict([("value", str(choice))])) option_node.text = str(choice) # preselect the default value if param.default == choice: option_node.attrib["selected"] = "true" elif type(param.restrictions) is _NumericRange: if param.type is not int and param.type is not float: raise InvalidModelException( "Expected either 'int' or 'float' in the numeric range restriction for " "parameter [%(name)s], but instead got [%(type)s]" % { "name": param.name, "type": type(param.restrictions) }) # extract the min and max values and add them as attributes # validate the provided min and max values if param.restrictions.n_min is not None: param_node.attrib["min"] = str(param.restrictions.n_min) if param.restrictions.n_max is not None: param_node.attrib["max"] = str(param.restrictions.n_max) elif type(param.restrictions) is _FileFormat: param_node.attrib["format"] = ','.join([ get_supported_file_type(i, supported_file_formats) if get_supported_file_type(i, supported_file_formats) else i for i in param.restrictions.formats ]) else: raise InvalidModelException( "Unrecognized restriction type [%(type)s] for parameter [%(name)s]" % { "type": type(param.restrictions), "name": param.name }) if param_type == "select" and param.default in param.restrictions.choices: param_node.attrib["optional"] = "False" else: param_node.attrib["optional"] = str(not param.required) if param_type == "text": # add size attribute... this is the length of a textbox field in Galaxy (it could also be 15x2, for instance) param_node.attrib["size"] = "30" # add sanitizer nodes, this is needed for special character like "[" # which are used for example by FeatureFinderMultiplex sanitizer_node = SubElement(param_node, "sanitizer") valid_node = SubElement(sanitizer_node, "valid", OrderedDict([("initial", "string.printable")])) add_child_node(valid_node, "remove", OrderedDict([("value", '\'')])) add_child_node(valid_node, "remove", OrderedDict([("value", '"')])) # check for default value if param.default is not None and param.default is not _Null: if type(param.default) is list: # we ASSUME that a list of parameters looks like: # $ tool -ignore He Ar Xe # meaning, that, for example, Helium, Argon and Xenon will be ignored param_node.attrib["value"] = ' '.join(map(str, param.default)) elif param_type != "boolean": param_node.attrib["value"] = str(param.default) else: # simple boolean with a default if param.default is True: param_node.attrib["checked"] = "true" else: if param.type is int or param.type is float: # galaxy requires "value" to be included for int/float # since no default was included, we need to figure out one in a clever way... but let the user know # that we are "thinking" for him/her logger.warning( "Generating default value for parameter [%s]. " "Galaxy requires the attribute 'value' to be set for integer/floats. " "Edit the CTD file and provide a suitable default value." % param.name, 1) # check if there's a min/max and try to use them default_value = None if param.restrictions is not None: if type(param.restrictions) is _NumericRange: default_value = param.restrictions.n_min if default_value is None: default_value = param.restrictions.n_max if default_value is None: # no min/max provided... just use 0 and see what happens default_value = 0 else: # should never be here, since we have validated this anyway... # this code is here just for documentation purposes # however, better safe than sorry! # (it could be that the code changes and then we have an ugly scenario) raise InvalidModelException( "Expected either a numeric range for parameter [%(name)s], " "but instead got [%(type)s]" % { "name": param.name, "type": type(param.restrictions) }) else: # no restrictions and no default value provided... # make up something default_value = 0 param_node.attrib["value"] = str(default_value) label = "%s parameter" % param.name help_text = "" if param.description is not None: label, help_text = generate_label_and_help(param.description) param_node.attrib["label"] = label param_node.attrib["help"] = "(-%s)" % param.name + " " + help_text
def to_xml(self, root): if 'Subpart' in root.label: elem = Element('subpart', label=root.label_id()) if root.node_type != "emptypart": title = SubElement(elem, 'title') title.text = root.title if len(root.label) == 3: elem.set('subpartLetter', root.label[-1]) toc = XMLWriteContent.toc_to_xml( self.layers['toc'][root.label_id()]) toc.set('label', root.label_id() + '-TOC') elem.append(toc) content = SubElement(elem, 'content') for child in root.children: sub_elem = self.to_xml(child) content.append(sub_elem) elif root.label[-1].isdigit() and len(root.label) == 2: elem = Element('section', sectionNum=root.label[-1], label=root.label_id()) subject = SubElement(elem, 'subject') subject.text = root.title if root.text.strip() != '' and len(root.children) == 0: label = root.label_id() + '-p1' paragraph = SubElement(elem, 'paragraph', marker='', label=label) par_content = SubElement(paragraph, 'content') par_content.text = root.text.strip() elif len(root.label) == 1: reg_string = '<regulation xmlns="eregs" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="eregs http://cfpb.github.io/regulations-schema/src/eregs.xsd"></regulation>' # noqa elem = fromstring(reg_string) title = root.title fdsys = self.fdsys(root.label_id()) elem.append(fdsys) preamble = self.preamble(root.label_id()) elem.append(preamble) part_num = root.label_id() part = SubElement(elem, 'part', label=part_num) toc = XMLWriteContent.toc_to_xml(self.layers['toc'][part_num]) part.append(toc) content = SubElement(part, 'content') for child in root.children: sub_elem = self.to_xml(child) content.append(sub_elem) # Add any analysis that might exist for this version try: self.add_analyses(elem) except Exception: print 'Could not create analyses for {}'.format(root.label) elif root.node_type == 'appendix' and len(root.label) == 2: # reset the section counter self.appendix_sections = 1 elem = Element('appendix', label=root.label_id(), appendixLetter=root.label[-1]) title = SubElement(elem, 'appendixTitle') title.text = root.title if root.label_id() in self.layers['toc']: toc = XMLWriteContent.toc_to_xml( self.layers['toc'][root.label_id()]) elem.append(toc) elif root.node_type == 'appendix' and len(root.label) == 3: elem = Element('appendixSection', appendixSecNum=str(self.appendix_sections), label=root.label_id()) subject = SubElement(elem, 'subject') subject.text = root.title self.appendix_sections += 1 if root.text.strip() != '' and len(root.children) == 0: label = root.label_id() + '-p1' paragraph = SubElement(elem, 'paragraph', marker='', label=label) par_content = SubElement(paragraph, 'content') par_content.text = root.text.strip() elif root.node_type == 'interp' and len(root.label) == 2: elem = Element('interpretations', label=root.label_id()) title = SubElement(elem, 'title') title.text = root.title elif root.node_type == 'interp' and \ root.label[1] in self.caps and \ len(root.label) <= 3: elem = Element('interpSection', label=root.label_id()) title = SubElement(elem, 'title') title.text = root.title # Look through the interpretations layer to see if this # label is the reference for any other. That other label is # our target. target = None for interp_target, references in \ self.layers['interpretations'].items(): if root.label_id() in [r['reference'] for r in references]: target = interp_target break if target is not None: elem.set('target', target) elif root.node_type == 'interp' and len(root.label) == 3 and \ (root.label[1].isdigit() or root.label[1] == 'Interp'): if root.label[1].isdigit(): elem = Element('interpSection', sectionNum=str(root.label[1]), label=root.label_id()) else: elem = Element('interpSection', label=root.label_id()) title = SubElement(elem, 'title') title.text = root.title # Look through the interpretations layer to see if this # label is the reference for any other. That other label is # our target. target = None for interp_target, references in \ self.layers['interpretations'].items(): if root.label_id() in [r['reference'] for r in references]: target = interp_target break if target is not None: elem.set('target', target) elif root.node_type == 'interp' and \ len(root.label) >= 3 and \ root.label[-1] == 'Interp' \ and root.label[1] in self.caps: # this is the case for hyphenated appendices like MS-1 in reg X elem = Element('interpParagraph', label=root.label_id()) title = SubElement(elem, 'title') title.text = root.title content = SubElement(elem, 'content') elif root.node_type == 'interp': # fall-through for all other interp nodes, which should be # paragraphs label = root.label_id() elem = Element('interpParagraph', label=label) # Look through the interpretations layer to see if this # label is the reference for any other. That other label is # our target. target = None for interp_target, references in \ self.layers['interpretations'].items(): if root.label_id() in [r['reference'] for r in references]: target = interp_target break if target is not None: elem.set('target', target) # Get the text text = root.text text = self.apply_layers(text, root.label_id()) if text.startswith('!'): text = '' # If there's a title or a keyterm, add it to the element if root.title: title = SubElement(elem, 'title') title.text = root.title elif root.label_id() in self.layers['keyterms']: # keyterm is not an inline layer keyterms_layer = self.layers['keyterms'] keyterm = keyterms_layer[root.label_id()][0]['key_term'] title = SubElement(elem, 'title', attrib={'type': 'keyterm'}) title.text = keyterm # If this paragraph has a marker in the markers layer, add # it to the element try: markers_layer = self.layers['paragraph-markers'] marker_item = markers_layer[root.label_id()] marker = marker_item[0]['text'] elem.set('marker', marker) except KeyError: pass try: content = fromstring('<content>' + text + '</content>') except XMLSyntaxError: content = fromstring('<content>MISSING CONTENT</content>') # graphics are special since they're not really inlined if root.label_id() in self.layers['graphics']: graphics = XMLWriteContent.apply_graphics( self.layers['graphics'][root.label_id()]) for graphic in graphics: content.append(graphic) elem.append(content) else: try: marker_item = self.layers['paragraph-markers'][root.label_id()] marker = marker_item[0]['text'] except KeyError: marker = '' elem = Element('paragraph', label=root.label_id(), marker=marker) if root.title: title = SubElement(elem, 'title') title.text = root.title else: if root.label_id() in self.layers['keyterms']: # keyterm is not an inline layer keyterms_layer = self.layers['keyterms'] keyterm = keyterms_layer[root.label_id()][0]['key_term'] title = SubElement(elem, 'title', attrib={'type': 'keyterm'}) title.text = keyterm text = self.apply_layers(root.text, root.label_id()) if text.startswith('!'): text = '' try: content = fromstring('<content>' + text + '</content>') except XMLSyntaxError: content = fromstring('<content>MISSING CONTENT</content>') # graphics are special since they're not really inlined if root.label_id() in self.layers['graphics']: graphics = XMLWriteContent.apply_graphics( self.layers['graphics'][root.label_id()]) for graphic in graphics: content.append(graphic) elem.append(content) if len(root.label) > 1 and ('Subpart' not in root.label): # the part is a special case for child in root.children: sub_elem = self.to_xml(child) elem.append(sub_elem) return elem
def add_child_node(parent_node, child_node_name, attributes=OrderedDict([])): child_node = SubElement(parent_node, child_node_name, attributes) return child_node
def preamble(self, reg_number): meta = self.layers['meta'][reg_number][0] elem = Element('preamble') agency = SubElement(elem, 'agency') agency.text = 'Bureau of Consumer Financial Protection' cfr = SubElement(elem, 'cfr') title = SubElement(cfr, 'title') title.text = str(meta['cfr_title_number']) section = SubElement(cfr, 'section') section.text = reg_number doc_number_elm = SubElement(elem, 'documentNumber') doc_number_elm.text = self.doc_number eff_date = SubElement(elem, 'effectiveDate') eff_date.text = meta['effective_date'] fr_url_elm = SubElement(elem, 'federalRegisterURL') fr_url_elm.text = self.notice['fr_url'] return elem
def toc_to_xml(toc): toc_elem = Element('tableOfContents') for item in toc: index = item['index'] title = item['title'] target = '-'.join(index) if index[-1].isdigit() and not index[1].isalpha(): toc_section = SubElement(toc_elem, 'tocSecEntry', target=target) toc_secnum = SubElement(toc_section, 'sectionNum') toc_secnum.text = str(index[-1]) toc_secsubj = SubElement(toc_section, 'sectionSubject') toc_secsubj.text = title else: toc_appentry = SubElement(toc_elem, 'tocAppEntry', target=target) toc_appletter = SubElement(toc_appentry, 'appendixLetter') toc_appsubj = SubElement(toc_appentry, 'appendixSubject') toc_appletter.text = index[-1] toc_appsubj.text = title return toc_elem
def apply_graphics(graphics_layer): graphics_elements = [] # graphics is a special layer because it's not inlined for graphic in graphics_layer: graphic_elem = Element('graphic') alt_text_elem = SubElement(graphic_elem, 'altText') text_elem = SubElement(graphic_elem, 'text') url_elem = SubElement(graphic_elem, 'url') alt_text_elem.text = graphic['alt'] text_elem.text = graphic['text'] url_elem.text = graphic['url'] if 'thumb_url' in graphic: thumb_url_elem = SubElement(graphic_elem, 'thumbUrl') thumb_url_elem.text = graphic['thumb_url'] graphics_elements.append(graphic_elem) return graphics_elements
def fdsys(self, reg_number, date='2012-01-01', orig_date='2012-01-01'): meta = self.layers['meta'][reg_number][0] elem = Element('fdsys') cfr_title_num = SubElement(elem, 'cfrTitleNum') cfr_title_num.text = str(meta['cfr_title_number']) cfr_title_text = SubElement(elem, 'cfrTitleText') cfr_title_text.text = meta['cfr_title_text'] volume = SubElement(elem, 'volume') volume.text = '8' date_elem = SubElement(elem, 'date') date_elem.text = meta['effective_date'] orig_date_elem = SubElement(elem, 'originalDate') orig_date_elem.text = orig_date title_elem = SubElement(elem, 'title') title_elem.text = meta['statutory_name'] return elem
#!/usr/bin/env python # coding:utf-8 #from xml.etree.ElementTree import Element, SubElement, tostring from lxml.etree import Element, SubElement, tostring import pprint from xml.dom.minidom import parseString node_root = Element('annotation') node_folder = SubElement(node_root, 'folder') node_folder.text = 'GTSDB' node_filename = SubElement(node_root, 'filename') node_filename.text = '000001.jpg' node_size = SubElement(node_root, 'size') node_width = SubElement(node_size, 'width') node_width.text = '500' node_height = SubElement(node_size, 'height') node_height.text = '375' node_depth = SubElement(node_size, 'depth') node_depth.text = '3' #it works !!! #for node_object = SubElement(node_root, 'object') node_name = SubElement(node_object, 'name')
def write_notice(self, notice, changes={}, reg_tree=None, left_doc_number=''): """ Write a notice. """ if reg_tree is None: raise RuntimeError("to write notices to XML, both a " "changeset and a reg tree are required.") full_path = os.path.join(settings.OUTPUT_DIR, self.path) dir_path = os.path.dirname(full_path) if not os.path.exists(dir_path): os.makedirs(dir_path) # Create a notice root element notice_string = '<notice xmlns="eregs" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="eregs http://cfpb.github.io/regulations-schema/src/eregs.xsd"></notice>' # noqa notice_elm = fromstring(notice_string) # Get the fdsys and preamble fdsys_elm = self.fdsys(reg_tree.label_id()) notice_elm.append(fdsys_elm) preamble_elm = self.preamble(reg_tree.label_id()) notice_elm.append(preamble_elm) # Because analysis kept in-line in RegML, and because the # diffing functionality that generated our `changes` doesn't # take analysis into account, we need to do so here. Analyzed # labels are included as "modified" in the changes dict. for label in self.layers['analyses']: if label not in changes: changes[label] = {'op': 'modified'} # Get the changeset changeset_elm = Element('changeset') changeset_elm.set('leftDocumentNumber', left_doc_number) changeset_elm.set('rightDocumentNumber', self.doc_number) for label, change in changes.items(): # For each change, generate a change element with the label # and operation as attributes. change_elm = SubElement(changeset_elm, 'change') change_elm.set('operation', change['op']) change_elm.set('label', label) # If the change is added/modified, we also need to include # the added/modified node. if change['op'] in ('added', 'modified'): # Lookup the new label in the regulation tree changed_node = find(reg_tree, label) # Append it to as XML to the change element content_elm = self.to_xml(changed_node) change_elm.append(content_elm) self.add_analyses(notice_elm) notice_elm.append(changeset_elm) xml_string = tostring(notice_elm, pretty_print=True, xml_declaration=True, encoding='UTF-8') # Write the file with open(full_path, 'w') as f: logger.info("Writing notice to {}".format(full_path)) print("Writing notice to {}".format(full_path)) f.write(xml_string)
def save_xml(image_name, lists, save_dir, width=1609, height=500, channel=3): ''' :param image_name:图片名 :param bbox:对应的bbox :param save_dir: :return: ''' from lxml.etree import Element, SubElement, tostring from xml.dom.minidom import parseString node_root = Element('annotation') node_folder = SubElement(node_root, 'folder') node_folder.text = 'VOC' node_filename = SubElement(node_root, 'filename') node_filename.text = str(image_name.replace('png', 'jpg')) node_size = SubElement(node_root, 'size') node_width = SubElement(node_size, 'width') node_width.text = '%s' % width node_height = SubElement(node_size, 'height') node_height.text = '%s' % height node_depth = SubElement(node_size, 'depth') node_depth.text = '%s' % channel # segmented node_segmented = SubElement(node_root, 'segmented') node_segmented.text = '%s' % 0 # lists=[[1,2,3,4,'cat'],[2,3,4,5,'car'],[5,6,9,8,'test']] for list in lists: xmin = list[0] ymin = list[1] xmax = list[2] ymax = list[3] node_object = SubElement(node_root, 'object') node_name = SubElement(node_object, 'name') node_name.text = list[4] pose = SubElement(node_object, 'pose') pose.text = 'Unspecified' node_truncated = SubElement(node_object, 'truncated') node_truncated.text = '0' node_difficult = SubElement(node_object, 'difficult') node_difficult.text = str(list[5]) node_bndbox = SubElement(node_object, 'bndbox') node_xmin = SubElement(node_bndbox, 'xmin') node_xmin.text = '%s' % xmin node_ymin = SubElement(node_bndbox, 'ymin') node_ymin.text = '%s' % ymin node_xmax = SubElement(node_bndbox, 'xmax') node_xmax.text = '%s' % xmax node_ymax = SubElement(node_bndbox, 'ymax') node_ymax.text = '%s' % ymax # if (xmax > xmin) | (ymax > ymin): # print(listnew) # print(pic_name) del lists[:] xml = tostring(node_root, pretty_print=True) dom = parseString(xml) save_xml = os.path.join(save_dir, image_name.replace('png', 'xml')) with open(save_xml, 'wb') as f: f.write(xml) return
img = None with open(mask_landmarks_label, "r") as f: line_list = list(f.readlines()) for line_i, line in enumerate(line_list): if line.startswith("#"): _line = line.replace("\n", "") path = _line[2:] filename = _line[2:].split('/')[1] folder = _line[2:].split('/')[0] img = cv2.imread(path) w, h, c = img.shape node_root = Element('annotation') node_folder = SubElement(node_root, 'folder') node_folder.text = folder node_filename = SubElement(node_root, 'filename') node_filename.text = filename node_filename = SubElement(node_root, 'path') node_filename.text = path node_size = SubElement(node_root, 'size') node_width = SubElement(node_size, 'width') node_width.text = str(w) node_height = SubElement(node_size, 'height') node_height.text = str(h) node_depth = SubElement(node_size, 'depth')
def __init__(self, id): logging.Log_Milestone("AniDB" + "_" + id) data = XMLFromURL(constants.ANIDB_HTTP_API_URL + id, id + ".xml", os.path.join("AniDB", id), CACHE_1HOUR * 24).xpath("""/anime""") if data != None: data = data[0] ##--------------------------------ID-----------------------------------## self.ID = id self.MetaType = "Anidb" self.Type = GetElementText(data, "type") ##--------------------------------Title--------------------------------## if data.xpath("""./titles"""): self.Title = functions.GetPreferedTitle(data.xpath("""./titles/title""")).encode('utf-8').strip().translate(constants.ReplaceChars) ##--------------------------------Summary------------------------------## if GetElementText(data, "description"): description = re.sub(r'(?m)^\*.*\n?', '', GetElementText(data, "description").replace("`", "'")).strip() self.Summary = re.sub(r"http://anidb\.net/[a-z]{2}[0-9]+ \[(.+?)\]", r"\1", description) ##--------------------------------Originally_Available_At--------------## if GetElementText(data, "startdate"): self.Originally_Available_At = GetElementText(data, "startdate") ##--------------------------------Rating-------------------------------## if GetElementText(data, "ratings/permanent") and GetElementText(data, "ratings/temporary"): self.Rating = (float(GetElementText(data, "ratings/permanent")) + float(GetElementText(data, "ratings/temporary"))) / 2 elif GetElementText(data, "ratings/permanent"): self.Rating = float(GetElementText(data, "ratings/permanent")) elif GetElementText(data, "ratings/temporary"): self.Rating = float(GetElementText(data, "ratings/temporary")) ##--------------------------------Studio-------------------------------## for creator in data.xpath("""./creators/name[@type="Animation Work"]"""): self.Studio = creator.text ##--------------------------------Countries----------------------------## for setting in data.xpath("""./tags/tag/name[text()="setting"]/.."""): for place in data.xpath("""./tags/tag[@parentid="%s"]""" % (setting.get("id"))): for planet in data.xpath("""./tags/tag[@parentid="%s"]""" % (place.get("id"))): for continent in data.xpath("""./tags/tag[@parentid="%s"]""" % (planet.get("id"))): for country in data.xpath("""./tags/tag[@parentid="%s" and @weight>="%s"]/name""" % (continent.get("id"), constants.MINIMUM_WEIGHT)): if self.Countries is None: self.Countries = [] self.Countries.append(country.text) ##--------------------------------Duration-----------------------------## for length in data.xpath("""./episodes/episode/epno[@type="1"]/.."""): if GetElementText(length, "length"): if self.Duration is None: self.Duration = 0 self.Duration = self.Duration + int(GetElementText(length, "length")) ##--------------------------------Genres-------------------------------## for element in data.xpath("""./tags/tag/name[text()="elements"]/.."""): for genre in data.xpath("""./tags/tag[@parentid="%s" and @weight>="%s"]/name""" % (element.get("id"), constants.MINIMUM_WEIGHT)): if self.Genres is None: self.Genres = [] self.Genres.append(str(genre.text).title()) ##--------------------------------Tags---------------------------------## for tag in data.xpath("""./tags/tag[@infobox="true"]/name"""): if self.Tags is None: self.Tags = [] self.Tags.append(str(tag.text).title()) ##--------------------------------Collections--------------------------## for collection in data.xpath("""./relatedanime/anime[(@type="Prequel") or (@type="Sequel")]"""): if self.Collections is None: self.Collections = [] self.Collections.append(collection.text) ##--------------------------------Content_Rating-----------------------## for indicators in data.xpath("""./tags/tag/name[text()="content indicators"]/.."""): ratingInt = 0 for tag in data.xpath("""./tags/tag[(@parentid="%s") and (@weight>="%s")]/name""" % (indicators.get("id"), constants.MINIMUM_WEIGHT)): if tag.text == "nudity": 1 if ratingInt < 1 else ratingInt elif tag.text == "sex": 2 if ratingInt < 2 else ratingInt elif tag.text == "violence": 2 if ratingInt < 2 else ratingInt if ratingInt == 0: self.Content_Rating = "PG-13" elif ratingInt == 1: self.Content_Rating = "R" elif ratingInt == 2: self.Content_Rating = "NC-17" ##--------------------------------Writers------------------------------## for writer in data.xpath("""./creators/name[(@type="Original Work") or (@type="Script") or (@type="Screenplay")]"""): if self.Writers is None: self.Writers = [] self.Writers.append(writer.text) ##--------------------------------Directors----------------------------## for director in data.xpath("""./creators/name[@type="Direction"]"""): if self.Directors is None: self.Directors = [] self.Directors.append(director.text) ##--------------------------------Producers----------------------------## for producer in data.xpath("""./creators/name[@type="Series Composition"]"""): if self.Producers is None: self.Producers = [] self.Producers.append(producer.text) ##--------------------------------Roles--------------------------------## roles = etree.tostring(E.Roles(), pretty_print=True, xml_declaration=True, encoding="UTF-8") roles = XML.ElementFromString(roles) for role in data.xpath("""./characters/character/charactertype[text()="Character"]/.."""): character_name = "" seiyuu_name = "" seiyuu_pic = "" if GetElementText(role, "name"): character_name = str(GetElementText(role, "name")) if GetElementText(role, "seiyuu"): seiyuu_name = GetElementText(role, "seiyuu") seiyuu_pic = "" if role.find('seiyuu').get('picture'): seiyuu_pic = constants.ANIDB_PIC_BASE_URL + role.find('seiyuu').get('picture') SubElement(roles, "Role", character_name = character_name, seiyuu_name = seiyuu_name, seiyuu_pic = seiyuu_pic) if not roles is None: self.Roles = roles ##--------------------------------Images------------------------------## if GetElementText(data, "picture"): season = etree.tostring(E.Images(), pretty_print=True, xml_declaration=True, encoding="UTF-8") season = XML.ElementFromString(season) bannerPath = GetElementText(data, "picture") mainUrl, thumbUrl, mainLocalPath, thumbLocalPath = functions.ParseImage(bannerPath, constants.ANIDB_PIC_BASE_URL, os.path.join("AniDB", id, "season"), constants.ANIDB_PIC_THUMB_URL % os.path.splitext(bannerPath)[0]) SubElement(season, "Image", id = "1", mainUrl = mainUrl, thumbUrl = thumbUrl, mainLocalPath = mainLocalPath, thumbLocalPath = thumbLocalPath, season = "1") self.Season = season self.Posters = copy.deepcopy(season) ##--------------------------------Themes-------------------------------## self.Themes = [] ##--------------------------------Links--------------------------------## if GetElementText(data, "resources"): links = etree.tostring(E.Links(), pretty_print=True, xml_declaration=True, encoding="UTF-8") links = XML.ElementFromString(links) value = [] for externalentity in data.xpath("""./resources/resource/*"""): for identifier in externalentity.xpath("""./identifier"""): if externalentity.getparent().get("type") == "1": SubElement(links, "Link", type = "ANN", url = constants.ANIDB_RESOURCES_ANN % (identifier.text), value = identifier.text) elif externalentity.getparent().get("type") == "2": SubElement(links, "Link", type = "MAL", url = constants.ANIDB_RESOURCES_MAL % (identifier.text), value = identifier.text) elif externalentity.getparent().get("type") == "3": value.append(identifier.text) elif externalentity.getparent().get("type") == "4": SubElement(links, "Link", type = "OfficialJP", url = identifier.text) elif externalentity.getparent().get("type") == "5": SubElement(links, "Link", type = "OfficialEN", url = identifier.text) elif externalentity.getparent().get("type") == "6": SubElement(links, "Link", type = "WikiEN", url = constants.ANIDB_RESOURCES_WIKIEN % (identifier.text), value = identifier.text) elif externalentity.getparent().get("type") == "7": SubElement(links, "Link", type = "WikiJP", url = constants.ANIDB_RESOURCES_WIKIJP % (identifier.text), value = identifier.text) elif externalentity.getparent().get("type") == "8": SubElement(links, "Link", type = "Schedule", url = constants.ANIDB_RESOURCES_SCHEDULE % (identifier.text), value = identifier.text) elif externalentity.getparent().get("type") == "9": SubElement(links, "Link", type = "AllCinema", url = constants.ANIDB_RESOURCES_ALLCINEMA % (identifier.text), value = identifier.text) elif externalentity.getparent().get("type") == "10": SubElement(links, "Link", type = "Anison", url = constants.ANIDB_RESOURCES_ANISON % (identifier.text), value = identifier.text) elif externalentity.getparent().get("type") == "11": SubElement(links, "Link", type = "Lain", url = constants.ANIDB_RESOURCES_LAIN % (identifier.text), value = identifier.text) elif externalentity.getparent().get("type") == "14": SubElement(links, "Link", type = "VNDB", url = constants.ANIDB_RESOURCES_VNDB % (identifier.text), value = identifier.text) elif externalentity.getparent().get("type") == "15": SubElement(links, "Link", type = "Marumegane", url = constants.ANIDB_RESOURCES_MARUMEGANE % (identifier.text), value = identifier.text) if externalentity.getparent().get("type") == "3": SubElement(links, "Link", type = "AnimeNfo", url = constants.ANIDB_RESOURCES_ANIMENFO % (u"%s" % value[0], u"%s" % value[1]), value = u"%s" % value) self.Links = links ##--------------------------------EpisodeCount-------------------------## self.EpisodeCount = int(GetElementText(data, "episodecount")) ##--------------------------------SpecialCount-------------------------## self.SpecialCount = len(data.xpath("""./episodes/episode/epno[@type="2"]""")) ##--------------------------------OP/ED_List---------------------------## self.OpList = [] self.EdList = [] for specials in data.xpath("""./episodes/episode/epno[@type="3"]/.."""): if functions.CleanTitle(functions.GetPreferedTitleNoType(specials.xpath("""./title"""))).startswith("Opening"): self.OpList.append(str(GetElementText(specials, "epno"))) if functions.CleanTitle(functions.GetPreferedTitleNoType(specials.xpath("""./title"""))).startswith("Ending"): self.EdList.append(str(GetElementText(specials, "epno"))) ##--------------------------------Episodes-----------------------------## if len(data.xpath("""./episodes/episode""")) > 0: self.Episodes = [] for item in data.xpath("""./episodes/episode"""): self.Episodes.append(self.Episode(item, id)) #Log("AniDB - __init__() - Populate Title: '%s', Network: '%s', Overview: '%s', FirstAired: '%s', Genre: '%s', ContentRating: '%s', Rating: '%s', Episodes: '%s', EpisodeCount: '%s', SpecialCount: '%s', OpCount: '%s', EdCount: '%s', Posters: '%s'" #% (self.Title, self.Network, self.Overview, self.FirstAired, self.Genre, self.ContentRating, self.Rating, self.Episodes, self.EpisodeCount, self.SpecialCount, len(self.OpList), len(self.EdList), self.Posters) ) logging.Log_Milestone("AniDB" + "_" + id)
def createXML(self): self.xmlTree = Element('app') SubElement(self.xmlTree, 'appID').text = '0x{:0X}'.format(self.appID) SubElement(self.xmlTree, 'name').text = self.name chRemoting = Element('remotingInfo') SubElement(chRemoting, 'protocolID').text = self.protocolID if self.format: SubElement(chRemoting, 'format').text = self.format if self.direction: SubElement(chRemoting, 'direction').text = self.direction self.xmlTree.append(chRemoting) if self.hasAppInfo: chAppInfo = Element('appInfo') SubElement(chAppInfo, 'appCategory').text = self.appCategory SubElement(chAppInfo, 'trustLevel').text = self.appTrustLevel self.xmlTree.append(chAppInfo) if self.hasDisplay: chDisplay = Element('displayInfo') SubElement(chDisplay, 'contentCategory').text = self.displayCategory SubElement(chDisplay, 'trustLevel').text = self.displayTrustLevel self.xmlTree.append(chDisplay) if self.hasAudio: chAudio = Element('audioInfo') SubElement(chAudio, 'audioType').text = self.audioType SubElement(chAudio, 'contentCategory').text = self.audioCategory self.xmlTree.append(chAudio) if self.resourceStatus: SubElement(self.xmlTree, 'resourceStatus').text = self.resourceStatus # self.xmlString = ElementTree.tostring(self.xmlTree, encoding='UTF-8', method='xml') return self.xmlTree
def test_issuer(self): root = Element(Q_NAMES['saml2p:AuthnRequest'], nsmap=EIDAS_NAMESPACES) SubElement(root, Q_NAMES['saml2:Issuer']).text = 'test-issuer' request = SAMLRequest(ElementTree(root), 'CZ', 'relay123') self.assertEqual(request.issuer, 'test-issuer')
def test_assertion_exists(self): root = Element(Q_NAMES['saml2p:Response']) assertion = SubElement(root, Q_NAMES['saml2:Assertion']) self.assertIs(SAMLResponse(ElementTree(root)).assertion, assertion)
width = '1632' height = '1224' depth = '3' num = 0 #see the processing speed for ann in anns: num += 1 print(num, ann) annpath = ann_src + ann filename = ann.replace('xml', 'jpg') # annotation node_root = Element('annotation') ## folder node_folder = SubElement(node_root, 'folder') node_folder.text = folder ## filename node_filename = SubElement(node_root, 'filename') node_filename.text = filename ## source node_source = SubElement(node_root, 'source') node_database = SubElement(node_source, 'database') node_database.text = database node_annotation = SubElement(node_source, 'annotation') node_annotation.text = annotation node_image = SubElement(node_source, 'image') node_image.text = image ## size node_size = SubElement(node_root, 'size') node_width = SubElement(node_size, 'width')
def test_create_light_response_missing_decrypted_assertion(self): root = Element(Q_NAMES['saml2p:Response'], nsmap=EIDAS_NAMESPACES) SubElement(root, Q_NAMES['saml2:EncryptedAssertion']) saml_response = SAMLResponse(ElementTree(root)) self.assertEqual(saml_response.create_light_response().attributes, {})