Skip to content

Commit

Permalink
minor portal_utils tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
dmichaels-harvard committed Jan 8, 2024
1 parent b1ebd0d commit 8a59409
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
13 changes: 8 additions & 5 deletions dcicutils/portal_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,12 @@ def get_schemas(self) -> dict:
def schema_name(name: str) -> str:
return to_camel_case(name.replace(" ", "") if not name.endswith(".json") else name[:-5])

def is_schema(self, schema_name_or_object: Union[str, dict], target_schema_name: str,
_schemas_super_type_map: Optional[list] = None) -> bool:
def is_schema_type(self, schema_name_or_object: Union[str, dict], target_schema_name: str,
_schemas_super_type_map: Optional[list] = None) -> bool:
"""
If the given (first) schema_name_or_object argument is a string then returns True iff the
given schema (type) name isa type of the given target schema (type) name, i.e. is the
given schema type is the given target schema type or has an ancestor which is that type.
given schema (type) name isa type of the given target schema (type) name, i.e. the given
schema type is the given target schema type or has an ancestor which is that type.
If the given (first) schema_name_or_object argument is a dictionary then
returns True iff this object value isa type of the given target schema type.
"""
Expand Down Expand Up @@ -326,10 +326,13 @@ def isinstance_schema(self, value: dict, target_schema_name: str) -> bool:
if value_types:
schemas_super_type_map = self.get_schemas_super_type_map()
for value_type in value_types:
if self.is_schema(value_type, target_schema_name, schemas_super_type_map):
if self.is_schema_type(value_type, target_schema_name, schemas_super_type_map):
return True
return False

def get_schema_type(self, value: dict) -> Optional[str]:
return value.get("@type", value.get("data_type")) if isinstance(value, dict) else None

@lru_cache(maxsize=1)
def get_schemas_super_type_map(self) -> dict:
"""
Expand Down
2 changes: 1 addition & 1 deletion dcicutils/structured_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ def is_file_schema(self, schema_name: str) -> bool:
"""
Returns True iff the given schema name isa File type, i.e. has an ancestor which is of type File.
"""
return self.is_schema(schema_name, FILE_SCHEMA_NAME)
return self.is_schema_type(schema_name, FILE_SCHEMA_NAME)

def ref_exists(self, type_name: str, value: str) -> List[str]:
resolved = []
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.7.0.1b18" # TODO: To become 8.7.1
version = "8.7.0.1b19" # TODO: To become 8.7.1
description = "Utility package for interacting with the 4DN Data Portal and other 4DN resources"
authors = ["4DN-DCIC Team <[email protected]>"]
license = "MIT"
Expand Down
32 changes: 16 additions & 16 deletions test/test_portal_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ def test_portal_schemas_super_type_map():

portal = Portal(raise_exception=False)

assert portal.is_schema("UnalignedReads", "UnalignedReads") is True
assert portal.is_schema("UnalignedReads", "unalignedreads") is True
assert portal.is_schema("UnalignedReads", "unaligned_reads") is True
assert portal.is_schema("UnalignedReads", "SubmittedFile") is True
assert portal.is_schema("UnalignedReads", "SUBMITTEDfILE") is True
assert portal.is_schema("UnalignedReads", "File") is True
assert portal.is_schema("UnalignedReads", "file") is True
assert portal.is_schema_type("UnalignedReads", "UnalignedReads") is True
assert portal.is_schema_type("UnalignedReads", "unalignedreads") is True
assert portal.is_schema_type("UnalignedReads", "unaligned_reads") is True
assert portal.is_schema_type("UnalignedReads", "SubmittedFile") is True
assert portal.is_schema_type("UnalignedReads", "SUBMITTEDfILE") is True
assert portal.is_schema_type("UnalignedReads", "File") is True
assert portal.is_schema_type("UnalignedReads", "file") is True

assert portal.isinstance_schema({"@type": "UnalignedReads"}, "UnalignedReads") is True
assert portal.isinstance_schema({"@type": "UnalignedReads"}, "UNALIGNEDREADS") is True
Expand All @@ -166,12 +166,12 @@ def test_portal_schemas_super_type_map():
assert portal.isinstance_schema({"@type": "SubmittedFile"}, "File") is True
assert portal.isinstance_schema({"@type": "foo", "data_type": "UnalignedReads"}, "UnalignedReads") is True

assert portal.is_schema({"@type": "UnalignedReads"}, "UnalignedReads") is True
assert portal.is_schema({"@type": "UnalignedReads"}, "UNALIGNEDREADS") is True
assert portal.is_schema({"@type": "UnalignedReads"}, "Unaligned_Reads") is True
assert portal.is_schema({"@type": "UnalignedReads"}, "SubmittedFile") is True
assert portal.is_schema({"@type": "UnalignedReads"}, "SUBMITTEDFILE") is True
assert portal.is_schema({"@type": "UnalignedReads"}, "SUBMITTED_FILE") is True
assert portal.is_schema({"@type": "SubmittedFile"}, "SUBMITTED_FILE") is True
assert portal.is_schema({"@type": "SubmittedFile"}, "File") is True
assert portal.is_schema({"@type": "foo", "data_type": "UnalignedReads"}, "UnalignedReads") is True
assert portal.is_schema_type({"@type": "UnalignedReads"}, "UnalignedReads") is True
assert portal.is_schema_type({"@type": "UnalignedReads"}, "UNALIGNEDREADS") is True
assert portal.is_schema_type({"@type": "UnalignedReads"}, "Unaligned_Reads") is True
assert portal.is_schema_type({"@type": "UnalignedReads"}, "SubmittedFile") is True
assert portal.is_schema_type({"@type": "UnalignedReads"}, "SUBMITTEDFILE") is True
assert portal.is_schema_type({"@type": "UnalignedReads"}, "SUBMITTED_FILE") is True
assert portal.is_schema_type({"@type": "SubmittedFile"}, "SUBMITTED_FILE") is True
assert portal.is_schema_type({"@type": "SubmittedFile"}, "File") is True
assert portal.is_schema_type({"@type": "foo", "data_type": "UnalignedReads"}, "UnalignedReads") is True

0 comments on commit 8a59409

Please sign in to comment.