@@ -80,7 +80,8 @@ def _cmp(x, y):
8080
8181 return sorted (tables .items (), cmp = _cmp )
8282
83- def dump_table (table , filename , con , std = None , delimiter = ',' , format = None , encoding = 'utf-8' , inspector = None ):
83+ def dump_table (table , filename , con , std = None , delimiter = ',' , format = None ,
84+ encoding = 'utf-8' , inspector = None ):
8485 from uliweb .utils .common import str_value
8586 from StringIO import StringIO
8687 import csv
@@ -102,19 +103,18 @@ def dump_table(table, filename, con, std=None, delimiter=',', format=None, encod
102103 result = do_ (table .select ())
103104 fields = [x .name for x in table .c ]
104105 if not format :
105- print >> std , '#' + ' ' .join (fields )
106+ print >> std , ' ' .join (fields )
106107 elif format == 'txt' :
107- print >> std , '#' + ' ,' .join (fields )
108+ print >> std , ',' .join (fields )
108109 n = 0
110+ if format == 'txt' :
111+ fw = csv .writer (std , delimiter = delimiter )
109112 for r in result :
110113 n += 1
111114 if not format :
112115 print >> std , r
113116 elif format == 'txt' :
114- buf = StringIO ()
115- fw = csv .writer (buf , delimiter = delimiter )
116- fw .writerow ([str_value (x , encoding = encoding ) for x in r ])
117- print >> std , buf .getvalue ().rstrip ()
117+ fw .writerow ([str_value (x , encoding = encoding , newline_escape = True ) for x in r ])
118118 else :
119119 raise Exception , "Can't support the text format %s" % format
120120
@@ -125,20 +125,22 @@ def load_table(table, filename, con, delimiter=',', format=None,
125125 import csv
126126 from uliweb .utils .date import to_date , to_datetime
127127
128+ if not os .path .exists (filename ):
129+ return "Skipped (data not found)"
130+
128131 table = reflect_table (con , table .name )
129132
130133 if delete :
131134 do_ (table .delete ())
132135
133- if not os .path .exists (filename ):
134- return "Skipped (data not found)"
135-
136136 b = time ()
137137 bulk = max (1 , bulk )
138138 f = fin = open (filename , 'rb' )
139139 try :
140140 first_line = f .readline ()
141- fields = first_line [1 :].strip ().split ()
141+ if first_line .startswith ('#' ):
142+ first_line = first_line [1 :]
143+ fields = first_line .strip ().split ()
142144 n = 0
143145 count = 0
144146 if format :
@@ -171,15 +173,15 @@ def load_table(table, filename, con, delimiter=',', format=None,
171173 params [c .name ] = record [c .name ]
172174 buf .append (params )
173175 if count >= bulk :
174- do_ (table .insert (), args = buf )
176+ do_ (table .insert (), args = ( buf , ) )
175177 count = 0
176178 buf = []
177179 except :
178180 log .error ('Error: Line %d' % n )
179181 raise
180182
181183 if buf :
182- do_ (table .insert (), args = buf )
184+ do_ (table .insert (), args = ( buf , ) )
183185
184186 return 'OK (%d/%lfs)' % (n , time ()- b )
185187 finally :
0 commit comments