Project

General

Profile

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 traceback
9
import xml.dom.minidom as minidom
10

    
11
sys.path.append(os.path.dirname(__file__)+"/../lib")
12

    
13
import opts
14
import Parser
15
import sql
16
import xml_dom
17
import xml_func
18

    
19
def metadata_value(name):
20
    if type(name) == str and name.startswith(':'): return name[1:]
21
    else: return None
22

    
23
def main():
24
    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
    limit = opts.get_env_var('n', None, env_names)
29
    if limit != None: limit = int(limit)
30
    commit = opts.env_flag('commit')
31
    
32
    # Get db config from env vars
33
    db_config_names = ['engine', 'host', 'user', 'password', 'database']
34
    def get_db_config(prefix):
35
        return opts.get_env_vars(db_config_names, prefix, env_names)
36
    in_db_config = get_db_config('in')
37
    out_db_config = get_db_config('out')
38
    in_is_db = 'engine' in in_db_config
39
    out_is_db = 'engine' in out_db_config
40
    
41
    # Parse args
42
    map_path = None
43
    try: _prog_name, map_path = sys.argv
44
    except ValueError:
45
        if in_is_db: usage_err()
46
    
47
    # Load map header
48
    in_is_xpaths = True
49
    if map_path != None:
50
        import copy
51
        import csv
52
        
53
        import xpath
54
        
55
        metadata = []
56
        mappings = []
57
        stream = open(map_path, 'rb')
58
        reader = csv.reader(stream)
59
        in_label, out_label = reader.next()[:2]
60
        def split_col_name(name):
61
            name, sep, root = name.partition(':')
62
            return name, sep != '', root
63
        in_label, in_is_xpaths, in_root = split_col_name(in_label)
64
        out_label, out_is_xpaths, out_root = split_col_name(out_label)
65
        assert out_is_xpaths # CSV output not supported yet
66
        has_types = out_root.startswith('/*s/') # outer elements are types
67
        for row in reader:
68
            in_, out = row[:2]
69
            if out != '':
70
                if out_is_xpaths: out = out_root+out
71
                mappings.append((in_, out))
72
        stream.close()
73
    in_is_xml = in_is_xpaths and not in_is_db
74
    
75
    if in_is_xml: doc0 = minidom.parse(sys.stdin)
76
    
77
    def process_input(use_row):
78
        '''Inputs datasource to XML tree, mapping if needed'''
79
        doc1 = xml_dom.create_doc(out_label)
80
        root = doc1.documentElement
81
        
82
        def process_rows(get_value, rows):
83
            '''Processes input values
84
            @param get_value f(in_, row):str
85
            '''
86
            for i, row in enumerate(rows):
87
                if not (limit == None or i < limit): break
88
                row_id = str(i)
89
                for in_, out in mappings:
90
                    value = metadata_value(in_)
91
                    if value == None: value = get_value(in_, row)
92
                    if value != None:
93
                        try: xpath.put_obj(root, out, row_id, has_types, value)
94
                        except Exception: traceback.print_exc()
95
        
96
        if map_path == None: return doc0
97
        elif in_is_db:
98
            assert in_is_xpaths
99
            
100
            import db_xml
101
            
102
            in_root_xml = xpath.path2xml(in_root)
103
            for i, mapping in enumerate(mappings):
104
                in_, out = mapping
105
                if metadata_value(in_) == None:
106
                    mappings[i] = (xpath.path2xml(in_root+'/'+in_), out)
107
            
108
            in_db = sql.connect(in_db_config)
109
            in_pkeys = {}
110
            def get_value(in_, row):
111
                pkey, = row
112
                in_ = in_.cloneNode(True) # don't modify orig value!
113
                xml_dom.set_id(xpath.get(in_, in_root), pkey)
114
                value = sql.value_or_none(db_xml.get(in_db, in_, in_pkeys))
115
                if value != None: return str(value)
116
                else: return None
117
            process_rows(get_value, sql.rows(db_xml.get(in_db, in_root_xml,
118
                in_pkeys, limit)))
119
            in_db.close()
120
        elif in_is_xml:
121
            def get_value(in_, row):
122
                node = xpath.get(row, in_)
123
                if node != None: return xml_dom.value(node)
124
                else: return None
125
            row0 = xpath.get(doc0.documentElement, in_root)
126
            process_rows(get_value, xml_dom.NodeElemIter(row0.parentNode))
127
        else: # input is CSV
128
            map_ = dict(mappings)
129
            reader = csv.reader(sys.stdin)
130
            cols = reader.next()
131
            col_idxs = dict([(value, idx) for idx, value in enumerate(cols)])
132
            for i, mapping in enumerate(mappings):
133
                in_, out = mapping
134
                if metadata_value(in_) == None:
135
                    try: mappings[i] = (col_idxs[in_], out)
136
                    except KeyError: pass
137
            
138
            def get_value(in_, row):
139
                value = row[in_]
140
                if value != '': return value
141
                else: return None
142
            process_rows(get_value, reader)
143
        xml_func.process(root)
144
        return doc1
145
    
146
    # Output XML tree
147
    if out_is_db:
148
        from psycopg2.extensions import ISOLATION_LEVEL_SERIALIZABLE
149
        import db_xml
150
        
151
        out_db = sql.connect(out_db_config)
152
        out_db.set_isolation_level(ISOLATION_LEVEL_SERIALIZABLE)
153
        try:
154
            row_ct_ref = [0]
155
            def use_row(root): db_xml.xml2db(out_db, root, commit, row_ct_ref)
156
            db_xml.xml2db(out_db, process_input(use_row).documentElement,
157
                commit, row_ct_ref)
158
            print 'Inserted '+str(row_ct_ref[0])+' rows'
159
        finally:
160
            out_db.rollback()
161
            out_db.close()
162
    else: # output is XML
163
        output = sys.stdout
164
        config = xml_dom.prettyxml_config
165
        doc = xml_dom.create_doc(out_label)
166
        doc.write_opening(output, **config)
167
        def use_row(root): root.writexml(output, indent=1, **config)
168
        map(use_row, process_input(use_row).firstChild)
169
        doc.write_closing(output, **config)
170

    
171
try: main()
172
except Parser.SyntaxException, e: raise SystemExit(str(e))
(7-7/14)