1+ #!/usr/bin/env python
2+ # vim: ai ts=4 sts=4 et sw=4 encoding=utf-8
3+
14import os
25import sys
36import 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 \n OPTIONS:\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 \n OPTIONS:\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
0 commit comments