Esempio n. 1
0
class xmlreport:
    def __init__(self, filename, xslfile):
        self.fp = open(filename, "wb")
        self.xmlgen = XMLGenerator(self.fp, "utf-8")
        self.xmlgen.startDocument()
        self.xmlgen.processingInstruction("xml-stylesheet", 'type="text/xsl" href="{}"'.format(xslfile))
        attrs = AttributesNSImpl({}, {})
        self.xmlgen.startElementNS((None, "report"), "report", attrs)

    def write(self, path, code, message):
        attr_vals = {
            (None, "code"): code,
            (None, "path"): path
        }
        attr_qnames = {
            (None, "code"): "code",
            (None, "path"): "path"
        }
        attrs = AttributesNSImpl(attr_vals, attr_qnames)
        self.xmlgen.startElementNS((None, "file"), "file", attrs)
        self.xmlgen.characters(message)
        self.xmlgen.endElementNS((None, "file"), "file")

    def newline(self):
        #self.fp.write("\n")
        self.xmlgen.characters("\n")

    def close(self):
        self.xmlgen.endElementNS((None, "report"), "report")
        self.xmlgen.endDocument()
        self.fp.close()
Esempio n. 2
0
 def characters(self, content: str) -> None:
     if content and re.search(r"[\x00-\x08\x0B-\x0C\x0E-\x1F]", content):
         # Fail loudly when content has control chars (unsupported in XML 1.0)
         # See http://www.w3.org/International/questions/qa-controls
         raise UnserializableContentError(
             "Control characters are not supported in XML 1.0")
     XMLGenerator.characters(self, content)
Esempio n. 3
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
Esempio n. 4
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
Esempio n. 5
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)
        self.xg = XMLGenerator(file, encoding=self.encoding)

    def start_exporting(self):
        self.xg.startDocument()
        self.xg.startElement(self.root_element, {})

    def export_item(self, item):
        self.xg.startElement(self.item_element, {})
        for name, value in self._get_serialized_fields(item, default_value=''):
            self._export_xml_field(name, value)
        self.xg.endElement(self.item_element)

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

    def _export_xml_field(self, name, serialized_value):
        self.xg.startElement(name, {})
        if hasattr(serialized_value, '__iter__'):
            for value in serialized_value:
                self._export_xml_field('value', value)
        else:
            self.xg.characters(serialized_value)
        self.xg.endElement(name)
Esempio n. 6
0
    def execute(self, transform_manager, input):
        with open(transform_manager('xml'), 'w') as output:
            transform_manager.start(self, [input])
            generator = XMLGenerator(output, encoding='utf-8')
            generator.startDocument()
            generator.startElement('TEI', {'xmlns':'http://www.tei-c.org/ns/1.0'})
            generator.startElement('text', {})
            generator.startElement('body', {})

            for sheet in self.sheets(input):
                generator.startElement('table', {})
                generator.startElement('head', {})
                generator.characters(sheet.name)
                generator.endElement('head')

                for i, row in enumerate(sheet.rows):
                    generator.startElement('row', {'n': unicode(int(i) + 1)})
                    for j, cell in enumerate(row.cells):
                        generator.startElement('cell', {'n': unicode(j + 1)})
                        generator.characters(unicode(cell))
                        generator.endElement('cell')
                    generator.endElement('row')
                generator.endElement('table')

            generator.endElement('body')
            generator.endElement('text')
            generator.endElement('TEI')

            transform_manager.end([output.name])
            return output.name
Esempio n. 7
0
class BBXMLRepoint(BBXML):
  """Repoints filenames in the XML.

  Silently converts paths to Windows-style path separators.

  Arguments:
    parser: the xml.sax.make_parser() object
    output: the output file object
    coords: a coordinate dictionary with int keys 'track' and 'clip'
    newname: the new name for the clip at those coordinates
  """

  def __init__(self, parser, output, coords, newname):
    super().__init__(parser)
    self._output = XMLGenerator(output)
    self._coords = coords
    # BitBox uses windows-style paths
    self._newname = pathlib.PureWindowsPath(newname) if newname != '' else ''

  def startElement(self, name, attrs):
    super().startElement(name, attrs)
    new_attrs = dict(attrs)
    if name == 'clip':
      if self._cur_track == self._coords['track'] and self._cur_clip == self._coords['clip']:
        new_attrs['file'] = "0"
        new_attrs['filename'] = str(self._newname)
    self._output.startElement(name, new_attrs)

  def endElement(self, name):
    super().endElement(name)
    self._output.endElement(name)

  def characters(self, content):
    self._output.characters(content)
