aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/data/plugins/on_event.py
diff options
context:
space:
mode:
authorGravatar Mason Larobina <mason.larobina@gmail.com>2010-04-04 04:44:03 +0800
committerGravatar Mason Larobina <mason.larobina@gmail.com>2010-04-04 04:44:03 +0800
commitd3cbe16bf16ff63c0e3db15d645e958712bd02d8 (patch)
tree6bea7bda62f07b73329fdb136a0b907661ca356c /examples/data/plugins/on_event.py
parentae15d257a858fe27090f3e5798357aea2f5a76da (diff)
Huge plugin & event manager upgrades.
1. Removed unused modules 2. Re-use event handlers with identical callbacks and args. 3. Removed plugin exceptions in favour of assertions. 4. Remove useless raw_keycmd and raw_bind config vars. 5. Implemented and use `after` and `cleanup` plugin hooks (correctly) 6. EM & plugins now use the python logging module to output messages 7. Null config items are removed automatically 8. Simpler mode plugin 9. The init plugins function is called after the INSTANCE_START event 10. New optparse option to silence event echoing to stdout 11. Close instance socket on INSTANCE_EXIT before event handling 12. Caught signals are logged 13. Show times on the messages in the log file 14. Refactor bind pluin to use uzbl.bindlet directly. 15. Refactor keycmd plugin to use uzbl.keycmd directly. 16. Refactored on_event plugin to use uzbl.on_events dict over UZBLS dict 17. Refactor completion plugin to use uzbl.completion set object. 18. Modified progress plugin to use config vars instead of `@progress k = v` 19. mode_config now a defaultdict(dict) (I.e. this allows you to `uzbl.mode_config[mode][var] = value` without needing to check `mode` is in the `uzbl.mode_config` dict). 20. Removed all default mode config values. 21. Removed all `get_mode()` and `set_mode(..)` functions (and the like). 22. Setting the mode is now done via the config object directly (I.e. `uzbl.config['mode'] = 'insert'`). 23. Uses the on_set plugin to watch for 'mode' and 'default_mode' config changes. 24. Don't raise the useless NEW_ON_SET event, missing ON_SET connect. 25. Plugin and EventHandler aren't suited as dict objects. 26. Also using collections.defaultdict(list) for uzbl.handlers dict. 27. Plugin `on_set.py` allows you to attach handlers to config var changes 28. Config plugin reduced to one `uzbl.config` dict-like object. 29. Update export and connect calls in plugins. 30. The functions connect, connect_dict, export, export_dict, require, logging are exported directly to the plugin namespace. 31. Moved parse_msg into Uzbl class. 32. Generally improved comments. 33. UzblEventDaemon now an object. 34. Various variable, function & class renames.
Diffstat (limited to 'examples/data/plugins/on_event.py')
-rw-r--r--examples/data/plugins/on_event.py69
1 files changed, 22 insertions, 47 deletions
diff --git a/examples/data/plugins/on_event.py b/examples/data/plugins/on_event.py
index b9c504a..5142275 100644
--- a/examples/data/plugins/on_event.py
+++ b/examples/data/plugins/on_event.py
@@ -20,36 +20,11 @@ Usage:
import sys
import re
-__export__ = ['get_on_events', 'on_event']
-
-UZBLS = {}
-
-
-def error(msg):
- sys.stderr.write('on_event plugin: error: %s\n' % msg)
-
-
-def add_instance(uzbl, *args):
- UZBLS[uzbl] = {}
-
-
-def del_instance(uzbl, *args):
- if uzbl in UZBLS:
- del UZBLS[uzbl]
-
-
-def get_on_events(uzbl):
- if uzbl not in UZBLS:
- add_instance(uzbl)
-
- return UZBLS[uzbl]
-
-
def event_handler(uzbl, *args, **kargs):
'''This function handles all the events being watched by various
on_event definitions and responds accordingly.'''
- events = get_on_events(uzbl)
+ events = uzbl.on_events
event = kargs['on_event']
if event not in events:
return
@@ -65,9 +40,9 @@ def on_event(uzbl, event, cmd):
'''Add a new event to watch and respond to.'''
event = event.upper()
- events = get_on_events(uzbl)
+ events = uzbl.on_events
if event not in events:
- uzbl.connect(event, event_handler, on_event=event)
+ connect(uzbl, event, event_handler, on_event=event)
events[event] = []
cmds = events[event]
@@ -80,28 +55,28 @@ def parse_on_event(uzbl, args):
Syntax: "event ON_EVENT <EVENT_NAME> commands".'''
- if not args:
- return error("missing on_event arguments")
-
- split = args.split(' ', 1)
- if len(split) != 2:
- return error("invalid ON_EVENT syntax: %r" % args)
+ args = args.strip()
+ assert args, 'missing on event arguments'
- event, cmd = split
- on_event(uzbl, event, cmd)
+ (event, command) = (args.split(' ', 1) + ['',])[:2]
+ assert event and command, 'missing on event command'
+ on_event(uzbl, event, command)
+# plugin init hook
def init(uzbl):
- # Event handling hooks.
- uzbl.connect_dict({
- 'INSTANCE_EXIT': del_instance,
- 'INSTANCE_START': add_instance,
- 'ON_EVENT': parse_on_event,
- })
+ '''Export functions and connect handlers to events.'''
+
+ connect(uzbl, 'ON_EVENT', parse_on_event)
- # Function exports to the uzbl object, `function(uzbl, *args, ..)`
- # becomes `uzbl.function(*args, ..)`.
- uzbl.export_dict({
- 'get_on_events': get_on_events,
- 'on_event': on_event,
+ export_dict(uzbl, {
+ 'on_event': on_event,
+ 'on_events': {},
})
+
+# plugin cleanup hook
+def cleanup(uzbl):
+ for handlers in uzbl.on_events.values():
+ del handlers[:]
+
+ uzbl.on_events.clear()