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>'))
Exemple #2
0
def manifest_xml(f, files):
    from xml.sax.saxutils import XMLGenerator
    xml = XMLGenerator(f, 'utf-8')
    xml.startDocument()

    uri = 'urn:oasis:names:tc:opendocument:xmlns:manifest:1.0'
    prefix = 'manifest'
    xml.startPrefixMapping(prefix, uri)

    def startElement(name, attrs):
        attrs = dict(((uri, n), v) for n, v in attrs.iteritems())
        xml.startElementNS((uri, name), prefix + ':' + name, attrs)

    def endElement(name):
        xml.endElementNS((uri, name), prefix + ':' + name)

    def file_entry(full_path, media_type, **kwargs):
        attrs = {'media-type': media_type, 'full-path': full_path}
        attrs.update(
            dict((n.replace('_', '-'), v) for n, v in kwargs.iteritems()))
        startElement('file-entry', attrs)
        endElement('file-entry')

    startElement('manifest', dict(version='1.2'))
    file_entry('/', 'application/vnd.oasis.opendocument.text', version='1.2')
    for e in files:
        e = dict(e)
        full_path = e.pop('full_path')
        media_type = e.pop('media_type', 'application/octet-stream')
        file_entry(full_path, media_type)
    endElement('manifest')

    xml.endPrefixMapping(prefix)
    xml.endDocument()
Exemple #3
0
def manifest_xml(f, files):
    from xml.sax.saxutils import XMLGenerator
    xml = XMLGenerator(f, 'utf-8')
    xml.startDocument()

    uri = 'urn:oasis:names:tc:opendocument:xmlns:manifest:1.0'
    prefix = 'manifest'
    xml.startPrefixMapping(prefix, uri)

    def startElement(name, attrs):
        attrs = dict( ((uri, n), v) for n, v in attrs.iteritems() )
        xml.startElementNS( (uri, name), prefix+':'+name, attrs)
    def endElement(name):
        xml.endElementNS( (uri, name), prefix+':'+name)

    def file_entry(full_path, media_type, **kwargs):
        attrs = {'media-type': media_type, 'full-path': full_path}
        attrs.update(dict((n.replace('_', '-'), v) for n, v in kwargs.iteritems()))
        startElement('file-entry', attrs)
        endElement('file-entry')

    startElement( 'manifest', dict(version='1.2') )
    file_entry('/', 'application/vnd.oasis.opendocument.text', version='1.2')
    for e in files:
        e = dict(e)
        full_path = e.pop('full_path')
        media_type = e.pop('media_type', 'application/octet-stream')
        file_entry(full_path, media_type)
    endElement( 'manifest' )

    xml.endPrefixMapping(prefix)
    xml.endDocument()
Exemple #4
0
def manifest_xml(f, files):
    from xml.sax.saxutils import XMLGenerator

    xml = XMLGenerator(f, "utf-8")
    xml.startDocument()

    uri = "urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"
    prefix = "manifest"
    xml.startPrefixMapping(prefix, uri)

    def startElement(name, attrs):
        attrs = dict(((uri, n), v) for n, v in attrs.iteritems())
        xml.startElementNS((uri, name), prefix + ":" + name, attrs)

    def endElement(name):
        xml.endElementNS((uri, name), prefix + ":" + name)

    def file_entry(full_path, media_type, **kwargs):
        attrs = {"media-type": media_type, "full-path": full_path}
        attrs.update(dict((n.replace("_", "-"), v) for n, v in kwargs.iteritems()))
        startElement("file-entry", attrs)
        endElement("file-entry")

    startElement("manifest", dict(version="1.2"))
    file_entry("/", "application/vnd.oasis.opendocument.text", version="1.2")
    for e in files:
        e = dict(e)
        full_path = e.pop("full_path")
        media_type = e.pop("media_type", "application/octet-stream")
        file_entry(full_path, media_type)
    endElement("manifest")

    xml.endPrefixMapping(prefix)
    xml.endDocument()
Exemple #5
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 = self.ioclass()
        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(),
            self.xml('<a:g1 xmlns:a="http://example.com/ns">'
                     '<a:g2 xml:lang="en">Hello</a:g2>'
                     '</a:g1>'))
Exemple #6
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>"),
        )
 def test_1463026_2_empty(self):
     result = self.ioclass()
     gen = XMLGenerator(result, short_empty_elements=True)
     gen.startDocument()
     gen.startPrefixMapping(None, 'qux')
     gen.startElementNS(('qux', 'a'), 'a', {})
     gen.endElementNS(('qux', 'a'), 'a')
     gen.endPrefixMapping(None)
     gen.endDocument()
     self.assertEqual(result.getvalue(), self.xml('<a xmlns="qux"/>'))
