1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#!/usr/bin/env python
"""
Simple Event Filtering
This example shows how you can filter events by using the ``Event.stop()``
method which prevents other event handlers listening to the event from
running. When this example is run you should only get a single line of
output "Hello World!".
.. code-block:: sh
$ ./filter.py
Hello World!
"""
from circuits import Component, Event
class hello(Event):
"""hello Event"""
class App(Component):
def hello(self, event):
"""Hello Event Handler"""
event.stop() # Stop further event processing
print('Hello World!')
def started(self, event, component):
"""
Started Event Handler
This is fired internally when your application starts up
and can be used to trigger events that only occur once
during startup.
"""
event.stop() # Stop further event processing
self.fire(hello()) # Fire a Hello event
raise SystemExit(0) # Terminate the application
# Start and "run" the system.
# We're deliberately creating two instances of ``App``
# so we can demonstrate event filtering.
app = App()
App().register(app) # 2nd App
app.run()
|