PageRenderTime 42ms CodeModel.GetById 31ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/scripts/api/sequencer_configuration_create.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 45 lines | 36 code | 3 blank | 6 comment | 7 complexity | 940f37ffbf02ac7b5ff3787732ffac1e MD5 | raw file
 1#!/usr/bin/env python
 2import os, sys#, traceback
 3sys.path.insert( 0, os.path.dirname( __file__ ) )
 4from common import submit, get
 5
 6def create_sequencer_configuration( key, base_url, request_form_filename, sample_form_filename, request_type_filename, email_addresses, return_formatted=True ):
 7    #create request_form
 8    data = {}
 9    data[ 'xml_text' ] = open( request_form_filename ).read()
10    request_form = submit( key, "%sforms" % base_url, data, return_formatted=False )[0]
11    #create sample_form
12    data = {}
13    data[ 'xml_text' ] = open( sample_form_filename ).read()
14    sample_form = submit( key, "%sforms" % base_url, data, return_formatted=False )[0]
15    #get user ids
16    user_ids = [ user['id'] for user in get( key, "%susers" % base_url ) if user['email'] in email_addresses ]
17    #create role, assign to user
18    data = {}
19    data[ 'name' ] = "request_type_role_%s_%s_%s name" % ( request_form['id'], sample_form['id'], '_'.join( email_addresses ) )
20    data[ 'description' ] = "request_type_role_%s_%s_%s description" % ( request_form['id'], sample_form['id'], '_'.join( email_addresses ) )
21    data[ 'user_ids' ] = user_ids
22    role_ids = [ role[ 'id' ] for role in submit( key, "%sroles" % base_url, data, return_formatted=False ) ]
23    #create request_type
24    data = {}
25    data[ 'request_form_id' ] = request_form[ 'id' ]
26    data[ 'sample_form_id' ] = sample_form[ 'id' ]
27    data[ 'role_ids' ] = role_ids
28    data[ 'xml_text' ] = open( request_type_filename ).read()
29    return submit( key, "%srequest_types" % base_url, data, return_formatted=return_formatted ) #create and print out results for request type
30
31def main():
32    try:
33        key = sys.argv[1]
34        base_url = sys.argv[2]
35        request_form_filename = sys.argv[3]
36        sample_form_filename = sys.argv[4]
37        request_type_filename = sys.argv[5]
38        email_addresses = sys.argv[6].split( ',' )
39    except IndexError:
40        print 'usage: %s key base_url request_form_xml_description_file sample_form_xml_description_file request_type_xml_description_file email_address1[,email_address2]' % os.path.basename( sys.argv[0] )
41        sys.exit( 1 )
42    return create_sequencer_configuration( key, base_url, request_form_filename, sample_form_filename, request_type_filename, email_addresses, return_formatted=True )
43
44if __name__ == "__main__":
45    main()