Revision 309
Added by Aaron Marcuse-Kubitza almost 13 years ago
bin/map | ||
---|---|---|
74 | 74 |
|
75 | 75 |
if in_is_xml: doc0 = minidom.parse(sys.stdin) |
76 | 76 |
|
77 |
def process_xml(use_row):
|
|
78 |
'''Maps datasource to XML tree'''
|
|
77 |
def process_input(use_row):
|
|
78 |
'''Inputs datasource to XML tree, mapping if needed'''
|
|
79 | 79 |
doc1 = xml_dom.create_doc(out_label) |
80 | 80 |
root = doc1.documentElement |
81 | 81 |
|
82 |
def process(get_value, rows): |
|
82 |
def process_rows(get_value, rows):
|
|
83 | 83 |
'''Processes input values |
84 | 84 |
@param get_value f(in_, row):str |
85 | 85 |
''' |
... | ... | |
93 | 93 |
try: xpath.put_obj(root, out, row_id, has_types, value) |
94 | 94 |
except Exception: traceback.print_exc() |
95 | 95 |
|
96 |
if in_is_db: |
|
96 |
if map_path == None: return doc0 |
|
97 |
elif in_is_db: |
|
97 | 98 |
assert in_is_xpaths |
98 | 99 |
|
99 | 100 |
import db_xml |
... | ... | |
113 | 114 |
value = sql.value_or_none(db_xml.get(in_db, in_, in_pkeys)) |
114 | 115 |
if value != None: return str(value) |
115 | 116 |
else: return None |
116 |
process(get_value, sql.rows(db_xml.get(in_db, in_root_xml, in_pkeys,
|
|
117 |
limit))) |
|
117 |
process_rows(get_value, sql.rows(db_xml.get(in_db, in_root_xml,
|
|
118 |
in_pkeys, limit)))
|
|
118 | 119 |
in_db.close() |
119 | 120 |
elif in_is_xml: |
120 | 121 |
def get_value(in_, row): |
... | ... | |
122 | 123 |
if node != None: return xml_dom.value(node) |
123 | 124 |
else: return None |
124 | 125 |
row0 = xpath.get(doc0.documentElement, in_root) |
125 |
process(get_value, xml_dom.NodeElemIter(row0.parentNode)) |
|
126 |
process_rows(get_value, xml_dom.NodeElemIter(row0.parentNode))
|
|
126 | 127 |
else: # input is CSV |
127 | 128 |
map_ = dict(mappings) |
128 | 129 |
reader = csv.reader(sys.stdin) |
... | ... | |
138 | 139 |
value = row[in_] |
139 | 140 |
if value != '': return value |
140 | 141 |
else: return None |
141 |
process(get_value, reader) |
|
142 |
process_rows(get_value, reader)
|
|
142 | 143 |
xml_func.process(root) |
143 | 144 |
return doc1 |
144 | 145 |
|
145 |
def get_xml(use_row): |
|
146 |
'''Inputs datasource to XML tree, mapping if needed''' |
|
147 |
if map_path != None: return process_xml(use_row) |
|
148 |
else: return doc0 |
|
149 |
|
|
150 | 146 |
# Output XML tree |
151 | 147 |
if out_is_db: |
152 | 148 |
from psycopg2.extensions import ISOLATION_LEVEL_SERIALIZABLE |
... | ... | |
157 | 153 |
try: |
158 | 154 |
row_ct_ref = [0] |
159 | 155 |
def use_row(root): db_xml.xml2db(out_db, root, commit, row_ct_ref) |
160 |
db_xml.xml2db(out_db, get_xml(use_row).documentElement, commit,
|
|
161 |
row_ct_ref) |
|
156 |
db_xml.xml2db(out_db, process_input(use_row).documentElement,
|
|
157 |
commit, row_ct_ref)
|
|
162 | 158 |
print 'Inserted '+str(row_ct_ref[0])+' rows' |
163 | 159 |
finally: |
164 | 160 |
out_db.rollback() |
... | ... | |
169 | 165 |
doc = xml_dom.create_doc(out_label) |
170 | 166 |
doc.write_opening(output, **config) |
171 | 167 |
def use_row(root): root.writexml(output, indent=1, **config) |
172 |
map(use_row, get_xml(use_row).firstChild)
|
|
168 |
map(use_row, process_input(use_row).firstChild)
|
|
173 | 169 |
doc.write_closing(output, **config) |
174 | 170 |
|
175 | 171 |
try: main() |
Also available in: Unified diff
bin/map: Merged get_xml() into process_xml(). Renamed process*() functions to distinguish them better.