Example #1
0
	def save(self, filename=None):
		if self.filename and filename is None: filename = self.filename
		if len(self.__dict__) == 0 or filename == None: return

		try:
			fileobj = open(filename, 'w')
		except:
			print 'ERROR>>> cannot write preferences into %s' % filename
			return

		writer = XMLGenerator(out=fileobj, encoding=self.system_encoding)
		writer.startDocument()
		defaults = XmlConfigParser.__dict__
		items = self.__dict__.items()
		items.sort()
		writer.startElement('preferences', {})
		writer.characters('\n')
		for key, value in items:
			if defaults.has_key(key) and defaults[key] == value: continue
			if key in ['filename', 'app']: continue
			writer.characters('\t')
			writer.startElement('%s' % key, {})

			str_value = path_unicode(value.__str__())
			if isinstance(value, str):
				str_value = "'%s'" % (escape_quote(str_value))

			writer.characters(str_value)

			writer.endElement('%s' % key)
			writer.characters('\n')
		writer.endElement('preferences')
		writer.endDocument()
		fileobj.close
class KanjiListToXmlFile:

    def __init__(self, filepath):
        self.file = open(filepath, "w+")
        self.handler = XMLGenerator(self.file, 'utf-8')
        self.handler.startDocument()
        self.handler.startElement('list', {})
        
    def __del__(self):
        self.handler.endElement('list')
        self.handler.endDocument()
        
    def kanjiToXml(self, kanji):
        self.handler.startElement('character', {})
        for el in kanji.element:
            if isinstance(kanji.element[el], unicode):
                self.handler.startElement(el, {})
                self.handler.characters(kanji.element[el])
                self.handler.endElement(el)
            elif isinstance(kanji.element[el], int):
                self.handler.startElement(el, {})
                self.handler.characters(str(kanji.element[el]))
                self.handler.endElement(el)
            elif isinstance(kanji.element[el], list):
                for m in kanji.element[el]:
                    self.handler.startElement(el, {})
                    self.handler.characters(m)
                    self.handler.endElement(el)
        self.handler.endElement('character')
        self.file.write('\n')
    
    def kanjiListToXml(self, kanjiList):
        for kanji in kanjiList:
            self.kanjiToXml(kanji)
Example #3
0
class XmlLogger(object):
    
    def __init__(self, path):
        if os.access(path, os.F_OK):
            os.rename(path, path + '.bak-' + str(load_time))
        self.output = open(path, 'w')
        self.xml_gen = XMLGenerator(self.output, 'utf-8')
        self.lock = RLock()

    def start_log(self, tag, attributes):
        self.doc_tag = tag
        with self.lock:
            self.xml_gen.startDocument()
            self.xml_gen.startElement(tag, attributes)

    def end_log(self):
        with self.lock:
            self.xml_gen.endElement(self.doc_tag)
            self.xml_gen.endDocument()

    @contextmanager
    def element(self, name, attrs={}):
        with self.lock:
            attrs = dict((key, str(value)) for key, value in attrs.items())
            self.text('\n')
            self.xml_gen.startElement(name, attrs)
            yield
            self.xml_gen.endElement(name)

    def text(self, text):
        with self.lock:
            self.xml_gen.characters(str(text))
Example #4
0
    def export(self,filename,transport):
        vprint( "osm-xml export...",1)

        #remember all nodes already exported
        unodes = {}

        fp = open(filename, "w")
        x = XMLGenerator(fp, "UTF-8")
        x.startDocument()
        x.startElement('osm',{"version":"0.6","generator":"crazy py script"})

        for w in self.ways.itervalues():
            if not 'highway' in w.tags:
                continue
            if transport == "all" or transport == "pt":
                if not (w.tags['highway']=='bus' or w.tags['highway']=='tram'):
                    continue
            if transport == "all" or transport == "hw":
                if (w.tags['highway']=='bus' or w.tags['highway']=='tram'):
                    continue
            w.toOSM(x)
            for nid in w.nds:
                if nid in unodes:#already used
                    continue
                unodes[nid]=True
                if w.nds.index(nid)==0 or w.nds.index(nid)==len(w.nds)-1:
                    self.nodes[nid].toOSM(x,True)
                else:
                    self.nodes[nid].toOSM(x)
        x.endElement('osm')
        x.endDocument()
Example #5
0
class XmlWriter(AbstractXmlWriter):

    def __init__(self, path):
        self.path = path
        self._output = open(path, 'wb')
        self._writer = XMLGenerator(self._output, 'UTF-8')
        self._writer.startDocument()
        self.closed = False

    def start(self, name, attributes={}, newline=True):
        attrs = AttributesImpl(attributes)
        self._writer.startElement(name, attrs)
        if newline:
            self.content('\n')

    def content(self, content):
        if content is not None:
            self._writer.characters(self._encode(content))

    def end(self, name, newline=True):
        self._writer.endElement(name)
        if newline:
            self.content('\n')

    def close(self):
        self._writer.endDocument()
        self._output.close()
        self.closed = True
Example #6
0
class XmlWriter(AbstractXmlWriter):

    def __init__(self, output):
        self._output = self._create_output(output)
        self._writer = XMLGenerator(self._output, encoding='UTF-8')
        self._writer.startDocument()
        self.closed = False

    def _create_output(self, output):
        return open(output, 'w') \
            if isinstance(output, basestring) else output

    def _start(self, name, attrs):
        self._writer.startElement(name, AttributesImpl(attrs))

    def _content(self, content):
        self._writer.characters(content)

    def _end(self, name):
        self._writer.endElement(name)

    def _newline(self):
        self._output.write('\n')

    # Workaround for http://ironpython.codeplex.com/workitem/29474
    if sys.platform == 'cli':
        def _escape(self, content):
            return AbstractXmlWriter._escape(self, content).encode('UTF-8')
Example #7
0
def main():
	optparser = optparse.OptionParser(usage="Usage: %prog [options] POFILE ...")
	optparser.add_option("-r", "--roundtrip", help="generate round-tripable output",
		action="store_true", dest="roundtrip", default=False)
	optparser.add_option("-o", "--output", help="write output to XMLFILE", metavar="XMLFILE",
		action="store", dest="output", default="-")
	(options, args) = optparser.parse_args()

	if options.output == "-":
		output = sys.stdout
	else:
		output = open(options.output, "w")

	writer = XMLGenerator(output or sys.stdout, "utf-8")
	writer.startDocument()
	writer.processingInstruction("xml-stylesheet", 'type="text/xsl" href="format-html.xsl"')
	writer.ignorableWhitespace("\n")

	writer.startElement("messages", {})
	writer.ignorableWhitespace("\n")

	msg = Message(writer, options.roundtrip)

	for l in fileinput.input(args):
		# Continuation string?
		m = re.match(r'\s*"(.*)"', l)
		if m:
			assert(msg.current)
			msg.current.append(unescape_c(m.group(1)))
			continue
		else:
			msg.flush()

		m = re.match(r'(?s)msgid "(.*)"', l)
		if m:
			msg.msgid = [unescape_c(m.group(1))]
			msg.current = msg.msgid
		m = re.match(r'(?s)msgstr "(.*)"', l)
		if m:
			msg.msgstr = [unescape_c(m.group(1))]
			msg.current = msg.msgstr

		m = re.match(r'# \s*(.*)', l)
		if m:
			msg.usrcomment.append(m.group(1))
		m = re.match(r'#\.\s*(.*)', l)
		if m:
			msg.dotcomment.append(m.group(1))
		m = re.match(r'#:\s*(.*)', l)
		if m:
			msg.reference.append(m.group(1))
		m = re.match(r'#,\s*(.*)', l)
		if m:
			msg.flags.append(m.group(1))

	msg.flush()

	writer.endElement("messages")
	writer.ignorableWhitespace("\n")
	writer.endDocument()
Example #8
0
 def startElement(self, tag, attrs={}):
     attrs_write = {'CREATED': gMF(attrs['created_mindmap']),
                    'MODIFIED': gMF(max(attrs['modified_mindmap'],
                                        attrs['modified_openerp'])),
                    'ID': attrs['id_mindmap'] or 'ID_' + str(random.randint(1, 10**10)),
                    'TEXT': attrs['name'],
                    }
     XMLGenerator.startElement(self, tag, attrs_write)
 def __init__(self, output, encoding, top_level_tag, attrs):
   xml_writer = XMLGenerator(output, encoding, True)
   xml_writer.startDocument()
   xml_writer.startElement(top_level_tag, attrs)
   self._xml_writer = xml_writer
   self.top_level_tag = top_level_tag
   self.ident=4
   self._xml_writer.characters('\n')
Example #10
0
 def __call__(self, url, data, coords, metadata):
     file = StringIO.StringIO()
     gen = XMLGenerator( file, 'UTF-8' )
     gen.startDocument()
     gen.startElement('img', {'usemap':'#map', 'src':str(url)})
     gen.endElement('img')
     self.writeMap( gen, data, coords, metadata ) 
     gen.endDocument()
     return file.getvalue()  
Example #11
0
    def test_xmlgen_basic_empty(self):
        result = StringIO()
        gen = XMLGenerator(result, short_empty_elements=True)
        gen.startDocument()
        gen.startElement("doc", {})
        gen.endElement("doc")
        gen.endDocument()

        self.assertEqual(result.getvalue(), start + "<doc/>")
Example #12
0
    def test_xmlgen_basic(self):
        result = StringIO()
        gen = XMLGenerator(result)
        gen.startDocument()
        gen.startElement("doc", {})
        gen.endElement("doc")
        gen.endDocument()

        self.assertEqual(result.getvalue(), start + "<doc></doc>")
 def output_photo_page(self):
     gen = XMLGenerator(self.wfile, 'utf-8')
     gen.startDocument()
     gen.startElement('html', {'lang': 'en'})
     html_generator = HtmlPhotoPageGenerator(gen)
     html_generator.output_head()
     html_generator.output_body()
     gen.endElement('html')
     gen.endDocument()
Example #14
0
def test_xmlgen_basic():
    result = StringIO()
    gen = XMLGenerator(result)
    gen.startDocument()
    gen.startElement("doc", {})
    gen.endElement("doc")
    gen.endDocument()

    return result.getvalue() == start + "<doc></doc>"
Example #15
0
    def startElement(self, name, attrs, indent = True, newl = True):
        if indent:
            self.ignorableWhitespace("".join(self.indents))
        self.indents.append(self.addindent)

        XMLGenerator.startElement(self, name, attrs)

        if newl:
            self.ignorableWhitespace(self.newl)
