-
Notifications
You must be signed in to change notification settings - Fork 1
/
casexml.py
83 lines (66 loc) · 2.22 KB
/
casexml.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4 encoding=utf-8
import os
import sys
import csv
from django.conf import settings
settings.configure()
from utils import transmit_form, save_casexmlform, check_file
import const as CONST
#Form Templates
from forms import CaseXMLInterface
class Main:
file_name = ""
save = True
template = ""
submit = False
def __init__(self):
''' Get arguments '''
args = sys.argv
if len(args):
try:
self.file_name = args[1]
self.template = args[2]
except:
self.file_name = ""
self.template = ""
if not check_file(self.file_name, "csv"):
print "Invalid file location or file does not exist." \
"Check if it has a .csv extenion"
else:
self.export_csv()
else:
self.help()
def help(self):
print (u"------------------------------------------\n"
"GENERATE CASEXML USING DATA AND TEMPLATE \n"
"------------------------------------------\n"
"Contains function to generate Casexml and save or \n"
"submit to CommcareHq\n"
"To run:\n $ python casexml.py csvfile template "
"\n\nOPTIONS:\n-------- \n"
"csvfile A full path of csv file to be exported to"
" CommcareHQ ")
def export_csv(self):
info = {}
#Check if template exist before
if not check_file(self.template, "xml"):
print "Template doesnot exist, or invalid template. Check if " \
"it has a .xml extenion"
return 0
try:
data = csv.reader(open(self.file_name, 'rb'))
except:
print "Data doesnt exist"
#Remove Header
header = data.next()
info = {}
#Loop through each row and get data
for x in data:
for label, value in zip(header, x):
info[label] = value
form = CaseXMLInterface(info, self.template)
save_casexmlform(form)
#transmit_form(form)
if __name__ == '__main__':
main = Main()