Example #1
0
    def test_spaces_in_tagname(self):
        buffer = StringIO()
        with self.assertRaises(ValueError):
            with elementflow.xml(buffer, u'root with space', indent = True, text_wrap = False) as xml:
                pass

        buffer = StringIO()
        with self.assertRaises(ValueError):
            with elementflow.xml(buffer, u'root', indent = True, text_wrap = False) as xml:
                xml.container(u'element with space')
Example #2
0
    def test_indent(self):
        buffer = BytesIO()
        with elementflow.xml(buffer, u'root', indent = True) as xml:
            with xml.container(u'a'):
                xml.element(u'b', text = ''.join(['blah '] * 20))
                xml.comment(u' '.join(['comment'] * 20))
        buffer.seek(0)
        self.assertEqual(
            buffer.getvalue(),
            _bytes(
"""<?xml version="1.0" encoding="utf-8"?>
<root>
  <a>
    <b>
      blah blah blah blah blah blah blah blah blah blah blah
      blah blah blah blah blah blah blah blah blah
    </b>
    <!--
      comment comment comment comment comment comment comment
      comment comment comment comment comment comment comment
      comment comment comment comment comment comment
    -->
  </a>
</root>
"""))
Example #3
0
 def test_map(self):
     data = [(1, u'One'), (2, u'Two'), (3, u'Three')]
     buffer = BytesIO()
     with elementflow.xml(buffer, u'root') as xml:
         xml.map(lambda item: (
             'item',
             {'key': str(item[0])},
             item[1],
         ), data)
         xml.map(lambda item: (item[1],), data)
     buffer.seek(0)
     tree = ET.parse(buffer)
     buffer = BytesIO()
     tree.write(buffer, encoding='utf-8')
     self.assertEqual(
       buffer.getvalue(),
       _bytes(
           '<root>'
             '<item key="1">One</item>'
             '<item key="2">Two</item>'
             '<item key="3">Three</item>'
             '<One /><Two /><Three />'
           '</root>'
       )
     )
Example #4
0
 def test_comment(self):
     buffer = StringIO()
     with elementflow.xml(buffer, u'root') as xml:
         xml.comment(u'comment')
     buffer.seek(0)
     self.assertEqual(
         buffer.getvalue(),
         '<?xml version="1.0" encoding="utf-8"?><root><!--comment--></root>'
     )
Example #5
0
 def test_comment(self):
     buffer = StringIO()
     with elementflow.xml(buffer, u'root') as xml:
         xml.comment(u'comment')
     buffer.seek(0)
     self.assertEqual(
         buffer.getvalue(),
         '<?xml version="1.0" encoding="utf-8"?><root><!--comment--></root>'
     )
Example #6
0
 def test_comment_with_double_hyphen(self):
     buffer = BytesIO()
     with elementflow.xml(buffer, u'root') as xml:
         xml.comment(u'--comm-->ent--')
     buffer.seek(0)
     self.assertEqual(
         buffer.getvalue(),
         _bytes('<?xml version="1.0" encoding="utf-8"?><root><!--comm>ent--></root>')
     )
Example #7
0
def ef_generator(file, count):
    with elementflow.xml(file, 'contacts') as xml:
        for i in range(count):
            with xml.container('person', {'id': unicode(i)}):
                xml.element('name', text='John & Smith')
                xml.element('email', text='*****@*****.**')
                with xml.container('phones'):
                    xml.element('phone', {'type': 'work'}, text='123456')
                    xml.element('phone', {'type': 'home'}, text='123456')
Example #8
0
def ef_generator(file, count):
    with elementflow.xml(file, 'contacts') as xml:
        for i in range(count):
            with xml.container('person', {'id': unicode(i)}):
                xml.element('name', text='John & Smith')
                xml.element('email', text='*****@*****.**')
                with xml.container('phones'):
                    xml.element('phone', {'type': 'work'}, text='123456')
                    xml.element('phone', {'type': 'home'}, text='123456')
Example #9
0
 def test_non_well_formed_on_exception(self):
     buffer = BytesIO()
     try:
         with elementflow.xml(buffer, u'root') as xml:
             xml.text(u'Text')
             raise Exception()
     except:
         pass
     buffer.seek(0)
     # Parsing this buffer should cause a parsing error due to unclosed
     # root element
     self.assertRaises(SyntaxError, lambda: ET.parse(buffer))
Example #10
0
 def test_non_well_formed_on_exception(self):
     buffer = StringIO()
     try:
         with elementflow.xml(buffer, u'root') as xml:
             xml.text(u'Text')
             raise Exception()
     except:
         pass
     buffer.seek(0)
     # Parsing this buffer should cause a parsing error due to unclosed
     # root element
     self.assertRaises(SyntaxError, lambda: ET.parse(buffer))
Example #11
0
 def test_namespaces(self):
     buffer = StringIO()
     with elementflow.xml(buffer, 'root', namespaces={'': 'urn:n', 'n1': 'urn:n1'}) as xml:
         xml.element('item')
         with xml.container('n2:item', namespaces={'n2': 'urn:n2'}):
             xml.element('item')
             xml.element('n1:item')
     buffer.seek(0)
     tree = ET.parse(buffer)
     root = tree.getroot()
     self.assertEquals(root.tag, '{urn:n}root')
     self.assertNotEqual(root.find('{urn:n}item'), None)
     self.assertNotEqual(root.find('{urn:n2}item/{urn:n}item'), None)
     self.assertNotEqual(root.find('{urn:n2}item/{urn:n1}item'), None)
