aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/data/plugins/config.py
diff options
context:
space:
mode:
authorGravatar Dieter Plaetinck <dieter@plaetinck.be>2010-01-02 20:11:15 +0100
committerGravatar Dieter Plaetinck <dieter@plaetinck.be>2010-01-02 20:11:15 +0100
commit9dd1370d0b7cd876f004f7a822b0357039252184 (patch)
treebf9fcf6c6b2332cf70948d225000ee1a0d77c0ea /examples/data/plugins/config.py
parent02995443bc8af38fc3bb3896c8d32eb0adc142d0 (diff)
remove the 'uzbl' subdirectory in examples/*/, since the sandbox they are no longer needed + update paths everywhere + remove examples/config/enchant (also not needed since sandbox) + bugfix: set /home/dieter in sandbox
Diffstat (limited to 'examples/data/plugins/config.py')
-rw-r--r--examples/data/plugins/config.py97
1 files changed, 97 insertions, 0 deletions
diff --git a/examples/data/plugins/config.py b/examples/data/plugins/config.py
new file mode 100644
index 0000000..4a848a3
--- /dev/null
+++ b/examples/data/plugins/config.py
@@ -0,0 +1,97 @@
+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):
+ # Event handling hooks.
+ uzbl.connect_dict({
+ 'INSTANCE_EXIT': del_instance,
+ 'INSTANCE_START': add_instance,
+ 'VARIABLE_SET': variable_set,
+ })
+
+ # Function exports to the uzbl object, `function(uzbl, *args, ..)`
+ # becomes `uzbl.function(*args, ..)`.
+ uzbl.export_dict({
+ 'get_config': get_config,
+ 'set': set,
+ })