75
75
76
76
from astropy .io import fits
77
77
from urllib .parse import urlparse
78
- from cadcdata import FileInfo , CadcDataClient , StorageInventoryClient
78
+ from cadcdata import FileInfo , StorageInventoryClient
79
79
from cadcutils import exceptions
80
80
81
81
92
92
93
93
class StorageClientWrapper :
94
94
"""
95
- Wrap the choice between CadcDataClient and StorageInventoryClient.
95
+ Wrap the metrics collection with StorageInventoryClient.
96
96
"""
97
97
98
- def __init__ (
99
- self ,
100
- subject ,
101
- using_storage_inventory = True ,
102
- resource_id = 'ivo://cadc.nrc.ca/uvic/minoc' ,
103
- metrics = None ,
104
- ):
98
+ def __init__ (self , subject , resource_id = 'ivo://cadc.nrc.ca/uvic/minoc' , metrics = None ):
105
99
"""
106
- :param subject: net.Subject instance for authentication and
107
- authorization
108
- :param using_storage_inventory: if True will use
109
- StorageInventoryClient for file operations at CADC. If False will
110
- use CadcDataClient.
111
- :param resource_id: str identifies the StorageInventoryClient
112
- endpoint. If using_storage_inventory is set to False, it's
113
- un-necessary.
114
- :param metrics: caom2pipe.manaage_composable.Metrics instance. If set,
115
- will track execution times, by action, from the beginning of
116
- the method invocation to the end of the method invocation,
117
- success or failure. Defaults to None, because fits2caom2 is
118
- a stand-alone application.
100
+ :param subject: net.Subject instance for authentication and authorization
101
+ :param resource_id: str identifies the StorageInventoryClient endpoint. Defaults to the installation closest to
102
+ most of the current invocations.
103
+ :param metrics: caom2pipe.manaage_composable.Metrics instance. If set, will track execution times, by action,
104
+ from the beginning of the method invocation to the end of the method invocation, success or failure.
105
+ Defaults to None, because fits2caom2 is a stand-alone application.
119
106
"""
120
- if using_storage_inventory :
121
- self ._cadc_client = StorageInventoryClient (
122
- subject = subject , resource_id = resource_id
123
- )
124
- else :
125
- self ._cadc_client = CadcDataClient (subject = subject )
126
- self ._use_si = using_storage_inventory
107
+ self ._cadc_client = StorageInventoryClient (subject = subject , resource_id = resource_id )
127
108
self ._metrics = metrics
128
109
self ._logger = logging .getLogger (self .__class__ .__name__ )
129
110
130
111
def _add_fail_metric (self , action , name ):
131
- """Single location for the check for a self._metrics member in the
132
- failure case."""
112
+ """Single location for the check for a self._metrics member in the failure case."""
133
113
if self ._metrics is not None :
134
- client_name = 'si' if self ._use_si else 'data'
135
- self ._metrics .observe_failure (action , client_name , name )
114
+ self ._metrics .observe_failure (action , 'si' , name )
136
115
137
116
def _add_metric (self , action , name , start , value ):
138
- """Single location for the check for a self._metrics member in the
139
- success case."""
117
+ """Single location for the check for a self._metrics member in the success case."""
140
118
if self ._metrics is not None :
141
- client_name = 'si' if self ._use_si else 'data'
142
- self ._metrics .observe (
143
- start ,
144
- StorageClientWrapper ._current (),
145
- value ,
146
- action ,
147
- client_name ,
148
- name ,
149
- )
119
+ self ._metrics .observe (start , StorageClientWrapper ._current (), value , action , 'si' , name )
150
120
151
121
def get (self , working_directory , uri ):
152
122
"""
@@ -156,15 +126,12 @@ def get(self, working_directory, uri):
156
126
:param uri: str this is an Artifact URI, representing the file to
157
127
be retrieved.
158
128
"""
159
- self ._logger .debug (f'Being get for { uri } in { working_directory } ' )
129
+ self ._logger .debug (f'Begin get for { uri } in { working_directory } ' )
160
130
start = StorageClientWrapper ._current ()
161
131
try :
162
132
archive , f_name = self ._decompose (uri )
163
133
fqn = path .join (working_directory , f_name )
164
- if self ._use_si :
165
- self ._cadc_client .cadcget (uri , dest = fqn )
166
- else :
167
- self ._cadc_client .get_file (archive , f_name , destination = fqn )
134
+ self ._cadc_client .cadcget (uri , dest = fqn )
168
135
except Exception as e :
169
136
self ._add_fail_metric ('get' , uri )
170
137
self ._logger .debug (traceback .format_exc ())
@@ -186,11 +153,7 @@ def get_head(self, uri):
186
153
try :
187
154
b = BytesIO ()
188
155
b .name = uri
189
- if self ._use_si :
190
- self ._cadc_client .cadcget (uri , b , fhead = True )
191
- else :
192
- archive , f_name = StorageClientWrapper ._decompose (uri )
193
- self ._cadc_client .get_file (archive , f_name , b , fhead = True )
156
+ self ._cadc_client .cadcget (uri , b , fhead = True )
194
157
fits_header = b .getvalue ().decode ('ascii' )
195
158
b .close ()
196
159
self ._add_metric ('get_head' , uri , start , len (fits_header ))
@@ -207,44 +170,28 @@ def get_head(self, uri):
207
170
208
171
def info (self , uri ):
209
172
"""
210
- Retrieve the descriptive metdata associated with a file.
173
+ Retrieve the descriptive metadata associated with a file.
211
174
:param uri: str that is an Artifact URI, representing the file for
212
175
which to retrieve metadata
213
176
:return: cadcdata.FileInfo instance, no scheme for md5sum
214
177
"""
215
178
self ._logger .debug (f'Begin info for { uri } ' )
216
179
try :
217
- if self ._use_si :
218
- result = self ._cadc_client .cadcinfo (uri )
219
- # make the result look like the other possible ways to
220
- # obtain metadata
221
- result .md5sum = result .md5sum .replace ('md5:' , '' )
222
- else :
223
- archive , f_name = StorageClientWrapper ._decompose (uri )
224
- temp = self ._cadc_client .get_file_info (archive , f_name )
225
- result = FileInfo (
226
- id = uri ,
227
- size = temp .get ('size' ),
228
- file_type = temp .get ('type' ),
229
- md5sum = temp .get ('md5sum' ).replace ('md5:' , '' ),
230
- encoding = temp .get ('encoding' ),
231
- )
180
+ result = self ._cadc_client .cadcinfo (uri )
181
+ # make the result look like the other possible ways to
182
+ # obtain metadata
183
+ result .md5sum = result .md5sum .replace ('md5:' , '' )
232
184
except exceptions .NotFoundException :
233
185
self ._logger .info (f'cadcinfo:: { uri } not found' )
234
186
result = None
235
187
self ._logger .debug ('End info' )
236
188
return result
237
189
238
- def put (self , working_directory , uri , stream = 'default' ):
190
+ def put (self , working_directory , uri ):
239
191
"""
240
192
Store a file at CADC.
241
- :param working_directory: str fully-qualified name of where to find
242
- the file on the local machine
243
- :param uri: str that is an Artifact URI, representing the file to
244
- be stored at CADC.
245
- :param stream: str representing the namespace used by the
246
- CadcDataClient. Not required if using the StorageInventoryClient.
247
- 'default' is default name for a lately-created ad archive.
193
+ :param working_directory: str fully-qualified name of where to find the file on the local machine
194
+ :param uri: str that is an Artifact URI, representing the file to be stored at CADC.
248
195
"""
249
196
self ._logger .debug (f'Begin put for { uri } in { working_directory } ' )
250
197
start = self ._current ()
@@ -255,41 +202,22 @@ def put(self, working_directory, uri, stream='default'):
255
202
try :
256
203
local_meta = get_local_file_info (fqn )
257
204
encoding = get_file_encoding (fqn )
258
- if self ._use_si :
259
- replace = True
260
- cadc_meta = self .info (uri )
261
- if cadc_meta is None :
262
- replace = False
263
- self ._logger .debug (
264
- f'uri { uri } src { fqn } replace { replace } file_type '
265
- f'{ local_meta .file_type } encoding { encoding } md5_checksum '
266
- f'{ local_meta .md5sum } '
267
- )
268
- self ._cadc_client .cadcput (
269
- uri ,
270
- src = fqn ,
271
- replace = replace ,
272
- file_type = local_meta .file_type ,
273
- file_encoding = encoding ,
274
- md5_checksum = local_meta .md5sum ,
275
- )
276
- else :
277
- archive , f_name = self ._decompose (uri )
278
- # libmagic does a worse job with guessing file types
279
- # than ad for .fits.gz => it will say 'binary'
280
- self ._logger .debug (
281
- f'archive { archive } f_name { f_name } archive_stream '
282
- f'{ stream } mime_type { local_meta .file_type } '
283
- f'mime_encoding { encoding } md5_check True '
284
- )
285
- self ._cadc_client .put_file (
286
- archive ,
287
- f_name ,
288
- archive_stream = stream ,
289
- mime_type = local_meta .file_type ,
290
- mime_encoding = encoding ,
291
- md5_check = True ,
292
- )
205
+ replace = True
206
+ cadc_meta = self .info (uri )
207
+ if cadc_meta is None :
208
+ replace = False
209
+ self ._logger .debug (
210
+ f'uri { uri } src { fqn } replace { replace } file_type { local_meta .file_type } encoding { encoding } '
211
+ f'md5_checksum { local_meta .md5sum } '
212
+ )
213
+ self ._cadc_client .cadcput (
214
+ uri ,
215
+ src = fqn ,
216
+ replace = replace ,
217
+ file_type = local_meta .file_type ,
218
+ file_encoding = encoding ,
219
+ md5_checksum = local_meta .md5sum ,
220
+ )
293
221
self ._logger .info (f'Stored { fqn } at CADC.' )
294
222
except Exception as e :
295
223
self ._add_fail_metric ('put' , uri )
@@ -311,19 +239,14 @@ def remove(self, uri):
311
239
"""
312
240
self ._logger .debug (f'Begin remove for { uri } ' )
313
241
start = StorageClientWrapper ._current ()
314
- if self ._use_si :
315
- try :
316
- self ._cadc_client .cadcremove (uri )
317
- except Exception as e :
318
- self ._add_fail_metric ('remove' , uri )
319
- self ._logger .debug (traceback .format_exc ())
320
- self ._logger .error (e )
321
- raise exceptions .UnexpectedException (
322
- f'Did not remove { uri } because { e } '
323
- )
324
- else :
325
- raise NotImplementedError (
326
- 'No remove functionality for CadcDataClient'
242
+ try :
243
+ self ._cadc_client .cadcremove (uri )
244
+ except Exception as e :
245
+ self ._add_fail_metric ('remove' , uri )
246
+ self ._logger .debug (traceback .format_exc ())
247
+ self ._logger .error (e )
248
+ raise exceptions .UnexpectedException (
249
+ f'Did not remove { uri } because { e } '
327
250
)
328
251
self ._add_metric ('remove' , uri , start , value = None )
329
252
self ._logger .debug ('End remove' )
0 commit comments