def iq_handler(self, xml): iq = CloudDotIQ(xml) print "Got IQ", type(iq), iq data = iq.data print "Data:", repr(data) numBytes = iq.numReturnBytes # Set the DM's flag that we're using the device. # This is definitely incorrect, but it may be good enough. self.dm.scanEvent.wait() try: self.dm.scanEvent.clear() self.device.write(data, modbus=True, checksum=False) result = self.device.read(numBytes, modbus=True) result = struct.pack("B" * len(result), *result) finally: self.dm.scanEvent.set() # We have the result, send it back in the response. etIq = self.Iq(xml=xml) result = base64.b64encode(result) reply = etIq.reply() dataElem = Element("data") dataElem.text = result qt = Element("query", {"xmlns": "jabber:iq:clouddot"}) qt.append(dataElem) print "qt", qt reply.appendxml(qt) print "etIq.reply() redux:", reply reply.send(block=False)
def rss2(feed, pretty_print=True): indent = '\n ' if pretty_print else '' indent2 = '\n ' if pretty_print else '' rss = Element('rss', version='2.0') rss.text = '\n ' if pretty_print else '' channel = SubElement(rss, 'channel') channel.text = indent channel.tail = '\n' if pretty_print else '' set_rss2_text(SubElement(channel, 'title'), feed.title) set_rss2_text(SubElement(channel, 'description'), feed.subtitle or '') SubElement(channel, 'link').text = feed.link SubElement(channel, 'lastBuildDate').text = rss2_date(feed.updated) if feed.language: SubElement(channel, 'language').text = feed.language if feed.rights: SubElement(channel, 'copyright').text = feed.rights if feed.logo: image = SubElement(channel, 'image') image.text = indent2 SubElement(image, 'url').text = feed.logo SubElement(image, 'title').text = '' SubElement(image, 'link').text = feed.link for child in image: child.tail = indent2 image[-1].tail = '\n ' if pretty_print else '' for entry in feed.entries: item = entry.rss2(pretty_print) item[-1].tail = indent channel.append(item) for child in channel: child.tail = indent channel[-1].tail = '\n ' if pretty_print else '' return rss
def _fill_in_location_element(xml_root, location, data_fields): fixture_fields = [ 'name', 'site_code', 'external_id', 'latitude', 'longitude', 'location_type', 'supply_point_id', ] for field in fixture_fields: field_node = Element(field) val = getattr(location, field) field_node.text = six.text_type(val if val is not None else '') xml_root.append(field_node) # in order to be indexed, custom data fields need to be top-level # so we stick them in there with the prefix data_ for field in data_fields: if field.index_in_fixture: field_node = Element(_get_indexed_field_name(field.slug)) val = location.metadata.get(field.slug) field_node.text = six.text_type(val if val is not None else '') xml_root.append(field_node) xml_root.append(_get_metadata_node(location, data_fields))
def _set_text(self, tag, text): elem = self._xmlnode.find(tag) if text != None: if elem == None: elem = Element(tag) self._xmlnode.append(elem) elem.text = text
def save(self): # Generate List in RAM root = None services = self.services controllers = self.controllers # Build Header from plugin import NAME, VERSION root = Element(NAME) root.set('version', VERSION) root.append(Comment(_("Don't edit this manually unless you really know what you are doing"))) # Build Body def build(root, instances, typ): for instance in instances: # Add module element = SubElement( root, typ, name = stringToXML(instance.getName()), enable = stringToXML(instance.getStringEnable()) ) # Add options options = instance.getStringOptions() if options: for key, value, description in options: SubElement( element, OPTION, key = stringToXML(key) ).text = stringToXML(value) return root if services: root = build( root, services, SERVICE) if controllers: root = build( root, controllers, CONTROLLER) self.writeXML( root )
def saveXML(self): if ChannelsBase.channels_changed: channels = ChannelsBase.channels # Generate List in RAM root = None #splog("saveXML channels", channels) splog("SP saveXML channels", len(channels)) # Build Header from plugin import NAME, VERSION root = Element(NAME) root.set('version', VERSION) root.append(Comment(_("Don't edit this manually unless you really know what you are doing"))) # Build Body def build(root, channels): if channels: for reference, namealternatives in channels.iteritems(): name, alternatives = namealternatives # Add channel element = SubElement( root, "Channel", name = stringToXML(name), reference = stringToXML(reference) ) # Add alternatives if alternatives: for name in alternatives: SubElement( element, "Alternative" ).text = stringToXML(name) return root root = build( root, channels ) self.writeXML( root )
def to_xml(self): node = Element(self.__class__.__name__, {}) for _annot in self._annotations: node.append(_annot.to_xml()) return node
def build_indicators_node(self): result = Element(self.INDICATORS) self._add_possible_dataset_specified_indicators_to_dict() for data_indicator in self._indicator_dic: result.append(self.build_indicator_node(self._indicator_dic[data_indicator])) self._root.append(result)
def add_nodes(self, G, graph_element): nodes_element = Element("nodes") for node, data in G.nodes_iter(data=True): node_data = data.copy() # node_id = node_data.pop("id", make_str(node)) kw = {"id": node_id} label = node_data.pop("label", make_str(node)) kw["label"] = label pid = node_data.pop("pid", False) if pid: kw["pid"] = pid # add node element with attributes node_element = Element("node", **kw) # add node element and attr subelements default = G.graph.get("node_default", {}) node_data = self.add_parents(node_element, node_data) node_data = self.add_slices(node_element, node_data) node_data = self.add_viz(node_element, node_data) node_data = self.add_attributes("node", node_element, node_data, default) nodes_element.append(node_element) graph_element.append(nodes_element)
def save_graph(self, filename, graph): root = Element("statespace") root.set("init-node-id", graph.get_root().get_id()) def save_node(node): node_el = SubElement(root, "node") node_el.set("id", str(node.get_id())) node_el.set("size", str(node.get_size())) return node_el def save_edge(edge, parent_node_el): edge_el = SubElement(parent_node_el, "arc") edge_el.set("node-id", edge.get_target().get_id()) edge_el.set("label", edge.get_label()) edge_el.set("events-count", str(edge.get_events_count())) edge_el.set("time", str(edge.get_time())) edge_el.set("pids", str(edge.get_pids())) return edge_el for n in graph.nodes.values(): n_el = save_node(n) for e in n.get_edges(): save_edge(e, n_el) with open(self.filename, "w") as f: f.write(utils.get_pretty_xml(root)) f.flush()
def add_edges(self, G, graph_element): def edge_key_data(G): # helper function to unify multigraph and graph edge iterator if G.is_multigraph(): for u, v, key, data in G.edges_iter(data=True, keys=True): edge_data = data.copy() edge_data.update(key=key) edge_id = edge_data.pop("id", None) if edge_id is None: edge_id = next(self.edge_id) yield u, v, edge_id, edge_data else: for u, v, data in G.edges_iter(data=True): edge_data = data.copy() edge_id = edge_data.pop("id", None) if edge_id is None: edge_id = next(self.edge_id) yield u, v, edge_id, edge_data edges_element = Element("edges") for u, v, key, edge_data in edge_key_data(G): kw = {"id": make_str(key)} edge_weight = edge_data.pop("weight", False) if edge_weight: kw["weight"] = make_str(edge_weight) edge_type = edge_data.pop("type", False) if edge_type: kw["type"] = make_str(edge_type) edge_element = Element("edge", source=make_str(u), target=make_str(v), **kw) default = G.graph.get("edge_default", {}) edge_data = self.add_viz(edge_element, edge_data) edge_data = self.add_attributes("edge", edge_element, edge_data, default) edges_element.append(edge_element) graph_element.append(edges_element)
def add_nodes(self, G, graph_element): nodes_element = Element('nodes') for node, data in G.nodes(data=True): node_data = data.copy() node_id = make_str(node_data.pop('id', node)) kw = {'id': node_id} label = make_str(node_data.pop('label', node)) kw['label'] = label try: pid = node_data.pop('pid') kw['pid'] = make_str(pid) except KeyError: pass # add node element with attributes node_element = Element('node', **kw) # add node element and attr subelements default = G.graph.get('node_default', {}) node_data = self.add_parents(node_element, node_data) if self.version == '1.1': node_data = self.add_slices(node_element, node_data) else: node_data = self.add_spells(node_element, node_data) node_data = self.add_viz(node_element, node_data) node_data = self.add_attributes('node', node_element, node_data, default) nodes_element.append(node_element) graph_element.append(nodes_element)
def write_xml(self, xml_doc=None, option=None, element_name=None): """ Method writes the XML representation of the external method object. """ if xml_doc is None: xml_obj = Element(self.prop_mo_meta.xml_attribute) else: if element_name == None: xml_obj = SubElement(xml_doc, self.prop_mo_meta.xml_attribute) else: xml_obj = SubElement(xml_doc, element_name) for prop in CoreUtils.get_property_list(self._class_id): prop_meta = CoreUtils.get_method_property_meta(self._class_id, prop) if prop_meta.io == "Output": continue if prop_meta.is_complex_type: if self.get_attr(prop) != None: self.__dict__[prop].write_xml(xml_obj, option, ImcUtils.word_l(prop)) #xml_obj.append(self.__dict__[prop].write_xml(xml_obj, option, ImcUtils.word_l(prop))) #print ET.tostring(xml_obj) elif self.get_attr(prop) != None: xml_obj.set(prop_meta.xml_attribute, self.get_attr(prop)) self.child_write_xml(xml_obj, option) # x_child = self.child_write_xml(xml_obj, option) # for xchild in x_child: # if xchild != None: # xml_obj.append(xchild) return xml_obj
def generate_sitemap(links): urlset = Element(nstag("urlset")) # , **nsmap_keyargs) # lxml urlset.text = "\n" for link in links: if isinstance(link, basestring): loc = link lastmod = changefreq = priority = None else: loc, lastmod, changefreq, priority = (tuple(link) + (None, None, None))[:4] url = SubElement(urlset, nstag("url")) url.text = url.tail = "\n" SubElement(url, nstag("loc")).text = loc if lastmod: if not isinstance(lastmod, basestring): if getattr(lastmod, "tzinfo", None) is not None: lastmod = lastmod.replace(tzinfo=None) - lastmod.utcoffset() lastmod = lastmod.strftime("%Y-%m-%dT%H:%M:%SZ") SubElement(url, nstag("lastmod")).text = lastmod if changefreq: SubElement(url, nstag("changefreq")).text = changefreq if priority is not None: SubElement(url, nstag("priority")).text = str(priority) for child in url: child.tail = "\n" return urlset
def add_nodes(self, G, graph_element): nodes_element = Element('nodes') for node,data in G.nodes_iter(data=True): node_data=data.copy() # node_id=node_data.pop('id',make_str(node)) kw={'id':node_id} label=node_data.pop('label',make_str(node)) kw['label']=label pid=node_data.pop('pid',False) if pid: kw['pid']=pid if 'start' in node_data: kw['start']=make_str(node_data.pop('start')) if 'end' in node_data: kw['end']=make_str(node_data.pop('end')) # add node element with attributes node_element = Element("node", **kw) # add node element and attr subelements default=G.graph.get('node_default',{}) node_data=self.add_parents(node_element, node_data) if self.version=='1.1': node_data=self.add_slices(node_element, node_data) else: node_data=self.add_spells(node_element, node_data) node_data=self.add_viz(node_element,node_data) node_data=self.add_attributes("node", node_element, node_data, default) nodes_element.append(node_element) graph_element.append(nodes_element)
def list_to_xml(l, item): el = Element("data") for val in l: child = Element(item) child.text = str(val) el.append(child) print(tostring(el))
def main(): options, args = parse_args() rootElement = Element('packages') packages = {} print "Searching for packages.config files:" for dirpath, subdirs, filenames in walk('src'): for filename in filenames: if filename == 'packages.config': filepath = join(dirpath, filename) print " " + filepath et = parse(filepath) for packageElement in et.findall('package'): pkgId = packageElement.get('id') pkgVersion = packageElement.get('version') packages[pkgId, pkgVersion] = packageElement print print "Writing projectdata/packages.config:" rootElement.extend([value for (key,value) in sorted(packages.items())]) indent(rootElement) tree = ElementTree(rootElement) dump(tree) tree.write('projectdata/packages.config')
def add_nodes(self, G, graph_element): nodes_element = Element("nodes") for node, data in G.nodes(data=True): node_data = data.copy() node_id = make_str(node_data.pop("id", node)) kw = {"id": node_id} label = make_str(node_data.pop("label", node)) kw["label"] = label try: pid = node_data.pop("pid") kw["pid"] = make_str(pid) except KeyError: pass # add node element with attributes node_element = Element("node", **kw) # add node element and attr subelements default = G.graph.get("node_default", {}) node_data = self.add_parents(node_element, node_data) if self.version == "1.1": node_data = self.add_slices(node_element, node_data) else: node_data = self.add_spells(node_element, node_data) node_data = self.add_viz(node_element, node_data) node_data = self.add_attributes("node", node_element, node_data, default) nodes_element.append(node_element) graph_element.append(nodes_element)
def start(self, tag, attrib=None): if tag == "_ignore_": return self._last elif tag in ("h", "p", "pre"): self._flush(need_eol=2) else: self._flush() # ~ print 'START', tag if tag == "h": if not (attrib and "level" in attrib): logger.warn('Missing "level" attribute for heading') attrib = attrib or {} attrib["level"] = 1 elif tag == "link": if not (attrib and "href" in attrib): logger.warn('Missing "href" attribute for link') attrib = attrib or {} attrib["href"] = "404" # TODO check other mandatory properties ! if attrib: self._last = Element(tag, attrib) else: self._last = Element(tag) if self._stack: self._stack[-1].append(self._last) else: assert tag == "zim-tree", 'root element needs to be "zim-tree"' self._stack.append(self._last) self._tail = False return self._last
def add_edges(self, G, graph_element): def edge_key_data(G): # helper function to unify multigraph and graph edge iterator if G.is_multigraph(): for u,v,key,data in G.edges_iter(data=True,keys=True): edge_data=data.copy() edge_data.update(key=key) edge_id=edge_data.pop('id',None) if edge_id is None: edge_id=next(self.edge_id) yield u,v,edge_id,edge_data else: for u,v,data in G.edges_iter(data=True): edge_data=data.copy() edge_id=edge_data.pop('id',None) if edge_id is None: edge_id=next(self.edge_id) yield u,v,edge_id,edge_data edges_element = Element('edges') for u,v,key,edge_data in edge_key_data(G): kw={'id':make_str(key)} try: edge_weight=edge_data.pop('weight') kw['weight']=make_str(edge_weight) except KeyError: pass try: edge_type=edge_data.pop('type') kw['type']=make_str(edge_type) except KeyError: pass try: start=edge_data.pop('start') kw['start']=make_str(start) self.alter_graph_mode_timeformat(start) except KeyError: pass try: end=edge_data.pop('end') kw['end']=make_str(end) self.alter_graph_mode_timeformat(end) except KeyError: pass source_id = make_str(G.node[u].get('id', u)) target_id = make_str(G.node[v].get('id', v)) edge_element = Element("edge", source=source_id,target=target_id, **kw) default=G.graph.get('edge_default',{}) if self.version == '1.1': edge_data=self.add_slices(edge_element, edge_data) else: edge_data=self.add_spells(edge_element, edge_data) edge_data=self.add_viz(edge_element,edge_data) edge_data=self.add_attributes("edge", edge_element, edge_data, default) edges_element.append(edge_element) graph_element.append(edges_element)
def serialize(self, **kwargs): """Return an elementtree representing this object""" variables = Element("variables") for variable in self.variables.values(): # Do not serialize internal variables if not variable.name.startswith("@"): variables.append(variable.serialize(**kwargs)) return variables
def build_slices_node(self): #Building node slices_node = Element(self.SLICES) #Building child nodes for data_slice in self._dataset.slices: slices_node.append(self.build_slice_node(data_slice)) #Appending node to root self._root.append(slices_node)
def descxml(self, done): if self in done: e = Element('ref', id=str(done[self])) else: done[self] = len(done) e = Element('iter', id=str(done[self])) e.append(self.elemall.descxml(done)) return e
def serialize(data, name='object'): content_elem = Element('contenttype') content_elem.attrib['name'] = name _serialize(content_elem, data) tree = ElementTree(content_elem) f = StringIO() tree.write(f, 'UTF-8') return f.getvalue()
def _get_metadata_node(location, data_fields): node = Element('location_data') # add default empty nodes for all known fields: http://manage.dimagi.com/default.asp?247786 for field in data_fields: element = Element(field.slug) element.text = six.text_type(location.metadata.get(field.slug, '')) node.append(element) return node
def _get_autodiscover_payload(email, encoding='utf-8'): # Builds a full Autodiscover XML request payload = Element('Autodiscover') payload.set('xmlns', REQUEST_NS) request = Element('Request') set_xml_attr(request, 'EMailAddress', email) set_xml_attr(request, 'AcceptableResponseSchema', RESPONSE_NS) payload.append(request) return ('<?xml version="1.0" encoding="%s"?>' % encoding).encode(encoding) + tostring(payload, encoding=encoding)
def add_spells(self,node_element,node_data): spells=node_data.pop('spells',False) if spells: spells_element=Element('spells') for start,end in spells: e=Element('spell',start=str(start),end=str(end)) spells_element.append(e) node_element.append(spells_element) return node_data
def add_slices(self, node_element, node_data): slices = node_data.pop("slices", False) if slices: slices_element = Element("slices") for start, end in slices: e = Element("slice", start=str(start), end=str(end)) slices_element.append(e) node_element.append(slices_element) return node_data
def create_node(tag, property_map, content): '''''新造一个节点 tag:节点标签 property_map:属性及属性值map content: 节点闭合标签里的文本内容 return 新节点''' element = Element(tag, property_map) element.text = content return element
def add_slices(self, node_or_edge_element, node_or_edge_data): slices = node_or_edge_data.pop('slices', False) if slices: slices_element = Element('slices') for start, end in slices: e = Element('slice', start=str(start), end=str(end)) slices_element.append(e) node_or_edge_element.append(slices_element) return node_or_edge_data
def add_graph(self, G): # Add a graph element to the XML if G.is_directed(): default = 'directed' else: default = 'undirected' graph_element = Element("graph", defaultedgetype=default, mode=self.mode) self.graph_element = graph_element self.add_nodes(G, graph_element) self.add_edges(G, graph_element) self.xml.append(graph_element)
def add_viz(self, element, node_data): viz = node_data.pop('viz', False) if viz: color = viz.get('color') if color is not None: if self.VERSION == '1.1': e = Element( "{%s}color" % self.NS_VIZ, r=str(color.get('r')), g=str(color.get('g')), b=str(color.get('b')), ) else: e = Element( "{%s}color" % self.NS_VIZ, r=str(color.get('r')), g=str(color.get('g')), b=str(color.get('b')), a=str(color.get('a')), ) element.append(e) size = viz.get('size') if size is not None: e = Element("{%s}size" % self.NS_VIZ, value=str(size)) element.append(e) thickness = viz.get('thickness') if thickness is not None: e = Element("{%s}thickness" % self.NS_VIZ, value=str(thickness)) element.append(e) shape = viz.get('shape') if shape is not None: if shape.startswith('http'): e = Element("{%s}shape" % self.NS_VIZ, value='image', uri=str(shape)) else: e = Element("{%s}shape" % self.NS_VIZ, value=str(shape.get)) element.append(e) position = viz.get('position') if position is not None: e = Element( "{%s}position" % self.NS_VIZ, x=str(position.get('x')), y=str(position.get('y')), z=str(position.get('z')), ) element.append(e) return node_data
def add_nodes(self, G, graph_element): nodes_element = Element('nodes') for node, data in G.nodes(data=True): node_data = data.copy() node_id = make_str(node_data.pop('id', node)) kw = {'id': node_id} label = make_str(node_data.pop('label', node)) kw['label'] = label try: pid = node_data.pop('pid') kw['pid'] = make_str(pid) except KeyError: pass try: start = node_data.pop('start') kw['start'] = make_str(start) self.alter_graph_mode_timeformat(start) except KeyError: pass try: end = node_data.pop('end') kw['end'] = make_str(end) self.alter_graph_mode_timeformat(end) except KeyError: pass # add node element with attributes node_element = Element('node', **kw) # add node element and attr subelements default = G.graph.get('node_default', {}) node_data = self.add_parents(node_element, node_data) if self.version == '1.1': node_data = self.add_slices(node_element, node_data) else: node_data = self.add_spells(node_element, node_data) node_data = self.add_viz(node_element, node_data) node_data = self.add_attributes('node', node_element, node_data, default) nodes_element.append(node_element) graph_element.append(nodes_element)
def _write_kodi_nfo(information, path): """Write the provided information to movie.nfo.""" click.echo("Writing movie.nfo...") root = Element("movie") SubElement(root, "title").text = information.get("title") SubElement(root, "originaltitle").text = information.get("title") SubElement(root, "sorttitle").text = information.get("title") SubElement(root, "set").text = information.get("set") SubElement(root, "year").text = information.get("release_date")[:4] SubElement(root, "plot").text = information.get("plot") SubElement(root, "studio").text = information.get("studio") tree = ElementTree(root) tree.write(os.path.join(path, "movie.nfo"), encoding="UTF-8")
def add_spells(self, node_or_edge_element, node_or_edge_data): spells = node_or_edge_data.pop('spells', False) if spells: spells_element = Element('spells') for start, end in spells: e = Element('spell') if start is not None: e.attrib['start'] = make_str(start) self.alter_graph_mode_timeformat(start) if end is not None: e.attrib['end'] = make_str(end) self.alter_graph_mode_timeformat(end) spells_element.append(e) node_or_edge_element.append(spells_element) return node_or_edge_data
def fileReadXML(filename, default=None, source=DEFAULT_MODULE_NAME, debug=False): dom = None try: with open( filename, "r" ) as fd: # This open gets around a possible file handle leak in Python's XML parser. try: dom = parse(fd).getroot() msg = "Read" except ParseError as err: fd.seek(0) content = fd.readlines() line, column = err.position print("[%s] XML Parse Error: '%s' in '%s'!" % (source, err, filename)) data = content[line - 1].replace("\t", " ").rstrip() print("[%s] XML Parse Error: '%s'" % (source, data)) print("[%s] XML Parse Error: '%s^%s'" % (source, "-" * column, " " * (len(data) - column - 1))) except Exception as err: print("[%s] Error: Unable to parse data in '%s' - '%s'!" % (source, filename, err)) except (IOError, OSError) as err: if err.errno == ENOENT: # ENOENT - No such file or directory. print("[%s] Warning: File '%s' does not exist!" % (source, filename)) else: print("[%s] Error %d: Opening file '%s'! (%s)" % (source, err.errno, filename, err.strerror)) except Exception as err: print("[%s] Error: Unexpected error opening/parsing file '%s'! (%s)" % (source, filename, err)) print_exc() if dom is None: if default and isinstance(default, str): dom = fromstring(default) msg = "Default (XML)" elif default and isinstance( default, type(Element(None)) ): # This handles a bug in Python 2 where the Element object is *not* a class type in cElementTree!!! dom = default msg = "Default (DOM)" else: msg = "Failed to read" if debug or forceDebug: print("[%s] Line %d: %s from XML file '%s'." % (source, stack()[1][0].f_lineno, msg, filename)) return dom
def descxml(self, done): if self in done: e = Element('ref', id=str(done[self])) else: done[self] = len(done) e = Element('iter', id=str(done[self])) e.append(self.elemall.descxml(done)) return e
def add_edges(self, G, graph_element): def edge_key_data(G): # helper function to unify multigraph and graph edge iterator if G.is_multigraph(): for u, v, key, data in G.edges_iter(data=True, keys=True): edge_data = data.copy() edge_data.update(key=key) edge_id = edge_data.pop('id', None) if edge_id is None: edge_id = self.edge_id.next() yield u, v, edge_id, edge_data else: for u, v, data in G.edges_iter(data=True): edge_data = data.copy() edge_id = edge_data.pop('id', None) if edge_id is None: edge_id = next(self.edge_id) yield u, v, edge_id, edge_data edges_element = Element('edges') for u, v, key, edge_data in edge_key_data(G): kw = {'id': make_str(key)} edge_weight = edge_data.pop('weight', False) if edge_weight: kw['weight'] = make_str(edge_weight) edge_type = edge_data.pop('type', False) if edge_type: kw['type'] = make_str(edge_type) edge_element = Element("edge", source=make_str(u), target=make_str(v), **kw) default = G.graph.get('edge_default', {}) edge_data = self.add_viz(edge_element, edge_data) edge_data = self.add_attributes("edge", edge_element, edge_data, default) edges_element.append(edge_element) graph_element.append(edges_element)
def __init__(self, encoding="utf-8", date=None, source_info_url=None, source_info_name=None, generator_info_url=None, generator_info_name=None): """ @param encoding: The text encoding that will be used. Defaults to 'utf-8' @param date: The date this data was generated. Optional @param source_info_url: A URL for information about the source of the data. Optional @param source_info_name: A human readable description of source_info_url. Optional @param generator_info_url: A URL for information about the program that is generating the XMLTV document. Optional @param generator_info_name: A human readable description of generator_info_url. Optional """ _debug_('in Writer:__init__()', 2) self.encoding = encoding self.data = { 'date': date, 'source_info_url': source_info_url, 'source_info_name': source_info_name, 'generator_info_url': generator_info_url, 'generator_info_name': generator_info_name } self.root = Element('tv') for attr in self.data.keys(): if self.data[attr]: self.root.set(attr, self.data[attr])
def serialize(self, **kwargs): """Return an elementtree representing this object""" # We have sprite objects and watcher nodes; make a tree of nodes sprites = Element("sprites") for item in self.sprites: if isinstance(item, Sprite): sprites.append(item.serialize(**kwargs)) else: # it is a 'watcher' node sprites.append(item) variables_node = self.variables.serialize(**kwargs) scripts_node = self.serialize_scripts(**kwargs) blocks_node = get_blocks_node(self.blocks, **kwargs) if self.costumes: costumes_node = self.costumes.serialize(**kwargs) else: costumes_node = None stage = Element("stage", name=self.name, width=data.number_to_string(self.width), height=data.number_to_string(self.height), costume=data.number_to_string(self.costume), tempo=data.number_to_string(self.tempo), threadsafe=data.bool_to_string(self.threadsafe), lines=self.lines, codify=data.bool_to_string(self.codify), scheduled=data.bool_to_string(self.scheduled), id=data.number_to_string(self.id)) for child in (self.pentrails, costumes_node, self.sounds, variables_node, blocks_node, scripts_node, sprites): if child is not None: stage.append(child) return stage
def _get_fixture_node(self, fixture_id, restore_user, locations_queryset, location_type_attrs, data_fields): root_node = Element('fixture', {'id': fixture_id, 'user_id': restore_user.user_id, 'indexed': 'true'}) outer_node = Element('locations') root_node.append(outer_node) all_locations = list(locations_queryset.order_by('site_code')) locations_by_id = {location.pk: location for location in all_locations} for location in all_locations: attrs = { 'type': location.location_type.code, 'id': location.location_id, } attrs.update({attr: '' for attr in location_type_attrs}) attrs['{}_id'.format(location.location_type.code)] = location.location_id current_location = location while current_location.parent_id: try: current_location = locations_by_id[current_location.parent_id] except KeyError: current_location = current_location.parent # For some reason this wasn't included in the locations we already fetched from corehq.util.soft_assert import soft_assert _soft_assert = soft_assert('{}@{}.com'.format('frener', 'dimagi')) message = ( "The flat location fixture didn't prefetch all parent " "locations: {domain}: {location_id}" ).format( domain=current_location.domain, location_id=current_location.location_id, ) _soft_assert(False, msg=message) attrs['{}_id'.format(current_location.location_type.code)] = current_location.location_id location_node = Element('location', attrs) _fill_in_location_element(location_node, location, data_fields) outer_node.append(location_node) return root_node
def __init__(self, graph=None, encoding="utf-8", mode='static', prettyprint=True, version='1.1draft'): try: import xml.etree.ElementTree except ImportError: raise ImportError('GEXF writer requires ' 'xml.elementtree.ElementTree') self.prettyprint = prettyprint self.mode = mode self.encoding = encoding self.set_version(version) self.xml = Element( "gexf", { 'xmlns': self.NS_GEXF, 'xmlns:xsi': self.NS_XSI, 'xmlns:viz': self.NS_VIZ, 'xsi:schemaLocation': self.SCHEMALOCATION, 'version': self.VERSION }) # counters for edge and attribute identifiers self.edge_id = itertools.count() self.attr_id = itertools.count() # default attributes are stored in dictionaries self.attr = {} self.attr['node'] = {} self.attr['edge'] = {} self.attr['node']['dynamic'] = {} self.attr['node']['static'] = {} self.attr['edge']['dynamic'] = {} self.attr['edge']['static'] = {} if graph is not None: self.add_graph(graph)
def to_xml(self): """Convert this :class:`MeasurandRecord` to an XML :class:`~xml.etree.ElementTree.Element`. Returns ------- :class:`~xml.etree.ElementTree.Element` The :class:`MeasurandRecord` as a XML :class:`~xml.etree.ElementTree.Element`. """ root = Element('MeasurandRecord') for name in ('calibration', 'conditions'): root.append(getattr(self, name).to_xml(tag=name)) for name in ('type', 'unit'): element = Element(name) element.text = getattr(self, name) root.append(element) return root
def _serialize_dict(parent_elem, data_dict): if data_dict.has_key("displayAs"): parent_elem.tag = "field" parent_elem.attrib["name"] = data_dict.get("name") parent_elem.attrib["displayAs"] = data_dict.get("displayAs") data = data_dict.get("value") if not isinstance(data, unicode): data = unicode(data) parent_elem.text = data return for k, v in data_dict.iteritems(): key_elem = Element(k) parent_elem.append(key_elem) _serialize(key_elem, v)
def WriteDialogToFile(filename, props): """Write the props to the file props can be either a dialog of a dictionary """ # if we are passed in a wrapped handle then # get the properties try: props[0].keys() except (TypeError, AttributeError): props = controls.GetDialogPropsFromHandle(props) # build a tree structure root = Element("DIALOG") root.set("_version_", "2.0") for ctrl in props: ctrlelem = SubElement(root, "CONTROL") for name, value in sorted(ctrl.items()): _SetNodeProps(ctrlelem, name, value) # wrap it in an ElementTree instance, and save as XML tree = ElementTree(root) tree.write(filename, encoding="utf-8")
def test_writer(self): a = Element('a') b = SubElement(a, 'b') b.append(Comment('a comment')) c = SubElement(b, 'c', d = 'e') f = SubElement(c, 'f') f.text = 'g' h = SubElement(c, 'h') h.text = 'i << >> << &&' b.append(ProcessingInstruction('pdk', 'processing instruction')) tree = ElementTree(a) output = stringio() write_pretty_xml(tree, output) self.assert_equals_long(expected_xml_output, output.getvalue())
def atom_author(author): if isinstance(author, basestring): author = [author, None, None] else: try: iter(author) except TypeError: raise TypeError('Inappropriate author value: %r' % author) author = (list(author) + [None, None])[:3] name, uri, email = author result = Element(atomtag('author')) if name: SubElement(result, atomtag('name')).text = name if uri: SubElement(result, atomtag('uri')).text = uri if email: SubElement(result, atomtag('email')).text = email return result
def write(self, data, filename): """Writes data to the xml file. Data is a Person instance.""" person_el = Element('person', format='0.7') height_el = SubElement(person_el, 'height') height_el.text = str(data.height) weight_el = SubElement(person_el, 'weight') measurements_el = SubElement(weight_el, 'measurements') plan_el = SubElement(weight_el, 'plan') for dataset in data.measurements: _add_dataset_to_element(dataset, measurements_el) for dataset in data.plan: _add_dataset_to_element(dataset, plan_el) user_tree = ElementTree(person_el) user_tree.write(filename, encoding='UTF-8')
def container_to_xml(listing, base_name): doc = Element('container', name=base_name) for record in listing: if 'subdir' in record: name = record.pop('subdir') sub = SubElement(doc, 'subdir', name=name) SubElement(sub, 'name').text = name else: sub = SubElement(doc, 'object') for field in ('name', 'hash', 'bytes', 'content_type', 'last_modified'): SubElement(sub, field).text = six.text_type( record.pop(field)) return to_xml(doc)
def __init__(self, label): self.data = Element("data") linkElement = SubElement(self.data, "mtg:MaltegoLink") linkElement.attrib = { 'xmlns:mtg': 'http://maltego.paterva.com/xml/mtgx', 'type': 'maltego.link.manual-link' } propertiesElement = SubElement(linkElement, "mtg:Properties") self.add_entity_property(propertiesElement, {'name': 'maltego.link.manual.type'}, label)
def generate_sitemap(links): urlset = Element(nstag('urlset')) #, **nsmap_keyargs) # lxml urlset.text = '\n' for link in links: if isinstance(link, basestring): loc = link lastmod = changefreq = priority = None else: loc, lastmod, changefreq, priority = (tuple(link) + (None, None, None))[:4] url = SubElement(urlset, nstag('url')) url.text = url.tail = '\n' SubElement(url, nstag('loc')).text = loc if lastmod: if not isinstance(lastmod, basestring): if getattr(lastmod, 'tzinfo', None) is not None: lastmod = lastmod.replace(tzinfo=None) - lastmod.utcoffset() lastmod = lastmod.strftime('%Y-%m-%dT%H:%M:%SZ') SubElement(url, nstag('lastmod')).text = lastmod if changefreq: SubElement(url, nstag('changefreq')).text = changefreq if priority is not None: SubElement(url, nstag('priority')).text = str(priority) for child in url: child.tail = '\n' return urlset
def ugcs(self, ugcs, atts, polygon=None): root = Element('ihis') ele = SubElement(root, "ugcs") self._addAttributes(ele, atts) if ugcs is not None: for ugc in ugcs: ugcE = SubElement(ele, "ugc_code") ugcE.text = ugc # if polygon provided, output it, we only have 1 polygon, and we # assume it is inclusive. if polygon is not None: self._encodePolygons(root, [(True, polygon)]) return ElementTree.tostring(root)
def add_data(self, name, element_type, value, scope='all', default=None): """ Reimplemented to handle JSON data. """ # JSON clean first to handle conversion of numpy arrays, tuples, etc. if isinstance(value, bytes): value = value.decode('ascii') value = json_clean(value) # Convert Python data type to GraphML data type. element_type = type(value) if element_type not in self.xml_type: raise nx.NetworkXError('GraphML writer does not support ' '%s as data values.' % element_type) xml_type = self.xml_type[element_type] key_id = self.get_key(name, xml_type, scope, default) data_element = Element('data', key=key_id) if xml_type == 'json': data_element.text = json.dumps(value) else: # From base class: cast to string. data_element.text = make_str(value) return data_element
def add_graph(self, graph, parent_node=None): """ Create a graph element (top-level or nested). """ # XXX: Mostly copied from base class `add_graph_element()`. default_edge_type = 'directed' if graph.is_directed() else 'undirected' graph_element = Element('graph', edgedefault=default_edge_type) default = {} data = { k:v for k,v in graph.graph.items() if k not in ['node_default','edge_default','port_default'] } self.add_attributes('graph', graph_element, data, default) self.add_nodes(graph, graph_element, parent_node) self.add_edges(graph, graph_element, parent_node) return graph_element
def to_xml(self, xml_doc=None): """ This method returns the xml element node for the current object with it's hierarchy. Args: xml_doc: document to which the Mo attributes are added. Can be None. option: not required for Generic Mo class object Example: from ucsmsdk.ucsmo import GenericMo\n args = {"a": 1, "b": 2, "c":3}\n obj = GenericMo("testLsA", "org-root", **args)\n obj1 = GenericMo("testLsB", "org-root", **args)\n obj.add_child(obj1)\n elem = obj.write_xml()\n import ucsmsdk.ucsxmlcodec as xc\n xc.to_xml_str(elem)\n Output: '<testLsA a="1" b="2" c="3" dn="org-root/" rn="">\n <testLsB a="1" b="2" c="3" dn="org-root/" rn="" />\n </testLsA>' """ if xml_doc is None: xml_obj = Element(ucscentralgenutils.word_l(self._class_id)) else: xml_obj = SubElement(xml_doc, ucscentralgenutils.word_l(self._class_id)) for key in self.__dict__: if not key.startswith('_'): xml_obj.set(key, getattr(self, key)) self.child_to_xml(xml_obj) return xml_obj
def single_tei_entry(x, to_lang): # entry entry = Element('entry') form = SubElement(entry, 'form') orth = SubElement(form, 'orth') if x['pronuns']: for p in x['pronuns']: pron = SubElement(form, 'pron') pron.text = p is_suffix = ( x['part_of_speech'] == 'suffix' or (x['part_of_speech'] in ('', None) and x['written_rep'].startswith('-')) ) if is_suffix: #assert x['written_rep'].startswith('-') orth.text = x['written_rep'][1:] pos_text = 'suffix' else: orth.text = x['written_rep'] pos_text = pos_mapping.get(x['part_of_speech'], (x['part_of_speech'], None))[0] # gramGrp gram_grp = Element('gramGrp') if pos_text: pos = SubElement(gram_grp, 'pos') pos.text = pos_text if x['gender']: gen = SubElement(gram_grp, 'gen') gen.text = gender_mapping[x['gender']] if list(gram_grp): entry.append(gram_grp) add_senses(entry, x, to_lang, is_suffix) return entry
def saveDictToXml(dict, filepath): dictEle = Element("dict") dictEle.set("description", "Chinese Character Dictionary") dictEle.set("count", str(len(dict))) dictEle.set("author", "yeqiang") cur_time = str(datetime.datetime.now()) dictEle.set("createtime", cur_time) for key in dict: cur_n = dict[key] print(cur_n) nodeEle = SubElement(dictEle, "node") nodeEle.set("key", cur_n.content) #nodeEle.set("content", cur_n.content) prePathsEle = SubElement(nodeEle, "pre_paths") prePathsEle.set("count", str(len(cur_n.prepaths))) for pathkey in cur_n.prepaths: prePathEle = SubElement(prePathsEle, "path") path = cur_n.prepaths[pathkey] #prePathEle.set("key",str(path.pid)) prePathEle.set("touched", str(path.touchtimes)) prePathEle.set("node", path.pre_n.content) #prePathEle.text = path.pid postPathsEle = SubElement(nodeEle, "post_paths") postPathsEle.set("count", str(len(cur_n.postpaths))) for pathkey in cur_n.postpaths: postPathEle = SubElement(postPathsEle, "path") path = cur_n.postpaths[pathkey] #postPathEle.set("key", str(path.pid)) postPathEle.set("touched", str(path.touchtimes)) postPathEle.set("node", path.post_n.content) tree = ElementTree(dictEle) tree.write(filepath, encoding="utf-8", xml_declaration="version = 1.0")
def descxml(self, done): if self in done: e = Element('ref', id=str(done[self])) else: done[self] = len(done) e = Element('compound', id=str(done[self])) for obj in self: e.append(obj.descxml(done)) return e
def create_listing(self, req, out_content_type, resp_headers, result, container, **kwargs): container_list = result['objects'] for p in result.get('prefixes', []): record = {'name': p, 'subdir': True} container_list.append(record) container_list.sort(key=lambda x: x['name']) ret = Response(request=req, headers=resp_headers, content_type=out_content_type, charset='utf-8') versions = kwargs.get('versions', False) slo = kwargs.get('slo', False) if out_content_type == 'application/json': ret.body = json.dumps([ self.update_data_record(r, versions, slo) for r in container_list ]).encode('utf-8') req.environ['swift.format_listing'] = False elif out_content_type.endswith('/xml'): doc = Element('container', name=container.decode('utf-8')) for obj in container_list: record = self.update_data_record(obj, versions, slo) if 'subdir' in record: name = record['subdir'].decode('utf-8') sub = SubElement(doc, 'subdir', name=name) SubElement(sub, 'name').text = name else: obj_element = SubElement(doc, 'object') for field in [ "name", "hash", "bytes", "content_type", "last_modified" ]: SubElement(obj_element, field).text = str( record.pop(field)).decode('utf-8') for field in sorted(record): SubElement(obj_element, field).text = str( record[field]).decode('utf-8') ret.body = tostring(doc, encoding='UTF-8').replace( "<?xml version='1.0' encoding='UTF-8'?>", '<?xml version="1.0" encoding="UTF-8"?>', 1) req.environ['swift.format_listing'] = False else: if not container_list: return HTTPNoContent(request=req, headers=resp_headers) ret.body = ('\n'.join(rec['name'] for rec in container_list) + '\n').encode('utf-8') return ret
def create_listing(self, req, out_content_type, info, metadata, container_list, container): list_meta = get_param(req, 'list_meta', 'f').lower() in TRUE_VALUES resp_headers = { 'X-Container-Object-Count': info['object_count'], 'X-Container-Bytes-Used': info['bytes_used'], 'X-Timestamp': info['created_at'], 'X-PUT-Timestamp': info['put_timestamp'], } for key, (value, timestamp) in metadata.iteritems(): if value and (key.lower() in self.save_headers or is_sys_or_user_meta('container', key)): resp_headers[key] = value ret = Response(request=req, headers=resp_headers, content_type=out_content_type, charset='utf-8') if out_content_type == 'application/json': ret.body = json.dumps([self.update_data_record(record, list_meta) for record in container_list]) elif out_content_type.endswith('/xml'): doc = Element('container', name=container.decode('utf-8')) for obj in container_list: record = self.update_data_record(obj, list_meta) if 'subdir' in record: name = record['subdir'].decode('utf-8') sub = SubElement(doc, 'subdir', name=name) SubElement(sub, 'name').text = name else: obj_element = SubElement(doc, 'object') for field in ["name", "hash", "bytes", "content_type", "last_modified"]: SubElement(obj_element, field).text = str( record.pop(field)).decode('utf-8') for field in sorted(record): if list_meta and field == 'metadata': meta = SubElement(obj_element, field) for k, v in record[field].iteritems(): SubElement(meta, k).text = str( v.decode('utf-8')) else: SubElement(obj_element, field).text = str( record[field]).decode('utf-8') ret.body = tostring(doc, encoding='UTF-8').replace( "<?xml version='1.0' encoding='UTF-8'?>", '<?xml version="1.0" encoding="UTF-8"?>', 1) else: if not container_list: return HTTPNoContent(request=req, headers=resp_headers) ret.body = '\n'.join(rec[0] for rec in container_list) + '\n' return ret