Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: preliminary support for XML, 2020 #464

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/tablib/formats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from ._csv import CSVFormat
from ._json import JSONFormat
from ._tsv import TSVFormat
from ._xml import XMLFormat

uninstalled_format_messages = {
"cli": {"package_name": "tabulate package", "extras_name": "cli"},
Expand Down Expand Up @@ -110,6 +111,7 @@ def register_builtins(self):
self.register('rst', 'tablib.formats._rst.ReSTFormat')
if find_spec('tabulate'):
self.register('cli', 'tablib.formats._cli.CLIFormat')
self.register('xml', XMLFormat())

def formats(self):
for key, frm in self._formats.items():
Expand Down
32 changes: 32 additions & 0 deletions src/tablib/formats/_xml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
""" Tablib - XML support.
"""

import xml.etree.ElementTree as ET


class XMLFormat:
title = 'xml'
extensions = ('xml',)

# TODO implement
# @classmethod
# def import_set(cls, dset, in_stream):
# dset.wipe()

@classmethod
def export_set(cls, dataset):
root = ET.Element('dataset')

for row_index, row in enumerate(dataset.dict):
row_node = ET.Element('row')
tags = dataset._data[row_index].tags
if tags:
row_node.set('tags', ','.join(tags))
for header, value in row.items():
value_node = ET.Element(header)
value_node.text = str(value)
row_node.append(value_node)

root.append(row_node)

return ET.tostring(root).decode()
31 changes: 31 additions & 0 deletions tests/test_tablib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1350,3 +1350,34 @@ def test_cli_export_grid(self):
'+---+---+---+\n| a | b | c |\n+---+---+---+',
tablib.Dataset(['a', 'b', 'c']).export('cli', tablefmt='grid')
)


class XMLTests(BaseTestCase):
def test_xml_export(self):
"""Verify exporting dataset object as XML."""

xml = '<dataset>'
for row in self.founders:
xml += '<row>'
for i, header in enumerate(self.founders.headers):
xml += '<{}>{}</{}>'.format(header, row[i], header)
xml += '</row>'
xml += '</dataset>'

self.assertEqual(xml, self.founders.xml)

def test_xml_export_row_with_tag(self):
"""Verify exporting dataset with row tags object as XML."""

local_data = tablib.Dataset(headers=['A', 'B'])
local_data.append(('a', 'b'), tags=['tag 1', 'tag 2'])

xml = '<dataset>'
for row_index, row in enumerate(local_data):
xml += '<row tags="{}">'.format(','.join(local_data._data[row_index].tags))
for header_index, header in enumerate(local_data.headers):
xml += '<{}>{}</{}>'.format(header, row[header_index], header)
xml += '</row>'
xml += '</dataset>'

self.assertEqual(xml, local_data.xml)