Skip to content

Commit

Permalink
Finalize SettingsManager into a working state, including SettingsMana…
Browse files Browse the repository at this point in the history
…gerAsDict
  • Loading branch information
LobaDK committed Jun 28, 2024
1 parent 944fd1b commit 54e0e3a
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 443 deletions.
36 changes: 26 additions & 10 deletions settings/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@


class AutosaveProtocol(Protocol):
_autosave_on_change: bool
_autosave_enabled: bool

def enable_autosave(self) -> None:
Expand All @@ -22,40 +21,57 @@ def disable_autosave(self) -> None:

def toggle_autosave_on(func: T) -> T:
"""
A decorator that specifically toggles the autosave feature on before and off after calling the decorated function.
A decorator that specifically toggles the autosave feature on before calling the decorated function and off after the function is called.
This is useful for functions that change the settings and should autosave the settings after the function is called.
This is useful for functions that change the settings and autosaving is explicitly desired in the scope of the function call.
Be aware toggling the autosave feature can be expensive, as each nested `ChangeDetectingDict` and `ChangeDetectingList` gets updated.
The target function must be an instance method part of a class that implements `enable_autosave` and `disable_autosave` methods,
as well as two boolean attributes `_autosave_on_change` and `_autosave_enabled`.
The target function must be an instance method part of a class that implements the following methods:
- `enable_autosave` to enable the autosave feature
- `disable_autosave` to disable the autosave feature
as well as the following attributes:
- `_autosave_enabled` to keep track of the current state of the autosave feature
"""

@wraps(wrapped=func)
def wrapper(self: AutosaveProtocol, *args, **kwargs) -> None:
if self._autosave_on_change and not self._autosave_enabled:
if not self._autosave_enabled:
self.enable_autosave()

func(self, *args, **kwargs)

if self._autosave_on_change and self._autosave_enabled:
if self._autosave_enabled:
self.disable_autosave()

return cast(T, wrapper)


def toggle_autosave_off(func: T) -> T:
"""
A decorator that specifically toggles the autosave feature off before and on after calling the decorated function.
A decorator that specifically toggles the autosave feature off before calling the decorated function and on after the function is called.
This is useful for functions that change the settings and autosaving is explicitly not desired in the scope of the function call.
Be aware toggling the autosave feature can be expensive, as each nested `ChangeDetectingDict` and `ChangeDetectingList` gets updated.
The target function must be an instance method part of a class that implements the following methods:
- `enable_autosave` to enable the autosave feature
- `disable_autosave` to disable the autosave feature
as well as the following attributes:
- `_autosave_enabled` to keep track of the current state of the autosave feature
"""

@wraps(wrapped=func)
def wrapper(self: AutosaveProtocol, *args, **kwargs) -> None:
if self._autosave_on_change and self._autosave_enabled:
if self._autosave_enabled:
self.disable_autosave()

func(self, *args, **kwargs)

if self._autosave_on_change and not self._autosave_enabled:
if self._autosave_enabled:
self.enable_autosave()

return cast(T, wrapper)
Loading

0 comments on commit 54e0e3a

Please sign in to comment.