Exemple #8
0
def codebook_xml(tags, file):
    """Export a codebook in REFI-QDA format for the given tags.
    """
    with contextlib.ExitStack() as stack:
        if not hasattr(file, 'write'):
            file = stack.enter_context(open(file, 'wb'))

        # http://schema.qdasoftware.org/versions/Codebook/v1.0/Codebook.xsd
        output = XMLGenerator(
            file,
            encoding='utf-8',
            short_empty_elements=True,
        )
        output.startDocument()
        output.startPrefixMapping(None, 'urn:QDA-XML:codebook:1.0')
        output.startElementNS(
            (None, 'CodeBook'),
            'CodeBook',
            AttributesNSImpl({(None, 'origin'): 'Taguette %s' % version},
                             {(None, 'origin'): 'origin'}),
        )
        output.startElementNS(
            (None, 'Codes'),
            'Codes',
            AttributesNSImpl({}, {}),
        )
        for tag in tags:
            guid = uuid.uuid5(TAGUETTE_NAMESPACE, tag.path)
            guid = str(guid).upper()
            output.startElementNS(
                (None, 'Code'),
                'Code',
                AttributesNSImpl(
                    {
                        (None, 'guid'): guid,
                        (None, 'name'): tag.path,
                        (None, 'isCodable'): 'true'
                    }, {
                        (None, 'guid'): 'guid',
                        (None, 'name'): 'name',
                        (None, 'isCodable'): 'isCodable'
                    }),
            )
            output.endElementNS((None, 'Code'), 'Code')
        output.endElementNS((None, 'Codes'), 'Codes')
        output.startElementNS(
            (None, 'Sets'),
            'Sets',
            AttributesNSImpl({}, {}),
        )
        output.endElementNS((None, 'Sets'), 'Sets')
        output.endElementNS((None, 'CodeBook'), 'CodeBook')
        output.endPrefixMapping(None)
        output.endDocument()
 def test_1463026_3(self):
     result = self.ioclass()
     gen = XMLGenerator(result)
     gen.startDocument()
     gen.startPrefixMapping('my', 'qux')
     gen.startElementNS(('qux', 'a'), 'a', {(None, 'b'): 'c'})
     gen.endElementNS(('qux', 'a'), 'a')
     gen.endPrefixMapping('my')
     gen.endDocument()
     self.assertEqual(result.getvalue(),
                      self.xml('<my:a xmlns:my="qux" b="c"></my:a>'))
Exemple #10
0
    def test_1463026_2_empty(self):
        result = self.ioclass()
        gen = XMLGenerator(result, short_empty_elements=True)

        gen.startDocument()
        gen.startPrefixMapping(None, 'qux')
        gen.startElementNS(('qux', 'a'), 'a', {})
        gen.endElementNS(('qux', 'a'), 'a')
        gen.endPrefixMapping(None)
        gen.endDocument()

        self.assertEqual(result.getvalue(), self.xml('<a xmlns="qux"/>'))
Exemple #11
0
def test_1463026_3():
    result = StringIO()
    gen = XMLGenerator(result)

    gen.startDocument()
    gen.startPrefixMapping('my', 'qux')
    gen.startElementNS(('qux', 'a'), 'a', {(None, 'b'): 'c'})
    gen.endElementNS(('qux', 'a'), 'a')
    gen.endPrefixMapping('my')
    gen.endDocument()

    return result.getvalue() == start + '<my:a xmlns:my="qux" b="c"></my:a>'
Exemple #12
0
    def test_1463026_3_empty(self):
        result = StringIO()
        gen = XMLGenerator(result, short_empty_elements=True)

        gen.startDocument()
        gen.startPrefixMapping("my", "qux")
        gen.startElementNS(("qux", "a"), "a", {(None, "b"): "c"})
        gen.endElementNS(("qux", "a"), "a")
        gen.endPrefixMapping("my")
        gen.endDocument()

        self.assertEqual(result.getvalue(), start + '<my:a xmlns:my="qux" b="c"/>')
Exemple #13
0
def test_1463026_2():
    result = StringIO()
    gen = XMLGenerator(result)

    gen.startDocument()
    gen.startPrefixMapping(None, "qux")
    gen.startElementNS(("qux", "a"), "a", {})
    gen.endElementNS(("qux", "a"), "a")
    gen.endPrefixMapping(None)
    gen.endDocument()

    return result.getvalue() == start + '<a xmlns="qux"></a>'
Exemple #14
0
def test_1463026_3():
    result = StringIO()
    gen = XMLGenerator(result)

    gen.startDocument()
    gen.startPrefixMapping("my", "qux")
    gen.startElementNS(("qux", "a"), "a", {(None, "b"): "c"})
    gen.endElementNS(("qux", "a"), "a")
    gen.endPrefixMapping("my")
    gen.endDocument()

    return result.getvalue() == start + '<my:a xmlns:my="qux" b="c"></my:a>'
Exemple #15
0
def test_1463026_2():
    result = StringIO()
    gen = XMLGenerator(result)

    gen.startDocument()
    gen.startPrefixMapping(None, 'qux')
    gen.startElementNS(('qux', 'a'), 'a', {})
    gen.endElementNS(('qux', 'a'), 'a')
    gen.endPrefixMapping(None)
    gen.endDocument()

    return result.getvalue() == start + '<a xmlns="qux"></a>'
    def test_1463026_2(self):
        result = StringIO()
        gen = XMLGenerator(result)

        gen.startDocument()
        gen.startPrefixMapping(None, 'qux')
        gen.startElementNS(('qux', 'a'), 'a', {})
        gen.endElementNS(('qux', 'a'), 'a')
        gen.endPrefixMapping(None)
        gen.endDocument()

        self.assertEqual(result.getvalue(), start+'<a xmlns="qux"></a>')
def test_1463026_3():
    result = StringIO()
    gen = XMLGenerator(result)

    gen.startDocument()
    gen.startPrefixMapping('my', 'qux')
    gen.startElementNS(('qux', 'a'), 'a', {(None, 'b'):'c'})
    gen.endElementNS(('qux', 'a'), 'a')
    gen.endPrefixMapping('my')
    gen.endDocument()

    return result.getvalue() == start+'<my:a xmlns:my="qux" b="c"></my:a>'
