Skip to content

Commit

Permalink
feat!: Expose primary_key and callbacks as public attributes
Browse files Browse the repository at this point in the history
Making these attributes public enables developers to use the callbacks for parsing configuration values that do not come in through a configuration file. REST API, command line, etc.

BREAKING CHANGE: These attributes are no longer protected (have the prepended underscore). Any method relying on the protected attributes will now fail since the variables are now public.

Signed-off-by: Jason C. Nucciarone <[email protected]>
  • Loading branch information
NucciTheBoss committed Feb 5, 2024
1 parent 3c414c6 commit d22733c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
8 changes: 4 additions & 4 deletions slurmutils/editors/_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def parse_model(line: str, pocket: Union[Dict, List], model) -> None:
# Word in front of the first `=` denotes the parent configuration knob key.
option, value = token.split("=", maxsplit=1)
if hasattr(model, attr := _pascal2snake(option)):
if attr in model._callbacks and (callback := model._callbacks[attr].parse) is not None:
if attr in model.callbacks and (callback := model.callbacks[attr].parse) is not None:
holder.update({option: callback(value)})
else:
holder.update({option: value})
Expand Down Expand Up @@ -195,7 +195,7 @@ def marshal_model(
# rely on a mutable default in the function signature.
ignore = set()

if primary_key := model._primary_key:
if primary_key := model.primary_key:
attr = _pascal2snake(primary_key)
primary_value = getattr(model, attr)
data = {primary_key: primary_value, **model.dict()[primary_value]}
Expand All @@ -206,8 +206,8 @@ def marshal_model(
if option not in ignore:
if hasattr(model, attr := _pascal2snake(option)):
if (
attr in model._callbacks
and (callback := model._callbacks[attr].marshal) is not None
attr in model.callbacks
and (callback := model.callbacks[attr].marshal) is not None
):
value = callback(value)

Expand Down
4 changes: 2 additions & 2 deletions slurmutils/models/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def __repr__(self):

@property
@abstractmethod
def _primary_key(self) -> Optional[str]:
def primary_key(self) -> Optional[str]:
"""Primary key for data model.
A primary key is required for data models that have a unique identifier
Expand All @@ -245,7 +245,7 @@ def _primary_key(self) -> Optional[str]:

@property
@abstractmethod
def _callbacks(self) -> MappingProxyType:
def callbacks(self) -> MappingProxyType:
"""Store callbacks.
This map will be queried during parsing and marshalling to determine if
Expand Down
24 changes: 12 additions & 12 deletions slurmutils/models/slurm.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def __init__(self, **kwargs):
super().__init__()
self._register.update({kwargs.pop("NodeName"): {**kwargs}})

_primary_key = "NodeName"
_callbacks = MappingProxyType(
primary_key = "NodeName"
callbacks = MappingProxyType(
{
"cpu_spec_list": CommaSeparatorCallback,
"features": CommaSeparatorCallback,
Expand Down Expand Up @@ -91,8 +91,8 @@ class DownNodes(BaseModel):
the slurm.conf manpage. `man slurm.conf.5`
"""

_primary_key = None
_callbacks = MappingProxyType(
primary_key = None
callbacks = MappingProxyType(
{
"down_nodes": CommaSeparatorCallback,
"reason": ReasonCallback,
Expand All @@ -115,8 +115,8 @@ def __init__(self, **kwargs):
super().__init__()
self._register.update({kwargs.pop("FrontendName"): {**kwargs}})

_primary_key = "FrontendName"
_callbacks = MappingProxyType(
primary_key = "FrontendName"
callbacks = MappingProxyType(
{
"allow_groups": CommaSeparatorCallback,
"allow_users": CommaSeparatorCallback,
Expand Down Expand Up @@ -148,8 +148,8 @@ def __init__(self, **kwargs):
super().__init__()
self._register.update({kwargs.pop("NodeSet"): {**kwargs}})

_primary_key = "NodeSet"
_callbacks = MappingProxyType({"nodes": CommaSeparatorCallback})
primary_key = "NodeSet"
callbacks = MappingProxyType({"nodes": CommaSeparatorCallback})

node_set = property(*primary_key_descriptors())
feature = property(*_nodeset_descriptors("Feature"))
Expand All @@ -167,8 +167,8 @@ def __init__(self, **kwargs):
super().__init__()
self._register.update({kwargs.pop("PartitionName"): {**kwargs}})

_primary_key = "PartitionName"
_callbacks = MappingProxyType(
primary_key = "PartitionName"
callbacks = MappingProxyType(
{
"alloc_nodes": CommaSeparatorCallback,
"allow_accounts": CommaSeparatorCallback,
Expand Down Expand Up @@ -465,8 +465,8 @@ class SlurmConfig(BaseModel):
the slurm.conf manpage. `man slurm.conf.5`
"""

_primary_key = None
_callbacks = MappingProxyType(
primary_key = None
callbacks = MappingProxyType(
{
"acct_storage_external_host": CommaSeparatorCallback,
"acct_storage_param": SlurmDictCallback,
Expand Down
4 changes: 2 additions & 2 deletions slurmutils/models/slurmdbd.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class SlurmdbdConfig(BaseModel):
the slurmdbd.conf manpage. `man slurmdbd.conf.5`
"""

_primary_key = None
_callbacks = MappingProxyType(
primary_key = None
callbacks = MappingProxyType(
{
"auth_alt_types": CommaSeparatorCallback,
"auth_alt_parameters": SlurmDictCallback,
Expand Down

0 comments on commit d22733c

Please sign in to comment.