forked from openwallet-foundation/acapy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request openwallet-foundation#1833 from dbluhm/feature/imp…
…rove-settings-types Improve typing of settings and add plugin settings object
- Loading branch information
Showing
7 changed files
with
140 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
"""Settings implementation for plugins.""" | ||
|
||
from typing import Any, Mapping, Optional | ||
|
||
from .base import BaseSettings | ||
|
||
|
||
PLUGIN_CONFIG_KEY = "plugin_config" | ||
|
||
|
||
class PluginSettings(BaseSettings): | ||
"""Retrieve immutable settings for plugins. | ||
Plugin settings should be retrieved by calling: | ||
PluginSettings.for_plugin(settings, "my_plugin", {"default": "values"}) | ||
This will extract the PLUGIN_CONFIG_KEY in "settings" and return a new | ||
PluginSettings instance. | ||
""" | ||
|
||
def __init__(self, values: Optional[Mapping[str, Any]] = None): | ||
"""Initialize a Settings object. | ||
Args: | ||
values: An optional dictionary of settings | ||
""" | ||
self._values = {} | ||
if values: | ||
self._values.update(values) | ||
|
||
def __contains__(self, index): | ||
"""Define 'in' operator.""" | ||
return index in self._values | ||
|
||
def __iter__(self): | ||
"""Iterate settings keys.""" | ||
return iter(self._values) | ||
|
||
def __len__(self): | ||
"""Fetch the length of the mapping.""" | ||
return len(self._values) | ||
|
||
def __bool__(self): | ||
"""Convert settings to a boolean.""" | ||
return True | ||
|
||
def copy(self) -> BaseSettings: | ||
"""Produce a copy of the settings instance.""" | ||
return PluginSettings(self._values) | ||
|
||
def extend(self, other: Mapping[str, Any]) -> BaseSettings: | ||
"""Merge another settings instance to produce a new instance.""" | ||
vals = self._values.copy() | ||
vals.update(other) | ||
return PluginSettings(vals) | ||
|
||
def get_value(self, *var_names: str, default: Any = None): | ||
"""Fetch a setting. | ||
Args: | ||
var_names: A list of variable name alternatives | ||
default: The default value to return if none are defined | ||
""" | ||
for k in var_names: | ||
if k in self._values: | ||
return self._values[k] | ||
return default | ||
|
||
@classmethod | ||
def for_plugin( | ||
cls, | ||
settings: BaseSettings, | ||
plugin: str, | ||
default: Optional[Mapping[str, Any]] = None, | ||
) -> "PluginSettings": | ||
"""Construct a PluginSettings object from another settings object. | ||
PLUGIN_CONFIG_KEY is read from settings. | ||
""" | ||
return cls(settings.get(PLUGIN_CONFIG_KEY, {}).get(plugin, default)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters