Revision 73
Added by Aaron Marcuse-Kubitza almost 13 years ago
map | ||
---|---|---|
22 | 22 |
out_db_config = get_db_config('out') |
23 | 23 |
in_is_db = in_db_config != None |
24 | 24 |
out_is_db = out_db_config != None |
25 |
uses_map = in_is_db or not out_is_db |
|
26 | 25 |
|
27 | 26 |
# Parse args |
27 |
map_path = None |
|
28 | 28 |
try: _prog_name, map_path = sys.argv |
29 | 29 |
except ValueError: |
30 |
if uses_map: raise SystemExit('Usage: '+opts.env_usage(env_names, True) |
|
31 |
+' [commit=1] '+sys.argv[0]+' [map_path] [<input] [>output]') |
|
30 |
if in_is_db or not out_is_db: raise SystemExit('Usage: ' |
|
31 |
+opts.env_usage(env_names, True)+' [commit=1] '+sys.argv[0] |
|
32 |
+' [map_path] [<input] [>output]') |
|
32 | 33 |
commit = opts.env_flag('commit') |
33 | 34 |
|
34 | 35 |
# Load map header |
35 | 36 |
in_is_xml = True |
36 |
if uses_map:
|
|
37 |
if map_path != None:
|
|
37 | 38 |
import copy |
38 | 39 |
import csv |
39 | 40 |
|
40 | 41 |
import xpath |
41 | 42 |
|
42 |
map_stream = open(map_path, 'rb') |
|
43 |
map_reader = csv.reader(map_stream) |
|
44 |
src, dest = map_reader.next()[:2] |
|
43 |
mappings = [] |
|
44 |
stream = open(map_path, 'rb') |
|
45 |
reader = csv.reader(stream) |
|
46 |
src, dest = reader.next()[:2] |
|
45 | 47 |
def split_col_name(name): |
46 | 48 |
name, sep, root = name.partition(':') |
47 | 49 |
return name, sep != '', root |
... | ... | |
49 | 51 |
dest, out_is_xml, dest_root = split_col_name(dest) |
50 | 52 |
assert out_is_xml |
51 | 53 |
has_types = dest_root.startswith('/*s/') # outer elements are types |
54 |
for row in reader: |
|
55 |
in_, out = row[:2] |
|
56 |
if out != '': |
|
57 |
try: out = xpath.parse(dest_root+out) |
|
58 |
except SyntaxException, ex: raise SystemExit(str(ex)) |
|
59 |
mappings.append((in_, out)) |
|
60 |
stream.close() |
|
52 | 61 |
|
53 | 62 |
# Input datasource to XML tree, mapping if needed |
54 | 63 |
if in_is_xml: doc = xml.dom.minidom.parse(sys.stdin) |
55 |
if uses_map:
|
|
64 |
if map_path != None:
|
|
56 | 65 |
from Parser import SyntaxException |
57 | 66 |
import xml_xpath |
58 | 67 |
|
59 |
map_ = {} |
|
60 |
for row in map_reader: |
|
61 |
in_, out = row[:2] |
|
62 |
if out != '': |
|
63 |
try: out = xpath.parse(dest_root+out) |
|
64 |
except SyntaxException, ex: raise SystemExit(str(ex)) |
|
65 |
if in_is_xml: pass # TODO: process the mapping |
|
66 |
elif in_is_db: pass # TODO: process the mapping |
|
67 |
else: map_[in_] = out |
|
68 |
map_stream.close() |
|
69 |
|
|
70 | 68 |
out_doc = xml.dom.minidom.getDOMImplementation().createDocument(None, |
71 | 69 |
dest, None) |
72 | 70 |
if in_is_xml: raise Exception('XML-XML mapping not supported yet') |
71 |
elif in_is_db: raise Exception('DB-XML mapping not supported yet') |
|
73 | 72 |
else: # input is CSV |
73 |
map_ = dict(mappings) |
|
74 | 74 |
reader = csv.reader(sys.stdin) |
75 | 75 |
fieldnames = reader.next() |
76 | 76 |
row_idx = 0 |
Also available in: Unified diff
map: Fixed bugs to enable mapping straight from CSV to a database. Still need a way to set plot.authorPlotCode for specimens data.