Example #12
0
 def test_namespaces(self):
     buffer = BytesIO()
     with elementflow.xml(buffer, 'root', namespaces={'': 'urn:n', 'n1': 'urn:n1'}) as xml:
         xml.element('item')
         with xml.container('n2:item', namespaces={'n2': 'urn:n2'}):
             xml.element('item')
             xml.element('n1:item')
     buffer.seek(0)
     tree = ET.parse(buffer)
     root = tree.getroot()
     self.assertEqual(root.tag, '{urn:n}root')
     self.assertNotEqual(root.find('{urn:n}item'), None)
     self.assertNotEqual(root.find('{urn:n2}item/{urn:n}item'), None)
     self.assertNotEqual(root.find('{urn:n2}item/{urn:n1}item'), None)
Example #13
0
    def test_indent_nowrap(self):
        buffer = StringIO()
        with elementflow.xml(buffer, u'root', indent = True, text_wrap = False) as xml:
            with xml.container(u'a'):
                xml.element(u'b', text = ''.join(['blah '] * 20))
        buffer.seek(0)
        self.assertEqual(
            buffer.getvalue(),
"""<?xml version="1.0" encoding="utf-8"?>
<root>
  <a>
    <b>blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah </b>
  </a>
</root>
""")
Example #14
0
 def test_xml(self):
     buffer = StringIO()
     with elementflow.xml(buffer, u'root') as xml:
         with xml.container(u'container', {u'key': u'"значение"'}):
             xml.text(u'<Текст> контейнера')
             xml.element(u'item')
         xml.element(u'item', text=u'Текст')
     buffer.seek(0)
     tree = ET.parse(buffer)
     buffer = StringIO()
     tree.write(buffer, encoding='utf-8')
     self.assertEqual(
         buffer.getvalue(), '<root>'
         '<container key="&quot;значение&quot;">'
         '&lt;Текст&gt; контейнера'
         '<item />'
         '</container>'
         '<item>Текст</item>'
         '</root>')
Example #15
0
 def test_xml(self):
     buffer = StringIO()
     with elementflow.xml(buffer, u'root') as xml:
         with xml.container(u'container', {u'key': u'"значение"'}):
             xml.text(u'<Текст> контейнера')
             xml.element(u'item')
         xml.element(u'item', text=u'Текст')
     buffer.seek(0)
     tree = ET.parse(buffer)
     buffer = StringIO()
     tree.write(buffer, encoding='utf-8')
     self.assertEqual(
       buffer.getvalue(),
       '<root>'
         '<container key="&quot;значение&quot;">'
           '&lt;Текст&gt; контейнера'
           '<item />'
         '</container>'
         '<item>Текст</item>'
       '</root>'
     )
Example #16
0
def convert(input_file, output_file):
    """ This function converts data from CSV file
        to the WXR.
    """
    reader = unicode_csv_reader(input_file)

    with elementflow.xml(
            output_file,
            'rss',
            attrs=dict(version='2.0'),
            namespaces=dict(
                excerpt="http://wordpress.org/export/1.0/excerpt/",
                content="http://purl.org/rss/1.0/modules/content/",
                wfw="http://wellformedweb.org/CommentAPI/",
                dc="http://purl.org/dc/elements/1.1/",
                wp="http://wordpress.org/export/1.0/",
            ),
            indent=True,
    ) as xml:
        for item in reader:
            write_item(xml, item)
Example #17
0
def convert(input_file, output_file):
    """ This function converts data from CSV file
        to the WXR.
    """
    reader = unicode_csv_reader(input_file)

    with elementflow.xml(
            output_file,
            'rss',
            attrs = dict(version = '2.0'),
            namespaces = dict(
                excerpt = "http://wordpress.org/export/1.0/excerpt/",
                content = "http://purl.org/rss/1.0/modules/content/",
                wfw = "http://wellformedweb.org/CommentAPI/",
                dc = "http://purl.org/dc/elements/1.1/",
                wp = "http://wordpress.org/export/1.0/",
            ),
            indent = True,
    ) as xml:
        for item in reader:
            write_item(xml, item)
Example #18
0
 def test_map(self):
     data = [(1, u'One'), (2, u'Two'), (3, u'Three')]
     buffer = StringIO()
     with elementflow.xml(buffer, u'root') as xml:
         xml.map(lambda (k, v): (
             'item',
             {'key': unicode(k)},
             v,
         ), data)
         xml.map(lambda (k, v): (v,), data)
     buffer.seek(0)
     tree = ET.parse(buffer)
     buffer = StringIO()
     tree.write(buffer, encoding='utf-8')
     self.assertEqual(
       buffer.getvalue(),
       '<root>'
         '<item key="1">One</item>'
         '<item key="2">Two</item>'
         '<item key="3">Three</item>'
         '<One /><Two /><Three />'
       '</root>'
     )
Example #19
0
 def test_map(self):
     data = [(1, u'One'), (2, u'Two'), (3, u'Three')]
     buffer = StringIO()
     with elementflow.xml(buffer, u'root') as xml:
         xml.map(lambda (k, v): (
             'item',
             {
                 'key': unicode(k)
             },
             v,
         ), data)
         xml.map(lambda (k, v): (v, ), data)
     buffer.seek(0)
     tree = ET.parse(buffer)
     buffer = StringIO()
     tree.write(buffer, encoding='utf-8')
     self.assertEqual(
         buffer.getvalue(), '<root>'
         '<item key="1">One</item>'
         '<item key="2">Two</item>'
         '<item key="3">Three</item>'
         '<One /><Two /><Three />'
         '</root>')
Example #20
0
 def g():
     with elementflow.xml(buffer, 'n:root', attrs={'n1:k': 'v'}, namespaces={'n': 'urn:n'}) as xml:
         pass
Example #21
0
 def g():
     with elementflow.xml(buffer, 'n:root', attrs={'n1:k': 'v'}, namespaces={'n': 'urn:n'}) as xml:
         pass