Exemple #18
0
    def test_1463026_2(self):
        result = self.ioclass()
        gen = XMLGenerator(result)

        gen.startDocument()
        gen.startPrefixMapping(None, 'qux')
        gen.startElementNS(('qux', 'a'), 'a', {})
        gen.endElementNS(('qux', 'a'), 'a')
        gen.endPrefixMapping(None)
        gen.endDocument()

        self.assertEqual(result.getvalue(), start + '<a xmlns="qux"></a>')
    def test_1463026_3_empty(self):
        result = StringIO()
        gen = XMLGenerator(result, short_empty_elements=True)

        gen.startDocument()
        gen.startPrefixMapping('my', 'qux')
        gen.startElementNS(('qux', 'a'), 'a', {(None, 'b'):'c'})
        gen.endElementNS(('qux', 'a'), 'a')
        gen.endPrefixMapping('my')
        gen.endDocument()

        self.assertEqual(result.getvalue(),
            start+'<my:a xmlns:my="qux" b="c"/>')
Exemple #20
0
    def test_1463026_3(self):
        result = self.ioclass()
        gen = XMLGenerator(result)

        gen.startDocument()
        gen.startPrefixMapping('my', 'qux')
        gen.startElementNS(('qux', 'a'), 'a', {(None, 'b'):'c'})
        gen.endElementNS(('qux', 'a'), 'a')
        gen.endPrefixMapping('my')
        gen.endDocument()

        self.assertEqual(result.getvalue(),
            self.xml('<my:a xmlns:my="qux" b="c"></my:a>'))
Exemple #21
0
    def test_1463026_3_empty(self):
        result = StringIO()
        gen = XMLGenerator(result, short_empty_elements=True)

        gen.startDocument()
        gen.startPrefixMapping('my', 'qux')
        gen.startElementNS(('qux', 'a'), 'a', {(None, 'b'):'c'})
        gen.endElementNS(('qux', 'a'), 'a')
        gen.endPrefixMapping('my')
        gen.endDocument()

        self.assertEqual(result.getvalue(),
            start+'<my:a xmlns:my="qux" b="c"/>')
 def test_xmlgen_ns_empty(self):
     result = self.ioclass()
     gen = XMLGenerator(result, short_empty_elements=True)
     gen.startDocument()
     gen.startPrefixMapping('ns1', ns_uri)
     gen.startElementNS((ns_uri, 'doc'), 'ns1:doc', {})
     gen.startElementNS((None, 'udoc'), None, {})
     gen.endElementNS((None, 'udoc'), None)
     gen.endElementNS((ns_uri, 'doc'), 'ns1:doc')
     gen.endPrefixMapping('ns1')
     gen.endDocument()
     self.assertEqual(
         result.getvalue(),
         self.xml('<ns1:doc xmlns:ns1="%s"><udoc/></ns1:doc>' % ns_uri))
Exemple #23
0
    def test_xmlgen_ns_empty(self):
        result = StringIO()
        gen = XMLGenerator(result, short_empty_elements=True)

        gen.startDocument()
        gen.startPrefixMapping("ns1", ns_uri)
        gen.startElementNS((ns_uri, "doc"), "ns1:doc", {})
        # add an unqualified name
        gen.startElementNS((None, "udoc"), None, {})
        gen.endElementNS((None, "udoc"), None)
        gen.endElementNS((ns_uri, "doc"), "ns1:doc")
        gen.endPrefixMapping("ns1")
        gen.endDocument()

        self.assertEqual(result.getvalue(), start + ('<ns1:doc xmlns:ns1="%s"><udoc/></ns1:doc>' % ns_uri))
Exemple #24
0
def test_xmlgen_ns():
    result = StringIO()
    gen = XMLGenerator(result)

    gen.startDocument()
    gen.startPrefixMapping("ns1", ns_uri)
    gen.startElementNS((ns_uri, "doc"), "ns1:doc", {})
    # add an unqualified name
    gen.startElementNS((None, "udoc"), None, {})
    gen.endElementNS((None, "udoc"), None)
    gen.endElementNS((ns_uri, "doc"), "ns1:doc")
    gen.endPrefixMapping("ns1")
    gen.endDocument()

    return result.getvalue() == start + ('<ns1:doc xmlns:ns1="%s"><udoc></udoc></ns1:doc>' % ns_uri)
Exemple #25
0
    def write_to(self, output):
        """Write indented XML to this file-like object."""
        generator = XMLGenerator(output, 'UTF-8', short_empty_elements=True)
        generator.startDocument()

        prefixes_used = list(
            self.add_prefixes_used(self.prefix_namespaces, set()))
        prefixes_used.sort()
        for prefix in prefixes_used:
            generator.startPrefixMapping(prefix,
                                         self.prefix_namespaces[prefix])
        self.sax_to(generator, prefix_namespaces=self.prefix_namespaces)
        for prefix in reversed(prefixes_used):
            generator.endPrefixMapping(prefix)
        generator.endDocument()
Exemple #26
0
    def test_xmlgen_ns(self):
        result = self.ioclass()
        gen = XMLGenerator(result)

        gen.startDocument()
        gen.startPrefixMapping("ns1", ns_uri)
        gen.startElementNS((ns_uri, "doc"), "ns1:doc", {})
        # add an unqualified name
        gen.startElementNS((None, "udoc"), None, {})
        gen.endElementNS((None, "udoc"), None)
        gen.endElementNS((ns_uri, "doc"), "ns1:doc")
        gen.endPrefixMapping("ns1")
        gen.endDocument()

        self.assertEqual(result.getvalue(), self.xml(
           '<ns1:doc xmlns:ns1="%s"><udoc></udoc></ns1:doc>' %
                                         ns_uri))
