AdaCore / AdaCore/e3-core

Implement context manager in EventManager

Open
#157 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
32
Forks
36
PR merge metrics
No merged PRs in 30d

Description

We very often need to do the following:

      send_event(e)  # notify start
      ...
      e.close()
      send_event(e)  # notify end

We could factorize such code in EventManager, e.g. with:

+++ b/e3/event/backends/base.py
@@ -1,6 +1,7 @@
 from __future__ import absolute_import, division, print_function

 import abc
+import contextlib
 import uuid

 import e3.env
@@ -79,3 +80,13 @@ class EventManager(object):
     def Event(self):
         """Return the Event class used by this EventManager."""
         pass  # all: no cover
+
+    @contextlib.contextmanager
+    def with_event(self, *args, **kwargs):
+        e = self.Event(*args, **kwargs)
+        try:
+            self.send_event(e)
+            yield e
+        finally:
+            e.close()
+            self.send_event(e)

Code not tested!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in e3/event/backends/base.py and inspect EventManager.Event and send_event. Verify the proposed context-manager behavior, including cleanup through the existing Event.close flow, and add focused tests since the issue says the code is untested; done means the start and end events are emitted around the managed block.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.