Example #16
0
	def write(self, filedate, id, programme):
		if programme[Fields.start].hour < self.config["files"]["start_hour"]:
			filedate -= timedelta(1)

		if filedate < self.now:
			return

		if filedate not in self.files:
			f = open(os.path.join(self.base, filedate.strftime("tv-%Y%m%d.xmltv")), "wb")
			g = XMLGenerator(f, "UTF-8")
			g.startDocument()
			f.write("<!DOCTYPE tv SYSTEM \"xmltv.dtd\">\n".encode("UTF-8"))
			g.startElement("tv", {"source-info-name": "Radio Times"})
			f.write("\n".encode("UTF-8"))
			for channel in self.config["channels"]:
				self._write_element(g, "channel", ("display-name", channel["disp"] if "disp" in channel else channel["name"]), {"id": channel["id"]})
				f.write("\n".encode("UTF-8"))
			self.files[filedate] = (f, g)

		(f, g) = self.files[filedate]
		attrs = collections.OrderedDict()
		attrs["channel"] = id
		attrs["start"] = programme[Fields.start].strftime("%Y%m%d%H%M%S")
		attrs["stop"] = programme[Fields.stop].strftime("%Y%m%d%H%M%S")
		g.startElement("programme", attrs)

		self._write_element(g, "title", programme[Fields.title])
		self._write_element(g, "sub-title", ": ".join(filter(None, [programme[Fields.sub_title], programme[Fields.episode]])))
		self._write_element(g, "desc", programme[Fields.desc])

		if programme[Fields.director] or programme[Fields.cast]:
			self._write_element(g, "credits", [("director", programme[Fields.director])] + [("actor", actor) for actor in programme[Fields.cast]])

		self._write_element(g, "year", programme[Fields.year])

		if programme[Fields.widescreen] or programme[Fields.black_and_white]:
			self._write_element(g, "video", [
				("aspect", "16:9" if programme[Fields.widescreen] else None),
				("colour", "no" if programme[Fields.black_and_white] else None)
			])

		self._write_element(g, "premiere", programme[Fields.premiere])
		self._write_element(g, "new", programme[Fields.new_series])
		self._write_element(g, "subtitles", programme[Fields.subtitles], {"type": "teletext"})

		if programme[Fields.star_rating]:
			self._write_element(g, "star-rating", ("value", programme[Fields.star_rating]))

		if programme[Fields.certificate]:
			self._write_element(g, "rating", ("value", programme[Fields.certificate]), {"system": "BBFC"})

		self._write_element(g, "category", programme[Fields.film])
		self._write_element(g, "category", programme[Fields.genre])

		g.endElement("programme")
		f.write("\n".encode("UTF-8"))
Example #17
0
def test_xmlgen_attr_escape_manydouble():
    result = StringIO()
    gen = XMLGenerator(result)

    gen.startDocument()
    gen.startElement("doc", {"a": '"\'"'})
    gen.endElement("doc")
    gen.endDocument()

    return result.getvalue() == start + "<doc a='\"&apos;\"'></doc>"
Example #18
0
def test_xmlgen_escaped_attr():
    result = StringIO()
    gen = XMLGenerator(result)

    gen.startDocument()
    gen.startElement("doc", {"x": unicode("\\u3042", "unicode-escape")})
    gen.endElement("doc")
    gen.endDocument()

    return result.getvalue() == start + '<doc x="&#12354;"></doc>'
Example #19
0
def test_xmlgen_attr_escape_manysingle():
    result = StringIO()
    gen = XMLGenerator(result)

    gen.startDocument()
    gen.startElement("doc", {"a": "'\"'"})
    gen.endElement("doc")
    gen.endDocument()

    return result.getvalue() == start + '<doc a="\'&quot;\'"></doc>'
Example #20
0
def test_xmlgen_pi():
    result = StringIO()
    gen = XMLGenerator(result)

    gen.startDocument()
    gen.processingInstruction("test", "data")
    gen.startElement("doc", {})
    gen.endElement("doc")
    gen.endDocument()

    return result.getvalue() == start + "<?test data?><doc></doc>"
Example #21
0
def test_xmlgen_ignorable():
    result = StringIO()
    gen = XMLGenerator(result)

    gen.startDocument()
    gen.startElement("doc", {})
    gen.ignorableWhitespace(" ")
    gen.endElement("doc")
    gen.endDocument()

    return result.getvalue() == start + "<doc> </doc>"
Example #22
0
def test_xmlgen_content_escape():
    result = StringIO()
    gen = XMLGenerator(result)

    gen.startDocument()
    gen.startElement("doc", {})
    gen.characters("<huhei&")
    gen.endElement("doc")
    gen.endDocument()

    return result.getvalue() == start + "<doc>&lt;huhei&amp;</doc>"
Example #23
0
    def startElement(self, name, attrs={}, children=False):
        """Starts an XML element.

        Arguments:
        name -- String of element name.
        attrs -- Dictionary of element attributes.
        children -- Boolean, whether the element has children or not.

        """
        self.ignorableWhitespace(self._indent * self._level)
        XMLGenerator.startElement(self, name, attrs)
Example #24
0
    def test_xmlgen_pi(self):
        result = StringIO()
        gen = XMLGenerator(result)

        gen.startDocument()
        gen.processingInstruction("test", "data")
        gen.startElement("doc", {})
        gen.endElement("doc")
        gen.endDocument()

        self.assertEqual(result.getvalue(), start + "<?test data?><doc></doc>")
Example #25
0
    def startElement(self, tagname, forceNewLine=True, **attrs):
        """Start a new element, with appropriate indentation."""
        if not self._newline_flag:
            self.ignorableWhitespace(' ' * self._indent_level)
        XMLGenerator.startElement(self, tagname,
                                  self.translate_name_aliases(**attrs))
        self._element_stack.append(tagname)
        self._newline_flag = True
        self._indent_level += self._indent_increment
	if forceNewLine:
		self.newline()
Example #26
0
    def test_xmlgen_content_empty(self):
        result = self.ioclass()
        gen = XMLGenerator(result, short_empty_elements=True)

        gen.startDocument()
        gen.startElement("doc", {})
        gen.characters("huhei")
        gen.endElement("doc")
        gen.endDocument()

        self.assertEqual(result.getvalue(), self.xml("<doc>huhei</doc>"))
Example #27
0
def test_xmlgen_escaped_content():
    result = StringIO()
    gen = XMLGenerator(result)

    gen.startDocument()
    gen.startElement("doc", {})
    gen.characters(unicode("\xa0\\u3042", "unicode-escape"))
    gen.endElement("doc")
    gen.endDocument()

    return result.getvalue() == start + "<doc>\xa0&#12354;</doc>"
Example #28
0
    def test_xmlgen_content(self):
        result = StringIO()
        gen = XMLGenerator(result)

        gen.startDocument()
        gen.startElement("doc", {})
        gen.characters("huhei")
        gen.endElement("doc")
        gen.endDocument()

        self.assertEqual(result.getvalue(), start + "<doc>huhei</doc>")
Example #29
0
    def test_xmlgen_ignorable(self):
        result = self.ioclass()
        gen = XMLGenerator(result)

        gen.startDocument()
        gen.startElement("doc", {})
        gen.ignorableWhitespace(" ")
        gen.endElement("doc")
        gen.endDocument()

        self.assertEqual(result.getvalue(), self.xml("<doc> </doc>"))
Example #30
0
    def test_xmlgen_ignorable_empty(self):
        result = StringIO()
        gen = XMLGenerator(result, short_empty_elements=True)

        gen.startDocument()
        gen.startElement("doc", {})
        gen.ignorableWhitespace(" ")
        gen.endElement("doc")
        gen.endDocument()

        self.assertEqual(result.getvalue(), start + "<doc> </doc>")
Example #31
0
def convert_from_qs_to_xml_file(qsfile, xmlfile=None):
 if(check_if_string(qsfile) and re.findall("^(http|https)\:\/\/", qsfile)):
  qsheaders = {'User-Agent': useragent_string};
  tree = urlparse.parse_qs(urllib2.urlopen(urllib2.Request(qsfile, None, qsheaders)).read());
 else:
  if(check_if_string(qsfile)):
   qsfile = open(qsfile, "r");
  qsfile.seek(0);
  tree = urlparse.parse_qs(qsfile.read());
  qsfile.close();
 bctree = tree;
 bctreeln = len(bctree);
 if(len(bctree['type'])<len(bctree['code']) or len(bctree['type'])==len(bctree['code'])):
  bctreeln = len(bctree['type']);
 if(len(bctree['code'])<len(bctree['type'])):
  bctreeln = len(bctree['code']);
 bctreect = 0;
 bcdrawlist = [];
 xmlout=StringIO();
 upcxml=XMLGenerator(xmlout, "utf-8");
 upcxml.startDocument();
 upcxml.startElement("barcodes", {});
 upcxml.characters("\n");
 while(bctreect < bctreeln):
  qsbarcode = {}
  qsbarcode.update({"type": bctree['type'][bctreect], "code": bctree['code'][bctreect]});
  try:
   qsbarcode.update({"file": bctree['file'][bctreect]});
  except KeyError:
   pass;
  except IndexError:
   pass;
  try:
   qsbarcode.update({"size": bctree['size'][bctreect]});
  except KeyError:
   pass;
  except IndexError:
   pass;
  try:
   qsbarcode.update({"hideinfo": bctree['hideinfo'][bctreect]});
  except KeyError:
   pass;
  except IndexError:
   pass;
  try:
   qsbarcode.update({"barheight": bctree['barheight'][bctreect]});
  except KeyError:
   pass;
  except IndexError:
   pass;
  try:
   qsbarcode.update({"textxy": bctree['textxy'][bctreect]});
  except KeyError:
   pass;
  except IndexError:
   pass;
  try:
   qsbarcode.update({"color": bctree['color'][bctreect]});
  except KeyError:
   pass;
  except IndexError:
   pass;
  upcxml.characters(" ");
  upcxml.startElement("barcode", qsbarcode);
  upcxml.endElement("barcode");
  upcxml.characters("\n");
  bctreect = bctreect + 1;
 upcxml.endElement("barcodes");
 upcxml.endDocument();
 xmlout.seek(0);
 if(xmlfile is not None):
  xmlofile = open(xmlfile, "w+");
  xmlofile.write(xmlout.read());
  xmlofile.close();
  return True;
 if(xmlfile is None):
  return xmlout.read();
Example #32
0
 def startElement(self, name, attrs):
     XMLGenerator.startElement(self, name, attrs)
     self.context.append((name, attrs))