Exemple #27
0
    def test_xmlgen_ns_empty(self):
        result = self.ioclass()
        gen = XMLGenerator(result, short_empty_elements=True)

        gen.startDocument()
        gen.startPrefixMapping("ns1", ns_uri)
        gen.startElementNS((ns_uri, "doc"), "ns1:doc", {})
        # add an unqualified name
        gen.startElementNS((None, "udoc"), None, {})
        gen.endElementNS((None, "udoc"), None)
        gen.endElementNS((ns_uri, "doc"), "ns1:doc")
        gen.endPrefixMapping("ns1")
        gen.endDocument()

        self.assertEqual(
            result.getvalue(),
            self.xml('<ns1:doc xmlns:ns1="%s"><udoc/></ns1:doc>' % ns_uri))
Exemple #28
0
def test_xmlgen_ns():
    result = StringIO()
    gen = XMLGenerator(result)

    gen.startDocument()
    gen.startPrefixMapping("ns1", ns_uri)
    gen.startElementNS((ns_uri, "doc"), "ns1:doc", {})
    # add an unqualified name
    gen.startElementNS((None, "udoc"), None, {})
    gen.endElementNS((None, "udoc"), None)
    gen.endElementNS((ns_uri, "doc"), "ns1:doc")
    gen.endPrefixMapping("ns1")
    gen.endDocument()

    return result.getvalue() == start + \
           ('<ns1:doc xmlns:ns1="%s"><udoc></udoc></ns1:doc>' %
                                         ns_uri)
 def test_5027_2(self):
     result = self.ioclass()
     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(),
         self.xml(
             '<a:g1 xmlns:a="http://example.com/ns"><a:g2 xml:lang="en">Hello</a:g2></a:g1>'
         ))
Exemple #30
0
    def get(self, project_id):
        PROM_EXPORT.labels('codebook', 'qdc').inc()
        project, _ = self.get_project(project_id)
        tags = list(project.tags)
        self.set_header('Content-Type', 'text/xml; charset=utf-8')
        self.set_header('Content-Disposition',
                        'attachment; filename="codebook.qdc"')

        # http://schema.qdasoftware.org/versions/Codebook/v1.0/Codebook.xsd
        output = XMLGenerator(WriteAdapter(self.write),
                              encoding='utf-8',
                              short_empty_elements=True)
        output.startDocument()
        output.startPrefixMapping(None, 'urn:QDA-XML:codebook:1.0')
        refi_qda.write_codebook(tags, output)
        output.endPrefixMapping(None)
        output.endDocument()
        return self.finish()
Exemple #31
0
 def run(self):
     xml = XMLGenerator(self.out, 'UTF-8', True)
     xml.startDocument()
     xml.startPrefixMapping('', self.SVG_NAMESPACE)
     xml.startPrefixMapping(self.XLINK_PREFIX, self.XLINK_NAMESPACE)
     canvasWidth = int(self.marginWidth + (self.count - 1) * self.offset)
     attrs = self._defaultNSAttrs({
         self._svgName('version'):
         self.SVG_VERSION,
         self._svgName('width'):
         str(canvasWidth),
         self._svgName('height'):
         str(self.canvasHeight),
         self._svgName('viewBox'):
         ('%d %d %d %g' % (0, 0, canvasWidth, self.canvasHeight))
     })
     xml.startElementNS(self.SVG_ELEMENT, None, attrs)
     self._defs(xml)
     self._contentGroup(xml)
     xml.ignorableWhitespace('\n')
     xml.endElementNS(self.SVG_ELEMENT, None)
     xml.endPrefixMapping('')
     xml.endPrefixMapping(self.XLINK_PREFIX)
     xml.endDocument()