Esempio n. 8
0
class XmlFormat(ModelEventHandler):
    def __init__(self, out):
        self.xmlgen = XMLGenerator(out, 'utf-8')
    def startDocument(self):
        self.xmlgen.startDocument()
    def startModel(self, model, attributes, **context):
        logger.debug('xmlmodel.XmlFormat: model: %s, %s', model.__name__, attributes)
        logger.debug('xmlmodel.XmlFormat: context: %s', context)
        recordid = context.get('recordid', ('UNKNOWN', 'UNKNOWN', -1))
        hwptag = context.get('hwptag', '')
        logger.info('xmlmodel.XmlFormat: rec:%d %s', recordid[2], hwptag)
        if model is Text:
            text = attributes.pop('text')
        else:
            text = None

        for x in startelement(context, self.xmlgen, (model, attributes)): x[0](*x[1:])

        if model is Text and text is not None:
            self.xmlgen.characters(text)

        unparsed = context.get('unparsed', '')
        if len(unparsed) > 0:
            logger.debug('UNPARSED: %s', hexdump(unparsed, True))
    def endModel(self, model):
        self.xmlgen.endElement(model.__name__)
    def endDocument(self):
        self.xmlgen.endDocument()
Esempio n. 9
0
    def test_5027_2(self):
        # The xml prefix (as in xml:lang below) is reserved and bound by
        # definition to http://www.w3.org/XML/1998/namespace.  XMLGenerator had
        # a bug whereby a KeyError is thrown because this namespace is missing
        # from a dictionary.
        #
        # This test demonstrates the bug by direct manipulation of the
        # XMLGenerator.
        result = StringIO()
        gen = XMLGenerator(result)

        gen.startDocument()
        gen.startPrefixMapping('a', 'http://example.com/ns')
        gen.startElementNS(('http://example.com/ns', 'g1'), 'g1', {})
        lang_attr = {('http://www.w3.org/XML/1998/namespace', 'lang'): 'en'}
        gen.startElementNS(('http://example.com/ns', 'g2'), 'g2', lang_attr)
        gen.characters('Hello')
        gen.endElementNS(('http://example.com/ns', 'g2'), 'g2')
        gen.endElementNS(('http://example.com/ns', 'g1'), 'g1')
        gen.endPrefixMapping('a')
        gen.endDocument()

        self.assertEqual(result.getvalue(),
                         start + (
                         '<a:g1 xmlns:a="http://example.com/ns">'
                          '<a:g2 xml:lang="en">Hello</a:g2>'
                         '</a:g1>'))
Esempio n. 10
0
    def test_5027_2(self):
        # The xml prefix (as in xml:lang below) is reserved and bound by
        # definition to http://www.w3.org/XML/1998/namespace.  XMLGenerator had
        # a bug whereby a KeyError is raised because this namespace is missing
        # from a dictionary.
        #
        # This test demonstrates the bug by direct manipulation of the
        # XMLGenerator.
        result = StringIO()
        gen = XMLGenerator(result)

        gen.startDocument()
        gen.startPrefixMapping("a", "http://example.com/ns")
        gen.startElementNS(("http://example.com/ns", "g1"), "g1", {})
        lang_attr = {("http://www.w3.org/XML/1998/namespace", "lang"): "en"}
        gen.startElementNS(("http://example.com/ns", "g2"), "g2", lang_attr)
        gen.characters("Hello")
        gen.endElementNS(("http://example.com/ns", "g2"), "g2")
        gen.endElementNS(("http://example.com/ns", "g1"), "g1")
        gen.endPrefixMapping("a")
        gen.endDocument()

        self.assertEqual(
            result.getvalue(),
            start + ('<a:g1 xmlns:a="http://example.com/ns">' '<a:g2 xml:lang="en">Hello</a:g2>' "</a:g1>"),
        )
Esempio n. 11
0
    def endElement(self, name):
        """
        Signals the end of an element.
        Data stored in global vars of the class, time to create our objects and fire their processing
        @param name: the name of the element
        @type name: str 
        @param attrs: of the element type as a string and the attrs parameter holds an object of the Attributes interface containing the attributes of the element.
        @type attrs: Attributes
        """
        self.ss_text = "".join(self.ss_text)
        #get rid of annoying leading spaces
        self.ss_text = self.ss_text.strip()
        
        #all of the elements have to be declared here
        #for each element, create the objects and clear "buffers"
        if name == self.IN_TAG["src"]:
            self.src = SimpleSentence(self.ss_text, self.ss_attributes)
            self.ss_text = []
        elif name == self.IN_TAG["tgt"]:
            self.tgt.append(SimpleSentence(self.ss_text, self.ss_attributes))
            self.ss_text = []
        elif name == self.IN_TAG["sent"]:
            #when the judged sentence gets closed, all previously inserted data have to be converted to objects 
            parallelsentence = ParallelSentence(self.src, self.tgt, self.ref, self.ps_attributes)

            #apply feature generators
            for fg in self.feature_generators:
                parallelsentence = fg.add_features_parallelsentence(parallelsentence)
                #parallelsentence.add_attributes( fg.get_features_parallelsentence(parallelsentence) )
            
            #print parallelsentence
            src = self.src
