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 |
53
|
aaronmk
|
|
15 |
84
|
aaronmk
|
def metadata_value(name):
|
16 |
|
|
if name.startswith(':'): return name[1:]
|
17 |
|
|
else: return None
|
18 |
|
|
|
19 |
53
|
aaronmk
|
def main():
|
20 |
|
|
# Get db config from env vars
|
21 |
|
|
db_config_names = ['host', 'user', 'password', 'database']
|
22 |
|
|
env_names = []
|
23 |
|
|
def get_db_config(prefix):
|
24 |
64
|
aaronmk
|
return opts.get_env_vars(db_config_names, prefix, env_names)
|
25 |
67
|
aaronmk
|
in_db_config = get_db_config('in')
|
26 |
|
|
out_db_config = get_db_config('out')
|
27 |
|
|
in_is_db = in_db_config != None
|
28 |
|
|
out_is_db = out_db_config != None
|
29 |
53
|
aaronmk
|
|
30 |
|
|
# Parse args
|
31 |
73
|
aaronmk
|
map_path = None
|
32 |
67
|
aaronmk
|
try: _prog_name, map_path = sys.argv
|
33 |
53
|
aaronmk
|
except ValueError:
|
34 |
73
|
aaronmk
|
if in_is_db or not out_is_db: raise SystemExit('Usage: '
|
35 |
|
|
+opts.env_usage(env_names, True)+' [commit=1] '+sys.argv[0]
|
36 |
|
|
+' [map_path] [<input] [>output]')
|
37 |
64
|
aaronmk
|
commit = opts.env_flag('commit')
|
38 |
53
|
aaronmk
|
|
39 |
57
|
aaronmk
|
# Load map header
|
40 |
|
|
in_is_xml = True
|
41 |
73
|
aaronmk
|
if map_path != None:
|
42 |
56
|
aaronmk
|
import copy
|
43 |
|
|
import csv
|
44 |
|
|
|
45 |
75
|
aaronmk
|
from Parser import SyntaxException
|
46 |
53
|
aaronmk
|
import xpath
|
47 |
|
|
|
48 |
73
|
aaronmk
|
mappings = []
|
49 |
|
|
stream = open(map_path, 'rb')
|
50 |
|
|
reader = csv.reader(stream)
|
51 |
|
|
src, dest = reader.next()[:2]
|
52 |
61
|
aaronmk
|
def split_col_name(name):
|
53 |
72
|
aaronmk
|
name, sep, root = name.partition(':')
|
54 |
|
|
return name, sep != '', root
|
55 |
67
|
aaronmk
|
src, in_is_xml, src_root = split_col_name(src)
|
56 |
|
|
dest, out_is_xml, dest_root = split_col_name(dest)
|
57 |
61
|
aaronmk
|
assert out_is_xml
|
58 |
67
|
aaronmk
|
has_types = dest_root.startswith('/*s/') # outer elements are types
|
59 |
73
|
aaronmk
|
for row in reader:
|
60 |
|
|
in_, out = row[:2]
|
61 |
|
|
if out != '':
|
62 |
84
|
aaronmk
|
try: mappings.append((in_, xpath.parse(dest_root+out)))
|
63 |
73
|
aaronmk
|
except SyntaxException, ex: raise SystemExit(str(ex))
|
64 |
|
|
stream.close()
|
65 |
56
|
aaronmk
|
|
66 |
57
|
aaronmk
|
# Input datasource to XML tree, mapping if needed
|
67 |
|
|
if in_is_xml: doc = xml.dom.minidom.parse(sys.stdin)
|
68 |
73
|
aaronmk
|
if map_path != None:
|
69 |
56
|
aaronmk
|
out_doc = xml.dom.minidom.getDOMImplementation().createDocument(None,
|
70 |
|
|
dest, None)
|
71 |
57
|
aaronmk
|
if in_is_xml: raise Exception('XML-XML mapping not supported yet')
|
72 |
73
|
aaronmk
|
elif in_is_db: raise Exception('DB-XML mapping not supported yet')
|
73 |
56
|
aaronmk
|
else: # input is CSV
|
74 |
84
|
aaronmk
|
metadata = []
|
75 |
|
|
map_ = {}
|
76 |
|
|
for in_, out in mappings:
|
77 |
|
|
value = metadata_value(in_)
|
78 |
|
|
if value != None: metadata.append((value, out))
|
79 |
|
|
else: map_[in_] = out
|
80 |
|
|
|
81 |
59
|
aaronmk
|
reader = csv.reader(sys.stdin)
|
82 |
84
|
aaronmk
|
cols = reader.next()
|
83 |
83
|
aaronmk
|
for row_idx, row in enumerate(reader):
|
84 |
56
|
aaronmk
|
row_id = str(row_idx)
|
85 |
84
|
aaronmk
|
def put_col(path, value):
|
86 |
|
|
xpath.put_obj(out_doc, path, row_id, has_types, value)
|
87 |
|
|
for value, out in metadata: put_col(out, value)
|
88 |
|
|
for i, col in enumerate(cols):
|
89 |
|
|
if row[i] != '' and col in map_: put_col(map_[col], row[i])
|
90 |
56
|
aaronmk
|
doc = out_doc
|
91 |
53
|
aaronmk
|
|
92 |
|
|
# Output XML tree
|
93 |
67
|
aaronmk
|
if out_db_config != None: # output is database
|
94 |
53
|
aaronmk
|
import psycopg2
|
95 |
|
|
from psycopg2.extensions import ISOLATION_LEVEL_SERIALIZABLE
|
96 |
|
|
|
97 |
|
|
import db_xml
|
98 |
|
|
|
99 |
67
|
aaronmk
|
db = psycopg2.connect(**out_db_config)
|
100 |
53
|
aaronmk
|
db.set_isolation_level(ISOLATION_LEVEL_SERIALIZABLE)
|
101 |
|
|
try:
|
102 |
|
|
row_ct_ref = [0]
|
103 |
|
|
db_xml.xml2db(db, doc.documentElement, row_ct_ref)
|
104 |
|
|
print 'Inserted '+str(row_ct_ref[0])+' rows'
|
105 |
|
|
if commit: db.commit()
|
106 |
|
|
finally:
|
107 |
|
|
db.rollback()
|
108 |
|
|
db.close()
|
109 |
|
|
else: doc.writexml(sys.stdout, addindent=' ', newl='\n') # output is XML
|
110 |
|
|
|
111 |
|
|
main()
|