Exemple #32
0
class SchemaGenerator(object):
    """
    This class generates XSD schema based on class description from qooxdoo API.
    Constructor takes dictinary with class description, generated by qxt.xsd.utils.ApiDataParser.
    
    """
    def __init__(self, classInfoDict):
        self.__classInfoDict = classInfoDict

    def generate(self, fileName):
        """
        Generates XSD based on given classes definitions. Writes it to specified fileName.
        """
        #TODO make proper error handling if something wrong with file.
        self.__output = open(fileName, "w")
        self.__doc = XMLGenerator(self.__output, "utf-8")
        self.__doc.startDocument()

        self.__start_schema()

        # generate attibute groups
        self.__generate_attribute_groups()

        # generate group of references

        # generate allGroup element
        self.__generate_all_group()

        # generate elements
        self.__generate_elements()

        # generate top level elements
        self.__generate_top_elements()

        self.__end_schema()

        self.__doc.endDocument()
        self.__output.close()

    def __start_schema(self):
        """Generates schema header."""

        self.__doc.startPrefixMapping(u'xsd', XSD_NS)
        self.__doc.startPrefixMapping(u'qx', QX_NS)
        self.__doc.startPrefixMapping(u'qxt', QXT_NS)

        shemaattr = AttributesNSImpl(
            {
                (None, 'targetNamespace'):
                "http://www.qxtransformer.org/qooxdoo/0.8",
                (None, 'elementFormDefault'): 'qualified',
                (None, 'attributeFormDefault'): 'unqualified'
            }, {})

        self.__doc.startElementNS((XSD_NS, u'schema'), u'xsd:schema',
                                  shemaattr)

    def __end_schema(self):
        """Generated closing tags for schema."""

        self.__doc.endElementNS((XSD_NS, u'schema'), u'xsd:schema')
        self.__doc.endPrefixMapping(u'xsd')

    def __generate_attribute_groups(self):
        """Geneartes all groups for API data.
        Iterates over al classes definitions.
        """
        for className, classInfo in self.__classInfoDict.iteritems():
            self.__generate_attribute_group(className, classInfo)

    def __generate_attribute_group(self, className, classInfo):
        """Generate an attribute group based on given classInfo for each class.
        Includes class property in group if it's not overriden from base class 
        (in other case it will be added for base class).
        """

        self.__doc.startElementNS(
            (XSD_NS, u'attributeGroup'), u'xsd:attributeGroup',
            AttributesNSImpl({(None, 'name'): className}, {}))

        #generate attrubute group only if class has any properties
        if classInfo.has_key('properties'):
            properties = classInfo['properties']
            for propName, prop in properties.iteritems():
                #include property only if it's not overriden from base class
                if not prop.get('overriddenFrom'):
                    self.__doc.startElementNS(
                        (XSD_NS, u'attribute'), u'xsd:attribute',
                        AttributesNSImpl({(None, 'name'): propName}, {}))

                    self.__doc.startElementNS((XSD_NS, u'annotation'),
                                              u'xsd:annotation',
                                              AttributesNSImpl({}, {}))
                    self.__doc.startElementNS((XSD_NS, u'documentation'),
                                              u'xsd:documentation',
                                              AttributesNSImpl({}, {}))

                    self.__doc.characters(prop.get('info', ''))

                    self.__doc.endElementNS((XSD_NS, u'documentation'),
                                            u'xsd:documentation')
                    self.__doc.endElementNS((XSD_NS, u'annotation'),
                                            u'xsd:annotation')
                    self.__doc.endElementNS((XSD_NS, u'attribute'),
                                            u'xsd:attribute')

        self.__doc.endElementNS((XSD_NS, u'attributeGroup'),
                                u'xsd:attributeGroup')

    def __generate_elements(self):
        """Generated all elements for given API data.
        Iterates over all definitions."""

        for className, classInfo in self.__classInfoDict.iteritems():

            tagName = self.__parse_tag_name(classInfo.get('tagName'))

            # generate tag only if tag name has namespace (tags without namespaces in config
            # are base tags only for inheritance inside config file)
            if tagName and tagName[0]:
                inheritance_chain = []
                self.__fill_inheritance_chain(inheritance_chain, className)

                self.__generate_element(tagName, className, classInfo,
                                        inheritance_chain,
                                        [('qx', 'allGroup')])

    def __generate_element(self, tagName, className, classInfo,
                           attributeGroups, elementGroup):
        """Generates an definition of XSD element.
        
        @param   tagName     tuple produced by __parse_tag_name
        @param   className   name of widget class
        @param   classInfo   dictionary with class information
        @param   attributeGroups   array with all attribute groups which this class includes (simply all inheritance chain)
        @param   elementGroup   array with all allowed child elements
        """

        self.__doc.startElementNS(
            (XSD_NS, u'element'), u'xsd:element',
            AttributesNSImpl({(None, 'name'): tagName[1]}, {}))

        self.__doc.startElementNS((XSD_NS, u'annotation'), u'xsd:annotation',
                                  AttributesNSImpl({}, {}))
        self.__doc.startElementNS(
            (XSD_NS, u'documentation'), u'xsd:documentation',
            AttributesNSImpl({}, {}))

        self.__doc.characters(classInfo.get('info', ''))

        self.__doc.endElementNS((XSD_NS, u'documentation'),
                                u'xsd:documentation')
        self.__doc.endElementNS((XSD_NS, u'annotation'), u'xsd:annotation')

        self.__doc.startElementNS((XSD_NS, u'complexType'), u'xsd:complexType',
                                  AttributesNSImpl({}, {}))

        #generate references for allowed child elements
        self.__doc.startElementNS((XSD_NS, u'sequence'), u'xsd:sequence',
                                  AttributesNSImpl({}, {}))

        for group in elementGroup:
            self.__doc.startElementNS(
                (XSD_NS, u'group'), u'xsd:group',
                AttributesNSImpl({(None, 'ref'): ":".join(group)}, {}))
            self.__doc.endElementNS((XSD_NS, u'group'), u'xsd:group')

        self.__doc.endElementNS((XSD_NS, u'sequence'), u'xsd:sequence')

        #generate attribute group
        for attrGroupName in attributeGroups:
            #TODO fix namespace of the generated group
            self.__doc.startElementNS(
                (XSD_NS, u'attributeGroup'), u'xsd:attributeGroup',
                AttributesNSImpl({(None, 'ref'): "qx:%s" % attrGroupName}, {}))

            self.__doc.endElementNS((XSD_NS, u'attributeGroup'),
                                    u'xsd:attributeGroup')

        self.__doc.endElementNS((XSD_NS, u'complexType'), u'xsd:complexType')
        self.__doc.endElementNS((XSD_NS, u'element'), u'xsd:element')

    def __generate_group(self, name, tagNames):
        """Generates group with references to allowed element."""

        self.__doc.startElementNS((XSD_NS, u'group'), u'xsd:group',
                                  AttributesNSImpl({(None, 'name'): name}, {}))

        self.__doc.startElementNS((XSD_NS, u'choice'), u'xsd:choice',
                                  AttributesNSImpl({}, {}))

        for tagName in tagNames:
            self.__doc.startElementNS((XSD_NS, u'element'), u'xsd:element',
                                      AttributesNSImpl(
                                          {
                                              (None, 'ref'): ':'.join(tagName),
                                              (None, 'minOccurs'): '0',
                                              (None, 'maxOccurs'): 'unbounded'
                                          }, {}))
            self.__doc.endElementNS((XSD_NS, u'element'), u'xsd:element')

        self.__doc.endElementNS((XSD_NS, u'choice'), u'xsd:choice')
        self.__doc.endElementNS((XSD_NS, u'group'), u'xsd:group')

    def __generate_all_group(self):
        """Genrates group with name 'allGroup' which includes all tags from XSD."""

        # getting all tags from class info (only with defined namespaces)
        groupTags = []
        for className, classInfo in self.__classInfoDict.iteritems():
            tagName = self.__parse_tag_name(classInfo.get('tagName'))
            # tag with defined namespace
            if tagName and tagName[0]:
                groupTags.append(tagName)

        # generate group
        self.__generate_group('allGroup', groupTags)

    def __generate_top_elements(self):
        """Generates a set of predefined top level elements."""

        self.__generate_element((None, 'application'), None, {}, [],
                                [('qx', 'allGroup')])

    def __fill_inheritance_chain(self, chain, className):
        """Fills inheritance chain for given className. 
        Chain will contain base class, base class of base class, ...
        Uses recursion.
        """

        chain.append(className)
        superClass = self.__classInfoDict[className].get('superClass')

        if superClass:
            self.__fill_inheritance_chain(chain, superClass)

    def __parse_tag_name(self, tagName):
        """Parses tag name and returns it as tuple."""

        if not tagName:
            return None

        parts = tagName.split(':')

        if len(parts) == 2:
            return (parts[0], parts[1])
        elif len(parts) == 1:
            return (None, tagName)
        else:
            return None
