1 |
53
|
aaronmk
|
#!/usr/bin/env python
|
2 |
|
|
# Maps one datasource to another, using a map spreadsheet if needed
|
3 |
|
|
# For outputting an XML file to a PostgreSQL database, use the general format of
|
4 |
|
|
# http://vegbank.org/vegdocs/xml/vegbank_example_ver1.0.2.xml
|
5 |
|
|
|
6 |
|
|
import os
|
7 |
|
|
import os.path
|
8 |
|
|
import sys
|
9 |
|
|
import xml.dom.minidom
|
10 |
|
|
|
11 |
|
|
sys.path.append(os.path.dirname(__file__)+"/lib")
|
12 |
|
|
|
13 |
64
|
aaronmk
|
import opts
|
14 |
133
|
aaronmk
|
from Parser import SyntaxException
|
15 |
131
|
aaronmk
|
import sql
|
16 |
133
|
aaronmk
|
import xml_dom
|
17 |
86
|
aaronmk
|
import xml_func
|
18 |
53
|
aaronmk
|
|
19 |
84
|
aaronmk
|
def metadata_value(name):
|
20 |
|
|
if name.startswith(':'): return name[1:]
|
21 |
|
|
else: return None
|
22 |
|
|
|
23 |
53
|
aaronmk
|
def main():
|
24 |
131
|
aaronmk
|
env_names = []
|
25 |
|
|
def usage_err():
|
26 |
|
|
raise SystemExit('Usage: '+opts.env_usage(env_names, True)
|
27 |
|
|
+' [commit=1] '+sys.argv[0]+' [map_path] [<input] [>output]')
|
28 |
|
|
|
29 |
53
|
aaronmk
|
# Get db config from env vars
|
30 |
131
|
aaronmk
|
db_config_names = ['engine', 'host', 'user', 'password', 'database']
|
31 |
53
|
aaronmk
|
def get_db_config(prefix):
|
32 |
64
|
aaronmk
|
return opts.get_env_vars(db_config_names, prefix, env_names)
|
33 |
67
|
aaronmk
|
in_db_config = get_db_config('in')
|
34 |
|
|
out_db_config = get_db_config('out')
|
35 |
131
|
aaronmk
|
in_is_db = 'engine' in in_db_config
|
36 |
|
|
out_is_db = 'engine' in out_db_config
|
37 |
53
|
aaronmk
|
|
38 |
|
|
# Parse args
|
39 |
73
|
aaronmk
|
map_path = None
|
40 |
67
|
aaronmk
|
try: _prog_name, map_path = sys.argv
|
41 |
53
|
aaronmk
|
except ValueError:
|
42 |
131
|
aaronmk
|
if in_is_db or not out_is_db: usage_err()
|
43 |
64
|
aaronmk
|
commit = opts.env_flag('commit')
|
44 |
53
|
aaronmk
|
|
45 |
57
|
aaronmk
|
# Load map header
|
46 |
130
|
aaronmk
|
in_is_xpaths = True
|
47 |
73
|
aaronmk
|
if map_path != None:
|
48 |
56
|
aaronmk
|
import copy
|
49 |
|
|
import csv
|
50 |
|
|
|
51 |
53
|
aaronmk
|
import xpath
|
52 |
|
|
|
53 |
133
|
aaronmk
|
metadata = []
|
54 |
73
|
aaronmk
|
mappings = []
|
55 |
|
|
stream = open(map_path, 'rb')
|
56 |
|
|
reader = csv.reader(stream)
|
57 |
|
|
src, dest = reader.next()[:2]
|
58 |
61
|
aaronmk
|
def split_col_name(name):
|
59 |
72
|
aaronmk
|
name, sep, root = name.partition(':')
|
60 |
|
|
return name, sep != '', root
|
61 |
130
|
aaronmk
|
src, in_is_xpaths, src_root = split_col_name(src)
|
62 |
|
|
dest, out_is_xpaths, dest_root = split_col_name(dest)
|
63 |
133
|
aaronmk
|
assert out_is_xpaths # CSV output not supported yet
|
64 |
67
|
aaronmk
|
has_types = dest_root.startswith('/*s/') # outer elements are types
|
65 |
73
|
aaronmk
|
for row in reader:
|
66 |
|
|
in_, out = row[:2]
|
67 |
|
|
if out != '':
|
68 |
133
|
aaronmk
|
value = metadata_value(in_)
|
69 |
|
|
is_metadata = value != None
|
70 |
|
|
if in_is_xpaths and not is_metadata:
|
71 |
|
|
in_ = xpath.parse(src_root+in_)
|
72 |
|
|
if out_is_xpaths: out = xpath.parse(dest_root+out)
|
73 |
|
|
if is_metadata: metadata.append((value, out))
|
74 |
|
|
else: mappings.append((in_, out))
|
75 |
73
|
aaronmk
|
stream.close()
|
76 |
130
|
aaronmk
|
in_is_xml = in_is_xpaths and not in_is_db
|
77 |
56
|
aaronmk
|
|
78 |
57
|
aaronmk
|
# Input datasource to XML tree, mapping if needed
|
79 |
116
|
aaronmk
|
if in_is_xml: doc0 = xml.dom.minidom.parse(sys.stdin)
|
80 |
73
|
aaronmk
|
if map_path != None:
|
81 |
133
|
aaronmk
|
doc1 = xml_dom.create_doc(dest)
|
82 |
126
|
aaronmk
|
if in_is_db:
|
83 |
130
|
aaronmk
|
assert in_is_xpaths
|
84 |
126
|
aaronmk
|
|
85 |
117
|
aaronmk
|
import db_xml
|
86 |
|
|
|
87 |
133
|
aaronmk
|
src_root = xpath.str2xml(src_root)
|
88 |
|
|
mappings = [(in_, xpath.path2xml(out)) for in_, out in mappings]
|
89 |
126
|
aaronmk
|
|
90 |
131
|
aaronmk
|
in_db = sql.connect(in_db_config)
|
91 |
133
|
aaronmk
|
in_pkeys = {}
|
92 |
|
|
for row_id in db_xml.get(in_db, src_root, in_pkeys):
|
93 |
|
|
def put_col(path, value):
|
94 |
|
|
xpath.put_obj(doc1, path, row_id, has_types, value)
|
95 |
|
|
for value, out in metadata: put_col(out, value)
|
96 |
|
|
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, )
|
99 |
117
|
aaronmk
|
in_db.close()
|
100 |
133
|
aaronmk
|
elif in_is_xml: raise SystemExit('XML input not supported yet')
|
101 |
56
|
aaronmk
|
else: # input is CSV
|
102 |
133
|
aaronmk
|
map_ = dict(mappings)
|
103 |
59
|
aaronmk
|
reader = csv.reader(sys.stdin)
|
104 |
84
|
aaronmk
|
cols = reader.next()
|
105 |
83
|
aaronmk
|
for row_idx, row in enumerate(reader):
|
106 |
56
|
aaronmk
|
row_id = str(row_idx)
|
107 |
84
|
aaronmk
|
def put_col(path, value):
|
108 |
116
|
aaronmk
|
xpath.put_obj(doc1, path, row_id, has_types, value)
|
109 |
84
|
aaronmk
|
for value, out in metadata: put_col(out, value)
|
110 |
|
|
for i, col in enumerate(cols):
|
111 |
|
|
if row[i] != '' and col in map_: put_col(map_[col], row[i])
|
112 |
116
|
aaronmk
|
xml_func.process(doc1)
|
113 |
|
|
else: doc1 = doc0
|
114 |
53
|
aaronmk
|
|
115 |
|
|
# Output XML tree
|
116 |
130
|
aaronmk
|
if out_is_db:
|
117 |
53
|
aaronmk
|
from psycopg2.extensions import ISOLATION_LEVEL_SERIALIZABLE
|
118 |
|
|
import db_xml
|
119 |
|
|
|
120 |
133
|
aaronmk
|
out_db = sql.connect(out_db_config)
|
121 |
|
|
out_db.set_isolation_level(ISOLATION_LEVEL_SERIALIZABLE)
|
122 |
53
|
aaronmk
|
try:
|
123 |
|
|
row_ct_ref = [0]
|
124 |
133
|
aaronmk
|
db_xml.xml2db(out_db, doc1.documentElement, row_ct_ref)
|
125 |
53
|
aaronmk
|
print 'Inserted '+str(row_ct_ref[0])+' rows'
|
126 |
133
|
aaronmk
|
if commit: out_db.commit()
|
127 |
53
|
aaronmk
|
finally:
|
128 |
133
|
aaronmk
|
out_db.rollback()
|
129 |
|
|
out_db.close()
|
130 |
116
|
aaronmk
|
else: doc1.writexml(sys.stdout, addindent=' ', newl='\n') # output is XML
|
131 |
53
|
aaronmk
|
|
132 |
133
|
aaronmk
|
try: main()
|
133 |
|
|
except SyntaxException, ex: raise SystemExit(str(ex))
|