Revision 135
Added by Aaron Marcuse-Kubitza about 13 years ago
map | ||
---|---|---|
3 | 3 |
# For outputting an XML file to a PostgreSQL database, use the general format of |
4 | 4 |
# http://vegbank.org/vegdocs/xml/vegbank_example_ver1.0.2.xml |
5 | 5 |
|
6 |
import os |
|
7 | 6 |
import os.path |
8 | 7 |
import sys |
9 | 8 |
import xml.dom.minidom |
... | ... | |
25 | 24 |
def usage_err(): |
26 | 25 |
raise SystemExit('Usage: '+opts.env_usage(env_names, True) |
27 | 26 |
+' [commit=1] '+sys.argv[0]+' [map_path] [<input] [>output]') |
27 |
limit = int(opts.get_env_var('n', sys.maxint, env_names)) |
|
28 |
commit = opts.env_flag('commit') |
|
28 | 29 |
|
29 | 30 |
# Get db config from env vars |
30 | 31 |
db_config_names = ['engine', 'host', 'user', 'password', 'database'] |
... | ... | |
39 | 40 |
map_path = None |
40 | 41 |
try: _prog_name, map_path = sys.argv |
41 | 42 |
except ValueError: |
42 |
if in_is_db or not out_is_db: usage_err() |
|
43 |
commit = opts.env_flag('commit') |
|
43 |
if in_is_db: usage_err() |
|
44 | 44 |
|
45 | 45 |
# Load map header |
46 | 46 |
in_is_xpaths = True |
... | ... | |
84 | 84 |
|
85 | 85 |
import db_xml |
86 | 86 |
|
87 |
src_root = xpath.str2xml(src_root) |
|
88 |
mappings = [(in_, xpath.path2xml(out)) for in_, out in mappings] |
|
87 |
src_root = xpath.parse(src_root) |
|
88 |
src_root_xml = xpath.path2xml(src_root) |
|
89 |
mappings = [(xpath.path2xml(in_), out) for in_, out in mappings] |
|
89 | 90 |
|
90 | 91 |
in_db = sql.connect(in_db_config) |
91 | 92 |
in_pkeys = {} |
92 |
for row_id in db_xml.get(in_db, src_root, in_pkeys): |
|
93 |
for row_idx, row in enumerate(sql.rows(db_xml.get(in_db, |
|
94 |
src_root_xml, in_pkeys))): |
|
95 |
if not row_idx < limit: break |
|
96 |
row_id, = row |
|
97 |
row_id = str(row_id) |
|
98 |
|
|
93 | 99 |
def put_col(path, value): |
94 | 100 |
xpath.put_obj(doc1, path, row_id, has_types, value) |
95 | 101 |
for value, out in metadata: put_col(out, value) |
96 | 102 |
for in_, out in mappings: |
97 |
put_col(out, db_xml.get(in_db, in_, in_pkeys)) |
|
98 |
xpath.put_obj(doc1, out, row_id, has_types, ) |
|
103 |
root = xpath.get(in_.ownerDocument, src_root) |
|
104 |
xml_dom.replace(root, root.cloneNode(False)) |
|
105 |
xml_dom.set_id(root, row_id) |
|
106 |
|
|
107 |
cur = db_xml.get(in_db, in_, in_pkeys) |
|
108 |
try: value = sql.value(cur) |
|
109 |
except StopIteration: continue |
|
110 |
put_col(out, str(value)) |
|
99 | 111 |
in_db.close() |
100 | 112 |
elif in_is_xml: raise SystemExit('XML input not supported yet') |
101 | 113 |
else: # input is CSV |
... | ... | |
103 | 115 |
reader = csv.reader(sys.stdin) |
104 | 116 |
cols = reader.next() |
105 | 117 |
for row_idx, row in enumerate(reader): |
118 |
if not row_idx < limit: break |
|
106 | 119 |
row_id = str(row_idx) |
120 |
|
|
107 | 121 |
def put_col(path, value): |
108 | 122 |
xpath.put_obj(doc1, path, row_id, has_types, value) |
109 | 123 |
for value, out in metadata: put_col(out, value) |
... | ... | |
127 | 141 |
finally: |
128 | 142 |
out_db.rollback() |
129 | 143 |
out_db.close() |
130 |
else: doc1.writexml(sys.stdout, addindent=' ', newl='\n') # output is XML
|
|
144 |
else: xml_dom.writexml(sys.stdout, doc1) # output is XML
|
|
131 | 145 |
|
132 | 146 |
try: main() |
133 | 147 |
except SyntaxException, ex: raise SystemExit(str(ex)) |
Also available in: Unified diff
map: Implemented DB input support for querying a single table