Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep attached events #85

Merged
merged 2 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
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
18 changes: 13 additions & 5 deletions src/miniflask/miniflask.py
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,7 @@ def register_default_module(self, module, **kwargs):

super().register_default_module(module, **kwargs)

def unregister_event(self, name: str, only_cache: bool = False):
def unregister_event(self, name: str, only_cache: bool = False, keep_attached_events: bool = True):
r"""
Clears an event by name.

Expand All @@ -1350,14 +1350,15 @@ def unregister_event(self, name: str, only_cache: bool = False):
- `only_cache`:
- Setting to `True` means that the event cache will be cleared. Upon the next call of `event.name` the cache will be rebuild.
- Setting to `False` means that the event cache will be cleared *and* the internal event objects will be removed as well. Upon the next call of `event.name` miniflask will not recognize the event anymore.
- `keep_attached_events`: By default (`True`), all events that are called together with this event (`before_`/`after_`-events) are kept. Setting to `False` clears those as well.
""" # noqa: W291
if hasattr(self.event, name):
delattr(self.event, name)
if not only_cache and name in self.event_objs:
del self.event_objs[name]
if "before_" + name in self.event_objs:
if not keep_attached_events and "before_" + name in self.event_objs:
self.unregister_event("before_" + name, only_cache)
if "after_" + name in self.event_objs:
if not keep_attached_events and "after_" + name in self.event_objs:
self.unregister_event("after_" + name, only_cache)

# define event
Expand All @@ -1374,14 +1375,21 @@ def register_event(self, name: str, fn, **kwargs):
super().register_event(name, fn, **kwargs)

# overwrite event definition
def overwrite_event(self, name: str, fn, **kwargs):
def overwrite_event(self, name: str, fn, keep_attached_events: bool = True, **kwargs):
r"""
Unregister an existing event & redefine it using another function.

**Note**:
The main difference between this function and `register_event` is that this method clears the cache *and* removes the event internally, while `register_event` only clears the cache. (This is especially important if one uses non-unique events).

Args:
- `name`: The event to clear from the event object.
- `only_cache`:
- Setting to `True` means that the event cache will be cleared. Upon the next call of `event.name` the cache will be rebuild.
- Setting to `False` means that the event cache will be cleared *and* the internal event objects will be removed as well. Upon the next call of `event.name` miniflask will not recognize the event anymore.
- `keep_attached_events`: By default (`True`), all events that are called together with this event (`before_`/`after_`-events) are kept. Setting to `False` clears those as well.
""" # noqa: W291
self.unregister_event(name, only_cache=False)
self.unregister_event(name, only_cache=False, keep_attached_events=keep_attached_events)
self._defined_events[name] = fn
super().register_event(name, fn, **kwargs)

Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

def main():
print("overwritten main event and removed attached as well")


def register(mf):
mf.overwrite_event('main', main, keep_attached_events=False)
10 changes: 10 additions & 0 deletions tests/event/overwrite_event/modules/module1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,15 @@ def main():
print("main event")


def before_main():
print("before main event")


def after_main():
print("after main event")


def register(mf):
mf.register_event('main', main, unique=False)
mf.register_event('before_main', before_main, unique=False)
mf.register_event('after_main', after_main, unique=False)
24 changes: 24 additions & 0 deletions tests/event/overwrite_event/test_overwrite_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ def test_overwrite_setup(capsys):
mf.event.main()
captured = capsys.readouterr()
assert captured.out == """
before main event
main event
after main event
""".lstrip()


Expand All @@ -30,7 +32,9 @@ def test_overwrite(capsys):
mf.event.main()
captured = capsys.readouterr()
assert captured.out == """
before main event
overwritten main event
after main event
""".lstrip()


Expand All @@ -48,7 +52,27 @@ def test_overwrite_during_event(capsys):
mf.event.main()
captured = capsys.readouterr()
assert captured.out == """
before main event
main event
after main event
overwrites event during init-event
before main event
overwritten main event during event
after main event
""".lstrip()


def test_overwrite_with_attached(capsys):
mf = miniflask.init(
module_dirs=str(Path(__file__).parent / "modules"),
debug=False
)

mf.load(["module1", "main_overwrite_with_attached"])
mf.parse_args([])
captured = capsys.readouterr()
mf.event.main()
captured = capsys.readouterr()
assert captured.out == """
overwritten main event and removed attached as well
""".lstrip()