-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
python-ecosys/pymitter: Added legacy branch from the original project.
Signed-off-by: Oliver Maye <[email protected]>
- Loading branch information
Oliver Maye
committed
Jan 8, 2025
1 parent
e4cf095
commit 686fbff
Showing
10 changed files
with
749 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Copyright (c) 2014-2021, Marcel Rieger | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
* Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
* Neither the name of the copyright holder nor the names of its contributors | ||
may be used to endorse or promote products derived from this software without | ||
specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
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,2 @@ | ||
include pymitter.py setup.py requirements.txt README.md LICENSE .flake8 | ||
global-exclude *.py[cod] __pycache__ |
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,183 @@ | ||
# pymitter | ||
|
||
This is a fork of the [original pymitter project](https://pypi.org/project/pymitter/) by Marcel Rieger. | ||
Sources are from the legacy/py2 branch which is a frozen v0.3.2 of that project. | ||
At this state, the implementation is compatible to Python >= v2.7 including | ||
MicroPython with a language level v3.4. | ||
|
||
Later versions of that project make use of type hints, which were introduced | ||
in Python 3.5. Type hints are currently not supported by MicroPython. | ||
|
||
|
||
## Features | ||
|
||
- Namespaces with wildcards | ||
- Times to listen (TTL) | ||
- Usage via decorators or callbacks | ||
- Lightweight implementation, good performance | ||
|
||
|
||
## Installation | ||
|
||
*pymitter* is a registered [MicroPython module](https://github.com/olimaye/micropython-lib), | ||
so the installation with *mip* is quite easy: | ||
|
||
```console | ||
mpremote mip install pymitter | ||
``` | ||
|
||
|
||
## Examples | ||
|
||
### Basic usage | ||
|
||
```python | ||
from pymitter import EventEmitter | ||
|
||
|
||
ee = EventEmitter() | ||
|
||
|
||
# decorator usage | ||
@ee.on("myevent") | ||
def handler1(arg): | ||
print("handler1 called with", arg) | ||
|
||
|
||
# callback usage | ||
def handler2(arg): | ||
print("handler2 called with", arg) | ||
|
||
|
||
ee.on("myotherevent", handler2) | ||
|
||
|
||
# emit | ||
ee.emit("myevent", "foo") | ||
# -> "handler1 called with foo" | ||
|
||
ee.emit("myotherevent", "bar") | ||
# -> "handler2 called with bar" | ||
``` | ||
|
||
|
||
### TTL (times to listen) | ||
|
||
```python | ||
from pymitter import EventEmitter | ||
|
||
|
||
ee = EventEmitter() | ||
|
||
|
||
@ee.once("myevent") | ||
def handler1(): | ||
print("handler1 called") | ||
|
||
|
||
@ee.on("myevent", ttl=10) | ||
def handler2(): | ||
print("handler2 called") | ||
|
||
|
||
ee.emit("myevent") | ||
# -> "handler1 called" | ||
# -> "handler2 called" | ||
|
||
ee.emit("myevent") | ||
# -> "handler2 called" | ||
``` | ||
|
||
|
||
### Wildcards | ||
|
||
```python | ||
from pymitter import EventEmitter | ||
|
||
|
||
ee = EventEmitter(wildcard=True) | ||
|
||
|
||
@ee.on("myevent.foo") | ||
def handler1(): | ||
print("handler1 called") | ||
|
||
|
||
@ee.on("myevent.bar") | ||
def handler2(): | ||
print("handler2 called") | ||
|
||
|
||
@ee.on("myevent.*") | ||
def hander3(): | ||
print("handler3 called") | ||
|
||
|
||
ee.emit("myevent.foo") | ||
# -> "handler1 called" | ||
# -> "handler3 called" | ||
|
||
ee.emit("myevent.bar") | ||
# -> "handler2 called" | ||
# -> "handler3 called" | ||
|
||
ee.emit("myevent.*") | ||
# -> "handler1 called" | ||
# -> "handler2 called" | ||
# -> "handler3 called" | ||
``` | ||
|
||
## API | ||
|
||
|
||
### ``EventEmitter(wildcard=False, delimiter=".", new_listener=False, max_listeners=-1)`` | ||
|
||
EventEmitter constructor. **Note**: always use *kwargs* for configuration. When *wildcard* is | ||
*True*, wildcards are used as shown in [this example](#wildcards). *delimiter* is used to seperate | ||
namespaces within events. If *new_listener* is *True*, the *"new_listener"* event is emitted every | ||
time a new listener is registered. Functions listening to this event are passed | ||
``(func, event=None)``. *max_listeners* defines the maximum number of listeners per event. Negative | ||
values mean infinity. | ||
|
||
- #### ``on(event, func=None, ttl=-1)`` | ||
Registers a function to an event. When *func* is *None*, decorator usage is assumed. *ttl* | ||
defines the times to listen. Negative values mean infinity. Returns the function. | ||
|
||
- #### ``once(event, func=None)`` | ||
Registers a function to an event with ``ttl = 1``. When *func* is *None*, decorator usage is | ||
assumed. Returns the function. | ||
|
||
- #### ``on_any(func=None)`` | ||
Registers a function that is called every time an event is emitted. When *func* is *None*, | ||
decorator usage is assumed. Returns the function. | ||
|
||
- #### ``off(event, func=None)`` | ||
Removes a function that is registered to an event. When *func* is *None*, decorator usage is | ||
assumed. Returns the function. | ||
|
||
- #### ``off_any(func=None)`` | ||
Removes a function that was registered via ``on_any()``. When *func* is *None*, decorator usage | ||
is assumed. Returns the function. | ||
|
||
- #### ``off_all()`` | ||
Removes all functions of all events. | ||
|
||
- #### ``listeners(event)`` | ||
Returns all functions that are registered to an event. Wildcards are not applied. | ||
|
||
- #### ``listeners_any()`` | ||
Returns all functions that were registered using ``on_any()``. | ||
|
||
- #### ``listeners_all()`` | ||
Returns all registered functions. | ||
|
||
- #### ``emit(event, *args, **kwargs)`` | ||
Emits an event. All functions of events that match *event* are invoked with *args* and *kwargs* | ||
in the exact order of their registeration. Wildcards might be applied. | ||
|
||
|
||
## Development | ||
|
||
- Source hosted at [GitHub](https://github.com/riga/pymitter) | ||
- Python module hostet at [PyPI](https://pypi.python.org/pypi/pymitter) | ||
- Report issues, questions, feature requests on [GitHub Issues](https://github.com/riga/pymitter/issues) |
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,53 @@ | ||
# coding: utf-8 | ||
|
||
# python imports | ||
import os | ||
import sys | ||
from pymitter import EventEmitter | ||
|
||
|
||
# create an EventEmitter instance | ||
ee = EventEmitter(wildcard=True, new_listener=True, max_listeners=-1) | ||
|
||
|
||
@ee.on("new_listener") | ||
def on_new(func, event=None): | ||
print("added listener", event, func) | ||
|
||
|
||
@ee.on("foo") | ||
def handler_foo1(arg): | ||
print("foo handler 1 called with", arg) | ||
|
||
|
||
@ee.on("foo") | ||
def handler_foo2(arg): | ||
print("foo handler 2 called with", arg) | ||
|
||
|
||
@ee.on("foo.*", ttl=1) | ||
def handler_fooall(arg): | ||
print("foo.* handler called with", arg) | ||
|
||
|
||
@ee.on("foo.bar") | ||
def handler_foobar(arg): | ||
print("foo.bar handler called with", arg) | ||
|
||
|
||
@ee.on_any() | ||
def handler_any(*args, **kwargs): | ||
print("called every time") | ||
|
||
|
||
print("emit foo") | ||
ee.emit("foo", "test") | ||
print(10 * "-") | ||
|
||
print("emit foo.bar") | ||
ee.emit("foo.bar", "test") | ||
print(10 * "-") | ||
|
||
print("emit foo.*") | ||
ee.emit("foo.*", "test") | ||
print(10 * "-") |
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,7 @@ | ||
metadata( | ||
description="Event subscription and publishing tools.", | ||
version="0.3.2", | ||
pypi="pymitter", | ||
) | ||
|
||
module("pymitter.py") |
Oops, something went wrong.