Example #33
0
class SeqXmlWriter(SequenceWriter):
    """Writes SeqRecords into seqXML file.

    SeqXML requires the sequence alphabet be explicitly RNA, DNA or protein,
    i.e. an instance or subclass of Bio.Alphabet.RNAAlphabet,
    Bio.Alphabet.DNAAlphabet or Bio.Alphabet.ProteinAlphabet.
    """
    def __init__(self,
                 target,
                 source=None,
                 source_version=None,
                 species=None,
                 ncbiTaxId=None):
        """Create Object and start the xml generator.

        Arguments:
         - target - Output stream opened in binary mode, or a path to a file.
         - source - The source program/database of the file, for example
           UniProt.
         - source_version - The version or release number of the source
           program or database from which the data originated.
         - species - The scientific name of the species of origin of all
           entries in the file.
         - ncbiTaxId - The NCBI taxonomy identifier of the species of origin.

        """
        super().__init__(target, "wb")
        handle = self.handle
        self.xml_generator = XMLGenerator(handle, "utf-8")
        self.xml_generator.startDocument()
        self.source = source
        self.source_version = source_version
        self.species = species
        self.ncbiTaxId = ncbiTaxId

    def write_header(self):
        """Write root node with document metadata."""
        attrs = {
            "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
            "xsi:noNamespaceSchemaLocation":
            "http://www.seqxml.org/0.4/seqxml.xsd",
            "seqXMLversion": "0.4",
        }

        if self.source is not None:
            attrs["source"] = self.source
        if self.source_version is not None:
            attrs["sourceVersion"] = self.source_version
        if self.species is not None:
            if not isinstance(self.species, str):
                raise TypeError("species should be of type string")
            attrs["speciesName"] = self.species
        if self.ncbiTaxId is not None:
            if not isinstance(self.ncbiTaxId, (str, int)):
                raise TypeError("ncbiTaxID should be of type string or int")
            attrs["ncbiTaxID"] = self.ncbiTaxId

        self.xml_generator.startElement("seqXML", AttributesImpl(attrs))

    def write_record(self, record):
        """Write one record."""
        if not record.id or record.id == "<unknown id>":
            raise ValueError("SeqXML requires identifier")

        if not isinstance(record.id, str):
            raise TypeError("Identifier should be of type string")

        attrb = {"id": record.id}

        if ("source" in record.annotations
                and self.source != record.annotations["source"]):
            if not isinstance(record.annotations["source"], str):
                raise TypeError("source should be of type string")
            attrb["source"] = record.annotations["source"]

        self.xml_generator.startElement("entry", AttributesImpl(attrb))
        self._write_species(record)
        self._write_description(record)
        self._write_seq(record)
        self._write_dbxrefs(record)
        self._write_properties(record)
        self.xml_generator.endElement("entry")

    def write_footer(self):
        """Close the root node and finish the XML document."""
        self.xml_generator.endElement("seqXML")
        self.xml_generator.endDocument()

    def _write_species(self, record):
        """Write the species if given (PRIVATE)."""
        local_ncbi_taxid = None
        if "ncbi_taxid" in record.annotations:
            local_ncbi_taxid = record.annotations["ncbi_taxid"]
            if isinstance(local_ncbi_taxid, list):
                # SwissProt parser uses a list (which could cope with chimeras)
                if len(local_ncbi_taxid) == 1:
                    local_ncbi_taxid = local_ncbi_taxid[0]
                elif len(local_ncbi_taxid) == 0:
                    local_ncbi_taxid = None
                else:
                    ValueError(
                        "Multiple entries for record.annotations['ncbi_taxid'], %r"
                        % local_ncbi_taxid)
        if "organism" in record.annotations and local_ncbi_taxid:
            local_org = record.annotations["organism"]

            if not isinstance(local_org, str):
                raise TypeError("organism should be of type string")

            if not isinstance(local_ncbi_taxid, (str, int)):
                raise TypeError("ncbiTaxID should be of type string or int")

            # The local species definition is only written if it differs from the global species definition
            if local_org != self.species or local_ncbi_taxid != self.ncbiTaxId:

                attr = {"name": local_org, "ncbiTaxID": str(local_ncbi_taxid)}
                self.xml_generator.startElement("species",
                                                AttributesImpl(attr))
                self.xml_generator.endElement("species")

    def _write_description(self, record):
        """Write the description if given (PRIVATE)."""
        if record.description:

            if not isinstance(record.description, str):
                raise TypeError("Description should be of type string")

            description = record.description
            if description == "<unknown description>":
                description = ""

            if len(record.description) > 0:
                self.xml_generator.startElement("description",
                                                AttributesImpl({}))
                self.xml_generator.characters(description)
                self.xml_generator.endElement("description")

    def _write_seq(self, record):
        """Write the sequence (PRIVATE).

        Note that SeqXML requires a DNA, RNA or protein alphabet.
        """
        if isinstance(record.seq, UnknownSeq):
            raise TypeError(
                "Sequence type is UnknownSeq but SeqXML requires sequence")

        seq = str(record.seq)

        if not len(seq) > 0:
            raise ValueError("The sequence length should be greater than 0")

        # Get the base alphabet (underneath any Gapped or StopCodon encoding)
        alpha = Alphabet._get_base_alphabet(record.seq.alphabet)
        if isinstance(alpha, Alphabet.RNAAlphabet):
            seqElem = "RNAseq"
        elif isinstance(alpha, Alphabet.DNAAlphabet):
            seqElem = "DNAseq"
        elif isinstance(alpha, Alphabet.ProteinAlphabet):
            seqElem = "AAseq"
        else:
            raise ValueError("Need a DNA, RNA or Protein alphabet")

        self.xml_generator.startElement(seqElem, AttributesImpl({}))
        self.xml_generator.characters(seq)
        self.xml_generator.endElement(seqElem)

    def _write_dbxrefs(self, record):
        """Write all database cross references (PRIVATE)."""
        if record.dbxrefs is not None:

            for dbxref in record.dbxrefs:

                if not isinstance(dbxref, str):
                    raise TypeError("dbxrefs should be of type list of string")
                if dbxref.find(":") < 1:
                    raise ValueError(
                        "dbxrefs should be in the form ['source:id', 'source:id' ]"
                    )

                dbsource, dbid = dbxref.split(":", 1)

                attr = {"source": dbsource, "id": dbid}
                self.xml_generator.startElement("DBRef", AttributesImpl(attr))
                self.xml_generator.endElement("DBRef")

    def _write_properties(self, record):
        """Write all annotations that are key value pairs with values of a primitive type or list of primitive types (PRIVATE)."""
        for key, value in record.annotations.items():

            if key not in ("organism", "ncbi_taxid", "source"):

                if value is None:

                    attr = {"name": key}
                    self.xml_generator.startElement("property",
                                                    AttributesImpl(attr))
                    self.xml_generator.endElement("property")

                elif isinstance(value, list):

                    for v in value:
                        if isinstance(value, (int, float, str)):
                            attr = {"name": key, "value": v}
                            self.xml_generator.startElement(
                                "property", AttributesImpl(attr))
                            self.xml_generator.endElement("property")

                elif isinstance(value, (int, float, str)):

                    attr = {"name": key, "value": str(value)}
                    self.xml_generator.startElement("property",
                                                    AttributesImpl(attr))
                    self.xml_generator.endElement("property")
Example #34
0
 def begin(self, digester, **attributes):
     '''
     @see: Rule.begin
     '''
     assert isinstance(digester, DigesterXMLUpdate), 'Invalid digester %s' % digester
     XMLGenerator.startElement(digester, digester.currentName(), attributes)
Example #35
0
    def store(self):
        self.id = self.userid
        self.version = STORAGE_FORMAT

        log.debug("storing User " + self.id)
        try:
            outf = TransactionalFile(
                User.determineFileName(self.getStoragePath()))
            gen = XMLGenerator(out=outf, encoding="UTF-8")
            gen.startDocument()
            gen.startElement("user", {})
            gen.characters("\n")
            for attr in self.__slots__ + ['userid', 'name', 'password']:
                if attr == "password":
                    gen.startElement("passwordhash", {})
                    gen.characters(self.password.encode("hex"))
                    gen.endElement("passwordhash")
                    gen.characters("\n")
                elif attr == "links":
                    gen.startElement("links", {})
                    gen.characters("\n")
                    for (name, url) in self.links.items():
                        gen.startElement("link", {
                            "name": name,
                            "url": url
                        })
                        gen.endElement("link")
                        gen.characters("\n")
                    gen.endElement("links")
                    gen.characters("\n")
                elif attr == "displaystrings":
                    gen.startElement("displaystrings", {})
                    gen.characters("\n")
                    for (name, string) in self.displaystrings.items():
                        gen.startElement("string", {
                            "name": name,
                            "value": (string or u"")
                        })
                        gen.endElement("string")
                        gen.characters("\n")
                    gen.endElement("displaystrings")
                    gen.characters("\n")
                elif attr == "categories":
                    gen.startElement("categories", {})
                    gen.characters("\n")
                    for cat in self.categories.values():
                        cat.store()
                        cat.putInXML(gen)
                    gen.endElement("categories")
                    gen.characters("\n")
                else:
                    # other attribute
                    gen.startElement(attr, {})
                    gen.characters(getXMLvalue(self, attr))
                    gen.endElement(attr)
                    gen.characters('\n')

            gen.endElement("user")
            gen.endDocument()
            outf.commit()
            os.chmod(outf.name, FILEPROT)
        except EnvironmentError, x:
            raise StorageError(str(x))
Example #36
0
def write_2(glos, filename):
    from xml.sax.saxutils import XMLGenerator
    from xml.sax.xmlreader import AttributesNSImpl
    xdbFp = open(filename, 'wb')
    fp = XMLGenerator(xdbFp, 'utf-8')
    attrs = AttributesNSImpl({}, {})
    fp.startElement(u'xfardic', attrs)
    for t in glos.info:
        fp.startElement(unicode(t[0]), attrs)
        fp.characters(unicode(t[1]))
        fp.endElement(unicode(t[0]))
    fp.endElement(u'xfardic')
    fp.startElement(u'words', attrs)
    for item in glos.data:
        try:
            tmpXmlFile.characters(item[1])
        except:
            myRaise(__file__)
            printAsError('While writing xdb file, an error on word "%s"' %
                         item[0])
            continue
        fp.startElement(u'word', attrs)
        fp.startElement(u'in', attrs)
        fp.characters(unicode(item[0]))
        fp.endElement(u'in')
        fp.startElement(u'out', attrs)
        fp.characters(unicode(item[1]))
        fp.endElement(u'out')
    fp.endElement(u'words')
    fp.endDocument()
    xdbFp.close()
