Revision 67
Added by Aaron Marcuse-Kubitza about 13 years ago
map | ||
---|---|---|
18 | 18 |
env_names = [] |
19 | 19 |
def get_db_config(prefix): |
20 | 20 |
return opts.get_env_vars(db_config_names, prefix, env_names) |
21 |
from_db_config = get_db_config('from')
|
|
22 |
to_db_config = get_db_config('to')
|
|
23 |
in_is_db = from_db_config != None
|
|
24 |
out_is_db = to_db_config != None
|
|
21 |
in_db_config = get_db_config('in')
|
|
22 |
out_db_config = get_db_config('out')
|
|
23 |
in_is_db = in_db_config != None
|
|
24 |
out_is_db = out_db_config != None
|
|
25 | 25 |
uses_map = in_is_db or not out_is_db |
26 | 26 |
|
27 | 27 |
# Parse args |
28 |
prog_name = sys.argv[0] |
|
29 |
try: prog_name, map_path = sys.argv |
|
28 |
try: _prog_name, map_path = sys.argv |
|
30 | 29 |
except ValueError: |
31 |
if uses_map: raise SystemExit('Usage: env'+''.join(map(lambda name: |
|
32 |
' ['+name+'=...]', env_names))+' [commit=1] '+prog_name |
|
33 |
+' [map_path] [<input] [>output]') |
|
30 |
if uses_map: raise SystemExit('Usage: '+opts.env_usage(env_names, True) |
|
31 |
+' [commit=1] '+sys.argv[0]+' [map_path] [<input] [>output]') |
|
34 | 32 |
commit = opts.env_flag('commit') |
35 | 33 |
|
36 | 34 |
# Load map header |
... | ... | |
45 | 43 |
map_reader = csv.reader(map_stream) |
46 | 44 |
src, dest = map_reader.next()[:2] |
47 | 45 |
def split_col_name(name): |
48 |
name, sep, prefix = name.partition('/')
|
|
49 |
return name, sep != '', sep+prefix
|
|
50 |
src, in_is_xml, src_prefix = split_col_name(src)
|
|
51 |
dest, out_is_xml, dest_prefix = split_col_name(dest)
|
|
46 |
name, sep, root = name.partition('/')
|
|
47 |
return name, sep != '', sep+root
|
|
48 |
src, in_is_xml, src_root = split_col_name(src)
|
|
49 |
dest, out_is_xml, dest_root = split_col_name(dest)
|
|
52 | 50 |
assert out_is_xml |
53 |
has_types = dest_prefix.startswith('/*s/') # outer elements are types
|
|
51 |
has_types = dest_root.startswith('/*s/') # outer elements are types
|
|
54 | 52 |
|
55 | 53 |
# Input datasource to XML tree, mapping if needed |
56 | 54 |
if in_is_xml: doc = xml.dom.minidom.parse(sys.stdin) |
... | ... | |
62 | 60 |
for row in map_reader: |
63 | 61 |
in_, out = row[:2] |
64 | 62 |
if out != '': |
65 |
try: out = xpath.parse(dest_prefix+out)
|
|
63 |
try: out = xpath.parse(dest_root+out)
|
|
66 | 64 |
except SyntaxException, ex: raise SystemExit(str(ex)) |
67 | 65 |
if in_is_xml: pass # TODO: process the mapping |
68 | 66 |
elif in_is_db: pass # TODO: process the mapping |
... | ... | |
89 | 87 |
doc = out_doc |
90 | 88 |
|
91 | 89 |
# Output XML tree |
92 |
if to_db_config != None: # output is database
|
|
90 |
if out_db_config != None: # output is database
|
|
93 | 91 |
import psycopg2 |
94 | 92 |
from psycopg2.extensions import ISOLATION_LEVEL_SERIALIZABLE |
95 | 93 |
|
96 | 94 |
import db_xml |
97 | 95 |
|
98 |
db = psycopg2.connect(**to_db_config)
|
|
96 |
db = psycopg2.connect(**out_db_config)
|
|
99 | 97 |
db.set_isolation_level(ISOLATION_LEVEL_SERIALIZABLE) |
100 | 98 |
try: |
101 | 99 |
row_ct_ref = [0] |
Also available in: Unified diff
Moved env usage string creation to opts.py. Changed db config var names to use in/out instead of from/to.