diff options
author | Mason Larobina <mason.larobina@gmail.com> | 2009-09-06 20:41:54 +0800 |
---|---|---|
committer | Mason Larobina <mason.larobina@gmail.com> | 2009-09-06 20:41:54 +0800 |
commit | 53ad9e51bbb04c3de86046663a81a43e2aba95cd (patch) | |
tree | bb5d82440506a17feeb6f897d4ea94194caf3047 /examples/data/uzbl/scripts | |
parent | aa600e2b7611e9c81ced9b405ecd1b2dc14a1ff2 (diff) |
Config type checking, events +SOCKET_SET -COOKIE & dump config on load.
Diffstat (limited to 'examples/data/uzbl/scripts')
-rwxr-xr-x | examples/data/uzbl/scripts/event_manager.py | 48 |
1 files changed, 25 insertions, 23 deletions
diff --git a/examples/data/uzbl/scripts/event_manager.py b/examples/data/uzbl/scripts/event_manager.py index 65089c1..0332178 100755 --- a/examples/data/uzbl/scripts/event_manager.py +++ b/examples/data/uzbl/scripts/event_manager.py @@ -45,7 +45,6 @@ import sys import select import re import types -import pprint import socket from traceback import print_exc @@ -83,13 +82,13 @@ config = { # Define some globals. _VALIDSETKEY = re.compile("^[a-zA-Z][a-zA-Z0-9_]*$").match _SCRIPTNAME = os.path.basename(sys.argv[0]) - +_TYPECONVERT = {'int': int, 'float': float, 'str': str} def echo(msg): '''Prints only if the verbose flag has been set.''' if config['verbose']: - sys.stderr.write("%s: %s\n" % (_SCRIPTNAME, msg)) + sys.stdout.write("%s: %s\n" % (_SCRIPTNAME, msg)) def counter(): @@ -177,12 +176,13 @@ class PluginManager(dict): if not hasattr(plugin, 'init'): raise ImportError('plugin missing main "init" function.') - print "Loaded plugin: %r" % name - except: print_exc() self._unload_plugin(name) + if len(self.keys()): + echo("loaded plugin(s): %s" % ', '.join(self.keys())) + def reload_plugins(self): '''Unload all loaded plugins then run load_plugins() again. @@ -214,6 +214,16 @@ class UzblInstance: '''Updates the config dict and relays any changes back to the uzbl instance via the set function.''' + if type(value) == types.BooleanType: + value = int(value) + + if key in self.keys() and type(value) != type(self[key]): + raise TypeError("%r for %r" % (type(value), key)) + + else: + # All custom variables are strings. + value = "" if value is None else str(value) + self._setcmd(key, value) dict.__setitem__(self, key, value) @@ -280,8 +290,6 @@ class UzblInstance: def _init_plugins(self): '''Call the init() function in every plugin.''' - pprint.pprint(self.plugins) - for plugin in self.plugins.keys(): try: self.plugins[plugin].init(self) @@ -344,7 +352,7 @@ class UzblInstance: d = {'handler': handler, 'args': args, 'kargs': kargs} self.handlers[event][id] = d - print "Added handler:", event, d + echo("added handler for %s: %r" % (event, d)) # The unique id is returned so that the newly created event handler can # be destroyed if need be. @@ -356,7 +364,7 @@ class UzblInstance: for event in self.handlers.keys(): if id in self.handlers[event].keys(): - print "Removed handler:", self.handlers[event][id] + echo("removed handler %d" % id) del self.handlers[event][id] @@ -377,7 +385,7 @@ class UzblInstance: if not cmd: if glob in self.binds.keys(): - print "Deleted bind:", self.binds[glob] + echo("deleted bind: %r" % self.binds[glob]) del self.binds[glob] d = {'glob': glob, 'once': True, 'hasargs': True, 'cmd': cmd} @@ -394,7 +402,7 @@ class UzblInstance: d['hasargs'] = False self.binds[glob] = d - print "Added bind:", d + echo("added bind: %r" % d) def set(self, key, value): @@ -453,20 +461,19 @@ class UzblInstance: def handle_event(self, event, args): '''Handle uzbl events internally before dispatch.''' + print event, args + if event == 'VARIABLE_SET': - l = args.split(' ', 1) - if len(l) == 1: + l = args.split(' ', 2) + if len(l) == 2: l.append("") - key, value = l - dict.__setitem__(self._config, key, value) + key, type, value = l + dict.__setitem__(self._config, key, _TYPECONVERT[type](value)) elif event == 'FIFO_SET': self.fifo_socket = args - # Workaround until SOCKET_SET is implemented. - self.socket_file = args.replace("fifo", "socket") - elif event == 'SOCKET_SET': self.socket_file = args @@ -477,12 +484,10 @@ class UzblInstance: def dispatch_event(self, event, args): '''Now send the event to any event handlers added with the connect function. In other words: handle plugin's event hooks.''' - unhandled = True if event in self.handlers.keys(): for hid in self.handlers[event]: try: - unhandled = False handler = self.handlers[event][hid] print "Executing handler:", event, handler self.exc_handler(handler, args) @@ -490,9 +495,6 @@ class UzblInstance: except: print_exc() - if unhandled: - print "Unhandled event:", event, args - def exc_handler(self, d, args): '''Handle handler.''' |