devtools/dataimport.py
changeset 4186 ca7e526b07b6
parent 4173 cfd5d3270f99
child 4252 6c4f109c2b03
equal deleted inserted replaced
4184:7002e91d304a 4186:ca7e526b07b6
    49   sys.exit(0)
    49   sys.exit(0)
    50 
    50 
    51 """
    51 """
    52 __docformat__ = "restructuredtext en"
    52 __docformat__ = "restructuredtext en"
    53 
    53 
    54 import sys, csv, traceback
    54 import sys
       
    55 import csv
       
    56 import traceback
       
    57 import os.path as osp
       
    58 from StringIO import StringIO
    55 
    59 
    56 from logilab.common import shellutils
    60 from logilab.common import shellutils
    57 from logilab.common.deprecation import deprecated
    61 from logilab.common.deprecation import deprecated
    58 
    62 
    59 def ucsvreader_pb(filepath, encoding='utf-8', separator=',', quote='"',
    63 def ucsvreader_pb(filepath, encoding='utf-8', separator=',', quote='"',
    60                   skipfirst=False, withpb=True):
    64                   skipfirst=False, withpb=True):
    61     """same as ucsvreader but a progress bar is displayed as we iter on rows"""
    65     """same as ucsvreader but a progress bar is displayed as we iter on rows"""
       
    66     if not osp.exists(filepath):
       
    67         raise Exception("file doesn't exists: %s" % filepath)
    62     rowcount = int(shellutils.Execute('wc -l "%s"' % filepath).out.strip().split()[0])
    68     rowcount = int(shellutils.Execute('wc -l "%s"' % filepath).out.strip().split()[0])
    63     if skipfirst:
    69     if skipfirst:
    64         rowcount -= 1
    70         rowcount -= 1
    65     if withpb:
    71     if withpb:
    66         pb = shellutils.ProgressBar(rowcount, 50)
    72         pb = shellutils.ProgressBar(rowcount, 50)
   324         except KeyError:
   330         except KeyError:
   325             self.check(key, entity[key], None)
   331             self.check(key, entity[key], None)
   326             entity[key] = default
   332             entity[key] = default
   327 
   333 
   328     def record_error(self, key, msg=None, type=None, value=None, tb=None):
   334     def record_error(self, key, msg=None, type=None, value=None, tb=None):
   329         import StringIO
   335         tmp = StringIO()
   330         tmp = StringIO.StringIO()
       
   331         if type is None:
   336         if type is None:
   332             traceback.print_exc(file=tmp)
   337             traceback.print_exc(file=tmp)
   333         else:
   338         else:
   334             traceback.print_exception(type, value, tb, file=tmp)
   339             traceback.print_exception(type, value, tb, file=tmp)
   335         print tmp.getvalue()
   340         print tmp.getvalue()