getslash / gossip Goto Github PK
View Code? Open in Web Editor NEWHooking and signaling library for python
Home Page: http://gossip.readthedocs.org/en/latest/
License: Other
Hooking and signaling library for python
Home Page: http://gossip.readthedocs.org/en/latest/
License: Other
Is it possible to use hooks on methods in a class? E.g.:
import gossip
class Foo:
def trigger(self):
gossip.trigger('this_hook')
@gossip.register('this_hook')
def triggered_by_hook(self):
print('triggered_by_hook')
foo = Foo()
foo.trigger()
There is currently TODO in the code for optimazing Group.unregister_token()
method.
Line 130 in 5a09968
(The comment will be removed from the code in my next PR).
The usage should be something like:
operation_context = gossip.define_contextback('name', args=(...), tags=..., ....)
And then for registration
@operation_context.register
@contextmanager
def my_contextmanager(arg1, arg2):
...
yield
....
Reproduction code:
import gossip
@gossip.register('hook_a')
def foo(**kwargs):
pass
gossip.define('hook_a', tags=('my', 'tags'), arg_names=('x', 'y'))
gossip.define('hook_b', tags=('my', 'tags'), arg_names=('z', 'y'))
@gossip.register('hook_b')
def bar(**kwargs):
pass
for hook in gossip.get_all_hooks():
print(f'Hook: {hook.full_name}, tags: {hook.tags}, arguments: {hook.get_argument_names()}')
The output:
Hook: hook_a, tags: ('my', 'tags'), arguments: ()
Hook: hook_b, tags: ('my', 'tags'), arguments: ('z', 'y')
hook_b
is as expected, but hook_a
should add x
& y
as arguments
The following code fails on AssertionError
:
import gossip
@gossip.register('a.b', token='token')
def foo():
pass
gossip.unregister_token('token')
assert not gossip.get_all_registrations() # <= Raise AssertionError
This would make the hook recursion resistant, meaning that it cannot be triggered while its trigger is handled.
Currently unregistering by token in the middle of a hook trigger causes hooks to be skipped
In [1]: import gossip
In [2]: gossip.define("")
Out[2]: <Hook ()>
how can I use gossip for objectmethods
>>> import gossip
>>> class MyClass(object):
... def trigger(self):
... gossip.trigger('my_hook')
... @gossip.register('my_hook')
... def my_func(self):
... print 42
...
>>> my_object = MyClass()
>>> my_object.trigger()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in trigger
File "/home/eli/qa/.venv/local/lib/python2.7/site-packages/gossip/hooks.py", line 189, in trigger
trigger_with_tags(hook_name, kwargs, None)
File "/home/eli/qa/.venv/local/lib/python2.7/site-packages/gossip/hooks.py", line 199, in trigger_with_tags
hook.trigger(kwargs or {}, tags)
File "/home/eli/qa/.venv/local/lib/python2.7/site-packages/gossip/hooks.py", line 152, in trigger
exception_policy.handle_exception(ctx, exc_info)
File "/home/eli/qa/.venv/local/lib/python2.7/site-packages/gossip/exception_policy.py", line 29, in handle_exception
reraise(exc_info[0], exc_info[1], exc_info[2])
File "/home/eli/qa/.venv/local/lib/python2.7/site-packages/gossip/hooks.py", line 169, in _call_registration
registration(**kwargs) # pylint: disable=star-args
File "/home/eli/qa/.venv/local/lib/python2.7/site-packages/gossip/registration.py", line 70, in __call__
return self.func(*args, **kwargs)
TypeError: my_func() takes exactly 1 argument (0 given)
since the self
parameter is not sent
Can you gossip python-gossip detail usage?
Thanks YOU
The code of hook registration for specific code block looks not elegant.
Currently there are two options for doing it:
hook = gossip.get_hook('my_hook')
reg = hook.register(func)
... # Code block
reg.unregister
or
goossip.register(func, 'my_hook', token='some_token')
... # Code block
gossip.unregister_token('some_token')
It would be nice to have:
with gossip.registrated(fun, 'my_hook'):
... # Code block
For example, raising an exception in a registration yields something like this:
[2014-06-16 15:16] WARNING: gossip.hooks: Exception occurred while calling <gossip.registration.Registration object at 0x7f66e37e8d50>
It would be better to attempt and describe the actual registration name (function/object)
In several cases it is necessary to register hooks in pairs (e.g. start/end or before/after of events). In some of these cases it is necessary to ensure that the "closing" hook isn't called unless the "hook" was called. This is particularly useful for plugin systems.
This feature will enable registering like this:
from gossip import Toggle
_toggle = Toggle()
@gossip.register(toggle_on=_toggle)
def before():
...
@gossip.register(toggle_off=_toggle)
def after():
...
Every project which uses gossip, should have its own implementation for listing the gossip hooks it supports.
It would be nice to have a sphinx extension which listing all the hooks of a specific gossip group for documentation.
We have a package that registers for many (different) gossip hooks.
Our package has a "disabling" context. We need a way to disable all our registrations during the context and re-enable them afterward.
Hi Rotem,
Thanks for developing Gossip. I love it!
I'm currently using it as part of a framework I'm writing, so I'm pre-defining a lot of hooks I use in different places. I always store the hook object and use it directly for registration (to have the hook name defined in only one place), but I'm hitting a wall when using arguments with these objects. I'm using strict groups.
# Works fine
myhook = gossip.define("mygroup.testhook", arg_names=["some_arg"])
@myhook.register
def some_function(some_arg):
pass
# Does not work:
myhook = gossip.define("mygroup.testhook", arg_names=["some_arg"], tags=["test"])
@myhook.register(tags=["test"])
def some_function(some_arg):
pass
I always end up with the following
TypeError: register() takes at least 2 arguments (2 given)
Do I need to wrap the register() call to get the functionality I want?
Cheers!
Tim
Originated by getslash/slash#490
Hello. Great job on this library! Very feature rich and easy to use.
I'm wondering if there's a way of specifying priority or weights when registering hooks. The provides/needs capabilities are not quite what I'm looking for. Glancing at the source code it seems there isn't a way of doing this in gossip, but maybe someone knows of a work-around?
This, and collecting all hook return values, are the only two things stopping me from completely ditching the blinker library.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.