Skip to content

Commit

Permalink
Merge pull request #316 from 4dn-dcic/dmichaels-misc-minor-updates-20…
Browse files Browse the repository at this point in the history
…240821

Added portal_utils.Portal.head method
  • Loading branch information
dmichaels-harvard authored Aug 23, 2024
2 parents 0fe4f08 + 734bba9 commit f424018
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ dcicutils
Change Log
----------

8.14.3
======

* 2024-08-22 (dmichaels)
* Modified structured_data property hook for "finish" attribute/callable.
* Added sheet hook to structured_data.
* Added portal_utils.Portal.head method.


8.14.2
======
* Corrected requests version (to ^2.27.0 from 2.31.0) on pyproject.toml to not be pinned; but doing
Expand Down
11 changes: 11 additions & 0 deletions dcicutils/portal_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,17 @@ def post_metadata(self, object_type: str, data: dict, check_only: bool = False)
add_on="check_only=True" if check_only else "")
return self.post(f"/{object_type}{'?check_only=True' if check_only else ''}", data).json()

def head(self, url: str, follow: bool = True, raise_exception: bool = False, **kwargs) -> Optional[int]:
try:
response = requests.head(self.url(url), **self._kwargs(**kwargs))
if response and response.status_code in [301, 302, 303, 307, 308] and (follow is not False):
response = response.follow()
return response.status_code
except Exception as e:
if raise_exception is True:
raise e
return None

def get_health(self) -> OptionalResponse:
return self.get("/health")

Expand Down
10 changes: 9 additions & 1 deletion dcicutils/structured_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def __init__(self, file: Optional[str] = None, portal: Optional[Union[VirtualApp
norefs: bool = False, merge: bool = False,
progress: Optional[Callable] = None,
validator_hook: Optional[Callable] = None,
validator_sheet_hook: Optional[Callable] = None,
debug_sleep: Optional[str] = None) -> None:
self._progress = progress if callable(progress) else None
self._data = {}
Expand All @@ -76,7 +77,8 @@ def __init__(self, file: Optional[str] = None, portal: Optional[Union[VirtualApp
self._autoadd_properties = autoadd if isinstance(autoadd, dict) and autoadd else None
self._norefs = True if norefs is True else False
self._merge = True if merge is True else False # New merge functionality (2024-05-25)
self._validator_hook = validator_hook if callable(validator_hook) else None # Testing support (2024-06-12)
self._validator_hook = validator_hook if callable(validator_hook) else None
self._validator_sheet_hook = validator_sheet_hook if callable(validator_sheet_hook) else None
self._debug_sleep = None
if debug_sleep:
try:
Expand Down Expand Up @@ -287,6 +289,10 @@ def _load_normal_file(self, file: str) -> None:
self._load_json_file(file)
elif file.endswith(".tar") or file.endswith(".zip"):
self._load_packed_file(file)
if (self._validator_hook and
hasattr(self._validator_hook, "finish") and
callable(finish_validator_hook := getattr(self._validator_hook, "finish"))): # noqa
finish_validator_hook(self)

def _load_packed_file(self, file: str) -> None:
for file in unpack_files(file, suffixes=ACCEPTABLE_FILE_SUFFIXES):
Expand All @@ -312,6 +318,8 @@ def get_counts() -> Tuple[int, int]:
order = {Schema.type_name(key): index for index, key in enumerate(self._order)} if self._order else {}
for sheet_name in sorted(excel.sheet_names, key=lambda key: order.get(Schema.type_name(key), sys.maxsize)):
self._load_reader(excel.sheet_reader(sheet_name), type_name=Schema.type_name(sheet_name))
if self._validator_sheet_hook:
self._validator_sheet_hook(self, sheet_name, self.data[sheet_name])
# TODO: Do we really need progress reporting for the below?
# Check for unresolved reference errors which really are not because of ordering.
# Yes such internal references will be handled correctly on actual database update via snovault.loadxl.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dcicutils"
version = "8.14.2"
version = "8.14.3"
description = "Utility package for interacting with the 4DN Data Portal and other 4DN resources"
authors = ["4DN-DCIC Team <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit f424018

Please sign in to comment.