annotate dynamic_utils.py @ 0:c74a1c7121ec draft default tip

planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
author geco-team
date Tue, 26 Jun 2018 08:59:49 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
1 #!/usr/bin/env python
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
2 # --------------------------------------------------------------------------------
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
3 # Class for the dynamic options in the GMQL tools
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
4 # --------------------------------------------------------------------------------
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
5 # Luana Brancato, luana.brancato@mail.polimi.it
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
6 # --------------------------------------------------------------------------------
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
7
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
8 import sys, requests
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
9
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
10
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
11 def validate(request_context, error_map, params, inputs):
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
12 """Generic validate function, it checks if the user is valid."""
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
13
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
14 user = params.get('authToken', '')
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
15
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
16 if user:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
17 try:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
18 validate_user(user.file_name)
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
19 except:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
20 error_msg = 'User has expired'
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
21 error_map['authToken'] = error_msg
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
22
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
23
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
24 def validate_upload(request_context, error_map, params, inputs):
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
25 """Validate function for uploading tool. It also checks the chosen ds name does not exists already."""
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
26
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
27 validate(request_context, error_map, params, inputs)
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
28
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
29 name = params.get('name')
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
30
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
31 user = params.get('authToken')
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
32
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
33 #This MUST be changed in the future to a parametric solution. Hopefully in the future Galaxy will allow
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
34 #validation without external scripts
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
35
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
36 url = 'http://genomic.elet.polimi.it/gmql-rest/datasets'
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
37
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
38 datasets = get(url, user=user.file_name)
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
39 list_datasets = [x['name'] for x in datasets['datasets']]
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
40
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
41 if name in list_datasets:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
42 error_msg = 'Dataset already exists. Choose another name.'
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
43 error_map['name'] = error_msg
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
44
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
45
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
46 def validate_variables(request_context, error_map, params, inputs):
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
47 """Validate function for gmql_compositor. It checks that all queries input variables
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
48 have been previously defined. """
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
49
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
50 validate(request_context, error_map, params, inputs)
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
51
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
52 output_vars = set([])
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
53
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
54 # TODO: Include in the check output variables eventually defined previously in another query
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
55
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
56 for op in params.get('operations'):
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
57 op_curr = op.get('operation')
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
58 if op_curr.get('input', ''):
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
59 input_var = op_curr.get('input').get('input_var', '')
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
60 if input_var:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
61 if input_var not in output_vars:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
62 error_msg = '%s has not been defined yet\n' % (input_var)
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
63 name = '|'.join(['operations_%d' % (op.get('__index__')), 'operation', 'input', 'input_var'])
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
64 error_map[name] = error_msg
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
65 else:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
66 for key in op_curr.keys():
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
67 if key.startswith('input_var'):
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
68 input_var = op_curr.get(key)
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
69 if input_var:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
70 if input_var not in output_vars:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
71 error_msg = '%s has not been defined yet\n' % (input_var)
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
72 name = '|'.join(['operations_%d' % (op.get('__index__')), 'operation', key])
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
73 error_map[name] = error_msg
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
74
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
75 # Update output_vars with the result of current operation
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
76 output_vars.add(op_curr.get('output_var'))
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
77
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
78
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
79 def validate_user(user):
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
80 """Check if the user is a valid one"""
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
81
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
82 if user:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
83 with open(user, 'r') as f:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
84 valid = f.readline().rstrip('\n').split('\t')[2]
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
85 if valid == 'False':
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
86 raise Exception, "User has expired"
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
87
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
88
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
89 def get_metadata_attr(user, ds, ds_list):
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
90 options = []
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
91
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
92 try:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
93 validate_user(user)
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
94 if ds_list:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
95
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
96 owner = ''
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
97
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
98 with open(ds_list, 'r') as f:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
99 for d in f.readlines():
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
100 if d.split('\t')[0] == ds:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
101 owner = d.split('\t')[1].rstrip('\n')
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
102 f.close()
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
103
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
104 attr_list = get_metadata(user, ds, str(owner))
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
105
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
106 for i, att in enumerate(attr_list['attributes']):
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
107 options.append((att.get('key', ' '), att.get('key', ' '), i == 0))
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
108
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
109 return options
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
110
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
111 else:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
112 return options
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
113 except:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
114 return options
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
115
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
116
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
117 def get_metadata_values(user, ds, ds_list, att):
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
118 options = []
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
119
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
120 try:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
121 validate_user(user)
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
122 if ds_list:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
123
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
124 owner = ''
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
125
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
126 with open(ds_list, 'r') as f:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
127 for d in f.readlines():
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
128 if d.split('\t')[0] == ds:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
129 owner = d.split('\t')[1].rstrip('\n')
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
130 f.close()
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
131
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
132 attr_list = get_metadata(user, ds, str(owner), att)
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
133
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
134 # By default first option is '*' i.e. any value
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
135 options.append(('any value', '*', 0))
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
136
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
137 for i, att in enumerate(attr_list['values']):
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
138 options.append(('%s (%d)' % (att.get('text', ' '), att.get('count', 0)), att.get('text', ' '), i == 0))
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
139
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
140 return options
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
141
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
142 else:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
143 return options
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
144 except:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
145 return options
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
146
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
147
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
148 def get_metadata(user, ds, owner='', att_name=''):
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
149 """Return the metadata attributes names for the given dataset"""
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
150
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
151 url = 'http://genomic.elet.polimi.it/gmql-rest/metadata/{datasetName}/filter'
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
152
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
153 # Format url
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
154 if (owner == 'public'):
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
155 url = url.format(datasetName='public.' + ds)
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
156 else:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
157 url = url.format(datasetName=ds)
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
158
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
159 if att_name:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
160 url = '{url}/{att}'.format(url=url, att=att_name)
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
161
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
162 content = dict()
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
163 content.update(attributes=[])
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
164
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
165 metadata = post(url, content, user=user)
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
166
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
167 return metadata
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
168
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
169
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
170 def read_token(input):
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
171 """It takes the tabular file with the information over the user
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
172 name authToken valid_flag
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
173 It checks if the user is still valid and extract the authToken for the REST calls"""
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
174
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
175 with open(input, 'r') as f_in:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
176 user = f_in.readline().rstrip('\n').split('\t')
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
177
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
178 if user[2]:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
179 token = user[1]
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
180 else:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
181 stop_err("This session is no longer valid")
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
182
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
183 return token
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
184
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
185
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
186 def get(url, user=None, response_type='json'):
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
187 """GET Request
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
188 :param url: url where to fetch the requested resource
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
189 :param user: for authenticated requests; if not provided make an unauthenticated request (es. for login)
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
190 :param response_type: type of the fetched response.
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
191 JSON ( Default )
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
192 TEXT
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
193 ZIP
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
194 FILE
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
195 """
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
196
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
197 # Set request headers
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
198 headers = dict()
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
199
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
200 if user:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
201 headers.update({'X-AUTH-TOKEN': read_token(user)})
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
202
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
203 if response_type == 'text':
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
204 headers.update({'Accept': 'text/plain'})
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
205 elif response_type == 'zip':
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
206 pass
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
207 elif response_type == 'file':
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
208 headers.update({'Accept': 'file'})
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
209 else:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
210 headers.update({'Accept': 'application/json'})
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
211
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
212 # Make the request
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
213 response = requests.get(url, headers=headers)
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
214
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
215 # Check returned server status
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
216 status_code = response.status_code
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
217
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
218 # Read result. If Server OK, read according to response_type. Raise an error otherwise.
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
219 if status_code == requests.codes.ok:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
220 if response_type == 'json':
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
221 return response.json()
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
222 elif response_type == 'text':
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
223 return response.text
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
224 else:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
225 return response
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
226 elif status_code == requests.codes.unauthorized:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
227 #expire_user(user)
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
228 stop_err("You are not authorized to do this. \nPlease login first.")
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
229 elif status_code == requests.codes.not_found:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
230 stop_err("Resource not found for this user.")
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
231 else:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
232 stop_err("Error {code}: {reason}\n{message}".format(code=status_code,
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
233 reason=response.reason,
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
234 message=response.content))
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
235
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
236 def post(url, payload, user=None, params=None, content_type='json', response_type='json') :
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
237 """ POST Request
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
238 :param url: url where to post data
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
239 :param payload: payload for the post request. Type is specified by content_type.
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
240 :param user: for authenticated requests; if not provided make an unauthenticated request (es. for registration)
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
241 :param params: optional query parameters
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
242 :param content_type
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
243 :param response_type: Default is json
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
244 """
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
245
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
246
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
247 # Set request headers
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
248 headers = dict()
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
249
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
250 if user:
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
251 headers.update({'X-AUTH-TOKEN': read_token(user)})
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
252
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
253 headers.update({'Accept': 'application/json'})
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
254
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
255 if content_type == 'text' :
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
256 headers.update({'Content-Type' : 'text/plain'})
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
257 response = requests.post(url, params=params, headers=headers, data=payload)
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
258 elif content_type == 'multiform' :
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
259 response = requests.post(url, params=params, headers=headers, files=payload)
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
260 else :
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
261 headers.update({'Content-Type': 'application/json'})
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
262 response = requests.post(url, params=params, headers=headers, json=payload)
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
263
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
264 # Check returned server status
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
265 status_code = response.status_code
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
266
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
267
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
268 if status_code == requests.codes.ok :
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
269 return response.json()
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
270 elif status_code == requests.codes.unauthorized :
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
271 #expire_user(user)
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
272 stop_err("You are not authorized to do this. \nPlease login first.")
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
273 else :
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
274 stop_err("Error {code}: {reason}\n{message}".format(code=status_code,
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
275 reason=response.reason,
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
276 message=response.content))
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
277
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
278
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
279 def stop_err(msg):
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
280 sys.stderr.write("%s\n" % msg)
c74a1c7121ec planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
geco-team
parents:
diff changeset
281 sys.exit()