itkdb-gtk 0.10.9.dev3__py3-none-any.whl → 0.10.10__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of itkdb-gtk might be problematic. Click here for more details.

itkdb_gtk/ITkDButils.py CHANGED
@@ -10,19 +10,31 @@ import itkdb
10
10
 
11
11
  # The response of the DB
12
12
  db_response = ""
13
+ attachment_urls = {}
14
+ uploaded_test_runs = []
13
15
 
14
16
 
15
17
  # define an Attachment object.
16
18
  class Attachment(object):
17
19
  """Encapsulates Attachment information."""
18
20
 
19
- def __init__(self, path=None, title=None, desc=None):
21
+ def __init__(self, path=None, url=None, title=None, desc=None):
20
22
  """Initialization."""
21
23
  if path is not None:
22
24
  self.path = Path(path).expanduser().resolve()
25
+ self.type = "file"
23
26
  else:
24
27
  self.path = None
25
28
 
29
+ if url is not None:
30
+ self.url = url
31
+ self.type = "link"
32
+ else:
33
+ self.url = None
34
+
35
+ if self.path and self.url:
36
+ raise ValueError("Invalid Attachment. Has both file and link.")
37
+
26
38
  self.title = title
27
39
  self.desc = desc
28
40
 
@@ -44,7 +56,6 @@ def get_db_response():
44
56
  It is stores in a global variable. Trust the function if call
45
57
  right after your interaction with the DB.
46
58
  """
47
- global db_response
48
59
  return db_response
49
60
 
50
61
 
@@ -72,7 +83,8 @@ def get_db_date(timestamp=None):
72
83
  try:
73
84
  this_date = dateutil.parser.parse(timestamp)
74
85
  out = date2string(this_date)
75
- except Exception:
86
+
87
+ except (OverflowError, dateutil.parser.ParserError):
76
88
  out = ""
77
89
 
78
90
  return out
@@ -161,13 +173,13 @@ def create_component_attachment(client, SN, file_path, title=None, description="
161
173
  return db_response
162
174
 
163
175
 
164
- def set_component_property(client, SN, property, value):
176
+ def set_component_property(client, SN, the_property, value):
165
177
  """Set the value of an object property.
166
178
 
167
179
  Args:
168
180
  client: The DB client
169
181
  SN: The object SN
170
- property: The property name
182
+ the_property: The property name
171
183
  value: The property value
172
184
 
173
185
  """
@@ -175,7 +187,7 @@ def set_component_property(client, SN, property, value):
175
187
  try:
176
188
  db_response = client.post('setComponentProperty',
177
189
  json={'component': SN,
178
- 'code': property,
190
+ 'code': the_property,
179
191
  'value': value})
180
192
  return db_response
181
193
 
@@ -247,7 +259,7 @@ def get_DB_component(client, SN):
247
259
  db_response = str(e)
248
260
  return None
249
261
 
250
- def upload_test(client, data, attachments=None):
262
+ def upload_test(client, data, attachments=None, check_runNumber=False):
251
263
  """Upload a test to the DB.
252
264
 
253
265
  Args:
@@ -260,39 +272,129 @@ def upload_test(client, data, attachments=None):
260
272
 
