[Python-checkins] python/nondist/sandbox/csv csv.py,1.4,1.5

andrewmcnamara@users.sourceforge.net andrewmcnamara@users.sourceforge.net
Thu, 30 Jan 2003 20:07:43 -0800


Update of /cvsroot/python/python/nondist/sandbox/csv
In directory sc8-pr-cvs1:/tmp/cvs-serv13504

Modified Files:
	csv.py 
Log Message:
Rename dialects from excel2000 to excel. Rename Error to be CSVError. 
Explicity fetch iterator in reader class, rather than simply calling 
next() (which only works for self-iterators).


Index: csv.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/csv/csv.py,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** csv.py	30 Jan 2003 13:11:21 -0000	1.4
--- csv.py	31 Jan 2003 04:07:40 -0000	1.5
***************
*** 3,31 ****
  QUOTE_MINIMAL, QUOTE_ALL, QUOTE_NONNUMERIC = range(3)
  
! class excel2000:
      quotechar = '"'
      delimiter = ','
      escapechar = None
      skipinitialspace = False
!     lineterminator = '\r\n'
      quoting = QUOTE_MINIMAL
  
! class excel2000_tab:
      delimiter = '\t'
  
  dialects = {
!     'excel': excel2000,
!     'excel2000': excel2000,
!     'excel-tab': excel2000_tab,
!     'excel2000-tab': excel2000_tab,
  }
  
! def set_dialect(name, dialect):
!     dialects[name] = dialect
! 
! def get_dialect(name):
!     return dialects[name]
! 
! class Error(Exception):
      pass
  
--- 3,25 ----
  QUOTE_MINIMAL, QUOTE_ALL, QUOTE_NONNUMERIC = range(3)
  
! class excel:
      quotechar = '"'
      delimiter = ','
      escapechar = None
      skipinitialspace = False
! # XXX - andrewm - This is causing weird errors from the _csv module - needs
! # investigation:
! #   lineterminator = '\r\n'
      quoting = QUOTE_MINIMAL
  
! class excel_tab(excel):
      delimiter = '\t'
  
  dialects = {
!     'excel': excel,
!     'excel-tab': excel_tab,
  }
  
! class CSVError(Exception):
      pass
  
***************
*** 35,42 ****
              dialect_obj = dialects[dialect]
          except KeyError:
!             raise Error('Unknown dialect')
          parser_options = {}
          for attr in dir(dialect_obj):
!             if attr[0] == '_':
                  continue
              parser_options[attr] = getattr(dialect_obj, attr)
--- 29,36 ----
              dialect_obj = dialects[dialect]
          except KeyError:
!             raise CSVError('Unknown dialect')
          parser_options = {}
          for attr in dir(dialect_obj):
!             if attr.startswith('_'):
                  continue
              parser_options[attr] = getattr(dialect_obj, attr)
***************
*** 45,53 ****
              self.parser = _csv.parser(**parser_options)
          except _csv.Error, e:
!             raise Error(e)
  
  class reader(OCcvs):
!     def __init__(self, fileobj, dialect = 'excel2000', **options):
!         self.fileobj = fileobj
          OCcvs.__init__(self, dialect, **options)
  
--- 39,47 ----
              self.parser = _csv.parser(**parser_options)
          except _csv.Error, e:
!             raise CSVError(e)
  
  class reader(OCcvs):
!     def __init__(self, iterobj, dialect = 'excel', **options):
!         self.iterobj = iter(iterobj)
          OCcvs.__init__(self, dialect, **options)
  
***************
*** 57,66 ****
      def next(self):
          while 1:
!             fields = self.parser.parse(self.fileobj.next())
              if fields:
                  return fields
  
  class writer(OCcvs):
!     def __init__(self, fileobj, dialect='excel2000', **options):
          self.fileobj = fileobj
          OCcvs.__init__(self, dialect, **options)
--- 51,60 ----
      def next(self):
          while 1:
!             fields = self.parser.parse(self.iterobj.next())
              if fields:
                  return fields
  
  class writer(OCcvs):
!     def __init__(self, fileobj, dialect='excel', **options):
          self.fileobj = fileobj
          OCcvs.__init__(self, dialect, **options)
***************
*** 83,84 ****
--- 77,102 ----
              except:
                  pass
+ 
+ def set_dialect(name, dialect):
+     dialects[name] = dialect
+ 
+ def get_dialect(name):
+     return dialects[name]
+ 
+ def list_dialects():
+     return dialects.keys()
+ 
+ # An alternate way of populating the dialects dictionary...
+ #def _init_dialects():
+ #    global dialects
+ #    mod = sys.modules[__name__]
+ #    for name in dir(mod):
+ #        attr = getattr(mod, name)
+ #        try:
+ #            if issubclass(attr, Dialect) and attr is not Dialect:
+ #                dialect = attr()
+ #                dialects[dialect.name] = dialect
+ #        except TypeError:
+ #            pass
+ #
+ #_init_dialects()