def __init__(self, raw_data: tuple, output_file: str): self.raw_data = raw_data self.output_file = output_file self.output_file = self.process_xml_extension(self.output_file) root = ET.Element("deck", attrib={"name": output_file[:-4]}) for data in raw_data: card = ET.Element("card") id_ = ET.SubElement(card, "id") id_.text = str(data[0]) front = ET.SubElement(card, "front") front.text = data[1] back = ET.SubElement(card, 'back') back.text = data[2] root.append(card) buf = BytesIO() buf.write(ET.tostring(root)) buf.seek(0) string = minidom.parse(buf).toprettyxml(indent=" " * 4) with open(self.output_file, mode="w+", encoding="utf-8") as file_: file_.write(string)
def async_handle_control(request, state_variables, xml_ns): body = yield from request.content.read() # read command and args el_request = ET.fromstring(body) el_body = el_request.find("envelope:Body", NS) el_command = el_body.find("./") command = el_command.tag.split("}")[1] args = {el_arg.tag: el_arg.text for el_arg in el_command.findall("./")} LOGGER.debug("Command: %s", command) LOGGER.debug("Args: %s", args) # do command result = {} for state_var in state_variables.values(): if state_var.matches_action(command): result = yield from state_var.do_command(command, **args) if result is None: return web.Response(status=404) LOGGER.debug("Result: %s", result) # return result response_base = ( '<?xml version="1.0" encoding="utf-8"?>' '<s:Envelope s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">' "<s:Body />" "</s:Envelope>") el_envelope = ET.fromstring(response_base) el_body = el_envelope.find("./envelope:Body", NS) el_response = ET.SubElement(el_body, "{" + xml_ns + "}" + command + "Response") for key, value in result.items(): el_arg = ET.SubElement(el_response, key) el_arg.text = str(value) text = ET.tostring(el_envelope) return web.Response(status=200, body=text)
def queryAlerts(server, index, maxAlerts): xml = """{ "sort": { "createTime": { "order": "desc" } } }""" returnData = "" es = Elasticsearch() res = es.search(index=index, doc_type="Alert", body={"query": { "match_all": {} }}) print("Got %d Hits:" % res['hits']['total']) EWSSimpleAlertInfo = xmlParser.Element('EWSSimpleAlertInfo') alerts = xmlParser.SubElement(EWSSimpleAlertInfo, "Alerts") for hit in res['hits']['hits']: requestString = "%(originalRequestString)s " % hit["_source"] print("%(originalRequestString)s " % hit["_source"]) alert = xmlParser.SubElement(alerts, "Alert") requestXML = xmlParser.SubElement(alert, "Request") requestXML.text = requestString returnData = tostring(EWSSimpleAlertInfo) return returnData
def async_notify_listeners(self, **kwargs): property = str(self.__class__.__name__) value = str(self.value) LOGGER.debug("async_notify_listeners(): %s -> %s", property, value) event_base = ('<Event xmlns="urn:schemas-upnp-org:metadata-1-0/RCS/">' '<InstanceID val="0" />' "</Event>") el_event = ET.fromstring(event_base) el_instance_id = el_event.find(".//rcs:InstanceID", NS) args = kwargs.copy() args.update({"val": value}) ET.SubElement(el_instance_id, "rcs:" + property, **args) notify_base = ( '<?xml version="1.0" encoding="utf-8"?>' '<e:propertyset xmlns:e="urn:schemas-upnp-org:event-1-0">' "<e:property>" "<LastChange />" "</e:property>" "</e:propertyset>") el_notify = ET.fromstring(notify_base) el_last_change = el_notify.find(".//LastChange", NS) el_last_change.text = ET.tostring(el_event).decode("utf-8") global SUBSCRIBED_CLIENTS service_name = self.SERVICE_NAME for sid, url in SUBSCRIBED_CLIENTS[service_name].items(): headers = {"SID": sid} with ClientSession(loop=asyncio.get_event_loop()) as session: with async_timeout.timeout(10): data = ET.tostring(el_notify) LOGGER.debug("Calling: %s", url) yield from session.request("NOTIFY", url, headers=headers, data=data)