-
Notifications
You must be signed in to change notification settings - Fork 11
/
ISPyBRestClientMockup.py
283 lines (223 loc) · 8.97 KB
/
ISPyBRestClientMockup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
"""
A client for ISPyB Webservices.
"""
import logging
from datetime import datetime
from urlparse import urljoin
from HardwareRepository.BaseHardwareObjects import HardwareObject
_CONNECTION_ERROR_MSG = "Could not connect to ISPyB, please verify that " + \
"the server is running and that your " + \
"configuration is correct"
_NO_TOKEN_MSG = "Could not connect to ISPyB, no valid REST token available."
class ISPyBRestClientMockup(HardwareObject):
"""
RESTful Web-service client for EXI.
"""
def __init__(self, name):
HardwareObject.__init__(self, name)
self.__disabled = False
self.__test_proposal = None
self.__translations = {}
self.__rest_root = None
self.__rest_username = None
self.__rest_token = None
self.__rest_token_timestamp = None
self.__test_proposal = {'status': {'code': 'ok'},
'Person': {'personId': 1,
'laboratoryId': 1,
'login': None,
'familyName':'operator on IDTESTeh1'},
'Proposal': {'code': 'idtest',
'title': 'operator on IDTESTeh1',
'personId': 1,
'number': '000',
'proposalId': 1,
'type': 'MX'},
'Session': [{'scheduled': 0,
'startDate': '2013-06-11 00:00:00',
'endDate': '2013-06-12 07:59:59',
'beamlineName': 'ID:TEST',
'timeStamp': datetime(2013, 6, 11, 9, 40, 36),
'comments': 'Session created by the BCM',
'sessionId': 34591,
'proposalId': 1, 'nbShifts': 3}],
'Laboratory': {'laboratoryId': 1,
'name': 'TEST eh1'}}
def init(self):
self.session_hwobj = self.getObjectByRole('session')
if self.session_hwobj:
self.beamline_name = self.session_hwobj.beamline_name
else:
self.beamline_name = 'ID:TEST'
logging.getLogger("requests").setLevel(logging.WARNING)
self.__rest_root = self.getProperty('restRoot').strip()
self.__rest_username = self.getProperty('restUserName').strip()
self.__rest_password = self.getProperty('restPass').strip()
self.__site = self.getProperty('site').strip()
self.__update_rest_token()
def __update_rest_token(self):
self.authenticate(self.__rest_username, self.__rest_password)
def authenticate(self, user, password):
"""
Authenticate with RESTfull services, updates the authentication token,
username and password used internally by this object.
:param str user: Username
:param str password: Password
:returns: None
"""
self.__rest_token = "#MOCKTOKEN123"
self.__rest_token_timestamp = datetime.now()
self.__rest_username = user
self.__rest_password = password
msg = "Authenticated to LIMS token is: %s" % self.__rest_root
logging.getLogger("ispyb_client").exception(msg)
def sample_link(self):
"""
Get the LIMS link to sample information
:returns: Link to sample information
"""
self.__update_rest_token()
return urljoin(self.__rest_root, "samples?token=%s" % self.__rest_token)
def get_dc_list(self):
"""
Get the list of data collections for the current session belonging to
the current proposal. (Given by the session object)
:returns: A list of LIMS DataCollection Objects
"""
return []
def get_dc(self, dc_id):
"""
Get data collection with id <dc_id>
:param int dc_id: The collection id
:returns: Data collection dict
"""
dc_dict = {}
return dc_dict
def get_dc_thumbnail(self, image_id):
"""
Get the image data for image with id <image_id>
:param int image_id: The image id
:returns: tuple on the form (file name, base64 encoded data)
"""
fname, data = ('', '')
return fname, data
def get_proposals_by_user(self, user_name):
"""
Descript. : gets all proposals for selected user
at first all proposals for user are obtained
then for each proposal all sessions are obtained
TODO: also user and laboratory should be obtained
"""
return [self.__test_proposal]
def get_proposal_sessions(self, proposal_id):
session_list = []
return session_list
def get_session_local_contact(self, session_id):
"""
Retrieves the person entry associated with the session id <session_id>
:param int session_id: session_id
:returns: Person dict
:rtype: dict
"""
return {'personId': 1,
'laboratoryId': 1,
'login': None,
'familyName':'operator on ID14eh1'}
def translate(self, code, what):
"""
Given a proposal code, returns the correct code to use in the GUI,
or what to send to LDAP, user office database, or the ISPyB database.
"""
try:
translated = self.__translations[code][what]
except KeyError:
translated = code
return translated
def store_data_collection(self, mx_collection, beamline_setup = None):
"""
Stores the data collection mx_collection, and the beamline setup
if provided.
:param mx_collection: The data collection parameters.
:type mx_collection: dict
:param beamline_setup: The beamline setup.
:type beamline_setup: dict
:returns: None
"""
print ("store_data_collection..." , mx_collection)
return None, None
def store_beamline_setup(self, session_id, beamline_setup):
"""
Stores the beamline setup dict <beamline_setup>.
:param session_id: The session id that the beamline_setup
should be associated with.
:type session_id: int
:param beamline_setup: The dictonary with beamline settings.
:type beamline_setup: dict
:returns beamline_setup_id: The database id of the beamline setup.
:rtype: str
"""
print("store_beamline_setup...", beamline_setup)
def update_data_collection(self, mx_collection, wait=False):
"""
Updates the datacollction mx_collection, this requires that the
collectionId attribute is set and exists in the database.
:param mx_collection: The dictionary with collections parameters.
:type mx_collection: dict
:returns: None
"""
print "update_data_collection... ", mx_collection
pass
def store_image(self, image_dict):
"""
Stores the image (image parameters) <image_dict>
:param image_dict: A dictonary with image pramaters.
:type image_dict: dict
:returns: None
"""
print("store_image ", image_dict)
def get_samples(self, proposal_id, session_id):
pass
def get_session_samples(self, proposal_id, session_id, sample_refs):
"""
Retrives the list of samples associated with the session <session_id>.
The samples from ISPyB is cross checked with the ones that are
currently in the sample changer.
The datamatrix code read by the sample changer is used in case
of conflict.
:param proposal_id: ISPyB proposal id.
:type proposal_id: int
:param session_id: ISPyB session id to retreive samples for.
:type session_id: int
:param sample_refs: The list of samples currently in the
sample changer. As a list of sample_ref
objects
:type sample_refs: list (of sample_ref objects).
:returns: A list with sample_ref objects.
:rtype: list
"""
pass
def get_bl_sample(self, bl_sample_id):
"""
Fetch the BLSample entry with the id bl_sample_id
:param bl_sample_id:
:type bl_sample_id: int
:returns: A BLSampleWSValue, defined in the wsdl.
:rtype: BLSampleWSValue
"""
pass
def disable(self):
self.__disabled = True
def enable(self):
self.__disabled = False
def store_data_collection_group(self, mx_collection):
"""
Stores or updates a DataCollectionGroup object.
The entry is updated of the group_id in the
mx_collection dictionary is set to an exisitng
DataCollectionGroup id.
:param mx_collection: The dictionary of values to create the object from.
:type mx_collection: dict
:returns: DataCollectionGroup id
:rtype: int
"""
pass