示例#1
0
 def writerow(self, row):
     root = Element(u'row')
     if self.id_col:
         root.attrib[u'_id'] = unicode(row[0])
         row = row[1:]
     for k, v in zip(self.columns, row):
         if v is None:
             SubElement(root, k).text = u'NULL'
             continue
         SubElement(root, k).text = unicode(v)
     ElementTree(root).write(self.output, encoding=u'utf-8')
     self.output.write(b'\n')
示例#2
0
    def get_xml_nodes(self, fixture_id, restore_user, locations_queryset,
                      data_fields):
        locations_db = LocationSet(locations_queryset)

        root_node = Element('fixture', {
            'id': fixture_id,
            'user_id': restore_user.user_id
        })
        root_locations = locations_db.root_locations

        if root_locations:
            _append_children(root_node, locations_db, root_locations,
                             data_fields)
        else:
            # There is a bug on mobile versions prior to 2.27 where
            # a parsing error will cause mobile to ignore the element
            # after this one if this element is empty.
            # So we have to add a dummy empty_element child to prevent
            # this element from being empty.
            root_node.append(Element("empty_element"))
        return [root_node]
示例#3
0
 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
示例#4
0
    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
示例#5
0
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")
示例#6
0
def create_msms_pipeline_analysis():
    msms_pipeline_analysis = Element('msms_pipeline_analysis')
    msms_pipeline_analysis.set('date',
                               datetime.today().strftime("%Y-%m-%dT%H:%M:%S"))
    msms_pipeline_analysis.set('xmlns',
                               "http://regis-web.systemsbiology.net/pepXML")
    msms_pipeline_analysis.set('xmlns:xsi',
                               "http://www.w3.org/2001/XMLSchema-instance")
    msms_pipeline_analysis.set(
        'xsi:schemaLocation',
        "http://sashimi.sourceforge.net/schema_revision/pepXML/pepXML_v115.xsd"
    )
    return msms_pipeline_analysis
示例#7
0
    def serialize(self, **kwargs):
        """Save out as an element tree"""
        definition = Element("block-definition",
                             s=self.specification,
                             category=self.category,
                             type=self.type)

        for child in (self.header, self.code, self.serialize_inputs(**kwargs),
                      self.script.serialize(**kwargs), self.extra_scripts):
            if child is not None:
                definition.append(child)

        return definition
示例#8
0
文件: gexf.py 项目: paulrt/evo
 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)
示例#9
0
def account_to_xml(listing, account_name):
    doc = Element('account', name=account_name)
    doc.text = '\n'
    for record in listing:
        if 'subdir' in record:
            name = record.pop('subdir')
            sub = SubElement(doc, 'subdir', name=name)
        else:
            sub = SubElement(doc, 'container')
            for field in ('name', 'count', 'bytes', 'last_modified'):
                SubElement(sub, field).text = six.text_type(record.pop(field))
        sub.tail = '\n'
    return to_xml(doc)
示例#10
0
    def to_xml(self, tag='RecordDict'):
        """Convert the :class:`RecordDict` to an XML :class:`~xml.etree.ElementTree.Element`

        Parameters
        ----------
        tag : :class:`str`
            The name of the :class:`~xml.etree.ElementTree.Element`.

        Returns
        -------
        :class:`~xml.etree.ElementTree.Element`
            The :class:`RecordDict` as an XML :class:`~xml.etree.ElementTree.Element`.
        """
        root = Element(tag)
        for k, v in self._mapping.items():
            if isinstance(v, RecordDict):
                element = v.to_xml(tag=k)
            else:
                element = Element(k)
                element.text = repr(v)
            root.append(element)
        return root
示例#11
0
def _dict_to_xml(data):
    """Converts a dict to xml"""
    if len(data) > 1:
        raise ValueError("Cannot serialize a dict with multiple root nodes")
    if len(data) == 0:
        raise ValueError("dict as no root nodes")
    name, value = six.next(six.iteritems(data))
    root = Element(name)
    _dict_xml_node(value, root)

    rough_string = ElementTree.tostring(root, 'latin-1')
    reparsed = minidom.parseString(rough_string)
    return reparsed.toprettyxml(indent="  ")
示例#12
0
 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 '
                                'dict types as data values.')
     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
示例#13
0
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)
示例#14
0
    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
示例#15
0
 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)
示例#16
0
    def _set_node_boilerplate(self, operator_name, source_product):
        node_id = self._set_node_id(operator_name)
        node = Element("node", {"id": node_id})
        SubElement(node, "operator").text = operator_name
        sources = SubElement(node, "sources")
        if isinstance(source_product, str):
            SubElement(sources, "sourceProduct", {"refid": source_product})
        elif isinstance(source_product, list):
            sources = SubElement(sources, "sourceProducts")
            sources.text = ",".join(source_product)
        parameters = SubElement(node, "parameters")

        return node_id, node, parameters
