• R/O
  • SSH

chkcsv: Commit

Default repository for chkcsv.py.


Commit MetaInfo

Revisionb20d063752918ef48e9dad4a88aceb1e95119215 (tree)
Zeit2019-01-03 13:30:53
AutorDreas Nielsen <dreas.nielsen@gmai...>
CommiterDreas Nielsen

Log Message

Corrected handling of 'next()' with csv library.

Ändern Zusammenfassung

Diff

diff -r 0bcecc677719 -r b20d06375291 chkcsv/chkcsv.py
--- a/chkcsv/chkcsv.py Sat Oct 27 13:44:49 2018 -0700
+++ b/chkcsv/chkcsv.py Wed Jan 02 20:30:53 2019 -0800
@@ -38,10 +38,11 @@
3838 # ---------- --------------------------------------------------------------
3939 # 2011-09-25 First version. Version 0.8.0.0. RDN.
4040 # 2018-10-27 Converted to run under both Python 2 and 3. Version 1.0.0. RDN.
41+# 2019-01-02 Corrected handling of next() for csv library. Version 1.0.1. RDN.
4142 # ============================================================================
4243
43-_version = "1.0.0"
44-_vdate = "2018-10-27"
44+_version = "1.0.1"
45+_vdate = "2019-01-02"
4546
4647 import sys
4748 from optparse import OptionParser
@@ -218,9 +219,9 @@
218219 def chk_bool(self, data):
219220 if len(data)==0:
220221 return None
221- return None if data in ('True', 'true', 'TRUE', 'T', 't', 'Yes', 'yes', 'YES', 'Y', 'y',
222- 'False', 'false', 'FALSE', 'F', 'f',
223- 'No', 'no', 'NO', 'N', 'n', True, False) else "unrecognized boolean"
222+ return None if data in (u'True', u'true', u'TRUE', u'T', u't', u'Yes', u'yes', u'YES', u'Y', u'y',
223+ u'False', u'false', u'FALSE', u'F', u'f',
224+ u'No', u'no', u'NO', u'N', u'n', True, False) else u"unrecognized boolean"
224225 def chk_datetime(self, data):
225226 if len(data)==0:
226227 return None
@@ -369,20 +370,34 @@
369370 def __iter__(self):
370371 return self
371372 def __next__(self):
372- return next(self.reader).encode('utf-8')
373+ if sys.version_info < (3,):
374+ return next(self.reader).encode('utf-8')
375+ else:
376+ return next(self.reader)
377+ def next(self):
378+ return self.__next__()
373379
374380 class UnicodeReader:
375381 """A CSV reader which will iterate over lines in the CSV file "f",
376382 which is encoded in the given encoding.
377383 """
378384 def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
379- f = UTF8Recoder(f, encoding)
380- self.reader = csv.reader(f, dialect=dialect, **kwds)
381- def __next__(self):
382- row = next(self.reader)
383- return [unicode(s, "utf-8") for s in row]
385+ uf = UTF8Recoder(f, encoding)
386+ self.reader = csv.reader(uf, dialect=dialect, **kwds)
384387 def __iter__(self):
385388 return self
389+ def next(self):
390+ if sys.version_info < (3,):
391+ row = self.reader.next()
392+ else:
393+ row = next(self.reader)
394+ return [type(u"")(s, "utf-8") for s in row]
395+ def __next__(self):
396+ if sys.version_info < (3,):
397+ row = self.reader.next()
398+ else:
399+ row = next(self.reader)
400+ return [type(u"")(s, "utf-8") for s in row]
386401
387402 def show_errors(errlist):
388403 """Write a list of error messages to stderr.
@@ -433,10 +448,11 @@
433448 :param encoding: The character encoding of the CSV file.
434449 """
435450 dialect = csv.Sniffer().sniff(open(csv_fname, "rt").readline())
436- if encoding:
451+ encoding = "utf-8" if not encoding else encoding
452+ if sys.version_info < (3,):
437453 inf = UnicodeReader(open(csv_fname, "rt"), dialect, encoding)
438454 else:
439- inf = csv.reader(open(csv_fname, "rt"), dialect)
455+ inf = csv.reader(open(csv_fname, mode="rt", encoding=encoding), dialect=dialect)
440456 colnames = next(inf)
441457 req_cols = [ c for c in cols if cols[c].column_required ]
442458 # Exit if all required columns are not present
@@ -532,18 +548,19 @@
532548
533549
534550 if __name__=='__main__':
535- try:
536- status = main()
537- except ChkCsvError as msg:
538- show_errors( [ (msg.errmsg, msg.infile, msg.line, msg.column) ] )
539- exit(1)
540- except SystemExit as x:
541- sys.exit(x)
542- except Exception:
543- strace = traceback.extract_tb(sys.exc_info()[2])[-1:]
544- lno = strace[0][1]
545- src = strace[0][3]
546- sys.stderr.write("%s: Uncaught exception %s (%s) on line %s (%s)." % (os.path.basename(sys.argv[0]), str(sys.exc_info()[0]), sys.exc_info()[1], lno, src))
547- sys.exit(1)
551+ status = main()
552+ #try:
553+ # status = main()
554+ #except ChkCsvError as msg:
555+ # show_errors( [ (msg.errmsg, msg.infile, msg.line, msg.column) ] )
556+ # exit(1)
557+ #except SystemExit as x:
558+ # sys.exit(x)
559+ #except Exception:
560+ # strace = traceback.extract_tb(sys.exc_info()[2])[-1:]
561+ # lno = strace[0][1]
562+ # src = strace[0][3]
563+ # sys.stderr.write("%s: Uncaught exception %s (%s) on line %s (%s)." % (os.path.basename(sys.argv[0]), str(sys.exc_info()[0]), sys.exc_info()[1], lno, src))
564+ # sys.exit(1)
548565 sys.exit(status)
549566
diff -r 0bcecc677719 -r b20d06375291 setup.py
--- a/setup.py Sat Oct 27 13:44:49 2018 -0700
+++ b/setup.py Wed Jan 02 20:30:53 2019 -0800
@@ -1,7 +1,7 @@
11 from distutils.core import setup
22
33 setup(name='chkcsv',
4- version='1.0.0',
4+ version='1.0.1',
55 description="Checks the format of a CSV file with respect to a specifed set of column names and types.",
66 author='Dreas Nielsen',
77 author_email='dreas.nielsen@gmail.com',
Show on old repository browser