dataimport.py
changeset 9901 161ec913aeec
parent 9900 9c7de09a6648
child 9902 62c586f32f93
equal deleted inserted replaced
9900:9c7de09a6648 9901:161ec913aeec
    74 import cPickle
    74 import cPickle
    75 import os.path as osp
    75 import os.path as osp
    76 import inspect
    76 import inspect
    77 from collections import defaultdict
    77 from collections import defaultdict
    78 from copy import copy
    78 from copy import copy
    79 from datetime import date, datetime
    79 from datetime import date, datetime, time
    80 from time import asctime
    80 from time import asctime
    81 from StringIO import StringIO
    81 from StringIO import StringIO
    82 
    82 
    83 from logilab.common import shellutils, attrdict
    83 from logilab.common import shellutils, attrdict
    84 from logilab.common.date import strptime
    84 from logilab.common.date import strptime
   457         value = value.replace('\\', r'\\')
   457         value = value.replace('\\', r'\\')
   458     if isinstance(value, unicode):
   458     if isinstance(value, unicode):
   459         value = value.encode(encoding)
   459         value = value.encode(encoding)
   460     return value
   460     return value
   461 
   461 
       
   462 def _copyfrom_buffer_convert_date(value, **opts):
       
   463     '''Convert date into "YYYY-MM-DD"'''
       
   464     # Do not use strftime, as it yields issue with date < 1900
       
   465     # (http://bugs.python.org/issue1777412)
       
   466     return '%04d-%02d-%02d' % (value.year, value.month, value.day)
       
   467 
   462 def _copyfrom_buffer_convert_datetime(value, **opts):
   468 def _copyfrom_buffer_convert_datetime(value, **opts):
   463     '''Convert date into "YYYY-MM-DD"'''
   469     '''Convert date into "YYYY-MM-DD HH:MM:SS.UUUUUU"'''
   464     # Do not use strftime, as it yields issue
   470     # Do not use strftime, as it yields issue with date < 1900
   465     # with date < 1900
   471     # (http://bugs.python.org/issue1777412)
   466     value = '%04d-%02d-%02d' % (value.year, value.month, value.day)
   472     return '%s %s' % (_copyfrom_buffer_convert_date(value, **opts),
   467     if isinstance(value, datetime):
   473                       _copyfrom_buffer_convert_time(value, **opts))
   468         value += ' %02d:%02d:%02d' % (value.hour,
   474 
   469                                       value.minutes,
   475 def _copyfrom_buffer_convert_time(value, **opts):
   470                                       value.second)
   476     '''Convert time into "HH:MM:SS.UUUUUU"'''
   471     return value
   477     return '%02d:%02d:%02d.%06d' % (value.hour, value.minute,
       
   478                                     value.second, value.microsecond)
   472 
   479 
   473 # (types, converter) list.
   480 # (types, converter) list.
   474 _COPYFROM_BUFFER_CONVERTERS = [
   481 _COPYFROM_BUFFER_CONVERTERS = [
   475     (type(None), _copyfrom_buffer_convert_None),
   482     (type(None), _copyfrom_buffer_convert_None),
   476     ((long, int, float), _copyfrom_buffer_convert_number),
   483     ((long, int, float), _copyfrom_buffer_convert_number),
   477     (basestring, _copyfrom_buffer_convert_string),
   484     (basestring, _copyfrom_buffer_convert_string),
   478     ((date, datetime), _copyfrom_buffer_convert_datetime)
   485     (datetime, _copyfrom_buffer_convert_datetime),
       
   486     (date, _copyfrom_buffer_convert_date),
       
   487     (time, _copyfrom_buffer_convert_time),
   479 ]
   488 ]
   480 
   489 
   481 def _create_copyfrom_buffer(data, columns, **convert_opts):
   490 def _create_copyfrom_buffer(data, columns, **convert_opts):
   482     """
   491     """
   483     Create a StringIO buffer for 'COPY FROM' command.
   492     Create a StringIO buffer for 'COPY FROM' command.