261
273
  """
262
274
  global db_response
275
+ global attachment_urls
276
+ global uploaded_test_runs
277
+
278
+ uploaded_test_runs = []
279
+ attachment_urls = {}
280
+ db_response = None
281
+
282
+ # Check the given run_number. If already existing, give another one which
283
+ # will try to be consecutive.
284
+ if check_runNumber:
285
+ # Get all the runNumbers in this test
286
+ test_list = client.get("listTestRunsByComponent",
287
+ json={
288
+ "filterMap":{
289
+ "serialNumber": data["component"],
290
+ "state": "ready",
291
+ "testType":[data["testType"]]
292
+ }
293
+ }
294
+ )
295
+ runN= {}
296
+ for T in test_list:
297
+ runN[T["runNumber"]] = 1
298
+
299
+ if data["runNumber"] in runN:
300
+ # if the given runNumber is there, try to create a new one.
301
+ #print("runNumber {} already in {} of {}".format(data["runNumber"], data["testType"], data["component"]))
302
+ try:
303
+ irun = int(data["runNumber"])
304
+ for i in range(irun+1, 100):
305
+ newRn = "{}".format(i)
306
+ if newRn not in runN:
307
+ data["runNumber"] = newRn
308
+ break
309
+
310
+ except ValueError:
311
+ for i in range(100):
312
+ newRn = "{}_{}".format(data["runNumber"], i+1)
313
+ if newRn not in runN:
314
+ data["runNumber"] = newRn
315
+ break
316
+
317
+ # Try to upload the test
263
318
  try:
264
319
  db_response = client.post("uploadTestRunResults", json=data)
265
320
  testRun = db_response["testRun"]["id"]
321
+ uploaded_test_runs.append(testRun)
322
+
323
+ # Handle attachments.
324
+ attachment_urls = {}
266
325
  if attachments is not None:
267
326
  if not isinstance(attachments, Iterable):
268
327
  attachments = (attachments)
269
328
 
270
329
  for att in attachments:
271
- path = Path(att.path).expanduser().resolve()
272
- if not path.exists():
273
- print("File {} does not exist".format(path))
274
- continue
275
-
276
330
  data = {"testRun": testRun,
277
331
  "title": att.title if att.title is not None else path.name,
278
332
  "description": att.desc if att.desc is not None else path.name,
279
- "type": "file",
333
+ "type": att.type,
280
334
  }
281
- filetype = mimetypes.guess_type(path.name)
282
- attachment = {'data': (path.name, open(path.as_posix(), 'rb'), filetype[0])}
335
+ if att.type == "file":
336
+ path = Path(att.path).expanduser().resolve()
337
+ if not path.exists():
338
+ print("File {} does not exist".format(path))
339
+ continue
340
+
341
+ data["url"] = path.name
342
+ filetype = mimetypes.guess_type(path.name)
343
+ attachment = {'data': (path.name, open(path.as_posix(), 'rb'), filetype[0])}
344
+ else:
345
+ data["url"] = att.url
346
+ filetype = mimetypes.guess_type(att.url)
347
+ attachment = {'data':(att.url, None, "text/x-uri") }
348
+
283
349
  db_response = client.post('createTestRunAttachment',
284
350
  data=data,
285
351
  files=attachment)
286
-
352
+ try:
353
+ url = db_response['url'].replace("https://eosatlas.cern.ch","https://cernbox.cern.ch/files/spaces")
354
+ attachment_urls[path.name] = url
355
+
356
+ except KeyError:
357
+ pass
358
+
287
359
 
288
360
  return None
289
361
 
290
362
  except Exception as e:
291
363
  return (str(e))
292
364
 
365
+ def set_test_run_parameter(session, test_run, parameter, value):
366
+ """Modify testRun Parameter
367
+
368
+ Args:
369
+ session: The ITkDB session
370
+ test_run: ID of test run
371
+ parameter: parameter code
372
+ value: The new value
373
+ """
374
+ global db_response
375
+ try:
376
+ db_response = session.post("setTestRunParameter",
377
+ json={"testRun": test_run, "code": parameter, "value": value})
378
+ return None
379
+
380
+ except Exception as E:
381
+ return (str(E))
382
+
383
+ def create_test_run_comment(session, test_run, comments):
384
+ """Adds a new comment in testRun."""
385
+ global db_response
386
+ if not isinstance(comments, Iterable):
387
+ comments = (comments)
388
+
389
+ try:
390
+ db_response = session.post("createTestRunComment", json={"testRun": test_run, "comments": comments})
391
+ return None
392
+
393
+ except Exception as E:
394
+ return (str(E))
293
395
 
294
396
  def create_shipment(session, sender, recipient, items, name=None, send=False,
295
- type="domestic", attachment=None, comments=None):
397
+ shipment_type="domestic", attachment=None, comments=None):
296
398
  """Create a chipment.
297
399
 
298
400
  Args:
@@ -311,7 +413,7 @@ def create_shipment(session, sender, recipient, items, name=None, send=False,
311
413
  if name is None:
312
414
  name = "From {} to {}".format(sender, recipient)
313
415
 
314
- if type not in ["domestic", "intraContinental", "continental"]:
416
+ if shipment_type not in ["domestic", "intraContinental", "continental"]:
315
417
  db_response = "Wrong shipment type."
316
418
  return None
317
419
 
@@ -330,7 +432,7 @@ def create_shipment(session, sender, recipient, items, name=None, send=False,
330
432
  "name": name,
331
433
  "sender": sender,
332
434
  "recipient": recipient,
333
- "type": type,
435
+ "type": shipment_type,
334
436
  "shipmentItems": items,
335
437
  }
336
438
 
@@ -569,3 +671,20 @@ def create_client():
569
671
  client.user.authenticate()
570
672
  print("Hello {} !".format(client.user.name))
571
673
  return client
674
+
675
+ def get_db_user(client):
676
+ """REturn PDB information of current user.
677
+
678
+ Args:
679
+ client (itkdb.Client): The DB client.
680
+
681
+ """
682
+ global db_response
683
+ if client is None:
684
+ return None
685
+
686
+ try:
687
+ db_response = client.get("getUser", json={"userIdentity": client.user.identity})
688
+ return db_response
689
+ except Exception:
690
+ return None