redi.utils package


redi.utils.GetEmrData module

This module is used to connect to an sftp server and retrieve the raw EMR file to be used as input for RED-I.

class redi.utils.GetEmrData.EmrFileAccessDetails(emr_sftp_project_name, emr_download_list, emr_host, emr_username, emr_password, emr_port, emr_private_key, emr_private_key_pass)[source]

Bases: object

Encapsulate the settings used to retrieve the EMR source file using an SFTP connection @see redi#_run()

redi.utils.GetEmrData.data_preprocessing(input_filename, output_filename)[source]
redi.utils.GetEmrData.download_files(destination, access_details)[source]

Download a file from the sftp server :destination the name of the file which will be downloaded :access_details holds info for accessing the source file over sftp

@see get_emr_data()

redi.utils.GetEmrData.generate_xml(input_filename, output_filename)[source]
redi.utils.GetEmrData.get_emr_data(conf_dir, connection_details)[source]

:conf_dir configuration directory name :connection_details EmrFileAccessDetails object

redi.utils.SimpleConfigParser module


Simple configuration file parser: Python module to parse configuration files without sections. Based on ConfigParser from the standard library.

Author: Philippe Lagadec

Project website:

Inspired from an idea posted by Fredrik Lundh:

Usage: see end of source code and

exception redi.utils.SimpleConfigParser.ConfigurationError[source]

Bases: exceptions.Exception

class redi.utils.SimpleConfigParser.SimpleConfigParser(defaults=None, dict_type=<class 'collections.OrderedDict'>, allow_no_value=False)[source]

Bases: ConfigParser.RawConfigParser

Simple configuration file parser: based on ConfigParser from the standard library, slightly modified to parse configuration files without sections.

Inspired from an idea posted by Fredrik Lundh:


handle required and default optional_parameters_dict


get the value of an option


get a list of available options


return True if an option is available, False otherwise. (NOTE: do not confuse with the original has_option)


Helper function for translating strings into booleans @see test/

redi.utils.csv2xml module

class redi.utils.csv2xml.Writer(ofile, args)[source]
write_field(field, index)[source]
redi.utils.csv2xml.cleanup_callback(option, opt, value, parser)[source]
redi.utils.csv2xml.openio(filename, mode, encoding, newline=None)[source]
redi.utils.csv2xml.replace(text, s, r)[source]

redi.utils.rawxml module

class redi.utils.rawxml.RawXml(project, path)[source]

Bases: object

This class is used to store details about the input file @see redi.batch.check_input_file()


Get the OS creation time


Return a string containing all details available about the xml file


Get the OS modification time


redi.utils.redcapClient module

class redi.utils.redcapClient.RedcapClient(redcap_uri, token, verify_ssl=True)[source]

Bases: object

Client for a REDCap server.

  • redcap_uri – URI for to REDCap server’s API
  • token – API Token for a REDCap project.
  • verify_ssl – verify the SSL certificate? (default: True)
  • RedcapError – if we failed to get the project’s metadata
  • RequestException – if some other network-related failure occurs
get_data_from_redcap(records_to_fetch=None, events_to_fetch=None, fields_to_fetch=None, forms_to_fetch=None, return_format='xml')[source]

Exports REDCap records.

  • records_to_fetch (list or None) – if specified, only includes records in this list. Otherwise, includes all records.
  • events_to_fetch (list or None) – if specified, only includes events in this list. Otherwise, includes all events.
  • fields_to_fetch (list or None) – if specified, only includes fields in this list. Otherwise, includes all fields
  • forms_to_fetch (list or None) – if specified, only includes forms in this list. Otherwise, includes all forms.
  • return_format – specifies the format of the REDCap response (default: xml)


send_data_to_redcap(data, max_retry_count, overwrite=False, retry_count=0)[source]

Sends records to REDCap.

  • of dict objects data (list) – records to send.
  • overwrite (bool) – treat blank values as intentional? (default: False) When sending a record, if a field is blank, by default REDCap will not overwrite any existing value with a blank.



RedcapError – if failed to send records for any reason.

:If MaxRetryError is caught, the function will try resending the same
data for a maximum of max_retry_count times before exitting. For each attempt the wait time before sending is (the_attempt_no * 6)

redi.utils.redi_email module

redi.utils.redi_email.add_attachment(msg, body)[source]

Add the html report as attachment

msg : MIMEMultipart
The object to which we attach the body content
body : string
The html content to be attached
redi.utils.redi_email.send_email(host, port, sender, to_addr_list, cc_addr_list, subject, msg_body)[source]

The email deliverer. Return True if the email was sent

to_addr_list : list
The recipients of the email
redi.utils.redi_email.send_email_data_import_completed(email_settings, body='')[source]

Email the html report after redi completed the data transfer Returns a dictionary, with one entry for each recipient that was refused

email_settings : dict
Email params produced by redi.get_email_settings()
body : string
The html content produced by transforming the xsl generated by redi.create_summary_report()
redi.utils.redi_email.send_email_input_data_unchanged(email_settings, raw_xml)[source]

Send a warning email to the redcap_support_receiver_email if the input file did not change for more than batch_warning_days Return True if the email was sent

email_settings : dictionary
The email delivery parameters
raw_xml : RawXml instance
The object storing details about the input file
redi.utils.redi_email.send_email_redcap_connection_error(email_settings, subject='', msg='')[source]

Return True if the email was sent. Notify the designated REDCap support person about problems with reaching REDCap

email_settings : dict
The dictionary with smtp server parameters
subject : str
The email subject
msg : str
The content to be emailed

redi.utils.throttle module

Utility module for throttling calls to a function

class redi.utils.throttle.Throttle(function, max_calls, interval_in_seconds=60)[source]

Bases: object

Limits the number of calls to a function to a given rate.

The rate limit is equal to the max_calls over the interval_in_seconds.

  • function – function to call after throttling
  • max_calls – maximum number of calls allowed
  • interval_in_seconds – size of the sliding window

Module contents