#            #print src.get_string()
#            for fg in self.feature_generators:
#                src = fg.add_features_src(src, parallelsentence)
#                #src.add_attributes( fg.get_features_src(src, parallelsentence) )
#            parallelsentence.set_source(src)

            #display modifications on output file
            XMLGenerator._write(self, "\n\t")
             
            XMLGenerator.startElement(self, name, parallelsentence.get_attributes())
                        
            XMLGenerator._write(self, "\n\t\t")
            XMLGenerator.startElement(self, self.OUT_TAG["src"], src.get_attributes())
            XMLGenerator.characters(self, src.get_string())
            XMLGenerator.endElement(self, self.OUT_TAG["src"])
            
            for tgt in parallelsentence.get_translations():
#                for fg in self.feature_generators:
#                    tgt = fg.add_features_tgt(tgt, parallelsentence)
#                    #tgt.add_attributes( fg.get_features_tgt(tgt, parallelsentence) )

                XMLGenerator._write(self, "\n\t\t")
                XMLGenerator.startElement(self, self.OUT_TAG["tgt"], tgt.get_attributes())
                XMLGenerator.characters(self, tgt.get_string())
                XMLGenerator.endElement(self, self.OUT_TAG["tgt"])
            
            XMLGenerator._write(self, "\n\t")
            XMLGenerator.endElement(self, name)
Esempio n. 12
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')
Esempio n. 13
0
   class xmlFile:
      def __init__(self,
                   filename):
         self._fp = open(filename, 'w')
         self._xg = XMLGenerator(self._fp)
         self._xg.startDocument()
         self._xg.startElement(u'ConfigRoot', {})

      def Write(self,
                item,
                val):
         attrs = {'id' : "%04i" % (int(val[1])) }
         self._xg.startElement(u'ConfigEntry', attrs)
         self._xg.startElement(u'objID', {})
         self._xg.characters(str(val[0]))
         self._xg.endElement(u'objID')
         self._xg.startElement(u'vmxCfgPath', {})
         self._xg.characters(escape(item).encode('UTF-8'))
         self._xg.endElement(u'vmxCfgPath')
         self._xg.endElement(u'ConfigEntry')

      def Close(self):
         self._xg.endElement(u'ConfigRoot')
         self._xg.endDocument()
         self._fp.close()
Esempio n. 14
0
    def test_5027_2(self):
        # The xml prefix (as in xml:lang below) is reserved and bound by
        # definition to http://www.w3.org/XML/1998/namespace.  XMLGenerator had
        # a bug whereby a KeyError is thrown because this namespace is missing
        # from a dictionary.
        #
        # This test demonstrates the bug by direct manipulation of the
        # XMLGenerator.
        result = StringIO()
        gen = XMLGenerator(result)

        gen.startDocument()
        gen.startPrefixMapping('a', 'http://example.com/ns')
        gen.startElementNS(('http://example.com/ns', 'g1'), 'g1', {})
        lang_attr = {('http://www.w3.org/XML/1998/namespace', 'lang'): 'en'}
        gen.startElementNS(('http://example.com/ns', 'g2'), 'g2', lang_attr)
        gen.characters('Hello')
        gen.endElementNS(('http://example.com/ns', 'g2'), 'g2')
        gen.endElementNS(('http://example.com/ns', 'g1'), 'g1')
        gen.endPrefixMapping('a')
        gen.endDocument()

        self.assertEqual(result.getvalue(),
                         start + (
                         '<a:g1 xmlns:a="http://example.com/ns">'
                          '<a:g2 xml:lang="en">Hello</a:g2>'
                         '</a:g1>'))
