Skip to content

Commit 7598138

Browse files
committed
Add a method Client.space_info() which returns information about the types and number of instances in a space.
1 parent 378056b commit 7598138

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

fairgraph/client.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
have_kg_core = False
3535

3636
from .errors import AuthenticationError, AuthorizationError, ResourceExistsError
37+
from .registry import lookup_type
3738

3839
if TYPE_CHECKING:
3940
from .kgobject import KGObject
@@ -583,7 +584,10 @@ def configure_space(self, space_name: Optional[str] = None, types: Optional[List
583584
space_name = f"collab-{collab_id}"
584585
result = self._kg_admin_client.create_space_definition(space=space_name)
585586
if result: # error
586-
raise Exception(f"Unable to configure KG space for space '{space_name}': {result}")
587+
err_msg = f"Unable to configure KG space for space '{space_name}': {result}"
588+
if not space_name.startswith("collab="):
589+
err_msg += f". If you are trying to configure a collab space, ensure the space name starts with 'collab-'"
590+
raise Exception(err_msg)
587591
for cls in types:
588592
result = self._kg_admin_client.assign_type_to_space(space=space_name, target_type=cls.type_)
589593
if result: # error
@@ -601,6 +605,29 @@ def move_to_space(self, uri: str, destination_space: str):
601605
if response.error:
602606
raise Exception(response.error)
603607

608+
def space_info(self, space_name: str, scope: str = "released"):
609+
"""
610+
Return information about the types and number of instances in a space.
611+
612+
The return format is a dictionary whose keys are classes and the values are the
613+
number of instances of each class in the given spaces.
614+
"""
615+
result = self._kg_client.types.list(space=space_name, stage=STAGE_MAP[scope])
616+
if result.error:
617+
raise Exception(result.error)
618+
response = {}
619+
for item in result.data:
620+
try:
621+
cls = lookup_type(item.identifier)
622+
except KeyError as err:
623+
if "vocab/meta/type/Query" in str(err):
624+
pass
625+
else:
626+
raise
627+
else:
628+
response[cls] = item.occurrences
629+
return response
630+
604631
def is_released(self, uri: str, with_children: bool = False) -> bool:
605632
"""
606633
Release status of a KG instance identified by its URI.

0 commit comments

Comments
 (0)