diff --git a/README.rst b/README.rst index 6258001..d9190bb 100644 --- a/README.rst +++ b/README.rst @@ -23,6 +23,7 @@ Output formats supported: - YAML (Sets + Books) - Pandas DataFrames (Sets) - HTML (Sets) +- Jira (Sets) - TSV (Sets) - ODS (Sets) - CSV (Sets) diff --git a/tablib/core.py b/tablib/core.py index f9951a9..6420ffa 100644 --- a/tablib/core.py +++ b/tablib/core.py @@ -632,7 +632,6 @@ class Dataset(object): """ pass - @property def latex(): """A LaTeX booktabs representation of the :class:`Dataset` object. If a @@ -642,6 +641,13 @@ class Dataset(object): """ pass + @property + def jira(): + """A Jira table representation of the :class:`Dataset` object. + + .. note:: This method can be used for export only. + """ + pass # ---- # Rows diff --git a/tablib/formats/__init__.py b/tablib/formats/__init__.py index 3eb747e..418e607 100644 --- a/tablib/formats/__init__.py +++ b/tablib/formats/__init__.py @@ -15,5 +15,6 @@ from . import _dbf as dbf from . import _latex as latex from . import _df as df from . import _rst as rst +from . import _jira as jira -available = (json, xls, yaml, csv, dbf, tsv, html, latex, xlsx, ods, df, rst) +available = (json, xls, yaml, csv, dbf, tsv, html, jira, latex, xlsx, ods, df, rst) diff --git a/tablib/formats/_jira.py b/tablib/formats/_jira.py new file mode 100644 index 0000000..55fce52 --- /dev/null +++ b/tablib/formats/_jira.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- + +"""Tablib - Jira table export support. + + Generates a Jira table from the dataset. +""" +from tablib.compat import unicode + +title = 'jira' + + +def export_set(dataset): + """Formats the dataset according to the Jira table syntax: + + ||heading 1||heading 2||heading 3|| + |col A1|col A2|col A3| + |col B1|col B2|col B3| + + :param dataset: dataset to serialize + :type dataset: tablib.core.Dataset + """ + + header = _get_header(dataset.headers) if dataset.headers else '' + body = _get_body(dataset) + return '%s\n%s' % (header, body) if header else body + + +def _get_body(dataset): + return '\n'.join([_serialize_row(row) for row in dataset]) + + +def _get_header(headers): + return _serialize_row(headers, delimiter='||') + + +def _serialize_row(row, delimiter='|'): + return '%s%s%s' % (delimiter, + delimiter.join([unicode(item) if item else ' ' for item in row]), + delimiter) diff --git a/test_tablib.py b/test_tablib.py index b945a2d..57b1b39 100755 --- a/test_tablib.py +++ b/test_tablib.py @@ -316,6 +316,23 @@ class TablibTestCase(unittest.TestCase): self.assertEqual(html, d.html) + def test_jira_export(self): + + expected = """||first_name||last_name||gpa|| +|John|Adams|90| +|George|Washington|67| +|Thomas|Jefferson|50|""" + self.assertEqual(expected, self.founders.jira) + + def test_jira_export_no_headers(self): + self.assertEqual('|a|b|c|', tablib.Dataset(['a', 'b', 'c']).jira) + + def test_jira_export_none_and_empty_values(self): + self.assertEqual('| | |c|', tablib.Dataset(['', None, 'c']).jira) + + def test_jira_export_empty_dataset(self): + self.assertTrue(tablib.Dataset().jira is not None) + def test_latex_export(self): """LaTeX export""" @@ -399,6 +416,7 @@ class TablibTestCase(unittest.TestCase): data.xlsx data.ods data.html + data.jira data.latex data.df data.rst @@ -421,6 +439,7 @@ class TablibTestCase(unittest.TestCase): data.xlsx data.ods data.html + data.jira data.latex data.rst