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 get_attr_id(self, title, attr_type, edge_or_node, default, mode): # find the id of the attribute or generate a new id try: return self.attr[edge_or_node][mode][title] except KeyError: # generate new id new_id = str(next(self.attr_id)) self.attr[edge_or_node][mode][title] = new_id attr_kwargs = {'id': new_id, 'title': title, 'type': attr_type} attribute = Element('attribute', **attr_kwargs) # add subelement for data default value if present default_title = default.get(title) if default_title is not None: default_element = Element('default') default_element.text = make_str(default_title) attribute.append(default_element) # new insert it into the XML attributes_element = None for a in self.graph_element.findall('attributes'): # find existing attributes element by class and mode a_class = a.get('class') a_mode = a.get('mode', 'static') # default mode is static if a_class == edge_or_node and a_mode == mode: attributes_element = a if attributes_element is None: # create new attributes element attr_kwargs = {'mode': mode, 'class': edge_or_node} attributes_element = Element('attributes', **attr_kwargs) self.graph_element.insert(0, attributes_element) attributes_element.append(attribute) return new_id
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 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(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 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 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 add_attributes(self, node_or_edge, xml_obj, data, default): # Add attrvalues to node or edge attvalues = Element("attvalues") if len(data) == 0: return data if "start" in data or "end" in data: mode = "dynamic" else: mode = "static" for k, v in list(data.items()): # rename generic multigraph key to avoid any name conflict if k == "key": k = "networkx_key" attr_id = self.get_attr_id(make_str(k), self.xml_type[type(v)], node_or_edge, default, mode) if type(v) == list: # dynamic data for val, start, end in v: e = Element("attvalue") e.attrib["for"] = attr_id e.attrib["value"] = make_str(val) e.attrib["start"] = make_str(start) e.attrib["end"] = make_str(end) attvalues.append(e) else: # static data e = Element("attvalue") e.attrib["for"] = attr_id e.attrib["value"] = make_str(v) attvalues.append(e) xml_obj.append(attvalues) return data
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 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 build_time_node(self, ref_time): #Building top node time_node = Element(self.TIME) #Managging different time instances if type(ref_time) is Instant: time_node.attrib[self.TIME_ATT_UNIT] = "instant" time_node.text = ref_time.get_time_string() elif type(ref_time) is YearInterval: time_node.attrib[self.TIME_ATT_UNIT] = "years" time_node.text = ref_time.get_time_string() elif type(ref_time) is MonthInterval: time_node.attrib[self.TIME_ATT_UNIT] = "months" time_node.text = ref_time.get_time_string() elif type(ref_time) is Interval: if ref_time.frequency == Interval.YEARLY: time_node.attrib[self.TIME_ATT_UNIT] = "years" else: time_node.attrib[self.TIME_ATT_UNIT] = "months" interval_node = Element(self.TIME_INTERVAL) beg_node = Element(self.TIME_INTERVAL_BEG) beg_node.text = str(ref_time.start_time) end_node = Element(self.TIME_INTERVAL_END) end_node.text = str(ref_time.end_time) interval_node.append(beg_node) interval_node.append(end_node) time_node.append(interval_node) else: raise RuntimeError("Unrecognized time type. Impossible to build node") #Returning final node return time_node
def add_attributes(self, node_or_edge, xml_obj, data, default): # Add attrvalues to node or edge attvalues=Element('attvalues') if len(data)==0: return data if 'start' in data or 'end' in data: mode='dynamic' else: mode='static' for k,v in data.items(): # rename generic multigraph key to avoid any name conflict if k == 'key': k='networkx_key' attr_id = self.get_attr_id(make_str(k), self.xml_type[type(v)], node_or_edge, default, mode) if type(v)==list: # dynamic data for val,start,end in v: e=Element("attvalue") e.attrib['for']=attr_id e.attrib['value']=make_str(val) e.attrib['start']=make_str(start) e.attrib['end']=make_str(end) attvalues.append(e) else: # static data e=Element("attvalue") e.attrib['for']=attr_id e.attrib['value']=make_str(v) attvalues.append(e) xml_obj.append(attvalues) return data
def to_xml(self): node = Element(self.__class__.__name__, {}) for _annot in self._annotations: node.append(_annot.to_xml()) return node
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 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 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 dict_to_xml(d, item): el = Element("data") for key, val in d.items(): child = Element(item) child.text = str(val) child.set("key", key) el.append(child) print(tostring(el))
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 _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 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 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 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 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_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 _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 descxml(self, done): if self in done: e = Element('ref', id=str(done[self])) else: done[self] = len(done) e = Element('dict', id=str(done[self])) e.append(self.key.descxml(done)) e.append(self.value.descxml(done)) return e
def add_parents(self,node_element,node_data): parents = node_data.pop('parents', False) if parents: parents_element = Element('parents') for p in parents: e = Element('parent') e.attrib['for'] = str(p) parents_element.append(e) node_element.append(parents_element) return node_data
def add_parents(self, node_element, node_data): parents = node_data.pop("parents", False) if parents: parents_element = Element("parents") for p in parents: e = Element("parent") e.attrib["for"] = str(p) parents_element.append(e) node_element.append(parents_element) return node_data
def New_Tree(old_root): new_root = Element('osm') new_root.set('version', old_root.attrib['version']) # some programs output either 'bounds' or 'bound' try: new_root.append(old_root.find('bounds')) except: new_root.append(old_root.find('bound')) return ElementTree.ElementTree(new_root)
def serialize_inputs(self, **kwargs): """Takes our input type data and turns it back into an XML tree""" inputs = Element("inputs") for item in self.input_types: inputs.append(Element("input", type=item)) return inputs
class GraphMLWriter(GraphML): def __init__(self, graph=None, encoding="utf-8", prettyprint=True): try: import xml.etree.ElementTree except ImportError: raise ImportError('GraphML writer requires ' 'xml.elementtree.ElementTree') self.prettyprint = prettyprint self.encoding = encoding self.xml = Element( "graphml", { 'xmlns': self.NS_GRAPHML, 'xmlns:xsi': self.NS_XSI, 'xsi:schemaLocation': self.SCHEMALOCATION }) self.keys = {} if graph is not None: self.add_graph_element(graph) def __str__(self): if self.prettyprint: self.indent(self.xml) s = tostring(self.xml).decode(self.encoding) return s def get_key(self, name, attr_type, scope, default): keys_key = (name, attr_type, scope) try: return self.keys[keys_key] except KeyError: new_id = "d%i" % len(list(self.keys)) self.keys[keys_key] = new_id key_kwargs = { "id": new_id, "for": scope, "attr.name": name, "attr.type": attr_type } key_element = Element("key", **key_kwargs) # add subelement for data default value if present if default is not None: default_element = Element("default") default_element.text = make_str(default) key_element.append(default_element) self.xml.insert(0, key_element) return new_id def add_data(self, name, element_type, value, scope="all", default=None): """ Make a data element for an edge or a node. Keep a log of the type in the keys table. """ if element_type not in self.xml_type: raise nx.NetworkXError('GraphML writer does not support ' '%s as data values.' % element_type) key_id = self.get_key(name, self.xml_type[element_type], scope, default) data_element = Element("data", key=key_id) data_element.text = make_str(value) return data_element def add_attributes(self, scope, xml_obj, data, default): """Appends attributes to edges or nodes. """ for k, v in data.items(): default_value = default.get(k) obj = self.add_data(make_str(k), type(v), make_str(v), scope=scope, default=default_value) xml_obj.append(obj) def add_nodes(self, G, graph_element): for node, data in G.nodes_iter(data=True): node_element = Element("node", id=make_str(node)) default = G.graph.get('node_default', {}) self.add_attributes("node", node_element, data, default) graph_element.append(node_element) def add_edges(self, G, graph_element): if G.is_multigraph(): for u, v, key, data in G.edges_iter(data=True, keys=True): edge_element = Element("edge", source=make_str(u), target=make_str(v)) default = G.graph.get('edge_default', {}) self.add_attributes("edge", edge_element, data, default) self.add_attributes("edge", edge_element, {'key': key}, default) graph_element.append(edge_element) else: for u, v, data in G.edges_iter(data=True): edge_element = Element("edge", source=make_str(u), target=make_str(v)) default = G.graph.get('edge_default', {}) self.add_attributes("edge", edge_element, data, default) graph_element.append(edge_element) def add_graph_element(self, G): """ Serialize graph G in GraphML to the stream. """ if G.is_directed(): default_edge_type = 'directed' else: default_edge_type = 'undirected' graphid = G.graph.pop('id', None) if graphid is None: graph_element = Element("graph", edgedefault=default_edge_type) else: graph_element = Element("graph", edgedefault=default_edge_type, id=graphid) default = {} data = dict((k, v) for (k, v) in G.graph.items() if k not in ['node_default', 'edge_default']) self.add_attributes("graph", graph_element, data, default) self.add_nodes(G, graph_element) self.add_edges(G, graph_element) self.xml.append(graph_element) def add_graphs(self, graph_list): """ Add many graphs to this GraphML document. """ for G in graph_list: self.add_graph_element(G) def dump(self, stream): if self.prettyprint: self.indent(self.xml) document = ElementTree(self.xml) header = '<?xml version="1.0" encoding="%s"?>' % self.encoding stream.write(header.encode(self.encoding)) document.write(stream, encoding=self.encoding) def indent(self, elem, level=0): # in-place prettyprint formatter i = "\n" + level * " " if len(elem): if not elem.text or not elem.text.strip(): elem.text = i + " " if not elem.tail or not elem.tail.strip(): elem.tail = i for elem in elem: self.indent(elem, level + 1) if not elem.tail or not elem.tail.strip(): elem.tail = i else: if level and (not elem.tail or not elem.tail.strip()): elem.tail = i
def serialize(self, **kwargs): """Save out as an element tree""" variable = Element("variable", name=self.name) if self.contents is not None: variable.append(self.contents.serialize(**kwargs)) return variable
def nifi_flow_xml(connectable, nifi_version=None, root=None, visited=None): if visited is None: visited = [] position = Element('position') position.set('x', '0.0') position.set('y', '0.0') comment = Element('comment') styles = Element('styles') bend_points = Element('bendPoints') label_index = Element('labelIndex') label_index.text = '1' z_index = Element('zIndex') z_index.text = '0' if root is None: res = Element('flowController') max_timer_driven_thread_count = Element('maxTimerDrivenThreadCount') max_timer_driven_thread_count.text = '10' res.append(max_timer_driven_thread_count) max_event_driven_thread_count = Element('maxEventDrivenThreadCount') max_event_driven_thread_count.text = '5' res.append(max_event_driven_thread_count) root_group = Element('rootGroup') root_group_id = Element('id') root_group_id_text = str(uuid.uuid4()) root_group_id.text = root_group_id_text root_group.append(root_group_id) root_group_name = Element('name') root_group_name.text = root_group_id_text root_group.append(root_group_name) res.append(root_group) root_group.append(position) root_group.append(comment) res.append(Element('controllerServices')) res.append(Element('reportingTasks')) res.set('encoding-version', '1.2') else: res = root visited.append(connectable) if hasattr(connectable, 'name'): connectable_name_text = connectable.name else: connectable_name_text = str(connectable.uuid) if isinstance(connectable, InputPort): input_port = Element('inputPort') input_port_id = Element('id') input_port_id.text = str(connectable.uuid) input_port.append(input_port_id) input_port_name = Element('name') input_port_name.text = connectable_name_text input_port.append(input_port_name) input_port.append(position) input_port.append(comment) input_port_scheduled_state = Element('scheduledState') input_port_scheduled_state.text = 'RUNNING' input_port.append(input_port_scheduled_state) input_port_max_concurrent_tasks = Element('maxConcurrentTasks') input_port_max_concurrent_tasks.text = '1' input_port.append(input_port_max_concurrent_tasks) res.iterfind('rootGroup').next().append(input_port) if isinstance(connectable, Processor): conn_destination = Element('processor') proc_id = Element('id') proc_id.text = str(connectable.uuid) conn_destination.append(proc_id) proc_name = Element('name') proc_name.text = connectable_name_text conn_destination.append(proc_name) conn_destination.append(position) conn_destination.append(styles) conn_destination.append(comment) proc_class = Element('class') proc_class.text = 'org.apache.nifi.processors.standard.' + connectable.clazz conn_destination.append(proc_class) proc_bundle = Element('bundle') proc_bundle_group = Element('group') proc_bundle_group.text = 'org.apache.nifi' proc_bundle.append(proc_bundle_group) proc_bundle_artifact = Element('artifact') proc_bundle_artifact.text = 'nifi-standard-nar' proc_bundle.append(proc_bundle_artifact) proc_bundle_version = Element('version') proc_bundle_version.text = nifi_version proc_bundle.append(proc_bundle_version) conn_destination.append(proc_bundle) proc_max_concurrent_tasks = Element('maxConcurrentTasks') proc_max_concurrent_tasks.text = '1' conn_destination.append(proc_max_concurrent_tasks) proc_scheduling_period = Element('schedulingPeriod') proc_scheduling_period.text = connectable.schedule['scheduling period'] conn_destination.append(proc_scheduling_period) proc_penalization_period = Element('penalizationPeriod') proc_penalization_period.text = connectable.schedule[ 'penalization period'] conn_destination.append(proc_penalization_period) proc_yield_period = Element('yieldPeriod') proc_yield_period.text = connectable.schedule['yield period'] conn_destination.append(proc_yield_period) proc_bulletin_level = Element('bulletinLevel') proc_bulletin_level.text = 'WARN' conn_destination.append(proc_bulletin_level) proc_loss_tolerant = Element('lossTolerant') proc_loss_tolerant.text = 'false' conn_destination.append(proc_loss_tolerant) proc_scheduled_state = Element('scheduledState') proc_scheduled_state.text = 'RUNNING' conn_destination.append(proc_scheduled_state) proc_scheduling_strategy = Element('schedulingStrategy') proc_scheduling_strategy.text = connectable.schedule[ 'scheduling strategy'] conn_destination.append(proc_scheduling_strategy) proc_execution_node = Element('executionNode') proc_execution_node.text = 'ALL' conn_destination.append(proc_execution_node) proc_run_duration_nanos = Element('runDurationNanos') proc_run_duration_nanos.text = str( connectable.schedule['run duration nanos']) conn_destination.append(proc_run_duration_nanos) for property_key, property_value in connectable.properties.iteritems(): proc_property = Element('property') proc_property_name = Element('name') proc_property_name.text = connectable.nifi_property_key( property_key) proc_property.append(proc_property_name) proc_property_value = Element('value') proc_property_value.text = property_value proc_property.append(proc_property_value) conn_destination.append(proc_property) for auto_terminate_rel in connectable.auto_terminate: proc_auto_terminated_relationship = Element( 'autoTerminatedRelationship') proc_auto_terminated_relationship.text = auto_terminate_rel conn_destination.append(proc_auto_terminated_relationship) res.iterfind('rootGroup').next().append(conn_destination) for svc in connectable.controller_services: if svc in visited: continue visited.append(svc) controller_service = Element('controllerService') controller_service_id = Element('id') controller_service_id.text = str(svc.id) controller_service.append(controller_service_id) controller_service_name = Element('name') controller_service_name.text = svc.name controller_service.append(controller_service_name) controller_service.append(comment) controller_service_class = Element('class') controller_service_class.text = svc.service_class, controller_service.append(controller_service_class) controller_service_bundle = Element('bundle') controller_service_bundle_group = Element('group') controller_service_bundle_group.text = svc.group controller_service_bundle.append(controller_service_bundle_group) controller_service_bundle_artifact = Element('artifact') controller_service_bundle_artifact.text = svc.artifact controller_service_bundle.append( controller_service_bundle_artifact) controller_service_bundle_version = Element('version') controller_service_bundle_version.text = nifi_version controller_service_bundle.append(controller_service_bundle_version) controller_service.append(controller_service_bundle) controller_enabled = Element('enabled') controller_enabled.text = 'true', controller_service.append(controller_enabled) for property_key, property_value in svc.properties: controller_service_property = Element('property') controller_service_property_name = Element('name') controller_service_property_name.text = property_key controller_service_property.append( controller_service_property_name) controller_service_property_value = Element('value') controller_service_property_value.text = property_value controller_service_property.append( controller_service_property_value) controller_service.append(controller_service_property) res.iterfind('rootGroup').next().append(controller_service) for conn_name in connectable.connections: conn_destinations = connectable.connections[conn_name] if isinstance(conn_destinations, list): for conn_destination in conn_destinations: connection = nifi_flow_xml_connection(res, bend_points, conn_name, connectable, label_index, conn_destination, z_index) res.iterfind('rootGroup').next().append(connection) if conn_destination not in visited: nifi_flow_xml(conn_destination, nifi_version, res, visited) else: connection = nifi_flow_xml_connection(res, bend_points, conn_name, connectable, label_index, conn_destinations, z_index) res.iterfind('rootGroup').next().append(connection) if conn_destinations not in visited: nifi_flow_xml(conn_destinations, nifi_version, res, visited) if root is None: return ('<?xml version="1.0" encoding="UTF-8" standalone="no"?>' + "\n" + elementTree.tostring(res, encoding='utf-8'))
def show_xmlvalue(self, tag, value, **attrs): e = Element(tag, **attrs) e.append(value.descxml({})) self.write(tostr(e)) return
class GEXFWriter(GEXF): # class for writing GEXF format files # use write_gexf() function 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 __str__(self): if self.prettyprint: self.indent(self.xml) s=tostring(self.xml).decode(self.encoding) return s 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_nodes(self, G, graph_element): nodes_element = Element('nodes') for node,data in G.nodes_iter(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 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 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_attributes(self, node_or_edge, xml_obj, data, default): # Add attrvalues to node or edge attvalues=Element('attvalues') if len(data)==0: return data if 'start' in data or 'end' in data: mode='dynamic' else: mode='static' for k,v in data.items(): # rename generic multigraph key to avoid any name conflict if k == 'key': k='networkx_key' attr_id = self.get_attr_id(make_str(k), self.xml_type[type(v)], node_or_edge, default, mode) if type(v)==list: # dynamic data for val,start,end in v: e=Element("attvalue") e.attrib['for']=attr_id e.attrib['value']=make_str(val) e.attrib['start']=make_str(start) e.attrib['end']=make_str(end) attvalues.append(e) else: # static data e=Element("attvalue") e.attrib['for']=attr_id e.attrib['value']=make_str(v) attvalues.append(e) xml_obj.append(attvalues) return data def get_attr_id(self, title, attr_type, edge_or_node, default, mode): # find the id of the attribute or generate a new id try: return self.attr[edge_or_node][mode][title] except KeyError: # generate new id new_id=str(next(self.attr_id)) self.attr[edge_or_node][mode][title] = new_id attr_kwargs = {"id":new_id, "title":title, "type":attr_type} attribute=Element("attribute",**attr_kwargs) # add subelement for data default value if present default_title=default.get(title) if default_title is not None: default_element=Element("default") default_element.text=make_str(default_title) attribute.append(default_element) # new insert it into the XML attributes_element=None for a in self.graph_element.findall("attributes"): # find existing attributes element by class and mode a_class=a.get('class') a_mode=a.get('mode','static') # default mode is static if a_class==edge_or_node and a_mode==mode: attributes_element=a if attributes_element is None: # create new attributes element attr_kwargs = {"mode":mode,"class":edge_or_node} attributes_element=Element('attributes', **attr_kwargs) self.graph_element.insert(0,attributes_element) attributes_element.append(attribute) return new_id 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_parents(self,node_element,node_data): parents=node_data.pop('parents',False) if parents: parents_element=Element('parents') for p in parents: e=Element('parent') e.attrib['for']=str(p) parents_element.append(e) node_element.append(parents_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 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 write(self, fh): # Serialize graph G in GEXF to the open fh if self.prettyprint: self.indent(self.xml) document = ElementTree(self.xml) header='<?xml version="1.0" encoding="%s"?>'%self.encoding fh.write(header.encode(self.encoding)) document.write(fh, encoding=self.encoding) def indent(self, elem, level=0): # in-place prettyprint formatter i = "\n" + level*" " if len(elem): if not elem.text or not elem.text.strip(): elem.text = i + " " if not elem.tail or not elem.tail.strip(): elem.tail = i for elem in elem: self.indent(elem, level+1) if not elem.tail or not elem.tail.strip(): elem.tail = i else: if level and (not elem.tail or not elem.tail.strip()): elem.tail = i
def write_everything_from_google_to_xml(country, outfile='weather.xml', hl=''): """ Write all the results from google to an xml file """ weather_reports = pywapi.get_everything_from_google(country, hl) xml_output = Element('Weather') for city in weather_reports: try: xml_city = Element('town') xml_name = Element('name') xml_name.text = city xml_city.append(xml_name) xml_temperature = Element('temperature') xml_temperature.text = weather_reports[city]['current_conditions'][ 'temp_c'] xml_city.append(xml_temperature) xml_humidity = Element('humidity') xml_humidity.text = weather_reports[city]['current_conditions'][ 'humidity'] xml_city.append(xml_humidity) xml_condition = Element('condition') xml_condition.text = weather_reports[city]['current_conditions'][ 'condition'] xml_city.append(xml_condition) xml_wind = Element('wind') xml_wind.text = weather_reports[city]['current_conditions'][ 'wind_condition'] xml_city.append(xml_wind) xml_output.append(xml_city) except KeyError: pass ElementTree(xml_output).write(outfile, 'UTF-8')
def saveXML(self): try: if ChannelsBase.channels_changed: ChannelsBase.channels_changed = False channels = ChannelsBase.channels # Generate List in RAM etree = None #log.debug("saveXML channels", channels) log.debug("SP saveXML channels", len(channels)) # XMLTV compatible channels file #TEST Do we need to write the xml header node # Build Header from plugin import NAME, VERSION root = Element("channels") root.set('version', VERSION) root.set('created_by', NAME) 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[:] if alternatives: # Add channel web = alternatives[0] element = SubElement(root, "channel", name=stringToXML(name), id=stringToXML(web)) element.text = stringToXML(reference) del alternatives[0] if alternatives: for web in alternatives: SubElement( element, "web").text = stringToXML(web) return root etree = ElementTree(build(root, channels)) indent(etree.getroot()) self.writeXML(etree) if config.plugins.seriesplugin.epgimport.value: log.debug("Write: xml channels for epgimport") try: path = "/etc/epgimport/wunschliste.channels.xml" etree.write(path, encoding='utf-8', xml_declaration=True) except Exception as e: log.exception("Exception in write XML: " + str(e)) if config.plugins.seriesplugin.xmltvimport.value: log.debug("Write: xml channels for xmltvimport") try: path = "/etc/xmltvimport/wunschliste.channels.xml" etree.write(path, encoding='utf-8', xml_declaration=True) except Exception as e: log.exception("Exception in write XML: " + str(e)) except Exception as e: log.exception("Exception in writeXML: " + str(e))
def _types_to_fixture(location_db, type, locs, data_fields): type_node = Element('%ss' % type.code) # hacky pluralization for loc in locs: type_node.append(_location_to_fixture(location_db, loc, type, data_fields)) return type_node
def write_everything_from_yahoo_to_xml(country, cities, outfile='weather.xml'): """ Write all the results from google to an xml file """ weather_reports = pywapi.get_everything_from_yahoo(country, cities) xml_output = Element('Weather') for city, report in weather_reports.items(): try: xml_city = Element('town') xml_name = Element('name') xml_name.text = city xml_city.append(xml_name) xml_temperature = Element('temperature') temp_c = report['wind']['chill'] temp_unit = report['units']['temperature'] temp_cond = ''.join([temp_c, ' ', temp_unit]) xml_temperature.text = temp_cond xml_city.append(xml_temperature) xml_humidity = Element('humidity') xml_humidity.text = report['atmosphere']['humidity'] xml_city.append(xml_humidity) xml_condition = Element('condition') xml_condition.text = report['condition']['text'] xml_city.append(xml_condition) xml_wind = Element('wind') beaufort = pywapi.wind_beaufort_scale(report['wind']['speed']) direction = pywapi.wind_direction(report['wind']['direction']) wind_cond = ''.join([beaufort, ' ', direction]) xml_wind.text = wind_cond xml_city.append(xml_wind) xml_output.append(xml_city) except KeyError: pass ElementTree(xml_output).write(outfile, 'UTF-8')
def serialize(self, **kwargs): """Return an elementtree representing this object""" costumes_node = Element("costumes") costumes_node.append(self.list_node.serialize(**kwargs)) return costumes_node
def serialize_scripts(self, **kwargs): """Returns a script node for use in serialization""" scripts_node = Element("scripts") for item in self.scripts: scripts_node.append(item.serialize(**kwargs)) return scripts_node
class GEXFWriter(GEXF): # class for writing GEXF format files # use write_gexf() function def __init__(self, graph=None, encoding='utf-8', prettyprint=True, version='1.2draft'): try: import xml.etree.ElementTree as ET except ImportError: raise ImportError('GEXF writer requires ' 'xml.elementtree.ElementTree') self.prettyprint = prettyprint self.encoding = encoding self.set_version(version) self.xml = Element( 'gexf', { 'xmlns': self.NS_GEXF, 'xmlns:xsi': self.NS_XSI, 'xsi:schemaLocation': self.SCHEMALOCATION, 'version': self.VERSION }) ET.register_namespace('viz', self.NS_VIZ) # 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 __str__(self): if self.prettyprint: self.indent(self.xml) s = tostring(self.xml).decode(self.encoding) return s def add_graph(self, G): # set graph attributes if G.graph.get('mode') == 'dynamic': mode = 'dynamic' else: mode = 'static' # Add a graph element to the XML if G.is_directed(): default = 'directed' else: default = 'undirected' name = G.graph.get('name', '') graph_element = Element('graph', defaultedgetype=default, mode=mode, name=name) self.graph_element = graph_element self.add_meta(G, graph_element) self.add_nodes(G, graph_element) self.add_edges(G, graph_element) self.xml.append(graph_element) def add_meta(self, G, graph_element): # add meta element with creator and date meta_element = Element('meta') SubElement(meta_element, 'creator').text = 'NetworkX {}'.format(nx.__version__) SubElement(meta_element, 'lastmodified').text = time.strftime('%d/%m/%Y') graph_element.append(meta_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 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, data, key in G.edges(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(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.nodes[u].get('id', u)) target_id = make_str(G.nodes[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 add_attributes(self, node_or_edge, xml_obj, data, default): # Add attrvalues to node or edge attvalues = Element('attvalues') if len(data) == 0: return data mode = 'static' for k, v in data.items(): # rename generic multigraph key to avoid any name conflict if k == 'key': k = 'networkx_key' val_type = type(v) if isinstance(v, list): # dynamic data for val, start, end in v: val_type = type(val) if start is not None or end is not None: mode = 'dynamic' self.alter_graph_mode_timeformat(start) self.alter_graph_mode_timeformat(end) break attr_id = self.get_attr_id(make_str(k), self.xml_type[val_type], node_or_edge, default, mode) for val, start, end in v: e = Element('attvalue') e.attrib['for'] = attr_id e.attrib['value'] = make_str(val) if start is not None: e.attrib['start'] = make_str(start) if end is not None: e.attrib['end'] = make_str(end) attvalues.append(e) else: # static data mode = 'static' attr_id = self.get_attr_id(make_str(k), self.xml_type[val_type], node_or_edge, default, mode) e = Element('attvalue') e.attrib['for'] = attr_id if isinstance(v, bool): e.attrib['value'] = make_str(v).lower() else: e.attrib['value'] = make_str(v) attvalues.append(e) xml_obj.append(attvalues) return data def get_attr_id(self, title, attr_type, edge_or_node, default, mode): # find the id of the attribute or generate a new id try: return self.attr[edge_or_node][mode][title] except KeyError: # generate new id new_id = str(next(self.attr_id)) self.attr[edge_or_node][mode][title] = new_id attr_kwargs = {'id': new_id, 'title': title, 'type': attr_type} attribute = Element('attribute', **attr_kwargs) # add subelement for data default value if present default_title = default.get(title) if default_title is not None: default_element = Element('default') default_element.text = make_str(default_title) attribute.append(default_element) # new insert it into the XML attributes_element = None for a in self.graph_element.findall('attributes'): # find existing attributes element by class and mode a_class = a.get('class') a_mode = a.get('mode', 'static') if a_class == edge_or_node and a_mode == mode: attributes_element = a if attributes_element is None: # create new attributes element attr_kwargs = {'mode': mode, 'class': edge_or_node} attributes_element = Element('attributes', **attr_kwargs) self.graph_element.insert(0, attributes_element) attributes_element.append(attribute) return new_id 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)) 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_parents(self, node_element, node_data): parents = node_data.pop('parents', False) if parents: parents_element = Element('parents') for p in parents: e = Element('parent') e.attrib['for'] = str(p) parents_element.append(e) node_element.append(parents_element) return node_data 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_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 alter_graph_mode_timeformat(self, start_or_end): # if 'start' or 'end' appears, alter Graph mode to dynamic and set timeformat if self.graph_element.get('mode') == 'static': if start_or_end is not None: if isinstance(start_or_end, str): timeformat = 'date' elif isinstance(start_or_end, float): timeformat = 'double' elif isinstance(start_or_end, int): timeformat = 'long' else: raise nx.NetworkXError(\ 'timeformat should be of the type int, float or str') self.graph_element.set('timeformat', timeformat) self.graph_element.set('mode', 'dynamic') def write(self, fh): # Serialize graph G in GEXF to the open fh if self.prettyprint: self.indent(self.xml) document = ElementTree(self.xml) document.write(fh, encoding=self.encoding, xml_declaration=True) def indent(self, elem, level=0): # in-place prettyprint formatter i = "\n" + " " * level if len(elem): if not elem.text or not elem.text.strip(): elem.text = i + " " if not elem.tail or not elem.tail.strip(): elem.tail = i for elem in elem: self.indent(elem, level + 1) if not elem.tail or not elem.tail.strip(): elem.tail = i else: if level and (not elem.tail or not elem.tail.strip()): elem.tail = i
def create_IATI_xml(iatidata, dir, o): node = Element('iati-activities') node.set("version", "1.0") current_datetime = datetime.now().replace(microsecond=0).isoformat() node.set("generated-datetime",current_datetime) for activity in iatidata: a = Element("iati-activity") a.set("xml:lang", o["lang"]) a.set("default-currency", o["default-currency"]) node.append(a) ro = Element("reporting-org") ro.set("ref", o["reporting-org"]["ref"]) ro.set("type", o["reporting-org"]["type"]) ro.text = o["reporting-org"]["text"] a.append(ro) for field in activity: for key, val in field.items(): if key == "transaction": transactions = Element("transaction") a.append(transactions) for trans_data, trans_data_value in val.items(): transaction_field = Element(trans_data) for attrib, attrib_value in trans_data_value.items(): if (attrib == 'text'): try: attrib_value.decode('ascii') except UnicodeDecodeError: transaction_field.text = attrib_value else: transaction_field.text = attrib_value else: try: str(attrib_value).decode('ascii') except UnicodeDecodeError: transaction_field.set(attrib, unicode(str(attrib_value), "utf-8")) else: transaction_field.set(attrib, str(attrib_value)) transactions.append(transaction_field) else: key = Element(key) for attrib, attrib_value in val.items(): if (attrib == 'text'): try: attrib_value.decode('ascii') except UnicodeDecodeError: key.text = unicode(attrib_value, "utf-8") else: key.text = attrib_value else: try: str(attrib_value).decode('ascii') except UnicodeDecodeError: key.set(attrib, unicode(str(attrib_value), "utf-8")) else: key.set(attrib, str(attrib_value)) a.append(key) doc = ElementTree(node) XMLfilename = dir + '/IATI.xml' doc.write(XMLfilename) print "IATI-XML file saved to ", XMLfilename
import xlrd from xml.etree.cElementTree import Element, ElementTree, SubElement, Comment wb = xlrd.open_workbook('./Docs/numbers.xls') ws = wb.sheet_by_index(0) content = list() for xr in range(ws.nrows): row = ws.row(xr) num_list = list() for i in row: value = i.value num_list.append(value) content.append(num_list) print(content) root = Element('root') comment = Comment('数字信息') root.append(comment) child = SubElement(root, 'numbers') child.text = str(content) tree = ElementTree(root) tree.write('./Docs/numbers.xml', encoding='utf8')
def nifi_flow_xml_connection(res, bend_points, conn_name, connectable, label_index, destination, z_index): connection = Element('connection') connection_id = Element('id') connection_id.text = str(uuid.uuid4()) connection.append(connection_id) connection_name = Element('name') connection.append(connection_name) connection.append(bend_points) connection.append(label_index) connection.append(z_index) connection_source_id = Element('sourceId') connection_source_id.text = str(connectable.uuid) connection.append(connection_source_id) connection_source_group_id = Element('sourceGroupId') connection_source_group_id.text = res.iterfind('rootGroup/id').next().text connection.append(connection_source_group_id) connection_source_type = Element('sourceType') if isinstance(connectable, Processor): connection_source_type.text = 'PROCESSOR' elif isinstance(connectable, InputPort): connection_source_type.text = 'INPUT_PORT' else: raise Exception('Unexpected source type: %s' % type(connectable)) connection.append(connection_source_type) connection_destination_id = Element('destinationId') connection_destination_id.text = str(destination.uuid) connection.append(connection_destination_id) connection_destination_group_id = Element('destinationGroupId') connection_destination_group_id.text = res.iterfind( 'rootGroup/id').next().text connection.append(connection_destination_group_id) connection_destination_type = Element('destinationType') if isinstance(destination, Processor): connection_destination_type.text = 'PROCESSOR' elif isinstance(destination, InputPort): connection_destination_type.text = 'INPUT_PORT' else: raise Exception('Unexpected destination type: %s' % type(destination)) connection.append(connection_destination_type) connection_relationship = Element('relationship') if not isinstance(connectable, InputPort): connection_relationship.text = conn_name connection.append(connection_relationship) connection_max_work_queue_size = Element('maxWorkQueueSize') connection_max_work_queue_size.text = '10000' connection.append(connection_max_work_queue_size) connection_max_work_queue_data_size = Element('maxWorkQueueDataSize') connection_max_work_queue_data_size.text = '1 GB' connection.append(connection_max_work_queue_data_size) connection_flow_file_expiration = Element('flowFileExpiration') connection_flow_file_expiration.text = '0 sec' connection.append(connection_flow_file_expiration) return connection
def saveConfig(self, xml_file="~/.faraday/config/user.xml"): """ Saves XML config on new file. """ ROOT = Element("faraday") API_CON_INFO_HOST = Element(CONST_API_CON_INFO_HOST) API_CON_INFO_HOST.text = self.getApiConInfoHost() ROOT.append(API_CON_INFO_HOST) API_CON_INFO_PORT = Element(CONST_API_CON_INFO_PORT) API_CON_INFO_PORT.text = str(self.getApiConInfoPort()) ROOT.append(API_CON_INFO_PORT) API_RESTFUL_CON_INFO_PORT = Element(CONST_API_RESTFUL_CON_INFO_PORT) API_RESTFUL_CON_INFO_PORT.text = str(self.getApiRestfulConInfoPort()) ROOT.append(API_RESTFUL_CON_INFO_PORT) APPNAME = Element(CONST_APPNAME) APPNAME.text = self.getAppname() ROOT.append(APPNAME) AUTH = Element(CONST_AUTH, encrypted="no", algorithm="OTR") AUTH.text = self.getAuth() ROOT.append(AUTH) AUTO_SHARE_WORKSPACE = Element(CONST_AUTO_SHARE_WORKSPACE) AUTO_SHARE_WORKSPACE.text = self.getAutoShareWorkspace() ROOT.append(AUTO_SHARE_WORKSPACE) CONFIG_PATH = Element(CONST_CONFIG_PATH) CONFIG_PATH.text = self.getConfigPath() ROOT.append(CONFIG_PATH) DATA_PATH = Element(CONST_DATA_PATH) DATA_PATH.text = self.getDataPath() ROOT.append(DATA_PATH) DEBUG_STATUS = Element(CONST_DEBUG_STATUS) DEBUG_STATUS.text = str(self.getDebugStatus()) ROOT.append(DEBUG_STATUS) DEFAULT_CATEGORY = Element(CONST_DEFAULT_CATEGORY) DEFAULT_CATEGORY.text = self.getDefaultCategory() ROOT.append(DEFAULT_CATEGORY) DEFAULT_TEMP_PATH = Element(CONST_DEFAULT_TEMP_PATH) DEFAULT_TEMP_PATH.text = self.getDefaultTempPath() ROOT.append(DEFAULT_TEMP_PATH) FONT = Element(CONST_FONT) FONT.text = self.getFont() ROOT.append(FONT) HOME_PATH = Element(CONST_HOME_PATH) HOME_PATH.text = self.getHomePath() ROOT.append(HOME_PATH) HOST_TREE_TOGGLE = Element(CONST_HOST_TREE_TOGGLE) HOST_TREE_TOGGLE.text = self.getHostTreeToggle() ROOT.append(HOST_TREE_TOGGLE) HSTACTIONS_PATH = Element(CONST_HSTACTIONS_PATH) HSTACTIONS_PATH.text = self.getHsactionsPath() ROOT.append(HSTACTIONS_PATH) ICONS_PATH = Element(CONST_ICONS_PATH) ICONS_PATH.text = self.getIconsPath() ROOT.append(ICONS_PATH) IMAGE_PATH = Element(CONST_IMAGE_PATH) IMAGE_PATH.text = self.getImagePath() ROOT.append(IMAGE_PATH) LOG_CONSOLE_TOGGLE = Element(CONST_LOG_CONSOLE_TOGGLE) LOG_CONSOLE_TOGGLE.text = self.getLogConsoleToggle() ROOT.append(LOG_CONSOLE_TOGGLE) NETWORK_LOCATION = Element(CONST_NETWORK_LOCATION) NETWORK_LOCATION.text = self.getNetworkLocation() ROOT.append(NETWORK_LOCATION) PERSISTENCE_PATH = Element(CONST_PERSISTENCE_PATH) PERSISTENCE_PATH.text = self.getPersistencePath() ROOT.append(PERSISTENCE_PATH) PERSPECTIVE_VIEW = Element(CONST_PERSPECTIVE_VIEW) PERSPECTIVE_VIEW.text = self.getPerspectiveView() ROOT.append(PERSPECTIVE_VIEW) REPO_PASSWORD = Element(CONST_REPO_PASSWORD) REPO_PASSWORD.text = self.getRepoPassword() ROOT.append(REPO_PASSWORD) REPO_URL = Element(CONST_REPO_URL, type="SVN") REPO_URL.text = self.getRepoUrl() ROOT.append(REPO_URL) REPO_USER = Element(CONST_REPO_USER) REPO_USER.text = self.getRepoUser() ROOT.append(REPO_USER) REPORT_PATH = Element(CONST_REPORT_PATH) REPORT_PATH.text = self.getReportPath() ROOT.append(REPORT_PATH) SHELL_MAXIMIZED = Element(CONST_SHELL_MAXIMIZED) SHELL_MAXIMIZED.text = self.getShellMaximized() ROOT.append(SHELL_MAXIMIZED) LAST_WORKSPACE = Element(CONST_LAST_WORKSPACE) LAST_WORKSPACE.text = self.getLastWorkspace() ROOT.append(LAST_WORKSPACE) COUCH_URI = Element(CONST_COUCH_URI) COUCH_URI.text = self.getCouchURI() ROOT.append(COUCH_URI) COUCH_IS_REPLICATED = Element(CONST_COUCH_ISREPLICATED) COUCH_IS_REPLICATED.text = str(self.getCouchIsReplicated()) ROOT.append(COUCH_IS_REPLICATED) COUCH_REPLICS = Element(CONST_COUCH_REPLICS) COUCH_REPLICS.text = self.getCouchReplics() ROOT.append(COUCH_REPLICS) VERSION = Element(CONST_VERSION) VERSION.text = self.getVersion() ROOT.append(VERSION) PLUGIN_SETTINGS = Element(CONST_PLUGIN_SETTINGS) PLUGIN_SETTINGS.text = json.dumps(self.getPluginSettings()) ROOT.append(PLUGIN_SETTINGS) UPDATE_URI = Element(CONST_UPDATEURI) UPDATE_URI.text = self.getUpdatesUri() ROOT.append(UPDATE_URI) TKT_URI = Element(CONST_TKTURI) TKT_URI.text = self.getTktPostUri() ROOT.append(TKT_URI) TKT_APIPARAMS = Element(CONST_TKTAPIPARAMS) TKT_APIPARAMS.text = self.getApiParams() ROOT.append(TKT_APIPARAMS) TKT_TEMPLATE = Element(CONST_TKTTEMPLATE) TKT_TEMPLATE.text = self.getTktTemplate() ROOT.append(TKT_TEMPLATE) self.indent(ROOT, 0) xml_file = os.path.expanduser(xml_file) ElementTree(ROOT).write(xml_file)
class GraphMLWriter(GraphML): def __init__(self, graph=None, encoding="utf-8", prettyprint=True, infer_numeric_types=False): try: import xml.etree.ElementTree except ImportError: raise ImportError('GraphML writer requires ' 'xml.elementtree.ElementTree') self.infer_numeric_types = infer_numeric_types self.prettyprint=prettyprint self.encoding = encoding self.xml = Element("graphml", {'xmlns':self.NS_GRAPHML, 'xmlns:xsi':self.NS_XSI, 'xsi:schemaLocation':self.SCHEMALOCATION} ) self.keys={} self.attributes = defaultdict(list) self.attribute_types = defaultdict(set) if graph is not None: self.add_graph_element(graph) def __str__(self): if self.prettyprint: self.indent(self.xml) s=tostring(self.xml).decode(self.encoding) return s def attr_type(self, name, scope, value): """Infer the attribute type of data named name. Currently this only supports inference of numeric types. If self.infer_numeric_types is false, type is used. Otherwise, pick the most general of types found across all values with name and scope. This means edges with data named 'weight' are treated separately from nodes with data named 'weight'. """ if self.infer_numeric_types: types = self.attribute_types[(name, scope)] try: chr(12345) # Fails on Py!=3. long = int # Py3K's int is our long type except ValueError: # Python 2.x pass if len(types) > 1: if float in types: return float elif long in types: return long else: return int else: return list(types)[0] else: return type(value) def get_key(self, name, attr_type, scope, default): keys_key = (name, attr_type, scope) try: return self.keys[keys_key] except KeyError: new_id = "d%i" % len(list(self.keys)) self.keys[keys_key] = new_id key_kwargs = {"id":new_id, "for":scope, "attr.name":name, "attr.type":attr_type} key_element=Element("key",**key_kwargs) # add subelement for data default value if present if default is not None: default_element=Element("default") default_element.text=make_str(default) key_element.append(default_element) self.xml.insert(0,key_element) return new_id def add_data(self, name, element_type, value, scope="all", default=None): """ Make a data element for an edge or a node. Keep a log of the type in the keys table. """ if element_type not in self.xml_type: raise nx.NetworkXError('GraphML writer does not support ' '%s as data values.'%element_type) key_id = self.get_key(name, self.xml_type[element_type], scope, default) data_element = Element("data", key=key_id) data_element.text = make_str(value) return data_element def add_attributes(self, scope, xml_obj, data, default): """Appends attribute data to edges or nodes, and stores type information to be added later. See add_graph_element. """ for k,v in data.items(): self.attribute_types[(make_str(k), scope)].add(type(v)) self.attributes[xml_obj].append([k, v, scope, default.get(k)]) def add_nodes(self, G, graph_element): for node,data in G.nodes(data=True): node_element = Element("node", id = make_str(node)) default=G.graph.get('node_default',{}) self.add_attributes("node", node_element, data, default) graph_element.append(node_element) def add_edges(self, G, graph_element): if G.is_multigraph(): for u,v,key,data in G.edges(data=True,keys=True): edge_element = Element("edge",source=make_str(u), target=make_str(v)) default=G.graph.get('edge_default',{}) self.add_attributes("edge", edge_element, data, default) self.add_attributes("edge", edge_element, {'key':key}, default) graph_element.append(edge_element) else: for u,v,data in G.edges(data=True): edge_element = Element("edge",source=make_str(u), target=make_str(v)) default=G.graph.get('edge_default',{}) self.add_attributes("edge", edge_element, data, default) graph_element.append(edge_element) def add_graph_element(self, G): """ Serialize graph G in GraphML to the stream. """ if G.is_directed(): default_edge_type='directed' else: default_edge_type='undirected' graphid=G.graph.pop('id',None) if graphid is None: graph_element = Element("graph", edgedefault = default_edge_type) else: graph_element = Element("graph", edgedefault = default_edge_type, id=graphid) default={} data=dict((k,v) for (k,v) in G.graph.items() if k not in ['node_default','edge_default']) self.add_attributes("graph", graph_element, data, default) self.add_nodes(G,graph_element) self.add_edges(G,graph_element) # self.attributes contains a mapping from XML Objects to a list of # data that needs to be added to them. # We postpone processing of this in order to do type inference/generalization. # See self.attr_type for (xml_obj, data) in self.attributes.items(): for (k, v, scope, default) in data: xml_obj.append(self.add_data(make_str(k), self.attr_type(k, scope, v), make_str(v), scope, default)) self.xml.append(graph_element) def add_graphs(self, graph_list): """ Add many graphs to this GraphML document. """ for G in graph_list: self.add_graph_element(G) def dump(self, stream): if self.prettyprint: self.indent(self.xml) document = ElementTree(self.xml) document.write(stream, encoding=self.encoding, xml_declaration=True) def indent(self, elem, level=0): # in-place prettyprint formatter i = "\n" + level*" " if len(elem): if not elem.text or not elem.text.strip(): elem.text = i + " " if not elem.tail or not elem.tail.strip(): elem.tail = i for elem in elem: self.indent(elem, level+1) if not elem.tail or not elem.tail.strip(): elem.tail = i else: if level and (not elem.tail or not elem.tail.strip()): elem.tail = i
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(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(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 = G.nodes[u].get('id', u) target_id = G.nodes[v].get('id', v) # Makes node with negative id the target if source_id < 0 and target_id > 0: target_id, source_id = source_id, target_id source_id_str = make_str(source_id) target_id_str = make_str(target_id) edge_element = Element('edge', source=source_id_str, target=target_id_str, **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 add_attributes(self, node_or_edge, xml_obj, data, default): # Add attrvalues to node or edge attvalues = Element('attvalues') if len(data) == 0: return data mode = 'static' for k, v in data.items(): # rename generic multigraph key to avoid any name conflict if k == 'key': k = 'networkx_key' val_type = type(v) if val_type not in self.xml_type: raise TypeError('attribute value type is not allowed: %s' % val_type) if isinstance(v, list): # dynamic data for val, start, end in v: val_type = type(val) if start is not None or end is not None: mode = 'dynamic' self.alter_graph_mode_timeformat(start) self.alter_graph_mode_timeformat(end) break attr_id = self.get_attr_id(make_str(k), self.xml_type[val_type], node_or_edge, default, mode) for val, start, end in v: e = Element('attvalue') e.attrib['for'] = attr_id e.attrib['value'] = make_str(val) # Handle nan, inf, -inf differently if e.attrib['value'] == 'inf': e.attrib['value'] = 'INF' elif e.attrib['value'] == 'nan': e.attrib['value'] = 'NaN' elif e.attrib['value'] == '-inf': e.attrib['value'] = '-INF' if start is not None: e.attrib['start'] = make_str(start) if end is not None: e.attrib['end'] = make_str(end) attvalues.append(e) else: # static data mode = 'static' attr_id = self.get_attr_id(make_str(k), self.xml_type[val_type], node_or_edge, default, mode) e = Element('attvalue') e.attrib['for'] = attr_id if isinstance(v, bool): e.attrib['value'] = make_str(v).lower() else: e.attrib['value'] = make_str(v) # Handle nan, inf, -inf differently if e.attrib['value'] == 'inf': e.attrib['value'] = 'INF' elif e.attrib['value'] == 'nan': e.attrib['value'] = 'NaN' elif e.attrib['value'] == '-inf': e.attrib['value'] = '-INF' attvalues.append(e) xml_obj.append(attvalues) return data
def serialize(self, **kwargs): """Save out as an element tree""" blocks_node = Element("blocks") for definition in self.definitions: blocks_node.append(definition.serialize(**kwargs)) return blocks_node
def to_xml(self): """Convert this :class:`CalibrationRecord` to an XML :class:`~xml.etree.ElementTree.Element`. Returns ------- :class:`~xml.etree.ElementTree.Element` The :class:`CalibrationRecord` as a XML :class:`~xml.etree.ElementTree.Element`. """ root = Element('CalibrationRecord') calibration_date = Element('calibration_date') calibration_date.text = self.calibration_date.isoformat() calibration_date.attrib['format'] = 'YYYY-MM-DD' root.append(calibration_date) calibration_cycle = Element('calibration_cycle') calibration_cycle.text = str(self.calibration_cycle) calibration_cycle.attrib['unit'] = 'years' root.append(calibration_cycle) measurands = Element('measurands') for measurand in self.measurands.values(): measurands.append(measurand.to_xml()) root.append(measurands) report_number = Element('report_number') report_number.text = self.report_number root.append(report_number) report_date = Element('report_date') report_date.text = self.report_date.isoformat() report_date.attrib['format'] = 'YYYY-MM-DD' root.append(report_date) return root
class DeviceXMLGenerator(object): def __init__(self, device, extra_elts=[]): self.device = device self.extra_elts = extra_elts def generate(self): self.device_element = Element("device") self._create_device() self._create_icon_list() self._create_service_list() self._create_embedded_devices() return self.device_element def _create_device(self): for elt in self.extra_elts: self.device_element.append(elt) element = SubElement(self.device_element, "deviceType") element.text = self.device.device_type element = SubElement(self.device_element, "friendlyName") element.text = self.device.friendly_name element = SubElement(self.device_element, "manufacturer") element.text = self.device.manufacturer element = SubElement(self.device_element, "manufacturerURL") element.text = self.device.manufacturer_url element = SubElement(self.device_element, "modelDescription") element.text = self.device.model_description element = SubElement(self.device_element, "modelName") element.text = self.device.model_name element = SubElement(self.device_element, "modelNumber") element.text = self.device.model_number element = SubElement(self.device_element, "modelURL") element.text = self.device.model_url element = SubElement(self.device_element, "serialNumber") element.text = self.device.serial_number element = SubElement(self.device_element, "UDN") element.text = self.device.udn element = SubElement(self.device_element, "UPC") element.text = self.device.upc element = SubElement(self.device_element, "presentationURL") element.text = self.device.presentation_url def _create_icon_list(self): #<device><iconList> device_icons = self.device.icons if len(device_icons) > 0: icon_list_element = SubElement(self.device_element, "iconList") for device_icon in device_icons: icon_element = SubElement(icon_list_element, "icon") element = SubElement(icon_element, "mimetype") element.text = device_icon.get_mimetype() element = SubElement(icon_element, "width") element.text = device_icon.get_width() element = SubElement(icon_element, "height") element.text = device_icon.get_height() element = SubElement(icon_element, "depth") element.text = device_icon.get_depth() element = SubElement(icon_element, "url") element.text = device_icon.get_url() def _create_service_list(self): device_services = self.device.services if len(device_services) > 0: service_list_element = SubElement(self.device_element, "serviceList") for k, device_service in device_services.items(): service_element = SubElement(service_list_element, "service") element = SubElement(service_element, "serviceType") element.text = device_service.service_type element = SubElement(service_element, "serviceId") element.text = device_service.id element = SubElement(service_element, "SCPDURL") element.text = device_service.scpd_url element = SubElement(service_element, "controlURL") element.text = device_service.control_url element = SubElement(service_element, "eventSubURL") element.text = device_service.event_sub_url element = SubElement(service_element, "presentationURL") element.text = device_service.presentation_url def _create_embedded_devices(self): if self.device.is_root_device(): embedded_devices = self.device.devices if len(embedded_devices) > 0: device_list_element = SubElement(self.device_element, "deviceList") for embedded_device in embedded_devices.values(): embedded_device_description = DeviceXMLGenerator( embedded_device) device_list_element.append( embedded_device_description.generate())