Skip to content

Commit

Permalink
Fixed error message when registering, and expose a method to execute …
Browse files Browse the repository at this point in the history
…lazy actions.
  • Loading branch information
crisely09 committed Feb 24, 2023
1 parent 2300e51 commit b27f0bf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
6 changes: 3 additions & 3 deletions kgforge/core/archetypes/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,17 @@ def mapper(self) -> Optional[Callable]:
# [C]RUD.

def register(
self, data: Union[Resource, List[Resource]], schema_id: str = None, debug: bool = False
self, data: Union[Resource, List[Resource]], schema_id: str = None,
debug: bool = False
) -> None:
# Replace None by self._register_many to switch to optimized bulk registration.
catch_exceptions = False if debug else True
run(
self._register_one,
None,
data,
required_synchronized=False,
execute_actions=True,
catch_exceptions=catch_exceptions,
catch_exceptions=not debug,
exception=RegistrationError,
monitored_status="_synchronized",
schema_id=schema_id,
Expand Down
15 changes: 15 additions & 0 deletions kgforge/core/forge.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
from kgforge.core.commons.dictionaries import with_defaults
from kgforge.core.commons.exceptions import ResolvingError
from kgforge.core.commons.execution import catch
from kgforge.core.commons.actions import (collect_lazy_actions,
execute_lazy_actions)
from kgforge.core.commons.imports import import_class
from kgforge.core.commons.strategies import ResolvingStrategy
from kgforge.core.conversions.dataframe import as_dataframe, from_dataframe
Expand Down Expand Up @@ -608,6 +610,19 @@ def elastic(
:return: List[Resource]
"""
return self._store.elastic(query, debug, limit, offset)

@catch
@staticmethod
def execute_lazy_actions(resources: Union[List[Resource], Resource]):
"""
Execute any lazy action present in a given resource or a list of resources
:param resources: The resources or list of resources from which actions will be executed
"""
resources = [resources] if not isinstance(resources, List) else resources
for resource in resources:
lazy_actions = collect_lazy_actions(resource)
execute_lazy_actions(resource, lazy_actions)

@catch
def download(
Expand Down
31 changes: 20 additions & 11 deletions kgforge/specializations/stores/bluebrain_nexus.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,17 @@ def mapper(self) -> Optional[Callable]:
return DictionaryMapper

def register(
self, data: Union[Resource, List[Resource]], schema_id: str = None, debug: bool = False
self, data: Union[Resource, List[Resource]], schema_id: str = None,
debug: bool = False
) -> None:
catch_exceptions = False if debug else True
run(
self._register_one,
self._register_many,
data,
required_synchronized=False,
execute_actions=True,
exception=RegistrationError,
catch_exceptions=catch_exceptions,
catch_exceptions=not debug,
monitored_status="_synchronized",
schema_id=schema_id,
)
Expand Down Expand Up @@ -554,14 +554,13 @@ def _update_one(self, resource: Resource, schema_id: str) -> None:
self.service.sync_metadata(resource, response.json())

def tag(self, data: Union[Resource, List[Resource]], value: str, debug: bool = False) -> None:
catch_exceptions = False if debug else True
run(
self._tag_one,
self._tag_many,
data,
id_required=True,
required_synchronized=True,
catch_exceptions=catch_exceptions,
catch_exceptions=not debug,
exception=TaggingError,
value=value,
)
Expand Down Expand Up @@ -606,14 +605,13 @@ def _tag_one(self, resource: Resource, value: str) -> None:
# CRU[D].

def deprecate(self, data: Union[Resource, List[Resource]], debug: bool = False) -> None:
catch_exceptions = False if debug else True
run(
self._deprecate_one,
self._deprecate_many,
data,
id_required=True,
required_synchronized=True,
catch_exceptions=catch_exceptions,
catch_exceptions=not debug,
exception=DeprecationError,
monitored_status="_synchronized",
)
Expand Down Expand Up @@ -1008,10 +1006,21 @@ def _error_from_response(response: Response) -> str:
if reason:
messages.append(reason)
if details:
if isinstance(details, dict):
messages.append(json.dumps(details))
else:
messages.append(str(details))
result = details.get('result', None)
if result:
the_details = result.get("detail", None)
if the_details:
if isinstance(the_details, list):
for detail in the_details:
messages.append(f"Reason: {detail['resultMessage']} "\
f"for the shape: {detail['sourceShape']}")
elif isinstance(the_details, dict):
messages.append(f"Reason: {detail['resultMessage']} "\
f"for the shape: {detail['sourceShape']}")
else:
messages.append(str(the_details))
else:
messages.append(str(result))
return ". ".join(messages)

def build_sparql_query_statements(context: Context, *conditions) -> Tuple[List, List]:
Expand Down

0 comments on commit b27f0bf

Please sign in to comment.