Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions simvue/api/objects/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import abc
import pathlib
import types
import typing
import inspect
import uuid
Expand Down Expand Up @@ -183,6 +184,7 @@ def __init__(
) -> None:
self._logger = logging.getLogger(f"simvue.{self.__class__.__name__}")
self._label: str = getattr(self, "_label", self.__class__.__name__.lower())
self._local: bool = _local
self._read_only: bool = _read_only
self._is_set: bool = False
self._endpoint: str = getattr(self, "_endpoint", f"{self._label}s")
Expand Down Expand Up @@ -232,7 +234,7 @@ def __init__(
if (
not self._identifier.startswith("offline_")
and self._read_only
and not _local
and not self._local
):
self._staging = self._get()

Expand Down Expand Up @@ -277,8 +279,25 @@ def _stage_to_other(self, obj_label: str, key: str, value: typing.Any) -> None:
json.dump(_staged_data, out_f, indent=2)

def _get_attribute(
self, attribute: str, *default, url: str | None = None
) -> typing.Any:
self,
attribute: str,
*,
url: str | None = None,
) -> object:
"""Retrieve an attribute for the given object.

Parameters
----------
attribute : str
name of attribute to retrieve
url : str | None, optional
alternative URL to use for retrieval.

Returns
-------
object
the attribute value
"""
# In the case where the object is read-only, staging is the data
# already retrieved from the server
_attribute_is_property: bool = attribute in self._properties
Expand All @@ -291,6 +310,8 @@ def _get_attribute(
try:
return self._staging[attribute]
except KeyError as e:
if self._local:
raise e
# If the key is not in staging, but the object is not in offline mode
# retrieve from the server and update cache instead
if not _offline_state and (
Expand All @@ -309,9 +330,6 @@ def _get_attribute(
)
return self._get(url=url)[attribute]
except KeyError as e:
if default:
return default[0]

if self._offline:
raise AttributeError(
f"A value for attribute '{attribute}' has "
Expand Down Expand Up @@ -711,8 +729,19 @@ def __str__(self) -> str:

def __repr__(self) -> str:
_out_str = f"{self.__class__.__module__}.{self.__class__.__qualname__}("
_out_str += ", ".join(
f"{property}={getattr(self, property)!r}" for property in self._properties
)
_property_values: list[str] = []

for property in self._properties:
try:
_value = getattr(self, property)
except KeyError:
continue

if isinstance(_value, types.GeneratorType):
continue

_property_values.append(f"{property}={_value!r}")

_out_str += ", ".join(_property_values)
_out_str += ")"
return _out_str