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
|
import re
import types
__export__ = ['set', 'get_config']
VALIDKEY = re.compile("^[a-zA-Z][a-zA-Z0-9_]*$").match
TYPECONVERT = {'int': int, 'float': float, 'str': unicode}
UZBLS = {}
def escape(value):
'''A real escaping function may be required.'''
return unicode(value)
def set(uzbl, key, value='', config=None, force=False):
'''Sends a: "set key = value" command to the uzbl instance. If force is
False then only send a set command if the values aren't equal.'''
if type(value) == types.BooleanType:
value = int(value)
else:
value = unicode(value)
if not VALIDKEY(key):
raise KeyError("%r" % key)
value = escape(value)
if '\n' in value:
value = value.replace("\n", "\\n")
if not force:
if config is None:
config = get_config(uzbl)
if key in config and config[key] == value:
return
uzbl.send('set %s = %s' % (key, value))
class ConfigDict(dict):
def __init__(self, uzbl):
self._uzbl = uzbl
def __setitem__(self, key, value):
'''Makes "config[key] = value" a wrapper for the set function.'''
set(self._uzbl, key, value, config=self)
def add_instance(uzbl, *args):
UZBLS[uzbl] = ConfigDict(uzbl)
def del_instance(uzbl, *args):
if uzbl in UZBLS:
del uzbl
def get_config(uzbl):
if uzbl not in UZBLS:
add_instance(uzbl)
return UZBLS[uzbl]
def variable_set(uzbl, args):
config = get_config(uzbl)
key, type, value = list(args.split(' ', 2) + ['',])[:3]
old = config[key] if key in config else None
value = TYPECONVERT[type](value)
dict.__setitem__(config, key, value)
if old != value:
uzbl.event("CONFIG_CHANGED", key, value)
def init(uzbl):
connects = {'VARIABLE_SET': variable_set,
'INSTANCE_START': add_instance,
'INSTANCE_EXIT': del_instance}
uzbl.connect_dict(connects)
|