dcicutils 8.14.0.1b28__py3-none-any.whl → 8.14.0.1b29__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -307,28 +307,76 @@ def _get_portal_object(portal: Portal, uuid: str,
307
307
  return None
308
308
  return metadata_types
309
309
 
310
- response = None
311
- try:
312
- if not uuid.startswith("/"):
313
- path = f"/{uuid}"
314
- else:
315
- path = uuid
316
- response = portal.get(path, raw=raw or inserts, database=database)
317
- except Exception as e:
318
- if "404" in str(e) and "not found" in str(e).lower():
319
- _print(f"Portal object not found at {portal.server}: {uuid}")
320
- _exit()
321
- _exit(f"Exception getting Portal object from {portal.server}: {uuid}\n{get_error_message(e)}")
322
- if not response:
323
- if check:
324
- return None
325
- _exit(f"Null response getting Portal object from {portal.server}: {uuid}")
326
- if response.status_code not in [200, 307]:
327
- # TODO: Understand why the /me endpoint returns HTTP status code 307, which is only why we mention it above.
328
- _exit(f"Invalid status code ({response.status_code}) getting Portal object from {portal.server}: {uuid}")
329
- if not response.json:
330
- _exit(f"Invalid JSON getting Portal object: {uuid}")
331
- response = response.json()
310
+ def write_insert_files(response: dict) -> None:
311
+ nonlocal insert_files, force
312
+ output_directory = insert_files if isinstance(insert_files, str) else os.getcwd()
313
+ for schema_name in response:
314
+ schema_data = response[schema_name]
315
+ file_name = f"{to_snake_case(schema_name)}.json"
316
+ file_path = os.path.join(output_directory, file_name)
317
+ message_verb = "Writing"
318
+ if os.path.exists(file_path):
319
+ message_verb = "Overwriting"
320
+ if os.path.isdir(file_path):
321
+ _print(f"WARNING: Output file already exists as a directory. SKIPPING: {file_path}")
322
+ continue
323
+ if not force:
324
+ _print(f"Output file already exists: {file_path}")
325
+ if not yes_or_no(f"Overwrite this file?"):
326
+ continue
327
+ if verbose:
328
+ _print(f"{message_verb} {schema_name} (object{'s' if len(schema_data) != 1 else ''}:"
329
+ f" {len(schema_data)}) file: {file_path}")
330
+ with io.open(file_path, "w") as f:
331
+ json.dump(schema_data, f, indent=4)
332
+
333
+ if os.path.exists(uuid) and inserts:
334
+ # Very special case: If given "uuid" (or other path) as actually a file then assume it
335
+ # contains a list of references (e.g. /Donor/3039a6ca-9849-432d-ad49-2c5630bcbee7) to fetch.
336
+ response = {}
337
+ if verbose:
338
+ _print(f"Reading references from file: {uuid}")
339
+ with io.open(uuid) as f:
340
+ for line in f:
341
+ if ((line := line.strip()) and (components := line.split("/")) and (len(components) > 1) and
342
+ (schema_name := components[1]) and (schema_name := _get_schema(portal, schema_name)[1])): # noqa
343
+ try:
344
+ if ((result := portal.get(line, raw=True, database=database)) and
345
+ (result.status_code in [200, 307]) and (result := result.json())): # noqa
346
+ if not response.get(schema_name):
347
+ response[schema_name] = []
348
+ response[schema_name].append(result)
349
+ continue
350
+ except Exception:
351
+ pass
352
+ _print(f"Cannot get reference: {line}")
353
+ if insert_files:
354
+ write_insert_files(response)
355
+ return response
356
+ else:
357
+ response = None
358
+ try:
359
+ if not uuid.startswith("/"):
360
+ path = f"/{uuid}"
361
+ else:
362
+ path = uuid
363
+ response = portal.get(path, raw=raw or inserts, database=database)
364
+ except Exception as e:
365
+ if "404" in str(e) and "not found" in str(e).lower():
366
+ _print(f"Portal object not found at {portal.server}: {uuid}")
367
+ _exit()
368
+ _exit(f"Exception getting Portal object from {portal.server}: {uuid}\n{get_error_message(e)}")
369
+ if not response:
370
+ if check:
371
+ return None
372
+ _exit(f"Null response getting Portal object from {portal.server}: {uuid}")
373
+ if response.status_code not in [200, 307]:
374
+ # TODO: Understand why the /me endpoint returns HTTP status code 307, which is only why we mention it above.
375
+ _exit(f"Invalid status code ({response.status_code}) getting Portal object from {portal.server}: {uuid}")
376
+ if not response.json:
377
+ _exit(f"Invalid JSON getting Portal object: {uuid}")
378
+ response = response.json()
379
+
332
380
  response_types = {}
333
381
  if inserts:
334
382
  # Format results as suitable for inserts (e.g. via update-portal-object).
@@ -376,26 +424,27 @@ def _get_portal_object(portal: Portal, uuid: str,
376
424
  (isinstance(response_type := response_cooked.json().get("@type"), list) and response_type)):
377
425
  response = {f"{response_type[0]}": [prune_data(response)]}
378
426
  if insert_files:
379
- output_directory = insert_files if isinstance(insert_files, str) else os.getcwd()
380
- for schema_name in response:
381
- schema_data = response[schema_name]
382
- file_name = f"{to_snake_case(schema_name)}.json"
383
- file_path = os.path.join(output_directory, file_name)
384
- message_verb = "Writing"
385
- if os.path.exists(file_path):
386
- message_verb = "Overwriting"
387
- if os.path.isdir(file_path):
388
- _print(f"WARNING: Output file already exists as a directory. SKIPPING: {file_path}")
389
- continue
390
- if not force:
391
- _print(f"Output file already exists: {file_path}")
392
- if not yes_or_no(f"Overwrite this file?"):
393
- continue
394
- if verbose:
395
- _print(f"{message_verb} {schema_name} (object{'s' if len(schema_data) != 1 else ''}:"
396
- f" {len(schema_data)}) file: {file_path}")
397
- with io.open(file_path, "w") as f:
398
- json.dump(schema_data, f, indent=4)
427
+ write_insert_files(response)
428
+ # output_directory = insert_files if isinstance(insert_files, str) else os.getcwd()
429
+ # for schema_name in response:
430
+ # schema_data = response[schema_name]
431
+ # file_name = f"{to_snake_case(schema_name)}.json"
432
+ # file_path = os.path.join(output_directory, file_name)
433
+ # message_verb = "Writing"
434
+ # if os.path.exists(file_path):
435
+ # message_verb = "Overwriting"
436
+ # if os.path.isdir(file_path):
437
+ # _print(f"WARNING: Output file already exists as a directory. SKIPPING: {file_path}")
438
+ # continue
439
+ # if not force:
440
+ # _print(f"Output file already exists: {file_path}")
441
+ # if not yes_or_no(f"Overwrite this file?"):
442
+ # continue
443
+ # if verbose:
444
+ # _print(f"{message_verb} {schema_name} (object{'s' if len(schema_data) != 1 else ''}:"
445
+ # f" {len(schema_data)}) file: {file_path}")
446
+ # with io.open(file_path, "w") as f:
447
+ # json.dump(schema_data, f, indent=4)
399
448
  elif raw:
400
449
  response.pop("schema_version", None)
401
450
  return response
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dcicutils
3
- Version: 8.14.0.1b28
3
+ Version: 8.14.0.1b29
4
4
  Summary: Utility package for interacting with the 4DN Data Portal and other 4DN resources
5
5
  Home-page: https://github.com/4dn-dcic/utils
6
6
  License: MIT
@@ -61,7 +61,7 @@ dcicutils/schema_utils.py,sha256=GmRm-XqZKJ6qine16SQF1txcby9WougDav_sYmKNs9E,124
61
61
  dcicutils/scripts/publish_to_pypi.py,sha256=sMd4WASQGlxlh7uLrt2eGkFRXYgONVmvIg8mClMS5RQ,13903
62
62
  dcicutils/scripts/run_license_checker.py,sha256=z2keYnRDZsHQbTeo1XORAXSXNJK5axVzL5LjiNqZ7jE,4184
63
63
  dcicutils/scripts/update_portal_object.py,sha256=BwZw1cZx2zhzp1ivn_DXUxfnmUyC5A9fAotjHV0fXf4,41875
64
- dcicutils/scripts/view_portal_object.py,sha256=vsJzS985JFEW6-xOe_ExkuE4Dk21G6d8xfC9RBUNwUg,38655
64
+ dcicutils/scripts/view_portal_object.py,sha256=mfaWIjVD_dtLNl8Gfue-9Xo1zBrfChe2Tr9J6aoA7sA,41274
65
65
  dcicutils/secrets_utils.py,sha256=8dppXAsiHhJzI6NmOcvJV5ldvKkQZzh3Fl-cb8Wm7MI,19745
66
66
  dcicutils/sheet_utils.py,sha256=VlmzteONW5VF_Q4vo0yA5vesz1ViUah1MZ_yA1rwZ0M,33629
67
67
  dcicutils/snapshot_utils.py,sha256=YDeI3vD-MhAtHwKDzfEm2q-n3l-da2yRpRR3xp0Ah1M,23021
@@ -75,8 +75,8 @@ dcicutils/trace_utils.py,sha256=g8kwV4ebEy5kXW6oOrEAUsurBcCROvwtZqz9fczsGRE,1769
75
75
  dcicutils/validation_utils.py,sha256=cMZIU2cY98FYtzK52z5WUYck7urH6JcqOuz9jkXpqzg,14797
76
76
  dcicutils/variant_utils.py,sha256=2H9azNx3xAj-MySg-uZ2SFqbWs4kZvf61JnK6b-h4Qw,4343
77
77
  dcicutils/zip_utils.py,sha256=_Y9EmL3D2dUZhxucxHvrtmmlbZmK4FpSsHEb7rGSJLU,3265
78
- dcicutils-8.14.0.1b28.dist-info/LICENSE.txt,sha256=qnwSmfnEWMl5l78VPDEzAmEbLVrRqQvfUQiHT0ehrOo,1102
79
- dcicutils-8.14.0.1b28.dist-info/METADATA,sha256=_Cl7Wklz95RtMUYcwgxsJk1mox2gnJeshyNHC3imAaA,3440
80
- dcicutils-8.14.0.1b28.dist-info/WHEEL,sha256=7Z8_27uaHI_UZAc4Uox4PpBhQ9Y5_modZXWMxtUi4NU,88
81
- dcicutils-8.14.0.1b28.dist-info/entry_points.txt,sha256=W6kEWdUJk9tQ4myAgpehPdebcwvCAZ7UgB-wyPgDUMg,335
82
- dcicutils-8.14.0.1b28.dist-info/RECORD,,
78
+ dcicutils-8.14.0.1b29.dist-info/LICENSE.txt,sha256=qnwSmfnEWMl5l78VPDEzAmEbLVrRqQvfUQiHT0ehrOo,1102
79
+ dcicutils-8.14.0.1b29.dist-info/METADATA,sha256=ht8DVYhzxCHVincEsvta3kwooBimQ6Iei03TjAYL24k,3440
80
+ dcicutils-8.14.0.1b29.dist-info/WHEEL,sha256=7Z8_27uaHI_UZAc4Uox4PpBhQ9Y5_modZXWMxtUi4NU,88
81
+ dcicutils-8.14.0.1b29.dist-info/entry_points.txt,sha256=W6kEWdUJk9tQ4myAgpehPdebcwvCAZ7UgB-wyPgDUMg,335
82
+ dcicutils-8.14.0.1b29.dist-info/RECORD,,