Esempio n. 15
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)
        self.xg = XMLGenerator(file, encoding=self.encoding)

    def start_exporting(self):
        self.xg.startDocument()
        self.xg.startElement(self.root_element, {})

    def export_item(self, item):
        self.xg.startElement(self.item_element, {})
        for name, value in self._get_serialized_fields(item, default_value=''):
            self._export_xml_field(name, value)
        self.xg.endElement(self.item_element)

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

    def _export_xml_field(self, name, serialized_value):
        self.xg.startElement(name, {})
        if hasattr(serialized_value, '__iter__'):
            for value in serialized_value:
                self._export_xml_field('value', value)
        else:
            self.xg.characters(serialized_value)
        self.xg.endElement(name)
Esempio n. 16
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))
    def output(self, stream = sys.stdout):
        xg = XMLGenerator(stream, encoding='utf-8')
        def elemWithContent(name, content):
            xg.startElement(name, AttributesImpl({}))
            if content is not None:
                xg.characters(content)
            xg.endElement(name)
        xg.startElement("opml", AttributesImpl({'version': '1.1'}))
        xg.characters("\n")
        xg.startElement("head", AttributesImpl({}))
        for key in ('title', 'dateCreated', 'dateModified', 'ownerName',
                    'ownerEmail', 'expansionState', 'vertScrollState',
                    'windowTop', 'windowBotton', 'windowRight', 'windowLeft'):
            if self.has_key(key) and self[key] != "":
                elemWithContent(key, self[key].encode('utf-8'))
        xg.endElement("head")
        xg.characters("\n")
        xg.startElement("body", AttributesImpl({}))
        for o in self.outlines:
            o.output(xg)

        xg.characters("\n")
        xg.endElement("body")
        xg.characters("\n")
        xg.endElement("opml")
        xg.characters("\n")
