11import re
22import xlrd
3+ from datetime import datetime
34
45from http import HTTPStatus
56from subprocess import (check_call ,
89from tempfile import NamedTemporaryFile
910
1011from mfr .extensions .tabular import compat
11- from mfr .core .exceptions import SubprocessError , CorruptedError
12+ from mfr .core .exceptions import SubprocessError , UnparseableTableError
1213from mfr .extensions .tabular .settings import (PSPP_CONVERT_BIN ,
1314 PSPP_CONVERT_TIMEOUT ,
1415 MAX_SIZE )
@@ -114,19 +115,25 @@ def to_bytes(fp):
114115 raise TypeError ("Expected binary file-like object; got text/str" )
115116
116117
118+ def _convert_value (value ):
119+ if isinstance (value , datetime ):
120+ return value .isoformat ()
121+ return value
122+
123+
117124def _extract_rows (fields , raw_rows ):
118125 rows = []
119126 for row in raw_rows :
120127 if len (rows ) >= MAX_SIZE :
121128 break
122- rows .append (dict (zip (fields , row )))
129+ rows .append (dict (zip (fields , map ( _convert_value , row ) )))
123130 return rows
124131
125132
126133def parse_xls (wb , sheets ):
127134 for sheet in wb .sheets ():
128135 if getattr (sheet , 'nrows' , None ) is None or getattr (sheet , 'ncols' , None ) is None :
129- raise CorruptedError
136+ raise UnparseableTableError
130137
131138 ncols = sheet .ncols
132139 max_cols = min (ncols , MAX_SIZE )
@@ -145,7 +152,7 @@ def parse_xlsx(wb, sheets):
145152 ws = wb [name ]
146153
147154 if getattr (ws , 'max_row' , None ) is None or getattr (ws , 'max_column' , None ) is None :
148- raise CorruptedError
155+ raise UnparseableTableError
149156
150157 ncols = getattr (ws , "max_column" , 0 )
151158 max_cols = min (ncols , MAX_SIZE )
0 commit comments