Exemple #33
0
def dump(obj,
         fp,
         *,
         skipkeys=False,
         check_circular=True,
         allow_nan=True,
         indent=None,
         default=None,
         sort_keys=False):
    """
    Serialize a value to a file in JSONx format.

    :param obj: Value to be serialized.
    :param fp: File-like object to write JSONx :class:`str` to.
    :param bool skipkeys: If true, then dictionary keys that are not of a basic
        type (:class:`str`, :class:`int`, :class:`float`, :class:`bool`,
        ``None``) will be skipped. Otherwise, a :exc:`TypeError` is raised.
        (Default: ``False``)
    :param bool check_circular: If false, then the circular reference check for
        container types will be skipped. Otherwise, a :exc:`ValueError` is
        raised. (Default: ``True``)
    :param bool allow_nan: If false, then it will be a :exc:`ValueError` to
        serialize out-of-range float values (``nan``, ``inf``, ``-inf``).
        Otherwise, their JavaScript equivalents (``NaN``, ``Infinity``,
        ``-Infinity``) will be used. (Default: ``True``)
    :param indent: If a positive integer, then JSON array elements and object
        members will be pretty-printed with that many spaces per level. If a
        string, that string is used to indent each level. An indent level of 0,
        negative, or ``""`` will only insert newlines. ``None`` selects the most
        compact representation. (Default: ``None``)
    :type indent: int or str
    :param default: If specified, it must be a function that gets called for
        objects that can’t otherwise be serialized, and it must return an
        encodable version of the object. If not specified, :exc:`TypeError` is
        raised. (Default: ``None``)
    :param bool sort_keys: If true, then the output of dictionaries will be
        sorted by key. (Default: ``False``)
    """
    def _attrs(name=None):
        return AttributesNSImpl(
            {(None, 'name'): name} if name is not None else {}, None)

    def _str(value):
        if value is None:
            return 'null'
        if isinstance(value, bool):
            return 'true' if value else 'false'
        if isinstance(value, float):
            if isnan(value):
                return 'NaN'
            if isinf(value):
                return 'Infinity' if value > 0 else '-Infinity'
        return str(value)

    def _dump(value, name=None):
        nonlocal stack, level
        if indent is not None:
            gen.ignorableWhitespace(indent * level)

        if isinstance(value, dict):
            if check_circular:
                if id(value) in stack:
                    raise ValueError('container has circular reference')
                stack.add(id(value))

            gen.startElementNS((JSONX_NS_URI, 'object'),
                               None,
                               attrs=_attrs(name))

            if value:
                if indent is not None:
                    gen.ignorableWhitespace('\n')
                level += 1

                for k, v in sorted(
                        value.items(),
                        key=lambda kv: kv[0]) if sort_keys else value.items():
                    if k is not None and not isinstance(
                            k, (str, int, float, bool)):
                        if not skipkeys:
                            raise TypeError(
                                'dictionary key is not of a basic type: %r' %
                                (k, ))
                        continue
                    _dump(v, name=_str(k))

                level -= 1
                if indent is not None:
                    gen.ignorableWhitespace(indent * level)

            gen.endElementNS((JSONX_NS_URI, 'object'), None)

            if check_circular:
                stack.remove(id(value))

        elif isinstance(value, list):
            if check_circular:
                if id(value) in stack:
                    raise ValueError('container has circular reference')
                stack.add(id(value))

            gen.startElementNS((JSONX_NS_URI, 'array'),
                               None,
                               attrs=_attrs(name))

            if value:
                if indent is not None:
                    gen.ignorableWhitespace('\n')
                level += 1

                for v in value:
                    _dump(v)

                level -= 1
                if indent is not None:
                    gen.ignorableWhitespace(indent * level)

            gen.endElementNS((JSONX_NS_URI, 'array'), None)

            if check_circular:
                stack.remove(id(value))

        elif isinstance(value, str):
            gen.startElementNS((JSONX_NS_URI, 'string'),
                               None,
                               attrs=_attrs(name))
            gen.characters(value)
            gen.endElementNS((JSONX_NS_URI, 'string'), None)

        elif isinstance(value, bool):
            gen.startElementNS((JSONX_NS_URI, 'boolean'),
                               None,
                               attrs=_attrs(name))
            gen.characters(_str(value))
            gen.endElementNS((JSONX_NS_URI, 'boolean'), None)

        elif isinstance(value, (int, float)):
            if not allow_nan and (isinf(value) or isnan(value)):
                raise ValueError('float value is out of range: %r' % value)

            gen.startElementNS((JSONX_NS_URI, 'number'),
                               None,
                               attrs=_attrs(name))
            gen.characters(_str(value))
            gen.endElementNS((JSONX_NS_URI, 'number'), None)

        elif value is None:
            gen.startElementNS((JSONX_NS_URI, 'null'),
                               None,
                               attrs=_attrs(name))
            gen.endElementNS((JSONX_NS_URI, 'null'), None)

        elif default:
            _dump(default(value), name=name)

        else:
            raise TypeError('cannot serialize object: %r' % (value, ))

        if indent is not None:
            gen.ignorableWhitespace('\n')

    stack = set()
    level = 0
    if indent is not None and isinstance(indent, int):
        indent = ' ' * indent if indent > 0 else ''

    gen = XMLGenerator(fp, encoding='UTF-8', short_empty_elements=True)
    gen.startDocument()
    gen.startPrefixMapping(JSONX_PREFIX, JSONX_NS_URI)
    _dump(obj)
    gen.endPrefixMapping(JSONX_PREFIX)
    gen.endDocument()
