3
3
import csv
4
4
import datetime
5
5
import configparser
6
- from cappy import API
6
+ from redcap import Project
7
7
from nacc .uds3 .filters import *
8
8
9
9
@@ -88,29 +88,54 @@ def read_config(config_path):
88
88
return config
89
89
90
90
91
- # Getting Data From RedCap
92
- def get_data_from_redcap (folder_name , config ):
91
+ def get_data_from_redcap_pycap (folder_name , config ):
93
92
# Enter the path for filters_config
94
93
try :
95
- token = config .get ('cappy ' , 'token' )
96
- redcap_url = config .get ('cappy ' , 'redcap_server' )
94
+ token = config .get ('pycap ' , 'token' )
95
+ redcap_url = config .get ('pycap ' , 'redcap_server' )
97
96
except Exception as e :
98
97
print ("Please check the config file and validate all the proper fields exist" , file = sys .stderr )
99
98
print (e )
100
99
raise e
101
100
102
- redcap_access_api = API (token , redcap_url , 'master.yaml' )
103
- res = redcap_access_api .export_records (adhoc_redcap_options = {
104
- 'format' : 'csv'
105
- })
101
+ redcap_project = Project (redcap_url , token )
102
+
103
+ # Get list of all fieldnames in project to create a csv header
104
+ assert hasattr (redcap_project , 'field_names' )
105
+ header_a = getattr (redcap_project , 'field_names' )
106
+
107
+ header_b = []
108
+ list_of_fields = redcap_project .export_field_names ()
109
+ for field in list_of_fields :
110
+ header_b .append (field ['export_field_name' ])
111
+
112
+ header_full = list (set (header_a + header_b ))
113
+ header_full .insert (1 , 'redcap_event_name' )
114
+
115
+ # Get list of all records present in project to iterate over
116
+ list_of_records = []
117
+ all_records = redcap_project .export_records (fields = ['ptid' ])
118
+ for record in all_records :
119
+ if record ['ptid' ] not in list_of_records :
120
+ list_of_records .append (record ['ptid' ])
121
+
122
+ chunked_records = []
123
+ # Break the list into chunks of 50
124
+ n = 50
125
+ for i in range (0 , len (list_of_records ), n ):
126
+ chunked_records .append (list_of_records [i :i + n ])
127
+
106
128
try :
107
- rawdata = str (res .text )
108
- myreader = csv .reader (rawdata .splitlines ())
129
+
109
130
try :
110
- with open (os .path .join (folder_name , "redcap_input.csv" ), "w" ) as file :
111
- writer = csv .writer (file , delimiter = ',' )
112
- for row in myreader :
113
- writer .writerow (row )
131
+ with open (os .path .join (folder_name , "redcap_input.csv" ), "w" ) as redcap_export :
132
+ writer = csv .DictWriter (redcap_export , fieldnames = header_full )
133
+ writer .writeheader ()
134
+ # header_mapping = next(reader)
135
+ for current_record_chunk in chunked_records :
136
+ data = redcap_project .export_records (records = current_record_chunk )
137
+ for row in data :
138
+ writer .writerow (row )
114
139
except Exception as e :
115
140
print ("Error in Writing" )
116
141
print (e )
@@ -136,7 +161,7 @@ def main():
136
161
config_path = sys .argv [1 ]
137
162
config = read_config (config_path )
138
163
139
- get_data_from_redcap (folder_name , config )
164
+ get_data_from_redcap_pycap (folder_name , config )
140
165
run_all_filters (folder_name , config_path )
141
166
142
167
exit ()
0 commit comments