Esempio n. 18
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' : smart_text(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' : smart_text(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()
Esempio n. 19
0
class XMLSerializer:
    """
    Serializes data to XML for MKM requests.
    Original author: https://github.com/evonove
    """
    def __init__(self):
        self.generator = None

    def serialize(self, data):
        """
        Serializes data to XML so that it can be sent to backend, if data is not a dictionary.

        :raise SerializationException: On serialize error.
        :param data: A dictionary containing the data to serialize
        :return: Returns a string containing data serialized to XML
        """

        if not isinstance(data, dict):
            raise SerializationException(
                "Can't serialize data, must be a dictionary.")

        stream = StringIO()
        self.generator = XMLGenerator(stream, 'utf-8')

        self.generator.startDocument()
        self.generator.startElement('request', {})

        self._parse(data)

        self.generator.endElement('request')
        self.generator.endDocument()

        return stream.getvalue()

    def _parse(self, data, previous_element_tag=None):
        """
        Recursively parses data and creates the relative elements.

        :param data: Data to parse
        :param previous_element_tag: When parsing a list we pass the previous element tag
        :return:
        """
        if isinstance(data, dict):
            for key in data:
                value = data[key]
                self._parse(value, key)

        elif isinstance(data, (list, tuple)):
            for item in data:
                if isinstance(item, dict):
                    self.generator.startElement(previous_element_tag, {})
                self._parse(item, previous_element_tag)
                if isinstance(item, dict):
                    self.generator.endElement(previous_element_tag)

        else:
            self.generator.startElement(previous_element_tag, {})
            self.generator.characters(f'{data}')
            self.generator.endElement(previous_element_tag)
Esempio n. 20
0
class XMLSerializer:
    """Serializes data to XML"""
    def __init__(self):
        self.generator = None

    def serialize(self, data):
        """
        Serializes data to XML so that it can be
        sent to backend, if data is not a dictionary
        raises a SerializationException

        Params:
            `data`: A dictionary containing the data to serialize

        Return:
            `xml`: Returns a string containing data serialized to XML
        """

        if not isinstance(data, dict):
            raise SerializationException(
                "Can't serialize data, must be a dictionary.")

        stream = StringIO()
        self.generator = XMLGenerator(stream, "utf-8")

        self.generator.startDocument()
        self.generator.startElement("request", {})

        self._parse(data)

        self.generator.endElement("request")
        self.generator.endDocument()

        return stream.getvalue()

    def _parse(self, data, previous_element_tag=None):
        """
        Parses data and creates the relative elements

        Params:
            `data`: Data to parse
            `previous_element_tag`: When parsing a list we pass the previous element tag
        """
        if isinstance(data, dict):
            for key in data:
                value = data[key]
                self._parse(value, key)

        elif isinstance(data, (list, tuple)):
            for item in data:
                self.generator.startElement(previous_element_tag, {})
                self._parse(item, previous_element_tag)
                self.generator.endElement(previous_element_tag)

        else:
            self.generator.startElement(previous_element_tag, {})
            self.generator.characters("%s" % data)
            self.generator.endElement(previous_element_tag)
Esempio n. 21
0
 def characters(self, content):
     try:
         if content.lstrip().startswith("<"):
             etree.fromstring(content)
             self._write(content)
         else:
             XMLGenerator.characters(self, content)
     except:
         XMLGenerator.characters(self, content)
Esempio n. 22
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>'))
Esempio n. 23
0
 def test_xmlgen_content_escape(self):
     result = self.ioclass()
     gen = XMLGenerator(result)
     gen.startDocument()
     gen.startElement('doc', {})
     gen.characters('<huhei&')
     gen.endElement('doc')
     gen.endDocument()
     self.assertEqual(result.getvalue(),
                      self.xml('<doc>&lt;huhei&amp;</doc>'))
Esempio n. 24
0
 def characters(self, content):
     try:
         if content.lstrip().startswith("<"):
             etree.fromstring(content)
             self._write(content)
         else:
             XMLGenerator.characters(self, content)
     except (AttributeError, Exception):
         #TODO could be more specific on errors caught
         XMLGenerator.characters(self, content)
Esempio n. 25
0
 def characters(self, content):
     try:
         if content.lstrip().startswith("<"):
             etree.fromstring(content)
             self._write(content)
         else:
             XMLGenerator.characters(self, content)
     except (AttributeError, ValueError, etree.XMLSyntaxError, Exception):
         #TODO could be more specific on errors caught
         XMLGenerator.characters(self, content)
Esempio n. 26
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>"
Esempio n. 27
0
 def test_xmlgen_unencodable(self):
     result = self.ioclass()
     gen = XMLGenerator(result, encoding='ascii')
     gen.startDocument()
     gen.startElement('doc', {'a': '€'})
     gen.characters('€')
     gen.endElement('doc')
     gen.endDocument()
     self.assertEqual(
         result.getvalue(),
         self.xml('<doc a="&#8364;">&#8364;</doc>', encoding='ascii'))
Esempio n. 28
0
class XMLStreamWriter(object):
    """ class for writing an xml file iteratively """


    def __init__(self, file_handle=None, header='<?xml version="1.0" ?>'):
        """ initializes the writer with a stream to write to. If
        `filehandle=None`, the output is written to sys.stdout """
        if file_handle is None:
            file_handle = sys.stdout
        self.file_handle = file_handle
        
        if header:
            self.file_handle.write(header)
            
        self._generator = XMLGenerator(file_handle, 'utf-8')
        self._tags = []


    def start_tag(self, name, attr=None, body=None, namespace=None):
        """ start tag `name` with attributes `attr` and body `body` """
        attr_vals = {}
        attr_keys = {}
        if attr is not None:
            for key, val in attr.items():
                key_tuple = (namespace, key)
                attr_vals[key_tuple] = str(val)
                attr_keys[key_tuple] = key
    
        attr_obj = AttributesNSImpl(attr_vals, attr_keys)
        self._generator.startElementNS((namespace, name), name, attr_obj)
        
        if body:
            self._generator.characters(str(body))
            
        self._tags.append(name)
    
    
    def end_tag(self, name=None, namespace=None):
        """ and tag `name` """
        close_tag = self._tags.pop()
        
        if name is not None:
            if name != close_tag:
                raise ValueError('Cannot close tag `%s`, since the last opened '
                                 'tag was `%s`' % (name, close_tag))
                
        
        self._generator.endElementNS((namespace, name), name)
    
    
    def tag(self, name, attr=None, body=None, namespace=None):
        """ write tag `name` """
        self.start_tag(name, attr, body, namespace)
        self.end_tag(name, namespace)     
Esempio n. 29
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>"
Esempio n. 30
0
    def test_xmlgen_content(self):
        result = self.ioclass()
        gen = XMLGenerator(result)

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

        self.assertEqual(result.getvalue(), self.xml("<doc>huhei</doc>"))
Esempio n. 31
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>"))
Esempio n. 32
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>"
Esempio n. 33
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>")
Esempio n. 34
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>"
    def test_xmlgen_content_empty(self):
        result = StringIO()
        gen = XMLGenerator(result, short_empty_elements=True)

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

        self.assertEqual(result.getvalue(), start + "<doc>huhei</doc>")
Esempio n. 36
0
 def test_xmlgen_fragment(self):
     result = self.ioclass()
     gen = XMLGenerator(result)
     gen.startElement('foo', {'a': '1.0'})
     gen.characters('Hello')
     gen.endElement('foo')
     gen.startElement('bar', {'b': '2.0'})
     gen.endElement('bar')
     self.assertEqual(
         result.getvalue(),
         self.xml('<foo a="1.0">Hello</foo><bar b="2.0"></bar>')
         [len(self.xml('')):])
Esempio n. 37
0
    def test_xmlgen_unencodable(self):
        result = self.ioclass()
        gen = XMLGenerator(result, encoding='ascii')

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

        self.assertEqual(result.getvalue(),
            self.xml('<doc a="&#8364;">&#8364;</doc>', encoding='ascii'))
Esempio n. 38
0
    def test_xmlgen_content_escape(self):
        result = self.ioclass()
        gen = XMLGenerator(result)

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

        self.assertEqual(result.getvalue(),
            self.xml("<doc>&lt;huhei&amp;</doc>"))
Esempio n. 39
0
    def test_xmlgen_content_escape(self):
        result = self.ioclass()
        gen = XMLGenerator(result)

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

        self.assertEqual(result.getvalue(),
                         start + "<doc>&lt;huhei&amp;</doc>")
Esempio n. 40
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 start_exporting(self):
        self.xg.startDocument()
        self.xg.startElement(self.root_element, {})

    def export_item(self, item):
        self.xg.startElement(self.item_element, {})
        for name, value in self._get_serialized_fields(item, default_value=''):
            self._export_xml_field(name, value)
        self.xg.endElement(self.item_element)

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

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

    # Workaround for http://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)
Esempio n. 41
0
    def test_xmlgen_unencodable(self):
        result = self.ioclass()
        gen = XMLGenerator(result, encoding='ascii')

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

        self.assertEqual(
            result.getvalue(), '<?xml version="1.0" encoding="ascii"?>\n'
            '<doc a="&#8364;">&#8364;</doc>')
Esempio n. 42
0
    def test_xmlgen_unencodable(self):
        result = self.ioclass()
        gen = XMLGenerator(result, encoding='ascii')

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

        self.assertEqual(result.getvalue(),
                '<?xml version="1.0" encoding="ascii"?>\n'
                '<doc a="&#8364;">&#8364;</doc>')
Esempio n. 43
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)
        self.xg = XMLGenerator(file, encoding=self.encoding)

    def start_exporting(self):
        self.xg.startDocument()
        self.xg.startElement(self.root_element, {})

    def export_item(self, item):
        self.xg.startElement(self.item_element, {})
        for name, value in self._get_serialized_fields(item, default_value=""):
            self._export_xml_field(name, value)
        self.xg.endElement(self.item_element)

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

    def _export_xml_field(self, name, serialized_value):
        self.xg.startElement(name, {})
        if hasattr(serialized_value, "items"):
            for subname, value in serialized_value.items():
                self._export_xml_field(subname, value)
        elif is_listlike(serialized_value):
            for value in serialized_value:
                self._export_xml_field("value", value)
        elif isinstance(serialized_value, six.text_type):
            self._xg_characters(serialized_value)
        else:
            self._xg_characters(str(serialized_value))
        self.xg.endElement(name)

    # Workaround for http://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)