Exemple #34
0
class ClipExtractor(ContentHandler):
    def __init__(self, output, clipId):
        self._sink = XMLGenerator(output, 'UTF-8', True)
        self._locator = None
        self._id = clipId

    def setDocumentLocator(self, locator):
        self._locator = locator
        self._skip = None
        self._stopAt = None
        return self._sink.setDocumentLocator(locator)

    def startDocument(self):
        #         self._prefixes = {}
        self.scale = self.units = None
        self._context = []
        self._outerSvgRendered = False
        return self._sink.startDocument()

    def endDocument(self):
        #         self._prefixes.clear()
        return self._sink.endDocument()

    def startPrefixMapping(self, prefix, uri):
        self._context.append(('xmlns', None, prefix, uri))
        #         mappings = self._prefixes.get(prefix)
        #         if mappings is None:
        #             self._prefixes[prefix] = mappings = []
        #         mappings.append(uri)
        return self._sink.startPrefixMapping(prefix, uri)

    def endPrefixMapping(self, prefix):
        context = self._context.pop()
        assert ('xmlns', None, prefix) == context[:-1]
        #         mappings = self._prefixes.get(prefix)
        #         assert mappings is not None
        #         mappings.pop()
        return self._sink.endPrefixMapping(prefix)

    def startElement(self, qname, attrs):
        raise xml.sax.SAXNotSupportedException(
            'This handler must be used with feature "%s"'
            ' turned on' % xml.sax.handler.feature_namespaces)
#         if 'xmlns' in attrs:
#             if self._context:
#                 raise xml.sax.SAXNotSupportedException(
#                     'This document must be parsed with feature "%s"'
#                     ' turned on'
#                     % xml.sax.handler.feature_namespaces
#                 )
#             else:
#                 self.startPrefixMapping('', attrs.get('xmlns'))
#         ns, name = self._splitNS(qname)
#         handler = self.ELEMENTS.get((ns, name))
#         if handler and handler[0]:
#             update = handler[0](self, attrs, ns, name)
#             if not update:
#                 if self._skip is None:
#                     self._skip = len(self._context) + 1
#             elif isinstance(update, collections.Sequence):
#                 attrs = update[0]
#                 if len(update) > 1:
#                     ns, name, qname = update[1:]
#         self._context.append((ns, name, qname, attrs.copy()))
#         if self._skip is None:
#             return self._sink.startElement(qname, attrs)

    def endElement(self, qname):
        raise xml.sax.SAXNotSupportedException(
            'This handler must be used with feature "%s"'
            ' turned on' % xml.sax.handler.feature_namespaces)
