Project

General

Profile

« Previous | Next » 

Revision 84

Added support for mapping datasource metadata

View differences:

map
12 12

  
13 13
import opts
14 14

  
15
def metadata_value(name):
16
    if name.startswith(':'): return name[1:]
17
    else: return None
18

  
15 19
def main():
16 20
    # Get db config from env vars
17 21
    db_config_names = ['host', 'user', 'password', 'database']
......
55 59
        for row in reader:
56 60
            in_, out = row[:2]
57 61
            if out != '':
58
                try: out = xpath.parse(dest_root+out)
62
                try: mappings.append((in_, xpath.parse(dest_root+out)))
59 63
                except SyntaxException, ex: raise SystemExit(str(ex))
60
                mappings.append((in_, out))
61 64
        stream.close()
62 65
    
63 66
    # Input datasource to XML tree, mapping if needed
......
68 71
        if in_is_xml: raise Exception('XML-XML mapping not supported yet')
69 72
        elif in_is_db: raise Exception('DB-XML mapping not supported yet')
70 73
        else: # input is CSV
71
            map_ = dict(mappings)
74
            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
            
72 81
            reader = csv.reader(sys.stdin)
73
            fieldnames = reader.next()
82
            cols = reader.next()
74 83
            for row_idx, row in enumerate(reader):
75 84
                row_id = str(row_idx)
76
                def put_col(name, value):
77
                    xpath.put_obj(out_doc, map_[name], row_id, has_types, value)
78
                for idx, name in enumerate(fieldnames):
79
                    if row[idx] != '' and name in map_: put_col(name, row[idx])
85
                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])
80 90
        doc = out_doc
81 91
    
82 92
    # Output XML tree

Also available in: Unified diff