1
|
#!/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.path
|
7
|
import sys
|
8
|
import xml.dom.minidom as minidom
|
9
|
|
10
|
sys.path.append(os.path.dirname(__file__)+"/../lib")
|
11
|
|
12
|
import exc
|
13
|
import opts
|
14
|
import Parser
|
15
|
import sql
|
16
|
import util
|
17
|
import xml_dom
|
18
|
import xml_func
|
19
|
|
20
|
def metadata_value(name):
|
21
|
if type(name) == str and name.startswith(':'): return name[1:]
|
22
|
else: return None
|
23
|
|
24
|
def main():
|
25
|
ex_tracker = exc.ExTracker()
|
26
|
|
27
|
env_names = []
|
28
|
def usage_err():
|
29
|
raise SystemExit('Usage: '+opts.env_usage(env_names, True)+' [commit=1]'
|
30
|
' [test=1] '+sys.argv[0]+' [map_path...] [<input] [>output]')
|
31
|
limit = opts.get_env_var('n', None, env_names)
|
32
|
if limit != None: limit = int(limit)
|
33
|
test = opts.env_flag('test')
|
34
|
commit = not test and opts.env_flag('commit') # never commit in test mode
|
35
|
|
36
|
# Get db config from env vars
|
37
|
db_config_names = ['engine', 'host', 'user', 'password', 'database']
|
38
|
def get_db_config(prefix):
|
39
|
return opts.get_env_vars(db_config_names, prefix, env_names)
|
40
|
in_db_config = get_db_config('in')
|
41
|
out_db_config = get_db_config('out')
|
42
|
in_is_db = 'engine' in in_db_config
|
43
|
out_is_db = 'engine' in out_db_config
|
44
|
|
45
|
# Parse args
|
46
|
# Parse args
|
47
|
map_paths = sys.argv[1:]
|
48
|
if map_paths == [] and (in_is_db or not out_is_db): usage_err()
|
49
|
map_path = None
|
50
|
if map_paths != []: map_path = map_paths[0]
|
51
|
|
52
|
# Load map header
|
53
|
in_is_xpaths = True
|
54
|
out_label = None
|
55
|
if map_path != None:
|
56
|
import copy
|
57
|
import csv
|
58
|
|
59
|
import xpath
|
60
|
|
61
|
metadata = []
|
62
|
mappings = []
|
63
|
stream = open(map_path, 'rb')
|
64
|
reader = csv.reader(stream)
|
65
|
in_label, out_label = reader.next()[:2]
|
66
|
def split_col_name(name):
|
67
|
name, sep, root = name.partition(':')
|
68
|
return name, sep != '', root
|
69
|
in_label, in_is_xpaths, in_root = split_col_name(in_label)
|
70
|
out_label, out_is_xpaths, out_root = split_col_name(out_label)
|
71
|
assert out_is_xpaths # CSV output not supported yet
|
72
|
has_types = out_root.startswith('/*s/') # outer elements are types
|
73
|
for row in reader:
|
74
|
in_, out = row[:2]
|
75
|
if out != '':
|
76
|
if out_is_xpaths: out = xpath.parse(out_root+out)
|
77
|
mappings.append((in_, out))
|
78
|
stream.close()
|
79
|
in_is_xml = in_is_xpaths and not in_is_db
|
80
|
|
81
|
if in_is_xml:
|
82
|
doc0 = minidom.parse(sys.stdin)
|
83
|
if out_label == None: out_label = doc0.documentElement.tagName
|
84
|
|
85
|
def process_input(root, process_row):
|
86
|
'''Inputs datasource to XML tree, mapping if needed'''
|
87
|
def process_rows(get_value, rows):
|
88
|
'''Processes input values
|
89
|
@param get_value f(in_, row):str
|
90
|
'''
|
91
|
for i, row in enumerate(rows):
|
92
|
if not (limit == None or i < limit): break
|
93
|
row_id = str(i)
|
94
|
for in_, out in mappings:
|
95
|
value = metadata_value(in_)
|
96
|
if value == None: value = get_value(in_, row)
|
97
|
if value != None:
|
98
|
xpath.put_obj(root, out, row_id, has_types, value)
|
99
|
process_row(row)
|
100
|
sys.stderr.write('Processed '+str(i+1)+' input rows\n')
|
101
|
|
102
|
if map_path == None:
|
103
|
iter_ = xml_dom.NodeElemIter(doc0.documentElement)
|
104
|
util.skip(iter_, xml_dom.is_text) # skip metadata
|
105
|
for child in iter_:
|
106
|
root.appendChild(child)
|
107
|
process_row(child)
|
108
|
elif in_is_db:
|
109
|
assert in_is_xpaths
|
110
|
|
111
|
import db_xml
|
112
|
|
113
|
in_root_xml = xpath.path2xml(in_root)
|
114
|
for i, mapping in enumerate(mappings):
|
115
|
in_, out = mapping
|
116
|
if metadata_value(in_) == None:
|
117
|
mappings[i] = (xpath.path2xml(in_root+'/'+in_), out)
|
118
|
|
119
|
in_db = sql.connect(in_db_config)
|
120
|
in_pkeys = {}
|
121
|
def get_value(in_, row):
|
122
|
pkey, = row
|
123
|
in_ = in_.cloneNode(True) # don't modify orig value!
|
124
|
xml_dom.set_id(xpath.get(in_, in_root), pkey)
|
125
|
value = sql.value_or_none(db_xml.get(in_db, in_, in_pkeys))
|
126
|
if value != None: return str(value)
|
127
|
else: return None
|
128
|
process_rows(get_value, sql.rows(db_xml.get(in_db, in_root_xml,
|
129
|
in_pkeys, limit)))
|
130
|
in_db.close()
|
131
|
elif in_is_xml:
|
132
|
def get_value(in_, row):
|
133
|
node = xpath.get(row, in_)
|
134
|
if node != None: return xml_dom.value(node)
|
135
|
else: return None
|
136
|
row0 = xpath.get(doc0.documentElement, in_root)
|
137
|
process_rows(get_value, xml_dom.NodeElemIter(row0.parentNode))
|
138
|
else: # input is CSV
|
139
|
map_ = dict(mappings)
|
140
|
reader = csv.reader(sys.stdin)
|
141
|
cols = reader.next()
|
142
|
col_idxs = dict([(value, idx) for idx, value in enumerate(cols)])
|
143
|
for i, mapping in enumerate(mappings):
|
144
|
in_, out = mapping
|
145
|
if metadata_value(in_) == None:
|
146
|
try: mappings[i] = (col_idxs[in_], out)
|
147
|
except KeyError: pass
|
148
|
|
149
|
def get_value(in_, row):
|
150
|
value = row[in_]
|
151
|
if value != '': return value
|
152
|
else: return None
|
153
|
process_rows(get_value, reader)
|
154
|
|
155
|
# Output XML tree
|
156
|
doc = xml_dom.create_doc(out_label)
|
157
|
root = doc.documentElement
|
158
|
if out_is_db:
|
159
|
from psycopg2.extensions import ISOLATION_LEVEL_SERIALIZABLE
|
160
|
import db_xml
|
161
|
|
162
|
out_db = sql.connect(out_db_config)
|
163
|
out_db.set_isolation_level(ISOLATION_LEVEL_SERIALIZABLE)
|
164
|
out_pkeys = {}
|
165
|
try:
|
166
|
if test: sql.empty_db(out_db)
|
167
|
row_ct_ref = [0]
|
168
|
|
169
|
def process_row(input_row):
|
170
|
def on_error(e):
|
171
|
exc.add_msg(e, 'output row:\n'+str(root))
|
172
|
exc.add_msg(e, 'input row:\n'+str(input_row))
|
173
|
ex_tracker.track(e)
|
174
|
|
175
|
xml_func.process(root, on_error)
|
176
|
if not xml_dom.is_empty(root):
|
177
|
assert xml_dom.has_one_child(root)
|
178
|
try:
|
179
|
sql.with_savepoint(out_db, lambda: db_xml.put(out_db,
|
180
|
root.firstChild, out_pkeys, row_ct_ref, on_error))
|
181
|
if commit: out_db.commit()
|
182
|
except sql.DatabaseErrors, e: on_error(e)
|
183
|
root.clear()
|
184
|
|
185
|
process_input(root, process_row)
|
186
|
sys.stderr.write('Inserted '+str(row_ct_ref[0])+
|
187
|
' new rows into database\n')
|
188
|
finally:
|
189
|
out_db.rollback()
|
190
|
out_db.close()
|
191
|
else: # output is XML
|
192
|
def process_row(input_row): pass
|
193
|
process_input(root, process_row)
|
194
|
xml_func.process(root)
|
195
|
doc.writexml(sys.stdout, **xml_dom.prettyxml_config)
|
196
|
|
197
|
try: main()
|
198
|
except Parser.SyntaxException, e: raise SystemExit(str(e))
|