#         ns, name = self._splitNS(qname)
#         handler = self.ELEMENTS.get((ns, name))
#         if handler and handler[1]:
#             handler[1](self, ns, name)
#         context = self._context.pop()
#         assert (ns, name) == context[:2]
#         if self._skip is None:
#             self._sink.endElement(qname)
#         elif len(self._context) < self._skip:
#             self._skip = None
#         if len(self._context) == self._stopAt:
#             raise self.Done('extraction complete',
#                             self._locator.getLineNumber(),
#                             self._locator.getColumnNumber()
#                         )

    def startElementNS(self, name, qname, attrs):
        if (None, 'xmlns') in attrs:
            self.startPrefixMapping(None, attrs.get('xmlns'))
        handler = self.ELEMENTS.get(name)
        if handler and handler[0]:
            update = handler[0](self, attrs, *name, qname)
            if not update:
                if self._skip is None:
                    self._skip = len(self._context) + 1
            elif isinstance(update, collections.Sequence):
                attrs = update[0]
                if len(update) > 1:
                    name = tuple(update[1:3])
                if len(update) > 3:
                    qname = update[3]
        self._context.append(name + (qname, attrs.copy()))
        if self._skip is None:
            return self._sink.startElementNS(name, qname, attrs)

    def endElementNS(self, name, qname):
        handler = self.ELEMENTS.get(name)
        if handler and handler[1]:
            handler[1](self, *name, qname)
        context = self._context.pop()
        assert name == context[:2]
        if self._skip is None:
            self._sink.endElementNS(name, qname)
        elif len(self._context) < self._skip:
            self._skip = None
        if len(self._context) == self._stopAt:
            while self._context:
                toClose = self._context.pop()
                if 'xmlns' == toClose[0]:
                    self._sink.endPrefixMapping(toClose[2])
                else:
                    self._sink.ignorableWhitespace('\n')
                    self._sink.endElementNS(toClose[:2], toClose[2])
            self._sink.endDocument()
            raise self.Done('extraction complete',
                            (self._locator.getLineNumber(),
                             self._locator.getColumnNumber()))

    def elementSvgEnd(self, *args):
        attrs = self._context[-1][3]
        if self.ATTR_ID in attrs and \
                self._id == attrs.get(self.ATTR_ID):
            self._stopAt = len(self._context) - 1

    def elementSvgStart(self, attrs, *args):
        if any((CardBackView.SVG_NAMESPACE, 'svg') == e[:2]
               for e in self._context):
            return self.ATTR_ID in attrs and \
                self._id == attrs.get(self.ATTR_ID)
        elif not 'viewBox' in attrs and not (None, 'viewBox') in attrs:
            self._error(None, 'Main <svg> element has no viewBox attribute')
        else:
            try:
                internalSize = attrs.get((None, 'viewBox'))
                if internalSize is None:
                    internalSize = attrs.get('viewBox')
                internalSize = self.parseBox(internalSize)[2:]
                externalSize = list(internalSize)
                units = [''] * 2
                i = -1
                for attr in CardBackView.ATTRS_SVG_DIMENSION:
                    i += 1
                    value, unit = self._distanceAttr(attrs, attr)
                    if unit.strip() == '%':
                        value = None
                    if value is not None:
                        externalSize[i] = value
                        units[i] = unit
                self.units = tuple(units)
                try:
                    self.scale = tuple(
                        e / i for e, i in zip(externalSize, internalSize))
                except ZeroDivisionError as error:
                    self._error(
                        None,
                        'Found <svg> with zero dimension(s), viewBox="%s"' %
                        attrs.get((None, 'viewBox')))
                return False
            except:
                self._error()

    def elementUseStart(self, attrs, ns, name, qname):
        target = attrs.get(CardBackView.XLINK_HREF_ATTR)
        if not target:
            return False
        target = target.strip()
        if '#' == target[0] and self._id == target[1:] \
             and not self._outerSvgRendered:
            try:
                outerSvg = next(e for e in self._context
                                if (CardBackView.SVG_NAMESPACE,
                                    'svg') == e[:2])
            except StopIteration:
                self._error(None, 'Found <use> element outside of an <svg>')
            size = []
            for attr in CardBackView.ATTRS_SVG_DIMENSION:
                value, unit = self._distanceAttr(attrs, attr)
                if unit.strip():
                    self._error(
                        None, 'Attribute <use %s="%s" ...> is not valid'
                        ' in this context, expected a unit-free number.' %
                        (attr, attrs.get(None, attr)))
                if value is None:
                    self._error(
                        None, 'Attribute `%s` is missing from <use> element,'
                        ' but is expected in this context.' % attr)
                size.append(value)
            sattrs = ('%.6f%s' % (s * v, u)
                      for s, v, u in zip(self.scale, size, self.units))
            qnames = {
                name: outerSvg[3].getQNameByName(name)
                for name in outerSvg[3].getNames()
            }
            attrMap = dict(outerSvg[3].items())
            attrMap[(None, 'viewBox')] = self.boxValue((0, 0) + tuple(size))
            for attr in zip(CardBackView.ATTRS_SVG_DIMENSION, sattrs):
                attrMap[(None, attr[0])] = attr[1]
            self._sink.startElementNS(outerSvg[:2], outerSvg[2],
                                      AttributesNSImpl(attrMap, qnames))
            self._sink.ignorableWhitespace('\n')
            self._outerSvgRendered = True
            qnames = {
                name: attrs.getQNameByName(name)
                for name in attrs.getNames()
            }
            attrMap = dict(attrs.items())
            for attr in ('x', 'y') + CardBackView.ATTRS_SVG_DIMENSION:
                qnames.pop((None, attr), None)
                attrMap.pop((None, attr), None)
            self._sink.startElementNS((ns, name), qname,
                                      AttributesNSImpl(attrMap, qnames))
            self._sink.endElementNS((ns, name), qname)
            self._sink.ignorableWhitespace('\n')
        return False

    def elementDefsStart(self, attrs, *args):
        if not self._outerSvgRendered:
            self._error(
                None, 'Element <use xlink:href="#%s" ...> must precede'
                ' the <defs> element in this context.' % self._id)
        self._skip = None
        return True

    ATTR_ID = (None, 'id')

    ELEMENTS = {
        (CardBackView.SVG_NAMESPACE, 'svg'): (elementSvgStart, elementSvgEnd),
        (CardBackView.SVG_NAMESPACE, 'use'): (elementUseStart, None),
        (CardBackView.SVG_NAMESPACE, 'defs'): (elementDefsStart, None),
    }

    def characters(self, content):
        if self._skip is None:
            return self._sink.characters(content)

    def ignorableWhitespace(self, whitespace):
        if self._skip is None:
            return self._sink.ignorableWhitespace(whitespace)

    def processingInstruction(self, target, data):
        if self._skip is None:
            return self._sink.processingInstruction(target, data)

    def skippedEntity(self, name):
        if self._skip is None:
            return self._sink.skippedEntity(name)

    def _error(self, error=True, message=''):
        if error == True:
            error = sys.exc_info()[1]
        if error is None:
            assert message
            error = SAXParseException(message, None, self._locator)
        elif not isinstance(error, SAXParseException):
            msg = error.args[0] if error.args else ''
            if msg:
                msg += ' '
            if message:
                msg += message + ' '
            error.args = (msg + 'at line %d, column %d.' %
                          (self._locator.getLineNumber(),
                           self._locator.getColumnNumber()), ) + error.args[1:]
        raise error

    class Done(xml.sax.SAXException):
        pass
Exemple #35
0
# regression test for SAX 2.0