The Blinker Herald includes helpers to easily emit signals using Blinker. Decorate a function or method with @blinker_herald.emit() and pre and post signals will automatically be emitted to connected handlers.
Project description
Blinker Herald
The Blinker Herald includes helpers to easily emit signals using the excelent blinker library.
Decorate a function or method with @blinker_herald.emit()
and pre and post signals will be automatically emitted to
all connected handlers.
Free software: ISC license
Documentation: https://blinker_herald.readthedocs.org.
Features
All the features provided by blinker
+ an easy decorator
@emit()to magically emit signals when your functions are called and before it returns a result.A
signalsnamespace proxy to discover the signals in your projectCustomizable for your needs
Usage
Let’s say you have a class and wants to emit a signal for a specific method:
from blinker_herald import emit
class SomeClass(object):
@emit()
def do_something(self, arg1):
# here is were magically the 'pre' signal will be sent
return 'something done'
# here is were magically the 'post' signal will be sent
using @emit decorator makes blinker_herald to emit a signal for that method
and now you can connect handlers to capture that signals
You can capture pre signal to manipulate the object:
SomeClass.do_something.pre.connect
def handle_pre(sender, signal_emitter, **kwargs):
signal_emitter.foo = 'bar'
signal_emitter.do_another_thing()
And you can also capture the post signal to log the results:
SomeClass.do_something.post.connect
def handle_post(sender, signal_emitter, result, **kwargs):
logger.info("The method {0} returned {1}".format(sender, result))
You can also use the namespace proxy blinker_herald.signals to connect
handlers to signals, the signal name is the prefix pre or post
followed by _ and the method name:
from blinker_herald import signals
@signals.pre_do_something.connect
def handle_pre(sender, signal_emitter, **kwargs):
...
If you have a lot of subclasses emitting signals with the same name and you need to capture only specific signals, you can specify that you want to listen to only one type of sender:
from blinker_herald import emit, signals, SENDER_CLASS
class BaseModel(object):
...
@emit(sender=SENDER_CLASS)
def create(self, **kwargs):
new_instance = my_project.new(self, **kwargs)
return new_instance
class One(BaseModel):
pass
class Two(BaseModel):
pass
Using SENDER_CLASS you can now connect to specific signal:
from blinker_herald import signals
@signals.post_create.connect_via(One)
def handle_post_only_for_one(sender, signal_emitter, result, **kwargs):
# sender is the class One (cls)
# signal the instance of the class One (self)
# result is the return of the method create
The above will handle the create method signal for the class One but not for the class Two
You can also be more specific about the signal you want to connect using the __ double underscore to provide method name:
from blinker_herald import signals
@signals.module_name__ClassName__post_method_name.connect
def handle_post(sender, signal_emitter, result, **kwargs):
...
The above will connect to the post signal emitted by module_name.ClassName.method_name
Credits
This software was first created by SatelliteQE team to provide signals to Robottelo and Nailgun
History
0.1.0 (2016-05-28)
First release on PyPI.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file blinker_herald-0.2.0.tar.gz.
File metadata
- Download URL: blinker_herald-0.2.0.tar.gz
- Upload date:
- Size: 42.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b1fd320aa594a5f0cd4cd2e07db97cdbbea00197fdcd38161c873864315351cd
|
|
| MD5 |
bbe974b9fe5b5a381dcbb1ae63fc35fd
|
|
| BLAKE2b-256 |
98fec3ef82a7d78ddcecff0cbae06433f0c0fd164910d5308af7594370ebfd11
|
File details
Details for the file blinker_herald-0.2.0-py2.py3-none-any.whl.
File metadata
- Download URL: blinker_herald-0.2.0-py2.py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2558c404a1ea0f62b99a167d0d8794bbc45fcd3b1e8d8bdfcab1dd1d2b1809f9
|
|
| MD5 |
11265eb654375e7a76f8ef322b22e757
|
|
| BLAKE2b-256 |
c501ec44660f2a35b3e280e2d8334fff6396657feaa2e59f9675604be586bf96
|