Example #37
0
    def end_test(self, result):
        """Called after each test."""
        failures = errors = skipped = disabled = 0
        if result.eofs == 'O':
            pass
        elif result.eofs == 'S':
            skipped = 1
        elif result.eofs == 'F':
            failures = 1
        elif result.eofs == 'E':
            errors = 1
        else:
            errors = 1
        classname = encode(result.case.uid.replace("/", "."))
        if classname.endswith('.py'):
            classname = classname[:-3]

        filename = os.path.join(self.outdir, '%s.xml' % (result.case.uid, ))
        dirname = os.path.dirname(filename)
        try:
            os.makedirs(dirname)
        except OSError as ex:
            if ex.errno != errno.EEXIST:
                raise
        f_report = open(filename, 'w')
        try:
            xml = XMLGenerator(f_report, encoding='utf-8')
            xml.startDocument()
            xml.startElement(
                'testsuite', {
                    'name': classname,
                    'tests': '%d' % (1, ),
                    'failures': '%d' % (failures, ),
                    'errors': '%d' % (errors, ),
                    'time': '%0.3f' % (result.duration / 1000.0, ),
                    'disabled': '%d' % (disabled, ),
                    'skipped': '%d' % (skipped, ),
                    'timestamp': self.now.isoformat(),
                    'hostname': os.uname()[1],
                })

            xml.startElement('properties', {})
            xml.startElement('property', {
                'name': 'hostname',
                'value': result.environment.hostname,
            })
            xml.endElement('property')
            xml.startElement('property', {
                'name': 'architecture',
                'value': result.environment.architecture,
            })
            xml.endElement('property')
            xml.startElement('property', {
                'name': 'role',
                'value': result.environment.role,
            })
            xml.endElement('property')
            xml.startElement(
                'property', {
                    'name': 'version',
                    'value': '%s' % (result.environment.ucs_version, ),
                })
            xml.endElement('property')
            if result.case.description:
                xml.startElement(
                    'property', {
                        'name':
                        'description',
                        'value':
                        encode(result.case.description or result.case.uid),
                    })
                xml.endElement('property')
            xml.endElement('properties')

            xml.startElement(
                'testcase',
                {
                    'name': 'test',
                    # 'assertions': '%d' % (0,),
                    'time': '%0.3f' % (result.duration / 1000.0, ),
                    'classname': classname,
                    # 'status': '???',
                })

            if skipped:
                try:
                    mime, content = result.artifacts['check']
                except KeyError:
                    msg = ''
                else:
                    msg = '\n'.join(['%s' % (c, ) for c in content])
                xml.startElement('skipped', {
                    'message': msg,
                })
                xml.endElement('skipped')
            elif errors:
                xml.startElement('error', {
                    'type': 'TestError',
                    'message': '%s' % (result.result, ),
                })
                xml.endElement('error')
            elif failures:
                msg = TestCodes.MESSAGE.get(result.reason, '')
                xml.startElement('failure', {
                    'type': 'TestFailure',
                    'message': msg,
                })
                xml.endElement('failure')

            try:
                mime, content = result.artifacts['stdout']
            except KeyError:
                pass
            else:
                xml.startElement('system-out', {})
                xml.characters(self.utf8(content))
                xml.endElement('system-out')

            try:
                mime, content = result.artifacts['stderr']
            except KeyError:
                pass
            else:
                xml.startElement('system-err', {})
                xml.characters(self.utf8(content))
                xml.endElement('system-err')

            xml.endElement('testcase')
            xml.endElement('testsuite')
            xml.endDocument()
        finally:
            f_report.close()
        super(Junit, self).end_test(result)
Example #38
0
    class XmlAnnotationWriter:
        def __init__(self, file):
            self.version = "1.1"
            self.file = file
            self.xmlgen = XMLGenerator(self.file, 'utf-8')
            self._level = 0

        def _indent(self, newline=True):
            if newline:
                self.xmlgen.ignorableWhitespace("\n")
            self.xmlgen.ignorableWhitespace("  " * self._level)

        def _add_version(self):
            self._indent()
            self.xmlgen.startElement("version", {})
            self.xmlgen.characters(self.version)
            self.xmlgen.endElement("version")

        def open_document(self):
            self.xmlgen.startDocument()

        def open_root(self):
            self.xmlgen.startElement("annotations", {})
            self._level += 1
            self._add_version()

        def _add_meta(self, meta):
            self._level += 1
            for k, v in meta.items():
                if isinstance(v, OrderedDict):
                    self._indent()
                    self.xmlgen.startElement(k, {})
                    self._add_meta(v)
                    self._indent()
                    self.xmlgen.endElement(k)
                elif isinstance(v, list):
                    self._indent()
                    self.xmlgen.startElement(k, {})
                    for tup in v:
                        self._add_meta(OrderedDict([tup]))
                    self._indent()
                    self.xmlgen.endElement(k)
                else:
                    self._indent()
                    self.xmlgen.startElement(k, {})
                    self.xmlgen.characters(v)
                    self.xmlgen.endElement(k)
            self._level -= 1

        def add_meta(self, meta):
            self._indent()
            self.xmlgen.startElement("meta", {})
            self._add_meta(meta)
            self._indent()
            self.xmlgen.endElement("meta")

        def open_track(self, track):
            self._indent()
            self.xmlgen.startElement("track", track)
            self._level += 1

        def open_image(self, image):
            self._indent()
            self.xmlgen.startElement("image", image)
            self._level += 1

        def open_box(self, box):
            self._indent()
            self.xmlgen.startElement("box", box)
            self._level += 1

        def open_polygon(self, polygon):
            self._indent()
            self.xmlgen.startElement("polygon", polygon)
            self._level += 1

        def open_polyline(self, polyline):
            self._indent()
            self.xmlgen.startElement("polyline", polyline)
            self._level += 1

        def open_points(self, points):
            self._indent()
            self.xmlgen.startElement("points", points)
            self._level += 1

        def open_cuboid(self, cuboid):
            self._indent()
            self.xmlgen.startElement("cuboid", cuboid)
            self._level += 1

        def open_tag(self, tag):
            self._indent()
            self.xmlgen.startElement("tag", tag)
            self._level += 1

        def add_attribute(self, attribute):
            self._indent()
            self.xmlgen.startElement("attribute", {"name": attribute["name"]})
            self.xmlgen.characters(attribute["value"])
            self.xmlgen.endElement("attribute")

        def close_box(self):
            self._level -= 1
            self._indent()
            self.xmlgen.endElement("box")

        def close_polygon(self):
            self._level -= 1
            self._indent()
            self.xmlgen.endElement("polygon")

        def close_polyline(self):
            self._level -= 1
            self._indent()
            self.xmlgen.endElement("polyline")

        def close_points(self):
            self._level -= 1
            self._indent()
            self.xmlgen.endElement("points")

        def close_cuboid(self):
            self._level -= 1
            self._indent()
            self.xmlgen.endElement("cuboid")

        def close_tag(self):
            self._level -= 1
            self._indent()
            self.xmlgen.endElement("tag")

        def close_image(self):
            self._level -= 1
            self._indent()
            self.xmlgen.endElement("image")

        def close_track(self):
            self._level -= 1
            self._indent()
            self.xmlgen.endElement("track")

        def close_root(self):
            self._level -= 1
            self._indent()
            self.xmlgen.endElement("annotations")
            self._indent()

        def close_document(self):
            self.xmlgen.endDocument()
Example #39
0
class XMLEncoderAdapter(object):
    """Adapts a WebAPIEncoder to output XML.

    This takes an existing encoder and adapts it to output a simple XML format.
    """
    def __init__(self, encoder, *args, **kwargs):
        self.encoder = encoder

    def encode(self, o, *args, **kwargs):
        self.level = 0
        self.doIndent = False

        stream = StringIO()
        self.xml = XMLGenerator(stream, settings.DEFAULT_CHARSET)
        self.xml.startDocument()
        self.startElement("rsp")
        self.__encode(o, *args, **kwargs)
        self.endElement("rsp")
        self.xml.endDocument()
        self.xml = None

        return stream.getvalue()

    def __encode(self, o, *args, **kwargs):
        if isinstance(o, dict):
            for key, value in six.iteritems(o):
                attrs = {}

                if isinstance(key, six.integer_types):
                    attrs['value'] = str(key)
                    key = 'int'

                self.startElement(key, attrs)
                self.__encode(value, *args, **kwargs)
                self.endElement(key)
        elif isinstance(o, (tuple, list)):
            self.startElement("array")

            for i in o:
                self.startElement("item")
                self.__encode(i, *args, **kwargs)
                self.endElement("item")

            self.endElement("array")
        elif isinstance(o, six.string_types):
            self.text(o)
        elif isinstance(o, six.integer_types):
            self.text("%d" % o)
        elif isinstance(o, bool):
            if o:
                self.text("True")
            else:
                self.text("False")
        elif o is None:
            pass
        else:
            result = self.encoder.encode(o, *args, **kwargs)

            if result is None:
                raise TypeError("%r is not XML serializable" % (o,))

            return self.__encode(result, *args, **kwargs)

    def startElement(self, name, attrs={}):
        self.addIndent()
        self.xml.startElement(name, attrs)
        self.level += 1
        self.doIndent = True

    def endElement(self, name):
        self.level -= 1
        self.addIndent()
        self.xml.endElement(name)
        self.doIndent = True

    def text(self, value):
        self.xml.characters(value)
        self.doIndent = False

    def addIndent(self):
        if self.doIndent:
            self.xml.ignorableWhitespace('\n' + ' ' * self.level)
