aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorGravatar Mason Larobina <mason.larobina@gmail.com>2009-09-15 16:58:45 +0800
committerGravatar Mason Larobina <mason.larobina@gmail.com>2009-09-15 16:58:45 +0800
commit2166c29233835fc2e51b37fd63921c6f3bb333ca (patch)
treeeb43d12c91c8a3e071d1e5c480684f450005ea7b /examples
parent8f45d7851c39a225e51976e00308daaa92cb3194 (diff)
Moved the uzbl config dict into its own config.py plugin.
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/data/uzbl/scripts/event_manager.py59
-rw-r--r--examples/data/uzbl/scripts/plugins/config.py79
-rw-r--r--examples/data/uzbl/scripts/plugins/keycmd.py10
3 files changed, 86 insertions, 62 deletions
diff --git a/examples/data/uzbl/scripts/event_manager.py b/examples/data/uzbl/scripts/event_manager.py
index 018c066..1e97b8f 100755
--- a/examples/data/uzbl/scripts/event_manager.py
+++ b/examples/data/uzbl/scripts/event_manager.py
@@ -85,9 +85,7 @@ 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.'''
@@ -277,35 +275,11 @@ class UzblInstance(object):
# Singleton plugin manager.
plugins = None
- # Internal uzbl config dict.
- class ConfigDict(dict):
- def __init__(self, setcmd):
- self._setcmd = setcmd
-
- def __setitem__(self, key, value):
- '''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)
-
-
def __init__(self):
'''Initialise event manager.'''
# Hold functions exported by plugins.
self._exports = {}
- self._config = self.ConfigDict(self.set)
self._running = None
self._buffer = ''
@@ -338,14 +312,6 @@ class UzblInstance(object):
return object.__getattribute__(self, name)
- def _get_config(self):
- '''Return the uzbl config dictionary.'''
-
- return self._config
-
- config = property(_get_config)
-
-
def _init_plugins(self):
'''Call the init() function in every plugin and expose all exposable
functions in the plugins root namespace.'''
@@ -487,21 +453,6 @@ class UzblInstance(object):
echo('unable to find & remove handler: %r' % handler)
- def set(self, key, value):
- '''Sets key "key" with value "value" in the uzbl instance.'''
-
- # TODO: Make a real escaping function.
- escape = str
-
- if not _VALIDSETKEY(key):
- raise KeyError("%r" % key)
-
- if '\n' in value:
- raise ValueError("invalid character: \\n")
-
- self.send('set %s = %s' % (key, escape(value)))
-
-
def listen_from_fd(self, fd):
'''Polls for event messages from fd.'''
@@ -609,15 +560,7 @@ class UzblInstance(object):
if event != "GEOMETRY_CHANGED":
print event, args
- if event == 'VARIABLE_SET':
- l = args.split(' ', 2)
- if len(l) == 2:
- l.append("")
-
- key, type, value = l
- dict.__setitem__(self._config, key, _TYPECONVERT[type](value))
-
- elif event == 'FIFO_SET':
+ if event == 'FIFO_SET':
self.uzbl_fifo = args
self._flush()
diff --git a/examples/data/uzbl/scripts/plugins/config.py b/examples/data/uzbl/scripts/plugins/config.py
new file mode 100644
index 0000000..dfa920e
--- /dev/null
+++ b/examples/data/uzbl/scripts/plugins/config.py
@@ -0,0 +1,79 @@
+import re
+import types
+
+__export__ = ['set', 'get_config']
+
+_VALIDSETKEY = re.compile("^[a-zA-Z][a-zA-Z0-9_]*$").match
+_TYPECONVERT = {'int': int, 'float': float, 'str': str}
+
+UZBLS = {}
+
+
+def escape(value):
+ '''A real escaping function may be required.'''
+
+ return str(value)
+
+
+def set(uzbl, key, value):
+ '''Sends a: "set key = value" command to the uzbl instance.'''
+
+ if type(value) == types.BooleanType:
+ value = int(value)
+
+ if not _VALIDSETKEY(key):
+ raise KeyError("%r" % key)
+
+ if '\n' in value:
+ value = value.replace("\n", "\\n")
+
+ uzbl.send('set %s = %s' % (key, escape(value)))
+
+
+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]
+
+
+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)
+
+
+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}
+
+ for (event, handler) in connects.items():
+ uzbl.connect(event, handler)
diff --git a/examples/data/uzbl/scripts/plugins/keycmd.py b/examples/data/uzbl/scripts/plugins/keycmd.py
index 7d835d8..10c7b5d 100644
--- a/examples/data/uzbl/scripts/plugins/keycmd.py
+++ b/examples/data/uzbl/scripts/plugins/keycmd.py
@@ -124,19 +124,21 @@ def clear_keycmd(uzbl):
k.wasmod = True
k.modcmd = False
- uzbl.config['keycmd'] = ""
+ uzbl.get_config()['keycmd'] = ""
uzbl.event("KEYCMD_CLEAR")
def update_event(uzbl, keylet):
'''Raise keycmd/modcmd update events.'''
+ config = uzbl.get_config()
+
if keylet.modcmd:
- uzbl.config['keycmd'] = keylet.to_string()
+ config['keycmd'] = keylet.to_string()
uzbl.event("MODCMD_UPDATE", keylet)
else:
- uzbl.config['keycmd'] = keylet.cmd
+ config['keycmd'] = keylet.cmd
uzbl.event("KEYCMD_UPDATE", keylet)
@@ -265,6 +267,6 @@ def init(uzbl):
'''Connect handlers to uzbl events.'''
uzbl.connect('INSTANCE_START', add_instance)
- uzbl.connect('INSTANCE_STOP', del_instance)
+ uzbl.connect('INSTANCE_EXIT', del_instance)
uzbl.connect('KEY_PRESS', key_press)
uzbl.connect('KEY_RELEASE', key_release)