Revision 316
Added by Aaron Marcuse-Kubitza about 13 years ago
map | ||
---|---|---|
75 | 75 |
|
76 | 76 |
if in_is_xml: doc0 = minidom.parse(sys.stdin) |
77 | 77 |
|
78 |
def process_input(use_row):
|
|
78 |
def process_input(root, process_row):
|
|
79 | 79 |
'''Inputs datasource to XML tree, mapping if needed''' |
80 |
doc1 = xml_dom.create_doc(out_label) |
|
81 |
root = doc1.documentElement |
|
82 |
|
|
83 | 80 |
def process_rows(get_value, rows): |
84 | 81 |
'''Processes input values |
85 | 82 |
@param get_value f(in_, row):str |
86 | 83 |
''' |
87 | 84 |
for i, row in enumerate(rows): |
88 |
try: |
|
89 |
if not (limit == None or i < limit): break |
|
90 |
row_id = str(i) |
|
91 |
for in_, out in mappings: |
|
92 |
value = metadata_value(in_) |
|
93 |
if value == None: value = get_value(in_, row) |
|
94 |
if value != None: |
|
95 |
xpath.put_obj(root, out, row_id, has_types, value) |
|
96 |
except Exception: traceback.print_exc() |
|
85 |
if not (limit == None or i < limit): break |
|
86 |
row_id = str(i) |
|
87 |
for in_, out in mappings: |
|
88 |
value = metadata_value(in_) |
|
89 |
if value == None: value = get_value(in_, row) |
|
90 |
if value != None: |
|
91 |
xpath.put_obj(root, out, row_id, has_types, value) |
|
92 |
process_row() |
|
97 | 93 |
|
98 | 94 |
if map_path == None: |
99 | 95 |
iter_ = xml_dom.NodeElemIter(doc0.documentElement) |
100 | 96 |
util.skip(iter_, xml_dom.is_text) # skip metadata |
101 |
#map(use_row, iter_)
|
|
97 |
map(process_row, iter_)
|
|
102 | 98 |
return doc0 |
103 | 99 |
elif in_is_db: |
104 | 100 |
assert in_is_xpaths |
... | ... | |
146 | 142 |
if value != '': return value |
147 | 143 |
else: return None |
148 | 144 |
process_rows(get_value, reader) |
149 |
xml_func.process(root) |
|
150 |
return doc1 |
|
151 | 145 |
|
152 | 146 |
# Output XML tree |
147 |
doc = xml_dom.create_doc(out_label) |
|
148 |
root = doc.documentElement |
|
153 | 149 |
if out_is_db: |
154 | 150 |
from psycopg2.extensions import ISOLATION_LEVEL_SERIALIZABLE |
155 | 151 |
import db_xml |
... | ... | |
159 | 155 |
out_pkeys = {} |
160 | 156 |
try: |
161 | 157 |
row_ct_ref = [0] |
162 |
def use_row(root): |
|
163 |
try: |
|
164 |
db_xml.put(out_db, root, False, row_ct_ref, out_pkeys) |
|
165 |
if commit: db.commit() |
|
166 |
except Exception: |
|
167 |
out_db.rollback() |
|
168 |
traceback.print_exc() |
|
169 |
map(use_row, process_input(use_row).firstChild) |
|
158 |
def process_row(): |
|
159 |
try: xml_func.process(root) |
|
160 |
except xml_func.SyntaxException: traceback.print_exc() |
|
161 |
else: |
|
162 |
assert xml_dom.has_one_child(root) |
|
163 |
child = root.firstChild |
|
164 |
try: |
|
165 |
db_xml.put(out_db, child, False, row_ct_ref, out_pkeys) |
|
166 |
if commit: db.commit() |
|
167 |
except Exception: |
|
168 |
out_db.rollback() |
|
169 |
traceback.print_exc() |
|
170 |
root.clear() |
|
171 |
process_input(root, process_row) |
|
170 | 172 |
print 'Inserted '+str(row_ct_ref[0])+' rows' |
171 | 173 |
finally: |
172 | 174 |
out_db.rollback() |
173 | 175 |
out_db.close() |
174 | 176 |
else: # output is XML |
175 |
output = sys.stdout |
|
176 |
config = xml_dom.prettyxml_config |
|
177 |
doc = xml_dom.create_doc(out_label) |
|
178 |
doc.write_opening(output, **config) |
|
179 |
def use_row(root): root.writexml(output, indent=1, **config) |
|
180 |
map(use_row, process_input(use_row).firstChild) |
|
181 |
doc.write_closing(output, **config) |
|
177 |
def process_row(): pass |
|
178 |
process_input(root, process_row) |
|
179 |
xml_func.process(root) |
|
180 |
doc.writexml(sys.stdout, **xml_dom.prettyxml_config) |
|
182 | 181 |
|
183 | 182 |
try: main() |
184 | 183 |
except Parser.SyntaxException, e: raise SystemExit(str(e)) |
Also available in: Unified diff
bin/map: Refactored to process each row separately when out_is_db