Esempio n. 44
0
 def test_xmlgen_encoding(self):
     encodings = ('iso-8859-15', 'utf-8', 'utf-8-sig', 'utf-16', 'utf-16be',
                  'utf-16le', 'utf-32', 'utf-32be', 'utf-32le')
     for encoding in encodings:
         result = self.ioclass()
         gen = XMLGenerator(result, encoding=encoding)
         gen.startDocument()
         gen.startElement('doc', {'a': '€'})
         gen.characters('€')
         gen.endElement('doc')
         gen.endDocument()
         self.assertEqual(
             result.getvalue(),
             self.xml('<doc a="€">€</doc>', encoding=encoding))
Esempio n. 45
0
    def store(self):
        self.makeId("blogentry")
        log.debug("storing BlogEntry %s/%d" % (self.datetime[0], self.id))
        try:
            outf = TransactionalFile(
                BlogEntry.determineFileName(self.getStoragePath(),
                                            self.datetime[0], self.id), "wb")
            gen = XMLGenerator(out=outf, encoding="UTF-8")
            gen.startDocument()
            gen.startElement("blogentry", {"id": str(self.id)})
            gen.characters("\n")
            for attr in self.__slots__:
                if attr == "datetime":
                    gen.startElement("timestamp", {
                        "date": self.datetime[0],
                        "time": self.datetime[1]
                    })
                    gen.endElement("timestamp")
                    gen.characters("\n")
                else:
                    # other attribute
                    gen.startElement(attr, {})
                    gen.characters(getXMLvalue(self, attr))
                    gen.endElement(attr)
                    gen.characters('\n')

            gen.endElement("blogentry")
            gen.endDocument()
            outf.commit()
            os.chmod(outf.name, FILEPROT)
        except EnvironmentError, x:
            raise StorageError(str(x))
