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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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()
|