Skip to content

Commit 2c5e209

Browse files
committed
Modified export script to work with csv only
1 parent c4345c9 commit 2c5e209

File tree

3 files changed

+33
-44
lines changed

3 files changed

+33
-44
lines changed

README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ casexml
22
=======
33
An application for generating and pushing CaseXML to commcareHQ
44

5-
This applications both works for CSV document or Dictionary and it auto fill data to casexml template.
5+
This applications both works for CSV document and it auto fill data to casexml template.
66
This template is then pushed to commcare or saved in the output folder.
77

88
How it works:
@@ -13,11 +13,10 @@ How it works:
1313

1414
# CSV heading should match the template tags
1515

16-
# To run: $ python casexml.py format filename_location template_name
16+
# To run: $ python casexml.py filename_location template_name
1717

1818
Variables
1919
==========
20-
format: This is format of data you want to read. It can be either csv, or dict for dictionary
2120

2221
filename_location: This is the exact file_name. Use absolute location of tttthe file and dont forget to add file extension.
2322

@@ -27,5 +26,5 @@ example:
2726
========
2827
To generate casexml for data in Patient.csv and the casexml template is patient.xml use this command.
2928

30-
$python casexml.py csv Patient.csv template/patient.xml
29+
$python casexml.py Patient.csv template/patient.xml
3130

casexml.py

+19-30
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#!/usr/bin/env python
2+
# vim: ai ts=4 sts=4 et sw=4 encoding=utf-8
3+
14
import os
25
import sys
36
import csv
@@ -20,55 +23,41 @@ class Main:
2023
save = True
2124
template = ""
2225
submit = False
23-
26+
2427
def __init__(self):
2528
''' Get arguments '''
2629
args = sys.argv
2730
if len(args):
2831
try:
2932
self.format = args[1].lower()
3033
self.file_name = args[2]
31-
self.save = args[4]
3234
self.template = args[3]
33-
self.submit = args[5]
3435
except:
3536
self.format = ""
3637
self.file_name = ""
37-
self.save = True
3838
self.template = ""
39-
self.submit = False
4039

41-
if self.format not in ('csv','dict'):
40+
if self.format not in ('csv'):
4241
self.help()
4342
else:
44-
if self.format =='csv':
45-
if not check_file(self.file_name, "csv"):
46-
print "Invalid file location or file does not exist." \
47-
"Check if it has a .csv extenion"
48-
else:
49-
self.export_csv()
50-
if self.format =='dict':
51-
print "Working on data dictionary"
43+
if not check_file(self.file_name, "csv"):
44+
print "Invalid file location or file does not exist." \
45+
"Check if it has a .csv extenion"
46+
else:
47+
self.export_csv()
5248
else:
5349
self.help()
5450

55-
5651
def help(self):
5752
print (u"------------------------------------------\n"
5853
"GENERATE CASEXML USING DATA AND TEMPLATE \n"
5954
"------------------------------------------\n"
6055
"Contains function to generate Casexml and save or \n"
6156
"submit to CommcareHq\n"
62-
"To run:\n $ python casexml.py datasource_type filename template "
63-
"save=True/False submit=True/False\n\nOPTIONS:\n-------- \n"
64-
"datasource_type Type of data Source. can be either csv, "
65-
"dictionary(data) \n"
66-
"filename Fullname of the file holding data., "
67-
" Incase of dictionary, pass the dictionary \n"
68-
"template Template Name, including .xml extension\n"
69-
"save If to save xml output or not. Forms "
70-
"are stored in output directory")
71-
57+
"To run:\n $ python casexml.py csvfile template "
58+
"\n\nOPTIONS:\n-------- \n"
59+
"csvfile A full path of csv file to be exported to"
60+
" CommcareHQ ")
7261

7362
def export_csv(self):
7463
info = {}
@@ -77,22 +66,22 @@ def export_csv(self):
7766
print "Template doesnot exist, or invalid template. Check if " \
7867
"it has a .xml extenion"
7968
return 0
80-
69+
8170
try:
8271
data = csv.reader(open(self.file_name, 'rb'))
8372
except:
8473
print "Data doesnt exist"
85-
86-
#Remove Header
74+
75+
#Remove Header
8776
header = data.next()
8877
info = {}
8978
#Loop through each row and get data
9079
for x in data:
9180
for label, value in zip(header, x):
9281
info[label] = value
93-
82+
9483
form = CaseXMLInterface(info, self.template)
95-
#save_casexmlform(self,form)
84+
save_casexmlform(form)
9685
transmit_form(form)
9786

9887

utils.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,30 @@
88
from datetime import datetime
99
import const as CONST
1010

11+
1112
def transmit_form(form):
1213
''' Submit data to commcare server '''
13-
14+
1415
xml_form = form.render()
1516
headers = {"Content-type": "text/xml", "Accept": "text/plain"}
16-
17+
1718
try:
1819
DOMAIN_URL = CONST.COMMCARE_URL
1920
SUBMIT_CASEXML = CONST.SUBMIT_TO_COMMCARE
2021
except:
2122
SUBMIT_CASEXML = False
2223
DOMAIN_URL = u""
23-
24+
2425
if SUBMIT_CASEXML:
2526

2627
print DOMAIN_URL
27-
28+
2829
url = DOMAIN_URL
2930
up = urlparse(url)
3031
conn = httplib.HTTPSConnection(up.netloc) if url.startswith("https") \
31-
else httplib.HTTPConnection(up.netloc)
32+
else httplib.HTTPConnection(up.netloc)
3233
conn.request('POST', up.path, xml_form, headers)
33-
resp = conn.getresponse()
34+
resp = conn.getresponse()
3435
responsetext = resp.read()
3536
assert resp.status == 201, 'Bad HTTP Response'
3637
assert "Thanks for submitting" in responsetext, "Bad response text"
@@ -45,12 +46,13 @@ def save_casexmlform(form):
4546
except:
4647
STORE_FORM = False
4748
STORE_LOCATION = u"/tmp"
48-
49+
4950
if STORE_FORM:
5051
print "Saving Form in output folder"
5152
f = open("%(path)s/casexml_%(date)s.xml" % {\
52-
'path': STORE_LOCATION, \
53-
'date': datetime.now().strftime("%Y_%m_%d_%H:%M:%S")}, 'wb', buffering=1)
53+
'path': STORE_LOCATION, \
54+
'date': datetime.now().strftime("%Y_%m_%d_%H:%M:%S")}, \
55+
'wb', buffering=1)
5456

5557
f.write(str(xml_form))
5658
f.close()
@@ -64,4 +66,3 @@ def check_file(file_name, ftype):
6466
exist = False
6567

6668
return exist
67-

0 commit comments

Comments
 (0)