Mercurial > repos > damion > versioned_data
comparison versioned_data_form.py @ 1:5c5027485f7d draft
Uploaded correct file
author | damion |
---|---|
date | Sun, 09 Aug 2015 16:07:50 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:d31a1bd74e63 | 1:5c5027485f7d |
---|---|
1 #!/usr/bin/python | |
2 # | |
3 import os | |
4 import sys | |
5 | |
6 # Extra step enables this script to locate vdb_common and vdb_retrieval | |
7 # From http://code.activestate.com/recipes/66062-determining-current-function-name/ | |
8 _self_dir = os.path.dirname(sys._getframe().f_code.co_filename) | |
9 sys.path.append(_self_dir) | |
10 | |
11 import vdb_common | |
12 import vdb_retrieval | |
13 | |
14 retrieval_obj = None # Global used here to manage user's currently selected retrieval info. | |
15 | |
16 | |
17 def vdb_init_tool_user(trans): | |
18 """ | |
19 Retrieves a user's api key if they have one, otherwise lets them know they need one | |
20 This function is automatically called from versioned_data.xml form on presentation to user | |
21 Note that this is how self.api_url gets back into form, for passage back to 2nd call via versioned_data.py | |
22 self.api_key is passed via secure <configfile> construct. | |
23 ALSO: squeezing history_id in this way since no other way to pass it. | |
24 "trans" is provided only by tool form presentation via <code file="..."> | |
25 BUT NOW SEE John Chilton's: https://gist.github.com/jmchilton/27c5bb05e155a611294d | |
26 See galaxy source code at https://galaxy-dist.readthedocs.org/en/latest/_modules/galaxy/web/framework.html, | |
27 See http://dev.list.galaxyproject.org/error-using-get-user-id-in-xml-file-in-new-Galaxy-td4665274.html | |
28 See http://dev.list.galaxyproject.org/hg-galaxy-2780-Real-Job-tm-support-for-the-library-upload-to-td4133384.html | |
29 master api key, set in galaxy config: #self.master_api_key = trans.app.config.master_api_key | |
30 """ | |
31 global retrieval_obj | |
32 | |
33 api_url = trans.request.application_url + '/api' | |
34 history_id = str(trans.security.encode_id(trans.history.id)) | |
35 user_api_key = None | |
36 #master_api_key = trans.app.config.master_api_key | |
37 | |
38 if trans.user: | |
39 | |
40 user_name = trans.user.username | |
41 | |
42 if trans.user.api_keys and len(trans.user.api_keys) > 0: | |
43 user_api_key = trans.user.api_keys[0].key #First key is always the active one? | |
44 items = [ { 'name': user_name, 'value': api_url + '-' + history_id, 'options':[], 'selected': True } ] | |
45 | |
46 else: | |
47 items = [ { 'name': user_name + ' - Note: you need a key (see "User" menu)!', 'value': '0', 'options':[], 'selected': False } ] | |
48 | |
49 else: | |
50 items = [ { 'name': 'You need to be logged in to use this tool!', 'value': '1', 'options':[], 'selected': False } ] | |
51 | |
52 retrieval_obj = vdb_retrieval.VDBRetrieval() | |
53 retrieval_obj.set_trans(api_url, history_id, user_api_key) #, master_api_key | |
54 | |
55 return items | |
56 | |
57 | |
58 def vdb_get_databases(): | |
59 """ | |
60 Called by Tool Form, retrieves list of versioned databases from galaxy library called "Versioned Data" | |
61 | |
62 @return [name,value,selected] array | |
63 """ | |
64 global retrieval_obj | |
65 items = retrieval_obj.get_library_data_store_list() | |
66 | |
67 if len(items) == 0: | |
68 # Not great: Communicating library problem by text in form select pulldown input. | |
69 items = [[vdb_retrieval.VDB_DATA_LIBRARY_FOLDER_ERROR, None, False]] | |
70 | |
71 return items | |
72 | |
73 | |
74 def vdb_get_versions(spec_file_id, global_retrieval_date): | |
75 """ | |
76 Retrieve applicable versions of given database. | |
77 Unfortunately this is only refreshed when form screen is refreshed | |
78 | |
79 @param dbKey [folder_id] | |
80 | |
81 @return [name,value,selected] array | |
82 """ | |
83 global retrieval_obj | |
84 | |
85 items = [] | |
86 if spec_file_id: | |
87 | |
88 data_store_spec = retrieval_obj.user_api.libraries.show_folder(retrieval_obj.library_id, spec_file_id) | |
89 | |
90 if data_store_spec: #OTHERWISE DOES THIS MEAN USER DOESN'T HAVE PERMISSIONS? VALIDATE | |
91 | |
92 file_name = data_store_spec['name'] # Short (no path), original file name, not galaxy-assigned file_name | |
93 data_store_type = retrieval_obj.test_data_store_type(file_name) | |
94 library_label_path = retrieval_obj.get_library_label_path(spec_file_id) | |
95 | |
96 if not data_store_type or not library_label_path: | |
97 # Cludgy method of sending message to user | |
98 items.append([vdb_retrieval.VDB_DATA_LIBRARY_FOLDER_ERROR, None, False]) | |
99 items.append([vdb_retrieval.VDB_DATA_LIBRARY_CONFIG_ERROR + '"' + vdb_retrieval.VDB_DATA_LIBRARY + '/' + str(library_label_path) + '/' + file_name + '"', None, False]) | |
100 return items | |
101 | |
102 _retrieval_date = vdb_common.parse_date(global_retrieval_date) | |
103 | |
104 # Loads interface for appropriate data source retrieval | |
105 ds_obj = retrieval_obj.get_data_store_gateway(data_store_type, spec_file_id) | |
106 items = ds_obj.get_version_options(global_retrieval_date=_retrieval_date) | |
107 | |
108 else: | |
109 items.append(['Unable to find' + spec_file_id + ':Check permissions?','',False]) | |
110 | |
111 return items | |
112 | |
113 | |
114 def vdb_get_workflows(dbKey): | |
115 """ | |
116 List appropriate workflows for database. These are indicated by prefix "Versioning:" in name. | |
117 Currently can see ALL workflows that are published; admin_api() receives this in all galaxy versions. | |
118 Seems like only some galaxy versions allow user_api() to also see published workflows. | |
119 Only alternative is to list only individual workflows that current user can see - ones they created, and published workflows; but versioneddata user needs to have permissions on these too. | |
120 | |
121 Future: Sensitivity: Some workflows apply only to some kinds of database | |
122 | |
123 @param dbKey [data_spec_id] | |
124 @return [name,value,selected] array | |
125 """ | |
126 global retrieval_obj | |
127 | |
128 data = [] | |
129 try: | |
130 workflows = retrieval_obj.admin_api.workflows.get_workflows(published=True) | |
131 | |
132 except Exception as err: | |
133 if err.message[-21:] == 'HTTP status code: 403': | |
134 data.append(['Error: User does not have permissions to see workflows (or they need to be published).' , 0, False]) | |
135 else: | |
136 data.append(['Error: In getting workflows: %s' % err.message , 0, False]) | |
137 return data | |
138 | |
139 oldName = "" | |
140 for workflow in workflows: | |
141 name = workflow['name'] | |
142 if name[0:11].lower() == "versioning:" and name != oldName: | |
143 # Interface Bug: If an item is published and is also shared personally with a user, it is shown twice. | |
144 data.append([name, workflow['id'], False]) | |
145 oldName = name | |
146 | |
147 return data | |
148 | |
149 |