diff options
author | Mason Larobina <mason.larobina@gmail.com> | 2009-10-15 00:08:45 +0800 |
---|---|---|
committer | Mason Larobina <mason.larobina@gmail.com> | 2009-10-15 00:08:45 +0800 |
commit | fdd8d07eba9e483baf618b6d8fd928fefb8465f3 (patch) | |
tree | 26ea2f4359db95f27ec80224d7f9b13766845eb0 /examples/data/uzbl/plugins/on_event.py | |
parent | 847ddc83ccbd7964898980f3f4dd520937ea3bbe (diff) |
Added multi-instance managing to event_manager.py
1. Moved plugin directory from './examples/data/scripts/plugins' to
'./examples/data/'.
2. Broke up the plugin manager class into two small functions.
3. Removed the handler objects ability to have non-callable handlers
given that there is a perfectly good on_event.py plugin which can
do exactly the same.
4. Gave event_manager daemon abilities similar to the cookie_daemon.
5. Using pid to track the event manager daemons running status.
6. Added the ability to load plugins from multiple locations.
7. Removed all outgoing message queues as this work-around is no longer
required after the newly added --connect-socket uzbl-core ability.
8. Removed native stdin/fifo reading ability. Use socat if required.
9. Updated uzbl-browser script to load example cookie_daemon if
cookie_daemon is not in $XDG_DATA_HOME/uzbl/scripts/
10. Added a new event_manager.py launcher uzbl-daemon.
11. Updated make test-dev-browser target to test uzbl-daemon also.
12. Added init like {start|stop|restart} to the event manager.
13. Added a fourth 'list' option to {start|stop|..} to list the plugins
and dirs of each plugin that would be loaded by the event manager.
Diffstat (limited to 'examples/data/uzbl/plugins/on_event.py')
-rw-r--r-- | examples/data/uzbl/plugins/on_event.py | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/examples/data/uzbl/plugins/on_event.py b/examples/data/uzbl/plugins/on_event.py new file mode 100644 index 0000000..242f9b0 --- /dev/null +++ b/examples/data/uzbl/plugins/on_event.py @@ -0,0 +1,117 @@ +'''Plugin provides arbitrary binding of uzbl events to uzbl commands. + +Formatting options: + %@ = space separated string of the arguments + %1 = argument 1 + %2 = argument 2 + %n = argument n + +Usage: + request ON_EVENT LINK_HOVER set selected_uri = $1 + --> LINK_HOVER http://uzbl.org/ + <-- set selected_uri = http://uzbl.org/ + + request ON_EVENT CONFIG_CHANGED print Config changed: %1 = %2 + --> CONFIG_CHANGED selected_uri http://uzbl.org/ + <-- print Config changed: selected_uri = http://uzbl.org/ +''' + +import sys +import re +from event_manager import config + +__export__ = ['get_on_events', 'on_event'] + +UZBLS = {} + +def echo(msg): + if config['verbose']: + print 'on_event plugin:', msg + + +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 expand(cmd, args): + '''Replaces "%@ %1 %2 %3..." with "<all args> <arg 0> <arg 1>...".''' + + if '%@' in cmd: + cmd = cmd.replace('%@', ' '.join(map(unicode, args))) + + for (index, arg) in enumerate(args): + index += 1 + if '%%%d' % index in cmd: + cmd = cmd.replace('%%%d' % index, unicode(arg)) + + return cmd + + +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) + event = kargs['on_event'] + if event not in events: + return + + commands = events[event] + for cmd in commands: + cmd = expand(cmd, args) + uzbl.send(cmd) + + +def on_event(uzbl, event, cmd): + '''Add a new event to watch and respond to.''' + + event = event.upper() + events = get_on_events(uzbl) + if event not in events: + uzbl.connect(event, event_handler, on_event=event) + events[event] = [] + + cmds = events[event] + if cmd not in cmds: + cmds.append(cmd) + + +def parse_on_event(uzbl, args): + '''Parse ON_EVENT events and pass them to the on_event function. + + 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) + + event, cmd = split + on_event(uzbl, event, cmd) + + +def init(uzbl): + + connects = {'ON_EVENT': parse_on_event, + 'INSTANCE_START': add_instance, + 'INSTANCE_EXIT': del_instance} + + uzbl.connect_dict(connects) |