Skip to content

Commit a889939

Browse files
committed
improve dumptable -t parameter
1 parent 6ee98ec commit a889939

2 files changed

Lines changed: 23 additions & 16 deletions

File tree

‎uliweb/contrib/orm/commands.py‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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:

‎uliweb/utils/common.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,8 @@ def simple_value(v, encoding='utf-8', none=False):
305305
else:
306306
return v
307307

308-
def str_value(v, encoding='utf-8', bool_int=True, none='NULL'):
308+
re_newline = re.compile(r'\r\n|\r|\n')
309+
def str_value(v, encoding='utf-8', bool_int=True, none='NULL', newline_escape=False):
309310
import datetime
310311
import decimal
311312

@@ -319,8 +320,12 @@ def str_value(v, encoding='utf-8', bool_int=True, none='NULL'):
319320
return v.strftime('%H:%M:%S')
320321
elif isinstance(v, decimal.Decimal):
321322
return str(v)
322-
elif isinstance(v, unicode):
323-
return v.encode(encoding)
323+
elif isinstance(v, (str, unicode)):
324+
if isinstance(v, unicode):
325+
v = v.encode(encoding)
326+
if newline_escape:
327+
v = re_newline.sub(r'\\n', v)
328+
return v
324329
elif v is None:
325330
return none
326331
elif isinstance(v, bool):

0 commit comments

Comments
 (0)