[dataimport] Have ucsvreader's API match that of csv.reader (closes #3705701)
authorRémi Cardona <remi.cardona@logilab.fr>
Thu, 03 Apr 2014 14:17:16 +0200
changeset 10091 09878c2f8621
parent 10090 0aebb1c0f849
child 10092 f0363da0b5a0
[dataimport] Have ucsvreader's API match that of csv.reader (closes #3705701) 'separator' and 'quote' are replaced with 'delimiter' and 'quotechar'.
dataimport.py
doc/3.20.rst
--- a/dataimport.py	Wed Nov 19 12:13:32 2014 +0100
+++ b/dataimport.py	Thu Apr 03 14:17:16 2014 +0200
@@ -99,9 +99,16 @@
     f.seek(0)
     return i+1
 
-def ucsvreader_pb(stream_or_path, encoding='utf-8', separator=',', quote='"',
-                  skipfirst=False, withpb=True, skip_empty=True):
+def ucsvreader_pb(stream_or_path, encoding='utf-8', delimiter=',', quotechar='"',
+                  skipfirst=False, withpb=True, skip_empty=True, separator=None,
+                  quote=None):
     """same as :func:`ucsvreader` but a progress bar is displayed as we iter on rows"""
+    if separator is not None:
+        delimiter = separator
+        warnings.warn("[3.20] 'separator' kwarg is deprecated, use 'delimiter' instead")
+    if quote is not None:
+        quotechar = quote
+        warnings.warn("[3.20] 'quote' kwarg is deprecated, use 'quotechar' instead")
     if isinstance(stream_or_path, basestring):
         if not osp.exists(stream_or_path):
             raise Exception("file doesn't exists: %s" % stream_or_path)
@@ -113,15 +120,16 @@
         rowcount -= 1
     if withpb:
         pb = shellutils.ProgressBar(rowcount, 50)
-    for urow in ucsvreader(stream, encoding, separator, quote,
+    for urow in ucsvreader(stream, encoding, delimiter, quotechar,
                            skipfirst=skipfirst, skip_empty=skip_empty):
         yield urow
         if withpb:
             pb.update()
     print ' %s rows imported' % rowcount
 
-def ucsvreader(stream, encoding='utf-8', separator=',', quote='"',
-               skipfirst=False, ignore_errors=False, skip_empty=True):
+def ucsvreader(stream, encoding='utf-8', delimiter=',', quotechar='"',
+               skipfirst=False, ignore_errors=False, skip_empty=True,
+               separator=None, quote=None):
     """A csv reader that accepts files with any encoding and outputs unicode
     strings
 
@@ -129,7 +137,13 @@
     separators) will be skipped. This is useful for Excel exports which may be
     full of such lines.
     """
-    it = iter(csv.reader(stream, delimiter=separator, quotechar=quote))
+    if separator is not None:
+        delimiter = separator
+        warnings.warn("[3.20] 'separator' kwarg is deprecated, use 'delimiter' instead")
+    if quote is not None:
+        quotechar = quote
+        warnings.warn("[3.20] 'quote' kwarg is deprecated, use 'quotechar' instead")
+    it = iter(csv.reader(stream, delimiter=delimiter, quotechar=quotechar))
     if not ignore_errors:
         if skipfirst:
             it.next()
--- a/doc/3.20.rst	Wed Nov 19 12:13:32 2014 +0100
+++ b/doc/3.20.rst	Thu Apr 03 14:17:16 2014 +0200
@@ -16,6 +16,17 @@
 
 .. _CWEP-002: http://hg.logilab.org/review/cwep/file/tip/CWEP-002.rst
 
+
+API Changes
+-----------
+
+* ``ucsvreader()`` and ``ucsvreader_pb()`` from the ``dataimport`` module have
+  2 new keyword arguments ``delimiter`` and ``quotechar`` to replace the
+  ``separator`` and ``quote`` arguments respectively. This makes the API match
+  that of Python's ``csv.reader()``.  The old arguments are still supported
+  though deprecated.
+
+
 Deprecated Code Drops
 ----------------------