Revision 1148
Added by Aaron Marcuse-Kubitza almost 13 years ago
map | ||
---|---|---|
121 | 121 |
doc0_root = doc0.documentElement |
122 | 122 |
if out_label == None: out_label = doc0_root.tagName |
123 | 123 |
|
124 |
def process_rows(process_row, rows, col_names=None):
|
|
124 |
def process_rows(process_row, rows): |
|
125 | 125 |
'''Processes input rows |
126 | 126 |
@param process_row(in_row, i) |
127 | 127 |
''' |
... | ... | |
130 | 130 |
if i < start: continue |
131 | 131 |
if end != None and i >= end: break |
132 | 132 |
process_row(row, i) |
133 |
|
|
134 | 133 |
row_ready(i, row) |
135 | 134 |
row_ct = i-start+1 |
136 | 135 |
return row_ct |
... | ... | |
159 | 158 |
|
160 | 159 |
in_db = connect_db(in_db_config) |
161 | 160 |
in_pkeys = {} |
162 |
|
|
163 | 161 |
cur = sql.select(in_db, table=in_root, fields=None, conds=None, |
164 | 162 |
limit=end, start=0) |
165 |
col_idxs = util.list_flip(sql.col_names(cur)) |
|
166 | 163 |
|
167 |
mappings_new = [] |
|
168 |
for i, mapping in enumerate(mappings): |
|
169 |
in_, out = mapping |
|
170 |
if metadata_value(in_) == None: |
|
171 |
try: mapping = (col_idxs[in_], out) |
|
172 |
except KeyError: continue |
|
173 |
mappings_new.append(mapping) |
|
174 |
mappings = mappings_new |
|
164 |
def get_value(in_, row): |
|
165 |
try: return util.cast(str, row[in_]) |
|
166 |
except KeyError: return None |
|
175 | 167 |
|
176 |
row_ct = map_rows(lambda in_, row: util.cast(str, row[in_]), |
|
177 |
sql.rows(cur)) |
|
168 |
col_names = list(sql.col_names(cur)) |
|
169 |
col_idxs = util.list_flip(col_names) |
|
170 |
def wrap_row(row): return util.ListDict(row, col_names, col_idxs) |
|
171 |
row_ct = map_rows(get_value, util.WrapIter(wrap_row, sql.rows(cur))) |
|
178 | 172 |
|
179 | 173 |
in_db.close() |
180 | 174 |
elif in_is_xml: |
... | ... | |
189 | 183 |
else: # input is CSV |
190 | 184 |
map_ = dict(mappings) |
191 | 185 |
reader = csv.reader(sys.stdin) |
192 |
cols = reader.next() |
|
193 |
col_idxs = util.list_flip(cols) |
|
194 | 186 |
|
195 |
mappings_new = [] |
|
196 |
for i, mapping in enumerate(mappings): |
|
197 |
in_, out = mapping |
|
198 |
if metadata_value(in_) == None: |
|
199 |
try: mapping = (col_idxs[in_], out) |
|
200 |
except KeyError: continue |
|
201 |
mappings_new.append(mapping) |
|
202 |
mappings = mappings_new |
|
187 |
def get_value(in_, row): |
|
188 |
try: return util.none_if(row[in_], '') |
|
189 |
except KeyError: return None |
|
203 | 190 |
|
204 |
def get_value(in_, row): |
|
205 |
value = row[in_] |
|
206 |
if value != '': return value |
|
207 |
else: return None |
|
208 |
row_ct = map_rows(get_value, reader) |
|
191 |
col_names = reader.next() |
|
192 |
col_idxs = util.list_flip(col_names) |
|
193 |
def wrap_row(row): return util.ListDict(row, col_names, col_idxs) |
|
194 |
row_ct = map_rows(get_value, util.WrapIter(wrap_row, reader)) |
|
209 | 195 |
|
210 | 196 |
return row_ct |
211 | 197 |
|
Also available in: Unified diff
bin/map: Use new util.ListDict and util.WrapIter to simplify getting rows by column name instead of index, and to enable a row to be printed with its column names in error messages