Esempio n. 46
0
    def test_xmlgen_fragment(self):
        result = self.ioclass()
        gen = XMLGenerator(result)

        # Don't call gen.startDocument()
        gen.startElement("foo", {"a": "1.0"})
        gen.characters("Hello")
        gen.endElement("foo")
        gen.startElement("bar", {"b": "2.0"})
        gen.endElement("bar")
        # Don't call gen.endDocument()

        self.assertEqual(result.getvalue(),
                         '<foo a="1.0">Hello</foo><bar b="2.0"></bar>')
Esempio n. 47
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
Esempio n. 48
0
    def test_xmlgen_fragment(self):
        result = self.ioclass()
        gen = XMLGenerator(result)

        # Don't call gen.startDocument()
        gen.startElement("foo", {"a": "1.0"})
        gen.characters("Hello")
        gen.endElement("foo")
        gen.startElement("bar", {"b": "2.0"})
        gen.endElement("bar")
        # Don't call gen.endDocument()

        self.assertEqual(result.getvalue(),
            self.xml('<foo a="1.0">Hello</foo><bar b="2.0"></bar>')[len(self.xml('')):])
Esempio n. 49
0
class BBXMLOverwrite(BBXML):
  """Overwrites clip metadata in the XML.

  Arguments:
    parser: the xml.sax.make_parser() object
    output: the output file object
    coords: a coordinate dictionary with int keys 'track' and 'clip'
    clipdata: a dictionary of clip data as returned by clips()
  """

  def __init__(self, parser, output, coords, clipdata):
    super().__init__(parser)
    self._output = XMLGenerator(output)
    self._coords = coords
    self._clipdata = clipdata
    self._in_clip = False

  def startElement(self, name, attrs):
    super().startElement(name, attrs)
    new_attrs = dict(attrs)
    if name == 'clip':
      self._in_clip = True
      if self._cur_track == self._coords['track'] and self._cur_clip == self._coords['clip']:
        for k in self._clipdata:
          if k == 'slices':
            continue
          new_attrs[k] = str(self._clipdata[k])
        self._output.startElement(name, new_attrs)
        self._output.characters('\n                ')
        self._output.startElement('slices', {})
        for sl in self._clipdata['slices']:
          self._output.characters('\n                    ')
          self._output.startElement('slice', {'pos': sl})
          self._output.endElement('slice')
        if self._clipdata['slices']:
          self._output.characters('\n                ')
        self._output.endElement('slices')
        return
    if name.startswith('slice'):
      if self._cur_track == self._coords['track'] and self._cur_clip == self._coords['clip']:
        return
    self._output.startElement(name, new_attrs)

  def endElement(self, name):
    super().endElement(name)
    # To make whitespace preservation work, we say we're done with a clip after
    # we see the close tag for the slices.
    if name == 'slices':
      self._in_clip = False
    if name.startswith('slice'):
      if self._cur_track == self._coords['track'] and self._cur_clip == self._coords['clip']:
        return
    self._output.endElement(name)

  def characters(self, content):
    if self._in_clip:
      if self._cur_track == self._coords['track'] and self._cur_clip == self._coords['clip']:
        return
    self._output.characters(content)
Esempio n. 50
0
 def xml_node( self, node ):
     if node not in self.nodes:
         raise ValueError("Unknown node.")
     output = cStringIO.StringIO()
     gen = XMLGenerator( output, 'utf-8' )
     gen.startDocument()
     output.write('<!DOCTYPE storage-mapping>\n')
     gen.startElement('storage-mapping',{})
     gen.characters('\n\t\t')
     self.nodes[node].xml(gen)
     gen.endElement('storage-mapping')
     gen.characters('\n')
     gen.endDocument()
     cherrypy.response.headers['Content-Type'] = 'text/xml'
     return output.getvalue()
Esempio n. 51
0
def get_check_result_as_xml(status, comment=None):
    out = StringIO.StringIO("")
    g = XMLGenerator(out, encoding='UTF-8')
    g.startDocument()
    g.startElement("response", {})
    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()