Example #40
0
class SeqXmlWriter(SequentialSequenceWriter):
    """Writes SeqRecords into seqXML file.

    SeqXML requires the sequence alphabet be explicitly RNA, DNA or protein,
    i.e. an instance or subclass of Bio.Alphapet.RNAAlphabet,
    Bio.Alphapet.DNAAlphabet or Bio.Alphapet.ProteinAlphabet.
    """
    def __init__(self,
                 handle,
                 source=None,
                 source_version=None,
                 species=None,
                 ncbiTaxId=None):
        """Create Object and start the xml generator."""

        SequentialSequenceWriter.__init__(self, handle)

        self.xml_generator = XMLGenerator(handle, "utf-8")
        self.xml_generator.startDocument()
        self.source = source
        self.source_version = source_version
        self.species = species
        self.ncbiTaxId = ncbiTaxId

    def write_header(self):
        """Write root node with document metadata."""
        SequentialSequenceWriter.write_header(self)

        attrs = {
            "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
            "xsi:noNamespaceSchemaLocation":
            "http://www.seqxml.org/0.4/seqxml.xsd",
            "seqXMLversion": "0.4"
        }

        if self.source is not None:
            attrs["source"] = self.source
        if self.source_version is not None:
            attrs["sourceVersion"] = self.source_ersion
        if self.species is not None:
            if not isinstance(species, basestring):
                raise TypeError("species should be of type string")
            attrs["speciesName"] = self.species
        if self.ncbiTaxId is not None:
            if not isinstance(self.ncbiTaxId, (basestring, int)):
                raise TypeError("ncbiTaxID should be of type string or int")
            attrs["ncbiTaxID"] = self.ncbiTaxId

        self.xml_generator.startElement("seqXML", AttributesImpl(attrs))

    def write_record(self, record):
        """Write one record."""

        if not record.id or record.id == "<unknown id>":
            raise ValueError("SeqXML requires identifier")

        if not isinstance(record.id, basestring):
            raise TypeError("Identifier should be of type string")

        attrb = {"id": record.id}

        if "source" in record.annotations and self.source != record.annotations[
                "source"]:
            if not isinstance(record.annotations["source"], basestring):
                raise TypeError("source should be of type string")
            attrb["source"] = record.annotations["source"]

        self.xml_generator.startElement("entry", AttributesImpl(attrb))
        self._write_species(record)
        self._write_description(record)
        self._write_seq(record)
        self._write_dbxrefs(record)
        self._write_properties(record)
        self.xml_generator.endElement("entry")

    def write_footer(self):
        """Close the root node and finish the XML document."""

        SequentialSequenceWriter.write_footer(self)

        self.xml_generator.endElement("seqXML")
        self.xml_generator.endDocument()

    def _write_species(self, record):
        """Write the species if given."""

        if "organism" in record.annotations and "ncbi_taxid" in record.annotations:

            if not isinstance(record.annotations["organism"], basestring):
                raise TypeError("organism should be of type string")

            if not isinstance(record.annotations["ncbi_taxid"],
                              (basestring, int)):
                raise TypeError("ncbiTaxID should be of type string or int")

            #The local species definition is only written if it differs from the global species definition
            if record.annotations[
                    "organism"] != self.species or record.annotations[
                        "ncbi_taxid"] != self.ncbiTaxId:

                attr = {
                    "name": record.annotations["organism"],
                    "ncbiTaxID": record.annotations["ncbi_taxid"]
                }
                self.xml_generator.startElement("species",
                                                AttributesImpl(attr))
                self.xml_generator.endElement("species")

    def _write_description(self, record):
        """Write the description if given."""

        if record.description:

            if not isinstance(record.description, basestring):
                raise TypeError("Description should be of type string")

            description = record.description
            if description == "<unknown description>":
                description = ""

            if len(record.description) > 0:
                self.xml_generator.startElement("description",
                                                AttributesImpl({}))
                self.xml_generator.characters(description)
                self.xml_generator.endElement("description")

    def _write_seq(self, record):
        """Write the sequence.

        Note that SeqXML requires a DNA, RNA or protein alphabet.
        """

        if isinstance(record.seq, UnknownSeq):
            raise TypeError(
                "Sequence type is UnknownSeq but SeqXML requires sequence")

        seq = str(record.seq)

        if not len(seq) > 0:
            raise ValueError("The sequence length should be greater than 0")

        #Get the base alphabet (underneath any Gapped or StopCodon encoding)
        alpha = Alphabet._get_base_alphabet(record.seq.alphabet)
        if isinstance(alpha, Alphabet.RNAAlphabet):
            seqElem = "RNAseq"
        elif isinstance(alpha, Alphabet.DNAAlphabet):
            seqElem = "DNAseq"
        elif isinstance(alpha, Alphabet.ProteinAlphabet):
            seqElem = "AAseq"
        else:
            raise ValueError("Need a DNA, RNA or Protein alphabet")

        self.xml_generator.startElement(seqElem, AttributesImpl({}))
        self.xml_generator.characters(seq)
        self.xml_generator.endElement(seqElem)

    def _write_dbxrefs(self, record):
        """Write all database cross references."""
        if record.dbxrefs is not None:

            for dbxref in record.dbxrefs:

                if not isinstance(dbxref, basestring):
                    raise TypeError("dbxrefs should be of type list of string")
                if dbxref.find(':') < 1:
                    raise ValueError(
                        "dbxrefs should be in the form ['source:id', 'source:id' ]"
                    )

                dbsource, dbid = dbxref.split(':', 1)

                attr = {"source": dbsource, "id": dbid}
                self.xml_generator.startElement("DBRef", AttributesImpl(attr))
                self.xml_generator.endElement("DBRef")

    def _write_properties(self, record):
        """Write all annotations that are key value pairs with values of a primitive type or list of primitive types."""

        for key, value in record.annotations.items():

            if key not in ("organism", "ncbi_taxid", "source"):

                if value is None:

                    attr = {"name": key}
                    self.xml_generator.startElement("property",
                                                    AttributesImpl(attr))
                    self.xml_generator.endElement("property")

                elif isinstance(value, list):

                    for v in value:
                        if isinstance(value, (int, float, basestring)):
                            attr = {"name": key, "value": v}
                            self.xml_generator.startElement(
                                "property", AttributesImpl(attr))
                            self.xml_generator.endElement("property")

                elif isinstance(value, (int, float, basestring)):

                    attr = {"name": key, "value": str(value)}
                    self.xml_generator.startElement("property",
                                                    AttributesImpl(attr))
                    self.xml_generator.endElement("property")
Example #41
0
File: asap.py Project: xielm12/QUIP
def param_to_xml(params, encoding='iso-8859-1'):
    from xml.sax.saxutils import XMLGenerator
    from StringIO import StringIO
    output = StringIO()
    xml = XMLGenerator(output, encoding)
    xml.startDocument()

    xml.startElement(
        'TS_params', {
            'cutoff': ' '.join([str(x) for x in params['rcut']]),
            'n_types': str(params['nspecies']),
            'betapol': str(params['betapol']),
            'maxipol': str(params['maxipol']),
            'tolpol': str(params['tolpol']),
            'pred_order': str(params['pred_order']),
            'yukalpha': str(params['yukalpha']),
            'yuksmoothlength': str(params['yuksmoothlength']),
            'tewald': params['tewald'] and 'T' or 'F',
            'raggio': str(params['raggio']),
            'a_ew': str(params['a_ew']),
            'gcut': str(params['gcut']),
            'iesr': ' '.join([str(x) for x in params.get('iesr', [0, 0, 0])])
        })
    ti_tj_to_index = {}
    n = 0
    for ti in range(params['nspecies']):
        for tj in range(params['nspecies']):
            if tj > ti: continue
            ti_tj_to_index[(ti, tj)] = n
            n += 1

    for ti in range(params['nspecies']):
        zi = atomic_number(params['species'][ti])
        xml.startElement(
            'per_type_data', {
                'type': str(ti + 1),
                'atomic_num': str(zi),
                'pol': str(params['pol'][ti]),
                'z': str(params['z'][ti])
            })
        xml.endElement('per_type_data')

        for tj in range(params['nspecies']):
            if tj > ti: continue
            idx = ti_tj_to_index[(ti, tj)]
            zj = atomic_number(params['species'][tj])
            xml.startElement(
                'per_pair_data', {
                    'atnum_i': str(zi),
                    'atnum_j': str(zj),
                    'D_ms': str(params['d_ms'][idx]),
                    'gamma_ms': str(params['gamma_ms'][idx]),
                    'R_ms': str(params['r_ms'][idx]),
                    'B_pol': str(params['bpol'][idx]),
                    'C_pol': str(params['cpol'][idx]),
                })
            xml.endElement('per_pair_data')

    xml.endElement('TS_params')
    xml.endDocument()
    return output.getvalue()
class SequenceWriter(object):
    def __init__(self, fname, sequence, strict, report_recv, report_sent, \
                 loopcnt, inter):

        self.fname = fname
        self.seq = sequence

        self.attr_loopcnt = loopcnt
        self.attr_inter = inter

        self.attr_strict = strict
        self.attr_recv = report_recv
        self.attr_sent = report_sent

    def save(self):
        for i in self.save_async():
            pass

    def save_async(self):
        output = open(self.fname, 'w')

        self.depth_idx = 0
        self.writer = XMLGenerator(output, 'utf-8')

        self.writer.startDocument()

        attr_vals = {
            'report_recv': (self.attr_recv) and '1' or '0',
            'report_sent': (self.attr_sent) and '1' or '0',
            'strict': (self.attr_strict) and '1' or '0'
        }

        if isinstance(self.attr_loopcnt, int):
            attr_vals['loopcnt'] = str(self.attr_loopcnt)
        if isinstance(self.attr_inter, (float, int)):
            attr_vals['inter'] = str(self.attr_inter)

        attrs = AttributesImpl(attr_vals)

        self.startElement('PMScapySequence', attrs)

        self.current_node = None

        idx = 0
        slen = float(len(self.seq))

        for node in self.seq.get_children():
            self.current_node = node
            self.write_node(node)

            idx += 1
            yield idx, (idx / slen) * 100.0, output.tell()

        self.current_node = None

        self.endElement('PMScapySequence')

        self.writer.endDocument()
        output.close()

    def writeSpaces(self, prepend='', append=''):
        idx = max(self.depth_idx - 1, 0)
        txt = '%s%s%s' % (prepend, '  ' * idx, append)

        if txt:
            self.writer.characters(txt)

    def startElement(self, name, attrs, indent=True):
        if indent:
            self.writer.characters('\n')

        self.depth_idx += 1

        self.writeSpaces()
        self.writer.startElement(name, attrs)

    def endElement(self, name, indent=True):
        if indent:
            self.writeSpaces('\n')

        self.writer.endElement(name)

        self.depth_idx -= 1

    def write_node(self, node):
        self.start_xml_node(node.get_data())

        for child_node in node.get_children():
            self.current_node = child_node
            self.write_node(child_node)

        self.end_xml_node()

    def start_xml_node(self, seq_packet):
        inter = seq_packet.inter
        filter = seq_packet.filter

        attr_vals = {'interval': str(inter), 'filter': filter or ''}

        attrs = AttributesImpl(attr_vals)
        self.startElement('SequencePacket', attrs)

        self.start_xml_packet(seq_packet.packet)

    def end_xml_node(self):
        self.endElement('SequencePacket')

    def start_xml_packet(self, metapacket):
        protocols = metapacket.get_protocols()

        for proto in protocols:
            attr_vals = {
                'id': get_proto_name(proto),
                'time': "%.6f" % proto.time
            }

            attrs = AttributesImpl(attr_vals)
            self.startElement('proto', attrs)

            for field in get_proto_fields(proto):
                if is_default_field(proto, field):
                    continue

                name = get_field_name(field)
                value = get_field_value(proto, field)

                attr_vals = {'id': name}

                self.writer.characters('\n')

                attrs = AttributesImpl(attr_vals)
                self.startElement('field', attrs, False)
                self.writer.characters(b64encode(str(value)))
                self.endElement('field', False)

        for idx in xrange(len(protocols)):
            self.endElement('proto')
Example #43
0
class XmlAnnotationWriter:
    VERSION = '1.1'

    def __init__(self, f):
        self.xmlgen = XMLGenerator(f, 'utf-8')
        self._level = 0

    def _indent(self, newline=True):
        if newline:
            self.xmlgen.ignorableWhitespace('\n')
        self.xmlgen.ignorableWhitespace('  ' * self._level)

    def _add_version(self):
        self._indent()
        self.xmlgen.startElement('version', {})
        self.xmlgen.characters(self.VERSION)
        self.xmlgen.endElement('version')

    def open_root(self):
        self.xmlgen.startDocument()
        self.xmlgen.startElement('annotations', {})
        self._level += 1
        self._add_version()

    def _add_meta(self, meta):
        self._level += 1
        for k, v in meta.items():
            if isinstance(v, OrderedDict):
                self._indent()
                self.xmlgen.startElement(k, {})
                self._add_meta(v)
                self._indent()
                self.xmlgen.endElement(k)
            elif isinstance(v, list):
                self._indent()
                self.xmlgen.startElement(k, {})
                for tup in v:
                    self._add_meta(OrderedDict([tup]))
                self._indent()
                self.xmlgen.endElement(k)
            else:
                self._indent()
                self.xmlgen.startElement(k, {})
                self.xmlgen.characters(v)
                self.xmlgen.endElement(k)
        self._level -= 1

    def write_meta(self, meta):
        self._indent()
        self.xmlgen.startElement('meta', {})
        self._add_meta(meta)
        self._indent()
        self.xmlgen.endElement('meta')

    def open_track(self, track):
        self._indent()
        self.xmlgen.startElement('track', track)
        self._level += 1

    def open_image(self, image):
        self._indent()
        self.xmlgen.startElement('image', image)
        self._level += 1

    def open_box(self, box):
        self._indent()
        self.xmlgen.startElement('box', box)
        self._level += 1

    def open_polygon(self, polygon):
        self._indent()
        self.xmlgen.startElement('polygon', polygon)
        self._level += 1

    def open_polyline(self, polyline):
        self._indent()
        self.xmlgen.startElement('polyline', polyline)
        self._level += 1

    def open_points(self, points):
        self._indent()
        self.xmlgen.startElement('points', points)
        self._level += 1

    def open_tag(self, tag):
        self._indent()
        self.xmlgen.startElement("tag", tag)
        self._level += 1

    def add_attribute(self, attribute):
        self._indent()
        self.xmlgen.startElement('attribute', {'name': attribute['name']})
        self.xmlgen.characters(attribute['value'])
        self.xmlgen.endElement('attribute')

    def _close_element(self, element):
        self._level -= 1
        self._indent()
        self.xmlgen.endElement(element)

    def close_box(self):
        self._close_element('box')

    def close_polygon(self):
        self._close_element('polygon')

    def close_polyline(self):
        self._close_element('polyline')

    def close_points(self):
        self._close_element('points')

    def close_tag(self):
        self._close_element('tag')

    def close_image(self):
        self._close_element('image')

    def close_track(self):
        self._close_element('track')

    def close_root(self):
        self._close_element('annotations')
        self.xmlgen.endDocument()
