-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadDbColumns.py
More file actions
65 lines (49 loc) · 1.33 KB
/
readDbColumns.py
File metadata and controls
65 lines (49 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
"""
Created on Tue June 17 2014
@author: jsmith
"""
import csv
def readDbCols(vals):
name = str(vals)
table = {}
table['name'] = []
table['type'] = []
table['nullable'] = []
table['default'] = []
table['description'] = []
table['#'] = []
table['drop'] = []
stanza = '''
<var name="{0}" precision="1" scale="None" type="{1}">
<label>{0}</label>
<description>{3}</description>
<definition><![CDATA[@{2}({0})]]></definition>
</var>'''
dictReader = csv.DictReader(open(vals, 'rb'), fieldnames=['name', 'type', 'nullable', 'default', 'description', '#', 'drop'], delimiter=',', quotechar='"')
writeDict(dr=dictReader,d=table)
writeStanzas(d=table,s=stanza,n=name)
def writeDict(dr,d):
for i in dr:
for j in i:
if j == 'type':
if i[j] == 'text':
d[j].append('c')
elif i[j] == 'boolean':
d[j].append('l')
elif i[j] == 'double precision':
d[j].append('d')
elif i[j] == 'integer':
d[j].append('i')
else:
d[j].append('NA')
else:
d[j].append(i[j])
return d
def writeStanzas(d,s,n):
f = open("vars.xml","w")
for i in range(0,len(d['name'])-1):
print i,s.format(d['name'][i],d['type'][i],n,d['description'][i])
f.write(str(s.format(d['name'][i],d['type'][i],n,d['description'][i]) + '\n'))
f.close()
readDbCols(raw_input("Select a db columns file: "))