1
|
#!/usr/bin/env python
|
2
|
# Converts a CSV dataset to XML using a map spreadsheet
|
3
|
|
4
|
import copy
|
5
|
import csv
|
6
|
import os.path
|
7
|
import re
|
8
|
import sys
|
9
|
from xml.dom.minidom import getDOMImplementation
|
10
|
|
11
|
sys.path.append(os.path.dirname(__file__)+"/lib")
|
12
|
import xpath
|
13
|
|
14
|
def main():
|
15
|
prog_name = sys.argv[0]
|
16
|
try: prog_name, map_path = sys.argv
|
17
|
except ValueError:
|
18
|
raise Exception('Usage: '+prog_name+' map_path <dataset >output')
|
19
|
|
20
|
# Get map
|
21
|
map = {}
|
22
|
has_types = False # whether outer elements are type containiners
|
23
|
stream = open(map_path, 'rb')
|
24
|
reader = csv.reader(stream, delimiter=',', quotechar='"')
|
25
|
src, dest = reader.next()[:2]
|
26
|
for row in reader:
|
27
|
name, path = row[:2]
|
28
|
if name != '' and path != '':
|
29
|
if path.startswith('/*s/'): has_types = True # *s used for type elem
|
30
|
path = path.replace('<name>', name)
|
31
|
map[name] = xpath.XpathParser(path).parse()
|
32
|
stream.close()
|
33
|
|
34
|
# Process dataset
|
35
|
doc = getDOMImplementation().createDocument(None, dest, None)
|
36
|
stream = sys.stdin
|
37
|
reader = csv.reader(stream, delimiter=',', quotechar='"')
|
38
|
fieldnames = reader.next()
|
39
|
row_idx = 0
|
40
|
for row in reader:
|
41
|
row_id = str(row_idx)
|
42
|
for idx, name in enumerate(fieldnames):
|
43
|
value = row[idx]
|
44
|
if value != '' and name in map:
|
45
|
path = copy.deepcopy(map[name]) # don't modify main value!
|
46
|
xpath.set_id(path, row_id, has_types)
|
47
|
xpath.set_value(path, value)
|
48
|
xpath.get(doc, path, True)
|
49
|
row_idx += 1
|
50
|
doc.writexml(sys.stdout, addindent=' ', newl='\n')
|
51
|
|
52
|
main()
|