Example #44
0
class DataCiteRenderer(object):

    scheme_uri_choices = {
        'ORCID': 'https:/orcid.org',
        'ROR': 'https://ror.org'
    }

    def __init__(self):
        self.stream = io.StringIO()
        self.xml = XMLGenerator(self.stream, 'utf-8')

    def render(self, data):
        self.data = data.datacite
        self.render_document()

        dom = parseString(self.stream.getvalue())
        return dom.toprettyxml(indent='    ')

    def render_node(self, tag, attrs, value):
        # remove None values from attrs
        attrs = dict((k, v) for k, v in attrs.items() if v)

        self.xml.startElement(tag, attrs)
        if value is not None:
            self.xml.characters(str(value))
        self.xml.endElement(tag)

    def render_document(self):
        self.xml.startDocument()
        self.render_resource()
        self.xml.endDocument()

    def render_resource(self):
        self.xml.startElement(
            'resource', {
                'xmlns:xsi':
                'http://www.w3.org/2001/XMLSchema-instance',
                'xmlns':
                'http://datacite.org/schema/kernel-4',
                'xsi:schemaLocation':
                'http://datacite.org/schema/kernel-4 http://schema.datacite.org/meta/kernel-4.3/metadata.xsd'
            })

        # identifier
        identifier = next(item for item in self.data.get('identifiers', [])
                          if item.get('identifierType') == 'DOI')
        self.render_node('identifier',
                         {'identifierType': identifier.get('identifierType')},
                         identifier.get('identifier'))

        # creators
        self.xml.startElement('creators', {})
        for creator in self.data.get('creators', []):
            self.xml.startElement('creator', {})

            self.render_node('creatorName', {'nameType': 'Personal'},
                             creator.get('name'))

            if creator.get('givenName'):
                self.render_node('givenName', {}, creator.get('givenName'))

            if creator.get('familyName'):
                self.render_node('familyName', {}, creator.get('familyName'))

            if creator.get('nameIdentifier'):
                scheme = creator.get('nameIdentifierScheme', 'ORCID')
                self.render_node(
                    'nameIdentifier', {
                        'nameIdentifierScheme': scheme,
                        'schemeURI': self.scheme_uri_choices.get(scheme)
                    }, creator.get('nameIdentifier'))

            for affiliation in creator.get('affiliations', []):
                if affiliation.get('affiliationIdentifier'):
                    affiliation_scheme = affiliation.get(
                        'affiliationIdentifierScheme', 'ROR')
                    affiliation_attrs = {
                        'affiliationIdentifier':
                        affiliation.get('affiliationIdentifier'),
                        'affiliationIdentifierScheme':
                        affiliation_scheme,
                        'schemeURI':
                        self.scheme_uri_choices.get(affiliation_scheme)
                    }
                else:
                    affiliation_attrs = {}

                self.render_node('affiliation', affiliation_attrs,
                                 affiliation.get('affiliation'))

            self.xml.endElement('creator')
        self.xml.endElement('creators')

        # titles
        self.xml.startElement('titles', {})
        for i, title in enumerate(self.data.get('titles', [])):
            if i == 0:
                self.render_node('title', {}, title.get('title'))
            else:
                self.render_node(
                    'title',
                    {'titleType': title.get('titleType', 'AlternativeTitle')},
                    title.get('title'))
        self.xml.endElement('titles')

        # publisher
        self.render_node('publisher', {}, self.data.get('publisher'))

        # publicationYear
        self.render_node('publicationYear', {},
                         self.data.get('publicationYear'))

        # subjects
        if 'subjects' in self.data:
            self.xml.startElement('subjects', {})
            for subject in self.data.get('subjects', []):
                self.render_node(
                    'subject', {
                        'subjectScheme': subject.get('subjectScheme'),
                        'schemeURI': subject.get('schemeURI'),
                        'valueURI': subject.get('valueURI')
                    }, subject.get('subject'))
            self.xml.endElement('subjects')

        # contributors
        if 'contributors' in self.data:
            self.xml.startElement('contributors', {})
            for contributor in self.data.get('contributors', []):
                self.xml.startElement(
                    'contributor',
                    {'contributorType': contributor.get('contributorType')})

                self.render_node(
                    'contributorName',
                    {'nameType': contributor.get('nameType', 'Personal')},
                    contributor.get('name'))

                if contributor.get('givenName'):
                    self.render_node('givenName', {},
                                     contributor.get('givenName'))

                if contributor.get('familyName'):
                    self.render_node('familyName', {},
                                     contributor.get('familyName'))

                if contributor.get('nameIdentifier'):
                    scheme = creator.get('nameIdentifierScheme', 'ORCID')
                    self.render_node(
                        'nameIdentifier', {
                            'nameIdentifierScheme': scheme,
                            'schemeURI': self.scheme_uri_choices.get(scheme)
                        }, contributor.get('nameIdentifier'))

                for affiliation in contributor.get('affiliations', []):
                    scheme = affiliation.get('affiliationIdentifierScheme',
                                             'ROR')
                    self.render_node(
                        'affiliation', {
                            'affiliationIdentifier':
                            affiliation.get('affiliationIdentifier'),
                            'affiliationIdentifierScheme':
                            scheme,
                            'schemeURI':
                            self.scheme_uri_choices.get(scheme)
                        }, affiliation.get('affiliation'))

                self.xml.endElement('contributor')
            self.xml.endElement('contributors')

        # dates
        if 'dates' in self.data:
            self.xml.startElement('dates', {})
            for date in self.data.get('dates', {}):
                self.render_node('date', {'dateType': date.get('dateType')},
                                 date.get('date'))
            self.xml.endElement('dates')

        # language
        self.render_node('language', {}, self.data.get('language', 'eng'))

        # resourceType
        self.render_node(
            'resourceType', {
                'resourceTypeGeneral':
                self.data.get('types', {}).get('resourceTypeGeneral',
                                               'Dataset')
            },
            self.data.get('types', {}).get('resourceType'))

        # alternateIdentifiers
        if 'alternateIdentifiers' in self.data:
            self.xml.startElement('alternateIdentifiers', {})
            for alternate_identifier in self.data.get('alternateIdentifiers',
                                                      []):
                self.render_node(
                    'alternateIdentifier', {
                        'alternateIdentifierType':
                        alternate_identifier.get('alternateIdentifierType')
                    }, alternate_identifier.get('alternateIdentifier'))
            self.xml.endElement('alternateIdentifiers')

        # relatedIdentifiers
        if 'relatedIdentifiers' in self.data:
            self.xml.startElement('relatedIdentifiers', {})
            for related_identifier in self.data.get('relatedIdentifiers', []):
                if related_identifier.get('relatedIdentifier'):
                    self.render_node(
                        'relatedIdentifier', {
                            'relatedIdentifierType':
                            related_identifier.get('relatedIdentifierType'),
                            'relationType':
                            related_identifier.get('relationType')
                        }, related_identifier.get('relatedIdentifier'))
            self.xml.endElement('relatedIdentifiers')

        # version
        if 'version' in self.data:
            self.render_node('version', {}, self.data.get('version'))

        # rightsList
        if 'rightsList' in self.data:
            self.xml.startElement('rightsList', {})
            for rights in self.data.get('rightsList', []):
                self.render_node('rights',
                                 {'rightsURI': rights.get('rightsURI')},
                                 rights.get('rights'))
            self.xml.endElement('rightsList')

        # descriptions
        if 'descriptions' in self.data:
            self.xml.startElement('descriptions', {})
            for description in self.data.get('descriptions', []):
                self.render_node(
                    'description', {
                        'descriptionType':
                        description.get('descriptionType', 'Abstract')
                    }, description.get('description'))
            self.xml.endElement('descriptions')

        # fundingReferences
        if 'fundingReferences' in self.data:
            self.xml.startElement('fundingReferences', {})
            for funding_reference in self.data.get('fundingReferences', []):
                self.xml.startElement('fundingReference', {})
                self.render_node('funderName', {},
                                 funding_reference.get('name'))

                if 'funderIdentifier' in funding_reference:
                    self.render_node(
                        'funderIdentifier', {
                            'funderIdentifierType':
                            affiliation.get('funderIdentifierType', 'ROR'),
                            'schemeURI':
                            affiliation.get('schemeURI', 'https://ror.org')
                        }, funding_reference.get('funderIdentifier'))

                if 'awardNumber' in funding_reference:
                    self.render_node(
                        'awardNumber',
                        {'awardURI': funding_reference.get('awardURI')},
                        funding_reference.get('awardNumber'))

                if 'awardTitle' in funding_reference:
                    self.render_node('awardTitle', {},
                                     funding_reference.get('awardTitle'))

                self.xml.endElement('fundingReference')
            self.xml.endElement('fundingReferences')

        self.xml.endElement('resource')
Example #45
0
 def startElement(self, name, attrs):
     XMLGenerator.startElement(self, name, attrs)
     self.context.append((name, attrs))
     if name == u'fileHeader':
         self.attrs = attrs
