annotate data_stores/vdb_data_stores.py @ 2:269d246ce6d0 draft default tip

Uploaded
author damion
date Fri, 23 Oct 2015 17:53:29 -0400
parents 5c5027485f7d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
1 import vdb_common
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
2 import vdb_retrieval # For message text
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
3
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
4 class VDBDataStore(object):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
5
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
6 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
7 Provides data store engine super-class with methods to list available versions, and to generate a version (potentially providing a link to cached version). Currently have options for git, folder, and kipper.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
8
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
9 get_data_store_gateway() method loads the appropriate data_stores/vdb_.... data store variant.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
10
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
11 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
12
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
13 def __init__(self, retrieval_obj, spec_file_id):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
14 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
15 Note that api is only needed for data store type = folder.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
16
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
17 @init self.type
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
18 @init self.base_file_name
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
19 @init self.library_label_path
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
20 @init self.data_store_path
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
21 @init self.global_retrieval_date
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
22
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
23 @sets self.library_version_path
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
24 @sets self.version_label
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
25 @sets self.version_path
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
26 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
27
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
28 self.admin_api = retrieval_obj.admin_api
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
29 self.library_id = retrieval_obj.library_id
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
30 self.library_label_path = retrieval_obj.get_library_label_path(spec_file_id)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
31 try:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
32 # Issue: Error probably gets trapped but is never reported back to Galaxy from thread via <code file="versioned_data_form.py" /> form's call?
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
33 # It appears galaxy user needs more than just "r" permission on this file, oddly?
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
34 spec = self.admin_api.libraries.show_folder(self.library_id, spec_file_id)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
35
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
36 except IOError as e:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
37 print 'Tried to fetch library folder spec file: %s. Check permissions?"' % spec_file_id
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
38 print "I/O error({0}): {1}".format(e.errno, e.strerror)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
39 sys.exit(1)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
40
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
41 self.type = retrieval_obj.test_data_store_type(spec['name'])
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
42
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
43 #Server absolute path to data_store spec file (can be Galaxy .dat file representing Galaxy library too.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
44
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
45 self.base_file_name = spec['file_name']
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
46
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
47 # In all cases a pointer file's content (data_store_path)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
48 # should point to a real server folder (that galaxy has permission to read).
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
49 # Exception to this is for pointer.folder, where content can be empty,
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
50 # in which case idea is to use library folder contents directly.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
51
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
52 with open(self.base_file_name,'r') as path_spec:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
53 self.data_store_path = path_spec.read().strip()
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
54 if len(self.data_store_path) > 0:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
55 # Let people forget to put a trailing slash on the folder path.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
56 if not self.data_store_path[-1] == '/':
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
57 self.data_store_path += '/'
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
58
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
59 # Generated on subsequent subclass call
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
60 self.library_version_path = None
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
61 self.version_label = None
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
62 self.version_path = None
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
63
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
64
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
65 def get_version_options(self, global_retrieval_date=0, version_name=None, selection=False):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
66 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
67 Provides list of available versions of a given archive. List is filtered by
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
68 optional global_retrieval_date or version id. For date filter, the version immediately
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
69 preceeding given datetime (includes same datetime) is returned.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
70 All comparisons are done by version NAME not id, because underlying db id might change.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
71
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
72 If global_retrieval_date datetime preceeds first version, no filtering is done.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
73
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
74 @param global_retrieval_date long unix time date to test entry against.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
75 @param version_name string Name of version
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
76 @param version_id string Looks like a number, or '' to pull latest id.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
77 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
78
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
79 data = []
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
80
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
81 date_match = vdb_common.date_matcher(global_retrieval_date)
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
82 found=False
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
83
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
84 for ptr, item in enumerate(self.versions):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
85 created = float(item['created'])
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
86 item_name = item['name']
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
87
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
88 # Note version_id is often "None", so must come last in conjunction.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
89 selected = (found == False) \
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
90 and ((item_name == version_name ) or date_match.next(created))
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
91
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
92 if selected == True:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
93 found = True
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
94 if selection==True:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
95 return item_name
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
96
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
97 # Folder type data stores should already have something resembling created date in name.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
98 if type(self).__name__ in 'VDBFolderDataStore VDBBiomajDataStore':
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
99 item_label = item['name']
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
100 else:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
101 item_label = vdb_common.lightDate(created) + '_' + item['name']
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
102
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
103 data.append([item_label, item['name'], selected])
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
104
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
105 if not found and len(self.versions) > 0:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
106 if global_retrieval_date: # Select oldest date version since no match above.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
107 item_name = data[-1][1]
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
108 data[-1][2] = True
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
109 else:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
110 item_name = data[0][1]
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
111 data[0][2] = True
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
112 if selection == True:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
113 return item_name
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
114
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
115
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
116 # For cosmetic display: Natural sort takes care of version keys that have mixed characters/numbers
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
117 data = sorted(data, key=lambda el: vdb_common.natural_sort_key(el[0]), reverse=True) #descending
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
118
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
119 #Always tag the first item as the most current one
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
120 if len(data) > 0:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
121 data[0][0] = data[0][0] + ' (current)'
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
122 else:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
123 data.append([vdb_retrieval.VDB_DATASET_NOT_AVAILABLE + ' Is pointer file content right? : ' + self.data_store_path,'',False])
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
124
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
125 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
126 globalFound = False
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
127 for i in range(len(data)):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
128 if data[i][2] == True:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
129 globalFound = True
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
130 break
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
131
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
132 if globalFound == False:
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
133 data[0][2] = True # And select it if no other date has been selcted
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
134
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
135 """
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
136
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
137 return data
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
138
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
139
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
140 def get_version(self, version_name):
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
141 # All subclasses must define this.
5c5027485f7d Uploaded correct file
damion
parents:
diff changeset
142 pass