aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/data/plugins/on_set.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_set.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_set.py')
-rw-r--r--examples/data/plugins/on_set.py92
1 files changed, 92 insertions, 0 deletions
diff --git a/examples/data/plugins/on_set.py b/examples/data/plugins/on_set.py
new file mode 100644
index 0000000..130b816
--- /dev/null
+++ b/examples/data/plugins/on_set.py
@@ -0,0 +1,92 @@
+from re import compile
+from functools import partial
+
+valid_glob = compile('^[A-Za-z0-9_\*\.]+$').match
+
+def make_matcher(glob):
+ '''Make matcher function from simple glob.'''
+
+ pattern = "^%s$" % glob.replace('*', '[^\s]*')
+ return compile(pattern).match
+
+
+def exec_handlers(uzbl, handlers, key, arg):
+ '''Execute the on_set handlers that matched the key.'''
+
+ for handler in handlers:
+ if callable(handler):
+ handler(key, arg)
+
+ else:
+ uzbl.send(uzbl.cmd_expand(handler, [key, arg]))
+
+
+def check_for_handlers(uzbl, key, arg):
+ '''Check for handlers for the current key.'''
+
+ for (matcher, handlers) in uzbl.on_sets.values():
+ if matcher(key):
+ exec_handlers(uzbl, handlers, key, arg)
+
+
+def on_set(uzbl, glob, handler, prepend=True):
+ '''Add a new handler for a config key change.
+
+ Structure of the `uzbl.on_sets` dict:
+ { glob : ( glob matcher function, handlers list ), .. }
+ '''
+
+ assert valid_glob(glob)
+
+ while '**' in glob:
+ glob = glob.replace('**', '*')
+
+ if callable(handler):
+ orig_handler = handler
+ if prepend:
+ handler = partial(handler, uzbl)
+
+ else:
+ orig_handler = handler = unicode(handler)
+
+ if glob in uzbl.on_sets:
+ (matcher, handlers) = uzbl.on_sets[glob]
+ handlers.append(handler)
+
+ else:
+ matcher = make_matcher(glob)
+ uzbl.on_sets[glob] = (matcher, [handler,])
+
+ uzbl.logger.info('on set %r call %r' % (glob, orig_handler))
+
+
+def parse_on_set(uzbl, args):
+ '''Parse `ON_SET <glob> <command>` event then pass arguments to the
+ `on_set(..)` function.'''
+
+ (glob, command) = (args.split(' ', 1) + [None,])[:2]
+ assert glob and command and valid_glob(glob)
+ on_set(uzbl, glob, command)
+
+
+# plugins init hook
+def init(uzbl):
+ require('config')
+ require('cmd_expand')
+
+ export_dict(uzbl, {
+ 'on_sets': {},
+ 'on_set': on_set,
+ })
+
+ connect_dict(uzbl, {
+ 'ON_SET': parse_on_set,
+ 'CONFIG_CHANGED': check_for_handlers,
+ })
+
+# plugins cleanup hook
+def cleanup(uzbl):
+ for (matcher, handlers) in uzbl.on_sets.values():
+ del handlers[:]
+
+ uzbl.on_sets.clear()