示例#17
0
    def outxml(self):
        """
        Returns XML with all the items found
        """
        self.resetposition()
        result = self.readnextitem()
        xmlroot = Element("PMRdata")
        xmlroot.set('version', __version__)
        xmlroot.set('generator', 'MDVx:PMRReader')
        xmlroot.append(
            Comment("Generated with MDVx by DJFio[DB] http://djfio.ru/mdv/"))

        while result[0] is not None:
            item = Element('item')
            item.set('matUUID', str(result[0]))
            item.set('name', str(result[1].decode(__module_encodind__)))
            item.set('project', str(result[2].decode(__module_encodind__)))
            item.set('srcUUID', str(result[3]))
            item.set('date', str(result[4]))
            xmlroot.append(item)
            result = self.readnextitem()
        return xmlroot
示例#18
0
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
示例#19
0
    def to_xml(self):
        """Convert this :class:`ConnectionRecord` to an XML :class:`~xml.etree.ElementTree.Element`.

        Note
        ----
        All values of the :class:`ConnectionRecord` are converted to a :class:`str`.

        Returns
        -------
        :class:`~xml.etree.ElementTree.Element`
            The :class:`ConnectionRecord` as a XML :class:`~xml.etree.ElementTree.Element`.
        """
        root = Element('ConnectionRecord')
        for name in ConnectionRecord._NAMES:
            value = getattr(self, name)
            element = Element(name)
            if name == 'properties':
                if not self._properties:
                    element.text = ''
                else:
                    for prop_key in sorted(self._properties):
                        prop_value = self._properties[prop_key]
                        prop = Element(prop_key)
                        if isinstance(prop_value, Enum):
                            prop.text = prop_value.name
                        elif prop_key.endswith('termination'):
                            prop.text = repr(prop_value)
                        elif isinstance(prop_value, bytes):
                            prop.text = repr(prop_value)
                        else:
                            prop.text = '{}'.format(prop_value)
                        element.append(prop)
            elif isinstance(value, Enum):
                element.text = value.name
            else:
                element.text = '{}'.format(value)
            root.append(element)
        return root
示例#20
0
    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)
示例#21
0
    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
示例#22
0
 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 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
示例#24
0
    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)
示例#25
0
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)
示例#26
0
    def serialize(self, **kwargs):
        """Save out as an element tree"""
        if self.var_name is None:
            # this is a standard block, not a variable block
            block = Element(BlockType.block_type_name_from_value(self.type),
                            s=self.function_name)
        else:
            # this is a var block (note: not a 'custom-block')
            block = Element("block", var=self.var_name)
        for arg in self.arguments:
            block.append(arg.serialize(**kwargs))

        # Always output a UUID
        # unless we are passed the option "only_source_uuids"
        # in which case, only UUIDs that we read in initially
        # will be outputted

        if self.deserialized_uuid or \
                kwargs.get("only_source_uuids", False) is False:
            assert self.uuid is not None
            block.set("uuid", str(self.uuid))

        return block
示例#27
0
 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')
示例#28
0
 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())
示例#29
0
	def writeXMLTVConfig(self):
		
		if self.epgimport is None and self.xmltvimport is None and self.crossepg is None:
			return
		
		if int(self.epgimportversion[0]) >= 5 and int(self.xmltvimportversion[0]) >= 5 and int(self.crossepgversion[0]) >= 5:
			return;
		
		if config.plugins.seriesplugin.epgimport.value == False and config.plugins.seriesplugin.xmltvimport.value == False and config.plugins.seriesplugin.crossepg.value == False:
			return
		
		# Build Header
		from plugin import NAME, VERSION
		root = Element("sources")
		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")))
		
		element = SubElement( root, "source", type = "gen_xmltv", channels = "wunschliste.channels.xml" )
		
		SubElement( element, "description" ).text = "Wunschliste XMLTV"
		SubElement( element, "url" ).text = config.plugins.seriesplugin.xmltv_url.value
		
		etree = ElementTree( root )
		
		indent(etree.getroot())
		
		if config.plugins.seriesplugin.epgimport.value:
			log.debug("Write: xml channels for epgimport")
			if self.epgimport:
				try:
					self.epgimport.writeXML( etree )
				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")
			if self.xmltvimport:
				try:
					self.xmltvimport.writeXML( etree )
				except Exception as e:
					log.exception("Exception in write XML: " + str(e))
		
		if config.plugins.seriesplugin.crossepg.value:
			log.debug("Write: xml channels for crossepg")
			if self.crossepg:
				try:
					self.crossepg.writeXML( etree )
				except Exception as e:
					log.exception("Exception in write XML: " + str(e))
示例#30
0
    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)