def convert(self, xmlroot): """ :param Element xmlroot: root element of xml to convert. :rtype: wsag_model.Agreement """ # for name, value in xmlroot.attrib.items(): # logger.debug('SLA xmlconverter: {} = {}'.format(name, value)) if xmlroot.tag in self.agreement_tags: result = Agreement() agreementId = str(QName(self._namespaces["wsag"], "AgreementId")) result.agreement_id = xmlroot.attrib[agreementId] elif xmlroot.tag in self.template_tags: result = Template() templateId = str(QName(self._namespaces["wsag"], "TemplateId")) result.template_id = xmlroot.attrib[templateId] else: raise ValueError("Not valid root element name: " + xmlroot.tag) context = xmlroot.find("wsag:Context", self._namespaces) result.context = self._parse_context(context) terms = xmlroot.find("wsag:Terms/wsag:All", self._namespaces) properties = terms.findall("wsag:ServiceProperties", self._namespaces) result.variables = self._parse_properties(properties) guarantees = terms.findall("wsag:GuaranteeTerm", self._namespaces) result.guaranteeterms = self._parse_guarantees(guarantees) return result
def create(): element = Element(QName(NS_SOAP, 'Reason')) value = Element(QName(NS_SOAP, 'Text')) element.append(value) return element
def probe_metric(service_url, metric): ''' Query the service at the given URL for the given metric value. Assumptions are made about the name of the method and output parameters which are only valid for the WanCommonInterfaceConfig service. ''' envelope = E( QName(ns['s'], 'Envelope'), { QName(ns['s'], 'encodingStyle'): 'http://schemas.xmlsoap.org/soap/encoding/' }) body = sE(envelope, QName(ns['s'], 'Body')) method = sE(body, QName(ns['i'], 'Get{}'.format(metric))) request_tree = ET(envelope) with io.BytesIO() as out: out.write(b'<?xml version="1.0"?>') request_tree.write(out, encoding='utf-8') out.write(b'\r\n') # or else my Belkin F5D8236-4 never responds... req = urllib.request.Request(service_url, out.getvalue()) req.add_header('Content-Type', 'text/xml') req.add_header('SOAPAction', '"{}#{}"'.format(ns['i'], 'Get{}'.format(metric))) with urllib.request.urlopen(req) as result: result_tree = ElementTree.parse(result) return int( result_tree.findtext('.//New{}'.format(metric), namespaces=ns))
def from_xml(cls, element: Element): if element[1].tag != QName(FES20, "LowerBoundary") or element[2].tag != QName( FES20, "UpperBoundary" ): raise ExternalParsingError( f"{element.tag} should have 3 child nodes: " f"(expression), <LowerBoundary>, <UpperBoundary>" ) lower = get_child(element, FES20, "LowerBoundary") upper = get_child(element, FES20, "UpperBoundary") if len(lower) != 1: raise ExternalParsingError( f"{lower.tag} should have 1 expression child node" ) if len(upper) != 1: raise ExternalParsingError( f"{upper.tag} should have 1 expression child node" ) return cls( expression=Expression.from_child_xml(element[0]), lowerBoundary=Expression.from_child_xml(lower[0]), upperBoundary=Expression.from_child_xml(upper[0]), _source=element.tag, )
def messages(self): logmessages_tag = QName(XMLNS, 'LogMessages').text message_tag = QName(XMLNS, 'Message').text el = self.xml.find(logmessages_tag) if not el: return list() return list(m.text for m in el.findall(message_tag))
def create(): element = Element(QName(NS_SOAP, 'Subcode')) value = Element(QName(NS_SOAP, 'Value')) element.append(value) return element
def tips_statuslist(self, status_list): el = Element(QName(XMLNS, 'EntityStatusList'), {'entity': self.entity}) for item in status_list: status = EntityStatusChoices.ENABLED if item[ 'enabled'] else EntityStatusChoices.DISABLED subel = SubElement(el, QName(XMLNS, status)) subel.text = item['name'] return el
def LastChange(self): root = Element(QName(self._event_ns, "Event")) inst = SubElement(root, QName(self._event_ns, "InstanceID"), {"val": "0"}) for name, value in self._changes.items(): SubElement(inst, QName(self._event_ns, name), {"val": value}) misc.set_ns_prefixes(root, {"": self._event_ns}) return ElementTree.tostring(root, encoding="utf-8").decode("utf-8")
def addresponse(self, path, root, depth): e = SubElement(root, QName("DAV:", 'response')) href = SubElement(e, QName("DAV:", 'href')) href.text = path propstat = SubElement(e, QName("DAV:", 'propstat')) prop = SubElement(propstat, QName("DAV:", 'resourcetype')) if os.path.isdir(path): SubElement(prop, QName("DAV:", 'collection'))
def add_array(node, items): rdf_type = next( c.rdf_type for c in XMP_CONTAINERS if isinstance(items, c.py_type) ) seq = ET.SubElement(node, QName(XMP_NS_RDF, rdf_type)) for item in items: el = ET.SubElement(seq, QName(XMP_NS_RDF, 'li')) el.text = item.replace('\x00', '')
def __init__(self, method, entity): self.path = f'{ROOT_PATH}/{method}/{entity}' self.entity = entity self.path = None self.xml = Element(QName(XMLNS, 'TipsApiRequest')) tips_header = SubElement(self.xml, QName(XMLNS, 'TipsHeader'), {'version': VERSION}) if entity in (EntityChoices.GUEST_USER, EntityChoices.ONBOARD_DEVICE): tips_header.set('source', 'Guest')
def parsePropElementHref(self, prop, result, is_list): # Grab the element data hrefs = tuple([URL(url=href.text, decode=True) for href in prop.findall(str(davxml.href))]) if not is_list: if len(hrefs) == 1: hrefs = hrefs[0] else: hrefs = "" result.addHrefProperty(QName(prop.tag), hrefs) result.addNodeProperty(QName(prop.tag), prop)
def test_qualified_attrib(self): ns, nsa = 'http://x.y.z/ns', 'http://x.y.z/ns/a' fruit = self.root.find('.//%s' % QName(ns, 'fruit')) # default ns, not qualitifed (TODO: why?) self.assertEqual(fruit.get('name'), 'Apple') self.assertIsNone(fruit.get(QName(ns, 'name'))) # non-default ns, qualified self.assertEqual(fruit.get(QName(nsa, 'name')), 'Apple (a)')
def LastChange(self): root = Element(QName(self._event_ns, "Event")) inst = SubElement(root, QName(self._event_ns, "InstanceID"), {"val": "0"}) for name, value in self._changes.items(): SubElement(inst, QName(self._event_ns, name), {"val": value}) misc.set_ns_prefixes(root, {"": self._event_ns}) writer = StringIO() ElementTree(root).write(writer, encoding="utf-8") return writer.getvalue()
def test_deserialize(self): convert = self.converter.deserialize invalid = ["a:b", "", " ", "{a}1b", "{a} b", "{|}b", "a a"] for inv in invalid: with self.assertRaises(ConverterError): convert(inv) self.assertEqual(QName("{a}b"), convert("{a}b")) self.assertEqual(QName("a"), convert("a", ns_map={})) self.assertEqual(QName("aa", "b"), convert("a:b", ns_map={"a": "aa"}))
def create(): client = Element(QName(NS_CEP, 'client')) last_update = Element(QName(NS_CEP, 'lastUpdate')) last_update.attrib[ATTR_NIL] = 'true' client.append(last_update) preferred_language = Element(QName(NS_CEP, 'preferredLanguage')) preferred_language.attrib[ATTR_NIL] = 'true' client.append(preferred_language) return client
def test_find_qname(self): ns, nsa = 'http://x.y.z/ns', 'http://x.y.z/ns/a' # default namespace color = self.root.find('.//%s' % QName(ns, 'color')) self.assertEqual(color.tag, '{http://x.y.z/ns}color') # Clark notation self.assertIsNone(self.root.find('.//color')) # another namespace color_a = self.root.find('.//%s' % QName(nsa, 'color')) self.assertEqual(color_a.tag, '{http://x.y.z/ns/a}color')
def from_element(cls, element): type = URI(QName(element.tag)) uri = URI(element.get(QName(RDF, 'about'))) type_class = cls.TYPE_MAP.get(type, cls) if cls is type_class: test = cls(type, uri) test.status = cls._status(element) test.description = cls._description(element) test.warning = cls._warning(element) else: test = type_class.from_element(element) return test
def __setitem__(self, key, val): if not self._updating: raise RuntimeError("Metadata not opened for editing, use with block") def add_array(node, items): rdf_type = next( c.rdf_type for c in XMP_CONTAINERS if isinstance(items, c.py_type) ) seq = ET.SubElement(node, QName(XMP_NS_RDF, rdf_type)) for item in items: el = ET.SubElement(seq, QName(XMP_NS_RDF, 'li')) el.text = item.replace('\x00', '') try: # Locate existing node to replace node, attrib, _oldval, parent = next(self._get_elements(key)) if attrib: if not isinstance(val, str): raise TypeError(val) node.set(attrib, val) elif isinstance(val, (list, set)): for child in node.findall('*'): node.remove(child) add_array(node, val) elif isinstance(val, str): for child in node.findall('*'): node.remove(child) node.text = val else: raise TypeError(val) except StopIteration: # Insert a new node rdf = self._xmp.find('.//rdf:RDF', self.NS) if isinstance(val, (list, set)): rdfdesc = ET.SubElement( rdf, QName(XMP_NS_RDF, 'Description'), attrib={ QName(XMP_NS_RDF, 'about'): '', }, ) node = ET.SubElement(rdfdesc, self._qname(key)) add_array(node, val) elif isinstance(val, str): rdfdesc = ET.SubElement( rdf, QName(XMP_NS_RDF, 'Description'), attrib={ QName(XMP_NS_RDF, 'about'): '', self._qname(key): val }, ) else: raise TypeError(val)
def getIMDBids(self): movies = [] enablers = [ tryInt(x) for x in splitString(self.conf('automation_urls_use')) ] urls = splitString(self.conf('automation_urls')) namespace = 'http://www.w3.org/2005/Atom' namespace_im = 'http://itunes.apple.com/rss' index = -1 for url in urls: index += 1 if len(enablers ) == 0 or len(enablers) < index or not enablers[index]: continue try: cache_key = 'itunes.rss.%s' % md5(url) rss_data = self.getCache(cache_key, url) data = XMLTree.fromstring(rss_data) if data is not None: entry_tag = str(QName(namespace, 'entry')) rss_movies = self.getElements(data, entry_tag) for movie in rss_movies: name_tag = str(QName(namespace_im, 'name')) name = self.getTextElement(movie, name_tag) releaseDate_tag = str( QName(namespace_im, 'releaseDate')) releaseDateText = self.getTextElement( movie, releaseDate_tag) year = datetime.datetime.strptime( releaseDateText, '%Y-%m-%dT00:00:00-07:00').strftime("%Y") imdb = self.search(name, year) if imdb and self.isMinimalMovie(imdb): movies.append(imdb['imdb']) except: log.error('Failed loading iTunes rss feed: %s %s', (url, traceback.format_exc())) return movies
def _on_play(self): if self.source is None or self.state == "PLAYING": return if self._timer: self._timer.unregister() from cocy.upnp import DIDL_LITE_NS duration = self.source_meta_dom.find( str(QName(DIDL_LITE_NS, "item")) + "/" + str(QName(DIDL_LITE_NS, "res"))) \ .get("duration") self.current_track_duration = duration_to_secs(duration) self._timer = Timer(self.current_track_duration, end_reached()).register(self) self.state = "PLAYING"
def __init__(self, geopackage_path, log=None): """ :param geopackage_path: the file path for the geopackaging we are working on :param srs_name: The code value for the SRS (CODE: 4326 in EPSG:4326) :param srs_org: The organization specifying the SRS (ORG: EPSG in EPSG:4326) :param log: an optional log file passed through from an outer context """ self.geopackage_path = geopackage_path self.log = log existing_metadata = self.__load_metadata() self.existing_metadata = dict() for _entry in existing_metadata: self.existing_metadata[_entry["id"]] = _entry # register the namespaces we will be using as we manipulate the data [ register_namespace(namespace.value[0], namespace.value[1]) for namespace in NameSpaces ] # check to see if there is existing data or not if len(self.existing_metadata): try: self.tree = ElementTree.fromstring( self.existing_metadata[1]["metadata"].encode( "ascii", "ignore")) except Exception: # metadata is invalid or not what we want self.tree = ElementTree.parse( source=MD_TREE).getroot() # Load base MD XML # set the creation date self.tree.set(QName(NameSpaces.ISM.value[1], "createDate"), datetime.date.today().isoformat()) else: self.tree = ElementTree.parse( source=MD_TREE).getroot() # Load base MD XML # set the creation date self.tree.set(QName(NameSpaces.ISM.value[1], "createDate"), datetime.date.today().isoformat()) # Fill out our DateStamp elem date_element = self.__build_tag(namespace=NameSpaces.GMD.value[1], tag=Tags.DATE_STAMP) self.__build_tag( namespace=NameSpaces.GCO.value[1], tag="Date", parent=date_element, text=str(datetime.date.today().isoformat()), ) # insert as the 3rd element bc spec self.tree.insert(3, date_element)
def _apply_changes(self): """Serialize our changes back to the PDF in memory Depending how we are initialized, leave our metadata mark and producer. """ if self.mark: self[QName(XMP_NS_XMP, 'MetadataDate')] = datetime.now().isoformat() self[QName(XMP_NS_PDF, 'Producer')] = 'pikepdf ' + pikepdf_version xml = self._get_xml_bytes() self._pdf.Root.Metadata = Stream(self._pdf, xml) self._pdf.Root.Metadata[Name.Type] = Name.Metadata self._pdf.Root.Metadata[Name.Subtype] = Name.XML if self.sync_docinfo: self._update_docinfo()
def getIMDBids(self): movies = [] rss_movies = self.getRSSData(self.urls['theater']) rating_tag = str(QName(self.urls['namespace'], 'tomatometer_percent')) for movie in rss_movies: value = self.getTextElement(movie, "title") result = re.search('(?<=%\s).*', value) if result: log.info2('Something smells...') rating = tryInt(self.getTextElement(movie, rating_tag)) name = result.group(0) if rating < tryInt(self.conf('tomatometer_percent')): log.info2('%s seems to be rotten...', name) else: log.info2('Found %s fresh enough movies, enqueuing: %s', (rating, name)) year = datetime.datetime.now().strftime("%Y") imdb = self.search(name, year) if imdb: movies.append(imdb['imdb']) return movies
def test_from_value(self): self.assertEqual(DataType.BOOLEAN, DataType.from_value(True)) self.assertEqual(DataType.INT, DataType.from_value(1)) self.assertEqual(DataType.FLOAT, DataType.from_value(1.1)) self.assertEqual(DataType.DECIMAL, DataType.from_value(Decimal(1.1))) self.assertEqual(DataType.QNAME, DataType.from_value(QName("a"))) self.assertEqual(DataType.STRING, DataType.from_value("a"))
def parsePropElement(self, prop, result, badstatus): # Here we need to detect the type of element and dispatch accordingly if badstatus: result.addBadProperty(QName(prop.tag), badstatus) # Determine what type of content we have else: children = prop.getchildren() children_length = len(children) if children_length == 0: self.parsePropElementText(prop, result) elif prop.text is None or not prop.text.strip(): if children_length == 1: if children[0].tag == davxml.href: self.parsePropElementHref(prop, result, False) else: self.parsePropElementUnknown(prop, result) else: allHref = True for child in children: if child.tag != davxml.href: allHref = False if allHref: self.parsePropElementHref(prop, result, True) else: self.parsePropElementUnknown(prop, result) else: self.parsePropElementUnknown(prop, result)
def create(): element = Element(QName(NS_CEP, 'requestFilter')) policy_oids = Element(QName(NS_CEP, 'policyOIDs')) policy_oids.attrib[ATTR_NIL] = 'true' element.append(policy_oids) client_version = Element(QName(NS_CEP, 'clientVersion')) client_version.attrib[ATTR_NIL] = 'true' element.append(client_version) server_version = Element(QName(NS_CEP, 'serverVersion')) server_version.attrib[ATTR_NIL] = 'true' element.append(server_version) return element
def find_weather(): for i in locationcode: url = WEATHER_URL % i url = url + METRIC_PARAMETER xml = ET.parse(urllib.urlopen(url)) root = xml.getroot() ns = "http://xml.weather.yahoo.com/ns/rss/1.0" site = root[0][0].text.split(' - ')[1] print "Current location is:", site # print "Current date is:", root[0][4].text # for condition in root.findall('yweather:condition'): # print "get it" # print condition.attrib body_tag = str(QName(ns, 'condition')) body = root[0][12].findall(body_tag) # print body[0].attrib['date'] dicweather = body[0].attrib # print ' -->',dicweather['date'] # print ' -->',dicweather['low'] # print ' -->',dicweather['high'] # print ' -->',dicweather['text'] # head = ['date','text','temp','low','location'] # write_file(filename, # head,dicweather['date'].split(',')[1].split('AEDT')[0],dicweather['text'],dicweather['temp'],site.split(',')[0]) store_db(dicweather['date'].split(',')[1].split('AEDT')[0], dicweather['text'], dicweather['temp'])
def _update_docinfo(self): """Update the PDF's DocumentInfo dictionary to match XMP metadata The standard mapping is described here: https://www.pdfa.org/pdfa-metadata-xmp-rdf-dublin-core/ """ self._pdf.docinfo # Touch object to ensure it exists for uri, element, docinfo_name, converter in self.DOCINFO_MAPPING: qname = QName(uri, element) try: value = self[qname] except KeyError: if docinfo_name in self._pdf.docinfo: del self._pdf.docinfo[docinfo_name] continue if converter: value = converter.docinfo_from_xmp(value) if value is None: del self._pdf.docinfo[docinfo_name] continue try: # Try to save pure ASCII self._pdf.docinfo[docinfo_name] = value.encode('ascii') except UnicodeEncodeError: # qpdf will serialize this as a UTF-16 with BOM string self._pdf.docinfo[docinfo_name] = value
def create_response(self, action_name: str, arguments: dict, encoding=None, envelope_attrib=None, typed=None): """ Creates a Soap response to the action with the specified arguments. """ register_namespace('', None) if encoding is None: encoding = self._encoding if envelope_attrib is None: envelope_attrib = self._envelope_attrib if typed is None: typed = self._typed envelope = Element("s:Envelope") if envelope_attrib: for eakey, eaval in envelope_attrib: envelope.attrib.update({eakey: eaval}) else: envelope.attrib.update({'xmlns:s': NS_SOAP_ENV}) envelope.attrib.update({'s:encodingStyle': encoding}) body = SubElement(envelope, "s:Body") action_name_tag_name = action_name + "Response" methElement = SubElement(body, action_name_tag_name) if encoding: methElement.set(NS_SOAP_ENV + "encodingStyle", encoding) if arguments: for arg_name, arg_val in arguments.items(): py_type = type(arg_val) soap_type = PYTHON_TO_SOAP_TYPE_MAP[py_type] if soap_type == 'xsd:string': arg_val = arg_val # pylint: disable=self-assigning-variable elif soap_type == 'xsd:int' or soap_type == 'xsd:float': arg_val = str(arg_val) elif soap_type == 'xsd:boolean': arg_val = "1" if arg_val else "0" argElement = SubElement(methElement, arg_name) if typed and soap_type: if not isinstance(type, QName): arg_type = QName(NS_XSD, soap_type) argElement.set(NS_XSI + "type", soap_type) argElement.text = arg_val else: methElement.text = "" envelope_content = xml_tostring(envelope, short_empty_elements=False) content = XML_DOCUMENT_DECLARATION + "\n" + str_cast(envelope_content) return content