Revision 1403
Added by Aaron Marcuse-Kubitza almost 13 years ago
map | ||
---|---|---|
162 | 162 |
xpath.put_obj(root, out, row_id, has_types, value) |
163 | 163 |
return process_rows(process_row, rows) |
164 | 164 |
|
165 |
def map_table(col_names, rows): |
|
166 |
col_idxs = util.list_flip(col_names) |
|
167 |
|
|
168 |
i = 0 |
|
169 |
while i < len(mappings): # mappings len changes in loop |
|
170 |
in_, out = mappings[i] |
|
171 |
if metadata_value(in_) == None: |
|
172 |
try: mappings[i] = (col_idxs[in_], out) |
|
173 |
except KeyError: del mappings[i]; continue # keep i the same |
|
174 |
i += 1 |
|
175 |
|
|
176 |
def get_value(in_, row): |
|
177 |
try: return row.list[in_] |
|
178 |
except IndexError: return None |
|
179 |
def wrap_row(row): return util.ListDict(row, col_names, col_idxs) |
|
180 |
|
|
181 |
return map_rows(get_value, util.WrapIter(wrap_row, rows)) |
|
182 |
|
|
165 | 183 |
if map_path == None: |
166 | 184 |
iter_ = xml_dom.NodeElemIter(doc0_root) |
167 | 185 |
util.skip(iter_, xml_dom.is_text) # skip metadata |
... | ... | |
173 | 191 |
in_pkeys = {} |
174 | 192 |
cur = sql.select(in_db, table=in_root, fields=None, conds=None, |
175 | 193 |
limit=end, start=0) |
176 |
col_names = list(sql.col_names(cur)) |
|
177 |
col_idxs = util.list_flip(col_names) |
|
194 |
row_ct = map_table(list(sql.col_names(cur)), sql.rows(cur)) |
|
178 | 195 |
|
179 |
mappings_new = [] |
|
180 |
for i, mapping in enumerate(mappings): |
|
181 |
in_, out = mapping |
|
182 |
if metadata_value(in_) == None: |
|
183 |
try: mapping = (col_idxs[in_], out) |
|
184 |
except KeyError: continue |
|
185 |
mappings_new.append(mapping) |
|
186 |
mappings = mappings_new |
|
187 |
|
|
188 |
def get_value(in_, row): |
|
189 |
try: return row.list[in_] |
|
190 |
except IndexError: return None |
|
191 |
def wrap_row(row): return util.ListDict(row, col_names, col_idxs) |
|
192 |
row_ct = map_rows(get_value, util.WrapIter(wrap_row, sql.rows(cur))) |
|
193 |
|
|
194 | 196 |
in_db.close() |
195 | 197 |
elif in_is_xml: |
196 | 198 |
def get_value(in_, row): |
... | ... | |
204 | 206 |
else: # input is CSV |
205 | 207 |
map_ = dict(mappings) |
206 | 208 |
reader, col_names = csvs.reader_and_header(sys.stdin) |
207 |
col_idxs = util.list_flip(col_names) |
|
208 |
|
|
209 |
mappings_new = [] |
|
210 |
for i, mapping in enumerate(mappings): |
|
211 |
in_, out = mapping |
|
212 |
if metadata_value(in_) == None: |
|
213 |
try: mapping = (col_idxs[in_], out) |
|
214 |
except KeyError: continue |
|
215 |
mappings_new.append(mapping) |
|
216 |
mappings = mappings_new |
|
217 |
|
|
218 |
def get_value(in_, row): |
|
219 |
try: return row.list[in_] |
|
220 |
except IndexError: return None |
|
221 |
def wrap_row(row): return util.ListDict(row, col_names, col_idxs) |
|
222 |
row_ct = map_rows(get_value, util.WrapIter(wrap_row, reader)) |
|
209 |
row_ct = map_table(col_names, reader) |
|
223 | 210 |
|
224 | 211 |
return row_ct |
225 | 212 |
|
Also available in: Unified diff
bin/map: Factored out code common to DB and CSV inputs into map_table()