-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hot configuration singleton to simplify changing configs during a…
- Loading branch information
Showing
5 changed files
with
61 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from typing import Any, Dict, Optional | ||
|
||
|
||
class HotConfig: | ||
""" | ||
This class is used for any configurations that are meant to be modified | ||
while the daemon is running (hence "hot"). This is meant to be a singleton. | ||
This is meant to be used to work around invasive changes to the codebase for | ||
exploratory experiments. Longer-term features should not be implemented | ||
using this class. | ||
""" | ||
|
||
@classmethod | ||
def instance(cls) -> "HotConfig": | ||
global _INSTANCE # pylint: disable=global-statement | ||
if _INSTANCE is None: | ||
_INSTANCE = cls() | ||
return _INSTANCE | ||
|
||
def __init__(self) -> None: | ||
self._config: Dict[str, Any] = {} | ||
|
||
def set_value(self, key: str, value: Any) -> None: | ||
self._config[key] = value | ||
|
||
def get_value(self, key: str, default: Optional[Any] = None) -> Any: | ||
try: | ||
return self._config[key] | ||
except KeyError: | ||
if default is not None: | ||
return default | ||
raise | ||
|
||
|
||
_INSTANCE: Optional[HotConfig] = None |
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