Example #46
0
class XmlItemExporter(BaseItemExporter):
    def __init__(self, file, **kwargs):
        self.item_element = kwargs.pop('item_element', 'item')
        self.root_element = kwargs.pop('root_element', 'items')
        self._configure(kwargs)
        if not self.encoding:
            self.encoding = 'utf-8'
        self.xg = XMLGenerator(file, encoding=self.encoding)

    def _beautify_newline(self, new_item=False):
        if self.indent is not None and (self.indent > 0 or new_item):
            self._xg_characters('\n')

    def _beautify_indent(self, depth=1):
        if self.indent:
            self._xg_characters(' ' * self.indent * depth)

    def start_exporting(self):
        self.xg.startDocument()
        self.xg.startElement(self.root_element, {})
        self._beautify_newline(new_item=True)

    def export_item(self, item):
        self._beautify_indent(depth=1)
        self.xg.startElement(self.item_element, {})
        self._beautify_newline()
        for name, value in self._get_serialized_fields(item, default_value=''):
            self._export_xml_field(name, value, depth=2)
        self._beautify_indent(depth=1)
        self.xg.endElement(self.item_element)
        self._beautify_newline(new_item=True)

    def finish_exporting(self):
        self.xg.endElement(self.root_element)
        self.xg.endDocument()

    def _export_xml_field(self, name, serialized_value, depth):
        self._beautify_indent(depth=depth)
        self.xg.startElement(name, {})
        if hasattr(serialized_value, 'items'):
            self._beautify_newline()
            for subname, value in serialized_value.items():
                self._export_xml_field(subname, value, depth=depth + 1)
            self._beautify_indent(depth=depth)
        elif is_listlike(serialized_value):
            self._beautify_newline()
            for value in serialized_value:
                self._export_xml_field('value', value, depth=depth + 1)
            self._beautify_indent(depth=depth)
        elif isinstance(serialized_value, six.text_type):
            self._xg_characters(serialized_value)
        else:
            self._xg_characters(str(serialized_value))
        self.xg.endElement(name)
        self._beautify_newline()

    # Workaround for https://bugs.python.org/issue17606
    # Before Python 2.7.4 xml.sax.saxutils required bytes;
    # since 2.7.4 it requires unicode. The bug is likely to be
    # fixed in 2.7.6, but 2.7.6 will still support unicode,
    # and Python 3.x will require unicode, so ">= 2.7.4" should be fine.
    if sys.version_info[:3] >= (2, 7, 4):

        def _xg_characters(self, serialized_value):
            if not isinstance(serialized_value, six.text_type):
                serialized_value = serialized_value.decode(self.encoding)
            return self.xg.characters(serialized_value)
    else:  # pragma: no cover

        def _xg_characters(self, serialized_value):
            return self.xg.characters(serialized_value)
        xg.startElement("note", {})
        xg.characters(glyph.note)
        xg.endElement("note")
    xg.endElement("glyph")


# KLUDGE for Awami:
newName = fl.font.file_name.replace(" ", "")
newName2 = newName.replace(".VFB", ".vfb")
outname = newName2.replace(".vfb", "_AP.xml")
if outname[25:31] == "AWAMIN" or outname[25:28] == "AWB":
    outname = "AwamiNastaliqRegular_AP.xml"
###############
fh = open(outname, "w")
print "filename =", outname
xg = XMLGenerator(fh, "utf-8")
xg.startDocument()

xg.startElement("font", {
    'name': unicode(fl.font.font_name),
    "upem": unicode(fl.font.upm)
})
for i in range(0, len(fl.font.glyphs)):
    print_glyph(fl.font, fl.font.glyphs[i], i)
xg.endElement("font")

xg.endDocument()
fh.close()

print 'done'
Example #48
0
class OSMWriter(object):
    def __init__(self, filename=None, fp=None):
        if filename:
            self.filename = filename
            self.fp = open(self.filename, 'wb')
        elif fp:
            self.fp = fp

        self.xmlfile = XMLGenerator(self.fp, 'utf-8')

        self.xmlfile.startDocument()
        # TODO include version
        self.xmlfile.startElement("osm", {
            'version': '0.6',
            'generator': 'Thomas @Mapabc 20200520-0.1'
        })

    def close(self, close_file=True):
        self.xmlfile.characters("\n")
        self.xmlfile.endElement("osm")
        self.xmlfile.endDocument()
        if close_file:
            self.fp.close()

    def bounds(self, bbox):
        attrs = {
            'minlon': t(bbox[0]),
            'minlat': t(bbox[1]),
            'maxlon': t(bbox[2]),
            'maxlat': t(bbox[3])
        }

        self.xmlfile.characters("\n")
        self.xmlfile.startElement("bounds", attrs)
        self.xmlfile.endElement("bounds")

    def node(self, id, lat, lon, tags=None, **metadata):
        tags = tags or {}

        self.xmlfile.characters("\n  ")

        if tags == {}:
            attrs = {'id': t(id), 'lat': t(lat), 'lon': t(lon)}
            for key, value in metadata.items():
                attrs[key] = t(value)
            self.xmlfile.startElement("node", attrs)
            self.xmlfile.endElement("node")
        else:
            attrs = {'id': t(id), 'lat': t(lat), 'lon': t(lon)}
            for key, value in metadata.items():
                attrs[key] = t(value)

            self.xmlfile.startElement("node", attrs)

            for key, value in tags.items():
                self.xmlfile.characters("\n    ")
                self.xmlfile.startElement("tag", {'k': key, 'v': value})
                self.xmlfile.endElement("tag")

            self.xmlfile.characters("\n  ")
            self.xmlfile.endElement("node")

    def way(self, id, tags, nodeids, **metadata):
        tags = tags or {}
        nodeids = nodeids or []

        self.xmlfile.characters("\n  ")
        attrs = {'id': t(id)}
        for key, value in metadata.items():
            attrs[key] = t(value)

        self.xmlfile.startElement("way", attrs)

        for nodeid in nodeids:
            self.xmlfile.characters("\n    ")
            self.xmlfile.startElement("nd", {'ref': t(nodeid)})
            self.xmlfile.endElement("nd")

        for key, value in tags.items():
            self.xmlfile.characters("\n    ")
            self.xmlfile.startElement("tag", {'k': key, 'v': value})
            self.xmlfile.endElement("tag")

        self.xmlfile.characters("\n  ")
        self.xmlfile.endElement("way")

    def relation(self, id, tags, members, **metadata):
        tags = tags or {}

        self.xmlfile.characters("\n  ")
        attrs = {'id': t(id)}
        for key, value in metadata.items():
            attrs[key] = t(value)

        self.xmlfile.startElement("relation", attrs)

        for member in members:
            attrs = {}
            if len(member) == 2:
                type, ref = member
                attrs = {'type': t(type), 'ref': t(ref)}
            else:
                type, ref, role = member
                attrs = {'type': t(type), 'ref': t(ref), 'role': t(role)}
            self.xmlfile.characters("\n    ")
            self.xmlfile.startElement("member", attrs)
            self.xmlfile.endElement("member")

        for key, value in tags.items():
            self.xmlfile.characters("\n    ")
            self.xmlfile.startElement("tag", {'k': key, 'v': value})
            self.xmlfile.endElement("tag")

        self.xmlfile.characters("\n  ")
        self.xmlfile.endElement("relation")
Example #49
0
class _XmlAnnotationWriter(_AnnotationWriter):
    def __init__(self, file):
        super().__init__(file, "1.0")
        self.xmlgen = XMLGenerator(self.file, 'utf-8')
        self._level = 0

    def _indent(self, newline=True):
        if newline:
            self.xmlgen.ignorableWhitespace("\n")
        self.xmlgen.ignorableWhitespace("  " * self._level)

    def _add_version(self):
        self._indent()
        self.xmlgen.startElement("version", {})
        self.xmlgen.characters(self.version)
        self.xmlgen.endElement("version")

    def open_root(self):
        self.xmlgen.startDocument()
        self.xmlgen.startElement("annotations", {})
        self._level += 1
        self._add_version()

    def _add_meta(self, meta):
        self._level += 1
        for k, v in meta.items():
            if isinstance(v, OrderedDict):
                self._indent()
                self.xmlgen.startElement(k, {})
                self._add_meta(v)
                self._indent()
                self.xmlgen.endElement(k)
            else:
                self._indent()
                self.xmlgen.startElement(k, {})
                self.xmlgen.characters(v)
                self.xmlgen.endElement(k)
        self._level -= 1

    def add_meta(self, meta):
        self._indent()
        self.xmlgen.startElement("meta", {})
        self._add_meta(meta)
        self._indent()
        self.xmlgen.endElement("meta")

    def open_track(self, track):
        self._indent()
        self.xmlgen.startElement("track", track)
        self._level += 1

    def open_image(self, image):
        self._indent()
        self.xmlgen.startElement("image", image)
        self._level += 1

    def open_box(self, box):
        self._indent()
        self.xmlgen.startElement("box", box)
        self._level += 1

    def add_attribute(self, attribute):
        self._indent()
        self.xmlgen.startElement("attribute", {"name": attribute["name"]})
        self.xmlgen.characters(attribute["value"])
        self.xmlgen.endElement("attribute")

    def close_box(self):
        self._level -= 1
        self._indent()
        self.xmlgen.endElement("box")

    def close_image(self):
        self._level -= 1
        self._indent()
        self.xmlgen.endElement("image")

    def close_track(self):
        self._level -= 1
        self._indent()
        self.xmlgen.endElement("track")

    def close_root(self):
        self._level -= 1
        self._indent()
        self.xmlgen.endElement("annotations")
        self.xmlgen.endDocument()
Example #50
0
 def startElement(self, name, attrs):
     if self.depth and not self.chars:
         XMLGenerator.characters(self, '\n' + '  ' * self.depth)
     XMLGenerator.startElement(self, name, attrs)
     self.depth += 1
     self.chars = False
Example #51
0
class XmlItemExporter(BaseItemExporter):
    """
    Exports Items in XML format to the specified file object.
    """
    def __init__(self, file, **kwargs):
        """

        :param file:  the file-like object to use for exporting the data.
        It's write method should accept bytes (a disk file opened in
        binary mode, a io.BytesIO object, etc)
        :param kwargs: root_element (str) – The name of root element in
        the exported XML.
        :param kwargs: item_element (str) – The name of each item element
        in the exported XML.

        A typical output of this exporter would be:
            <?xml version="1.0" encoding="utf-8"?>
            <items>
              <item>
                <name>Color TV</name>
                <price>1200</price>
             </item>
              <item>
                <name>DVD player</name>
                <price>200</price>
             </item>
            </items>

        Unless overridden in the serialize_field() method, multi-valued
        fields are exported by serializing each value inside a <value>
        element. This is for convenience, as multi-valued fields are
        very common.

        For example, the item:
        >>> Item(name=['John', 'Doe'], age='23')

        Would be serialized as:
            <?xml version="1.0" encoding="utf-8"?>
            <items>
              <item>
                <name>
                  <value>John</value>
                  <value>Doe</value>
                </name>
                <age>23</age>
              </item>
            </items>
        """
        super().__init__()
        self.item_element = kwargs.pop('item_element', 'item')
        self.root_element = kwargs.pop('root_element', 'items')
        self._configure(kwargs)
        if not self.encoding:
            self.encoding = 'utf-8'
        self.xg = XMLGenerator(file, encoding=self.encoding)

    def _beautify_newline(self, new_item=False):
        if self.indent is not None and (self.indent > 0 or new_item):
            self._xg_characters('\n')

    def _beautify_indent(self, depth=1):
        if self.indent:
            self._xg_characters(' ' * self.indent * depth)

    def start_exporting(self):
        self.xg.startDocument()
        self.xg.startElement(self.root_element, {})
        self._beautify_newline(new_item=True)

    def export_item(self, item):
        self._beautify_indent(depth=1)
        self.xg.startElement(self.item_element, {})
        self._beautify_newline()
        for name, value in self._get_serialized_fields(item, default_value=''):
            self._export_xml_field(name, value, depth=2)
        self._beautify_indent(depth=1)
        self.xg.endElement(self.item_element)
        self._beautify_newline(new_item=True)

    def finish_exporting(self):
        self.xg.endElement(self.root_element)
        self.xg.endDocument()

    def _export_xml_field(self, name, serialized_value, depth):
        self._beautify_indent(depth=depth)
        self.xg.startElement(name, {})
        if hasattr(serialized_value, 'items'):
            self._beautify_newline()
            for subname, value in serialized_value.items():
                self._export_xml_field(subname, value, depth=depth + 1)
            self._beautify_indent(depth=depth)
        elif is_listlike(serialized_value):
            self._beautify_newline()
            for value in serialized_value:
                self._export_xml_field('value', value, depth=depth + 1)
            self._beautify_indent(depth=depth)
        elif isinstance(serialized_value, str):
            self._xg_characters(serialized_value)
        else:
            self._xg_characters(str(serialized_value))
        self.xg.endElement(name)
        self._beautify_newline()

    # Workaround for https://bugs.python.org/issue17606
    if sys.version_info[:3] >= (2, 7, 4):

        def _xg_characters(self, serialized_value):
            if not isinstance(serialized_value, str):
                serialized_value = serialized_value.decode(self.encoding)
            return self.xg.characters(serialized_value)
    else:  # pragma: no cover

        def _xg_characters(self, serialized_value):
            return self.xg.characters(serialized_value)
Example #52
0
class Logger:
    def __init__(self):
        self.logfile = os.environ.get("LOGFILE", "/dev/null")
        self.logfile_handle = open(self.logfile, "wb")
        self.xml = XMLGenerator(self.logfile_handle, encoding="utf-8")
        self.queue = queue.Queue(1000)

        self.xml.startDocument()
        self.xml.startElement("logfile", attrs={})

    def close(self):
        self.xml.endElement("logfile")
        self.xml.endDocument()
        self.logfile_handle.close()

    def sanitise(self, message):
        return "".join(ch for ch in message
                       if unicodedata.category(ch)[0] != "C")

    def maybe_prefix(self, message, attributes):
        if "machine" in attributes:
            return "{}: {}".format(attributes["machine"], message)
        return message

    def log_line(self, message, attributes):
        self.xml.startElement("line", attributes)
        self.xml.characters(message)
        self.xml.endElement("line")

    def log(self, message, attributes={}):
        eprint(self.maybe_prefix(message, attributes))
        self.drain_log_queue()
        self.log_line(message, attributes)

    def enqueue(self, message):
        self.queue.put(message)

    def drain_log_queue(self):
        try:
            while True:
                item = self.queue.get_nowait()
                attributes = {"machine": item["machine"], "type": "serial"}
                self.log_line(self.sanitise(item["msg"]), attributes)
        except queue.Empty:
            pass

    @contextmanager
    def nested(self, message, attributes={}):
        eprint(self.maybe_prefix(message, attributes))

        self.xml.startElement("nest", attrs={})
        self.xml.startElement("head", attributes)
        self.xml.characters(message)
        self.xml.endElement("head")

        tic = time.time()
        self.drain_log_queue()
        yield
        self.drain_log_queue()
        toc = time.time()
        self.log("({:.2f} seconds)".format(toc - tic))

        self.xml.endElement("nest")
Example #53
0
 def startElement(self, name, attrs):
     if self.schrijf:
         XMLGenerator.startElement(self, name, attrs)
     if name == 'vertel':
         self.schrijf = False
Example #54
0
 def _open(self):
     """open a new communication channel from src to dst"""
     xmlgen = XMLGenerator(self.worker, encoding=ENCODING)
     xmlgen.startElement('channel', {'version': __version__})
Example #55
0
class Logger:
    def __init__(self) -> None:
        self.logfile = os.environ.get("LOGFILE", "/dev/null")
        self.logfile_handle = codecs.open(self.logfile, "wb")
        self.xml = XMLGenerator(self.logfile_handle, encoding="utf-8")
        self.queue: "Queue[Dict[str, str]]" = Queue()

        self.xml.startDocument()
        self.xml.startElement("logfile", attrs={})

        self._print_serial_logs = True

    def close(self) -> None:
        self.xml.endElement("logfile")
        self.xml.endDocument()
        self.logfile_handle.close()

    def sanitise(self, message: str) -> str:
        return "".join(ch for ch in message if unicodedata.category(ch)[0] != "C")

    def maybe_prefix(self, message: str, attributes: Dict[str, str]) -> str:
        if "machine" in attributes:
            return "{}: {}".format(attributes["machine"], message)
        return message

    def log_line(self, message: str, attributes: Dict[str, str]) -> None:
        self.xml.startElement("line", attributes)
        self.xml.characters(message)
        self.xml.endElement("line")

    def log(self, message: str, attributes: Dict[str, str] = {}) -> None:
        eprint(self.maybe_prefix(message, attributes))
        self.drain_log_queue()
        self.log_line(message, attributes)

    def log_serial(self, message: str, machine: str) -> None:
        self.enqueue({"msg": message, "machine": machine, "type": "serial"})
        if self._print_serial_logs:
            eprint(Style.DIM + "{} # {}".format(machine, message) + Style.RESET_ALL)

    def enqueue(self, item: Dict[str, str]) -> None:
        self.queue.put(item)

    def drain_log_queue(self) -> None:
        try:
            while True:
                item = self.queue.get_nowait()
                msg = self.sanitise(item["msg"])
                del item["msg"]
                self.log_line(msg, item)
        except Empty:
            pass

    @contextmanager
    def nested(self, message: str, attributes: Dict[str, str] = {}) -> Iterator[None]:
        eprint(self.maybe_prefix(message, attributes))

        self.xml.startElement("nest", attrs={})
        self.xml.startElement("head", attributes)
        self.xml.characters(message)
        self.xml.endElement("head")

        tic = time.time()
        self.drain_log_queue()
        yield
        self.drain_log_queue()
        toc = time.time()
        self.log("({:.2f} seconds)".format(toc - tic))

        self.xml.endElement("nest")
Example #56
0
 def _processBegin(self, node, name, attributes):
     '''
     @see: Digester._processBegin
     '''
     if not Digester._processBegin(self, node, name, attributes):
         XMLGenerator.startElement(self, name, attributes)
Example #57
0
def get_update_result_as_xml(status, txn_id, prv_txn, sum, comment=None):
    out = StringIO.StringIO("")
    g = XMLGenerator(out, encoding='UTF-8')
    g.startDocument()
    g.startElement("response", {})
    g.startElement("mgpe_txn_id", {})
    g.characters(txn_id)
    g.endElement("mgpe_txn_id")
    g.startElement("txn_id", {})
    g.characters(txn_id)
    g.endElement("txn_id")
    g.startElement("prv_txn", {})
    g.characters(prv_txn)
    g.endElement("prv_txn")
    g.startElement("sum", {})
    g.characters(sum)
    g.endElement("sum")
    g.startElement("result", {})
    g.characters(status)
    g.endElement("result")
    if comment:
        g.startElement("comment", {})
        g.characters(comment)
        g.endElement("comment")
    g.endElement("response")
    g.endDocument()
    return out.getvalue()
Example #58
0
 def func(out):
     gen = XMLGenerator(out)
     gen.startDocument()
     gen.startElement("doc", {})
Example #59
0
    def dump_xml(self, output_dir):
        """
        Dumps test result to xml
        """
        self.buffer = False

        with open(os.path.join(output_dir, 'junit.xml'), 'w') as output:
            document = XMLGenerator(output, 'utf-8')
            document.startDocument()
            document.startElement('testsuites', AttributesImpl({}))

            suites = groupby(self.testInfos,
                             key=lambda test_info: self.test_case_name(
                                 test_info.test_method))
            for suite_name, suite in suites:
                document.startElement('testsuite',
                                      AttributesImpl({'name': suite_name}))

                for test_info in suite:
                    document.startElement(
                        'testcase',
                        AttributesImpl({
                            'classname':
                            suite_name,
                            'name':
                            self.test_method_name(test_info.test_method),
                            'time':
                            '%3f' % total_seconds(test_info.end_time -
                                                  test_info.start_time)
                        }))

                    if test_info.result == TestInfo.RESULT.ERROR:
                        document.startElement(
                            'error',
                            AttributesImpl(
                                {'message': unicode(test_info.err[1])}))
                        document.characters(
                            self._exc_info_to_string(test_info.err,
                                                     test_info.test_method))
                        document.endElement('error')
                    elif test_info.result == TestInfo.RESULT.FAILURE:
                        document.startElement(
                            'failure',
                            AttributesImpl(
                                {'message': unicode(test_info.err[1])}))
                        document.characters(
                            self._exc_info_to_string(test_info.err,
                                                     test_info.test_method))
                        document.endElement('failure')
                    elif test_info.result == TestInfo.RESULT.UNEXPECTED_SUCCESS:
                        document.startElement(
                            'error',
                            AttributesImpl({'message': 'Unexpected success'}))
                        document.endElement('error')
                    elif test_info.result == TestInfo.RESULT.SKIPPED:
                        document.startElement('skipped', AttributesImpl({}))
                        document.characters(test_info.reason)
                        document.endElement('skipped')

                    if test_info.stdout:
                        document.startElement('system-out', AttributesImpl({}))
                        document.characters(test_info.stdout)
                        document.endElement('system-out')

                    if test_info.stderr:
                        document.startElement('system-err', AttributesImpl({}))
                        document.characters(test_info.stderr)
                        document.endElement('system-err')
                    document.endElement('testcase')

                document.endElement('testsuite')

            document.endElement('testsuites')
            document.endDocument()
Example #60
0
def startAtomFeed(filename, date):
    generator = XMLGenerator(open(filename, 'wb'), 'utf-8')

    generator.startDocument()

    generator.startElement(
        u'feed', AttributesImpl({u'xmlns': u'http://www.w3.org/2005/Atom'}))

    generator.startElement(u'id', AttributesImpl({}))
    generator.characters(u'http://www.wine-staging.com/news.xml')
    generator.endElement(u'id')

    generator.startElement(u'title', AttributesImpl({}))
    generator.characters(u'Wine Staging')
    generator.endElement(u'title')

    generator.startElement(u'updated', AttributesImpl({}))
    generator.characters(date.decode('utf8') + u"T00:00:00Z")
    generator.endElement(u'updated')

    generator.startElement(
        u'link', AttributesImpl({
            u'rel': u'self',
            u'href': u'/news.xml'
        }))
    generator.endElement(u'link')

    generator.startElement(
        u'link',
        AttributesImpl({
            u'rel': u'alternate',
            u'type': u'text/html',
            u'href': u'/news.html'
        }))
    generator.endElement(u'link')

    return generator