Repository: jazzband/tablib
Pull request #431 (2019-11-11 21:35:32)

Documented csv import/export options from standard lib


Patch:
diff --git a/docs/conf.py b/docs/conf.py
index 8df446e..2294b30 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -22,7 +22,11 @@ from pkg_resources import get_distribution
 
 # Add any Sphinx extension module names here, as strings. They can be extensions
 # coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
-extensions = ['sphinx.ext.autodoc', 'sphinx.ext.todo', 'sphinx.ext.coverage', 'sphinx.ext.viewcode']
+extensions = [
+    'sphinx.ext.autodoc', 'sphinx.ext.todo', 'sphinx.ext.coverage',
+    'sphinx.ext.viewcode', 'sphinx.ext.intersphinx'
+]
+intersphinx_mapping = {'python': ('https://docs.python.org/3', None)}
 
 # Add any paths that contain templates here, relative to this directory.
 templates_path = ['_templates']
diff --git a/docs/formats.rst b/docs/formats.rst
index 489f57a..42b8cd5 100644
--- a/docs/formats.rst
+++ b/docs/formats.rst
@@ -21,6 +21,14 @@ When exporting with the ``csv`` format, the top row will contain headers, if
 they have been set. Otherwise, the top row will contain the first row of the
 dataset.
 
+When importing a CSV data source or exporting a dataset as CSV, you can pass any
+parameter supported by the :py:func:`csv.reader` and :py:func:`csv.writer`
+functions. For example::
+
+    tablib.import_set(your_data_stream, format='csv', dialect='unix')
+
+    dataset.export('csv', delimiter=' ', quotechar='|')
+
 .. admonition:: Line endings
 
      Exporting uses \\r\\n line endings by default so, make sure to include
diff --git a/src/tablib/formats/_rst.py b/src/tablib/formats/_rst.py
index 9e2cc73..793bce9 100644
--- a/src/tablib/formats/_rst.py
+++ b/src/tablib/formats/_rst.py
@@ -113,7 +113,7 @@ class ReSTFormat:
         lines = []
         wrapper = TextWrapper()
         if column_widths is None:
-            column_widths = _get_column_widths(dataset, pad_len=2)
+            column_widths = cls._get_column_widths(dataset, pad_len=2)
         border = '  '.join(['=' * w for w in column_widths])
 
         lines.append(border)


Test patch:
diff --git a/tests/test_tablib.py b/tests/test_tablib.py
index 61c1f3d..87482e0 100755
--- a/tests/test_tablib.py
+++ b/tests/test_tablib.py
@@ -660,6 +660,28 @@ class RSTTests(BaseTestCase):
             '==========  =========  ==='
         )
 
+    def test_rst_export_set(self):
+        # Arrange
+        data = tablib.Dataset()
+        data.append(self.john)
+        data.headers = self.headers
+        fmt = registry.get_format("rst")
+
+        # Act
+        out1 = fmt.export_set(data)
+        out2 = fmt.export_set_as_simple_table(data)
+
+        # Assert
+        self.assertEqual(out1, out2)
+        self.assertEqual(
+            out1,
+            "==========  =========  ===\n"
+            "first_name  last_name  gpa\n"
+            "==========  =========  ===\n"
+            "John        Adams      90 \n"
+            "==========  =========  ===",
+        )
+
 
 class CSVTests(BaseTestCase):
     def test_csv_format_detect(self):
@@ -778,6 +800,12 @@ class CSVTests(BaseTestCase):
 
         self.assertEqual(csv, self.founders.csv)
 
+    def test_csv_export_options(self):
+        """Exporting support csv.writer() parameters."""
+        data.append(('1. a', '2. b', '3. c'))
+        result = data.export('csv', delimiter=' ', quotechar='|')
+        self.assertEqual(result, '|1. a| |2. b| |3. c|\r\n')
+
     def test_csv_stream_export(self):
         """Verify exporting dataset object as CSV from file object."""