Esempio n. 52
0
 def xml_node(self, node):
     if node not in self.nodes:
         raise ValueError("Unknown node.")
     output = StringIO.StringIO()
     gen = XMLGenerator(output, 'utf-8')
     gen.startDocument()
     output.write('<!DOCTYPE storage-mapping>\n')
     gen.startElement('storage-mapping', {})
     gen.characters('\n\t\t')
     self.nodes[node].xml(gen)
     gen.endElement('storage-mapping')
     gen.characters('\n')
     gen.endDocument()
     cherrypy.response.headers['Content-Type'] = 'text/xml'
     return output.getvalue()
Esempio n. 53
0
 def endElement(self, name):
     if self.founditem:
         if name == "user":
             self.founditem = False
             XMLGenerator.startElement(self, 'user', self.user_attrs)
             for id_, naam, tekst in self.cats:
                 XMLGenerator.startElement(self, "categorie", {
                     "id": str(id_),
                     "naam": naam
                 })
                 XMLGenerator.characters(self, tekst)
                 XMLGenerator.endElement(self, "categorie")
             XMLGenerator.endElement(self, 'user')
     else:
         XMLGenerator.endElement(self, name)
Esempio n. 54
0
class _PrettyXMLWriter(object):
    def __init__(self,
                 output,
                 encoding='UTF-8',
                 namespace=('bibtex', 'http://bibtexml.sf.net/'),
                 header=True):
        self.prefix, self.uri = namespace
        self.generator = XMLGenerator(output, encoding=encoding)
        if header:
            self.generator.startDocument()
        self.generator.startPrefixMapping(self.prefix, self.uri)
        self.stack = []

    def write(self, data):
        self.generator.characters(data)

    def newline(self):
        self.write('\n')

    def indent_line(self):
        self.write(' ' * (len(self.stack) * 4))

    def start(self, tag, attrs=None, newline=True):
        if attrs is None:
            attrs = {}
        else:
            attrs = {(None, key): value for key, value in attrs.items()}
        self.indent_line()
        self.stack.append(tag)
        self.generator.startElementNS((self.uri, tag), tag,
                                      AttributesImpl(attrs))
        if newline:
            self.newline()

    def end(self, indent=True):
        tag = self.stack.pop()
        if indent:
            self.indent_line()
        self.generator.endElementNS((self.uri, tag), tag)
        self.newline()

    def element(self, tag, data):
        self.start(tag, newline=False)
        self.write(data)
        self.end(indent=False)

    def close(self):
        self.generator.endDocument()
Esempio n. 55
0
    def xml(self):
        """generate XML version of a configuration message"""
        out = StringIO()
        generator = XMLGenerator(out)

        # "stringify" entries for XML conversion
        state = {}
        for k in self.attr:
            state[k] = str(getattr(self, k))

        generator.startElement('message', state)
        generator.characters(self.data)
        generator.endElement('message')
        xml_msg = out.getvalue()
        out.close()
        return xml_msg
Esempio n. 56
0
    def test_xmlgen_encoding(self):
        encodings = ('iso-8859-15', 'utf-8', 'utf-8-sig',
                     'utf-16', 'utf-16be', 'utf-16le',
                     'utf-32', 'utf-32be', 'utf-32le')
        for encoding in encodings:
            result = self.ioclass()
            gen = XMLGenerator(result, encoding=encoding)

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

            self.assertEqual(result.getvalue(),
                self.xml('<doc a="\u20ac">\u20ac</doc>', encoding=encoding))
Esempio n. 57
0
 def startDocument( self, output, encoding='UTF-8' ):
   gen =  XMLGenerator( output, encoding )
   gen.startDocument()
   try:
       static_location = '/static/content'
       static_object = self.globals['static']
       static_location = static_object.metadata.get('base_url','/static')
       static_location += '/content'
   except:
       pass
   output.write('<?xml-stylesheet type="text/xsl" href="%s/%s"?>\n' % \
       (static_location, self.XSLT_NAME) )
   output.write('<!DOCTYPE graphtool-data>\n')
   gen.startElement('graphtool',{})
   gen.characters("\n\t")
   return gen
Esempio n. 58
0
    def test_xmlgen_encoding(self):
        encodings = ('iso-8859-15', 'utf-8',
                     'utf-16be', 'utf-16le',
                     'utf-32be', 'utf-32le')
        for encoding in encodings:
            result = self.ioclass()
            gen = XMLGenerator(result, encoding=encoding)

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

            self.assertEqual(result.getvalue(), (
                u'<?xml version="1.0" encoding="%s"?>\n'
                u'<doc a="\u20ac">\u20ac</doc>' % encoding
                ).encode(encoding, 'xmlcharrefreplace'))