aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/data/plugins/config.py
diff options
context:
space:
mode:
authorGravatar Mason Larobina <mason.larobina@gmail.com>2010-04-04 04:44:03 +0800
committerGravatar Mason Larobina <mason.larobina@gmail.com>2010-04-04 04:44:03 +0800
commitd3cbe16bf16ff63c0e3db15d645e958712bd02d8 (patch)
tree6bea7bda62f07b73329fdb136a0b907661ca356c /examples/data/plugins/config.py
parentae15d257a858fe27090f3e5798357aea2f5a76da (diff)
Huge plugin & event manager upgrades.
1. Removed unused modules 2. Re-use event handlers with identical callbacks and args. 3. Removed plugin exceptions in favour of assertions. 4. Remove useless raw_keycmd and raw_bind config vars. 5. Implemented and use `after` and `cleanup` plugin hooks (correctly) 6. EM & plugins now use the python logging module to output messages 7. Null config items are removed automatically 8. Simpler mode plugin 9. The init plugins function is called after the INSTANCE_START event 10. New optparse option to silence event echoing to stdout 11. Close instance socket on INSTANCE_EXIT before event handling 12. Caught signals are logged 13. Show times on the messages in the log file 14. Refactor bind pluin to use uzbl.bindlet directly. 15. Refactor keycmd plugin to use uzbl.keycmd directly. 16. Refactored on_event plugin to use uzbl.on_events dict over UZBLS dict 17. Refactor completion plugin to use uzbl.completion set object. 18. Modified progress plugin to use config vars instead of `@progress k = v` 19. mode_config now a defaultdict(dict) (I.e. this allows you to `uzbl.mode_config[mode][var] = value` without needing to check `mode` is in the `uzbl.mode_config` dict). 20. Removed all default mode config values. 21. Removed all `get_mode()` and `set_mode(..)` functions (and the like). 22. Setting the mode is now done via the config object directly (I.e. `uzbl.config['mode'] = 'insert'`). 23. Uses the on_set plugin to watch for 'mode' and 'default_mode' config changes. 24. Don't raise the useless NEW_ON_SET event, missing ON_SET connect. 25. Plugin and EventHandler aren't suited as dict objects. 26. Also using collections.defaultdict(list) for uzbl.handlers dict. 27. Plugin `on_set.py` allows you to attach handlers to config var changes 28. Config plugin reduced to one `uzbl.config` dict-like object. 29. Update export and connect calls in plugins. 30. The functions connect, connect_dict, export, export_dict, require, logging are exported directly to the plugin namespace. 31. Moved parse_msg into Uzbl class. 32. Generally improved comments. 33. UzblEventDaemon now an object. 34. Various variable, function & class renames.
Diffstat (limited to 'examples/data/plugins/config.py')
-rw-r--r--examples/data/plugins/config.py129
1 files changed, 61 insertions, 68 deletions
diff --git a/examples/data/plugins/config.py b/examples/data/plugins/config.py
index 4a848a3..ed2d761 100644
--- a/examples/data/plugins/config.py
+++ b/examples/data/plugins/config.py
@@ -1,97 +1,90 @@
-import re
-import types
+from re import compile
+from types import BooleanType
+from UserDict import DictMixin
-__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)
+valid_key = compile('^[A-Za-z0-9_\.]+$').match
+types = {'int': int, 'float': float, 'str': unicode}
+escape = lambda s: unicode(s).replace('\n', '\\n')
+class Config(DictMixin):
+ def __init__(self, uzbl):
+ self.uzbl = uzbl
-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.'''
+ # Create the base dict and map allowed methods to `self`.
+ self.data = data = {}
- if type(value) == types.BooleanType:
- value = int(value)
+ methods = ['__contains__', '__getitem__', '__iter__',
+ '__len__', 'get', 'has_key', 'items', 'iteritems',
+ 'iterkeys', 'itervalues', 'values']
- else:
- value = unicode(value)
+ for method in methods:
+ setattr(self, method, getattr(data, method))
- if not VALIDKEY(key):
- raise KeyError("%r" % key)
- value = escape(value)
- if '\n' in value:
- value = value.replace("\n", "\\n")
+ def __setitem__(self, key, value):
+ self.set(key, value)
- if not force:
- if config is None:
- config = get_config(uzbl)
+ def __delitem__(self, key):
+ self.set(key)
- if key in config and config[key] == value:
- return
+ def update(self, other=None, **kwargs):
+ if other is None:
+ other = {}
- uzbl.send('set %s = %s' % (key, value))
+ for (key, value) in dict(other).items() + kwargs.items():
+ self[key] = value
-class ConfigDict(dict):
- def __init__(self, uzbl):
- self._uzbl = uzbl
+ def set(self, key, value='', force=False):
+ '''Generates a `set <key> = <value>` command string to send to the
+ current uzbl instance.
- def __setitem__(self, key, value):
- '''Makes "config[key] = value" a wrapper for the set function.'''
+ Note that the config dict isn't updated by this function. The config
+ dict is only updated after a successful `VARIABLE_SET ..` event
+ returns from the uzbl instance.'''
- set(self._uzbl, key, value, config=self)
+ assert valid_key(key)
+ if type(value) == BooleanType:
+ value = int(value)
-def add_instance(uzbl, *args):
- UZBLS[uzbl] = ConfigDict(uzbl)
+ else:
+ value = escape(value)
+ if not force and key in self and self[key] == value:
+ return
-def del_instance(uzbl, *args):
- if uzbl in UZBLS:
- del uzbl
+ self.uzbl.send(u'set %s = %s' % (key, value))
-def get_config(uzbl):
- if uzbl not in UZBLS:
- add_instance(uzbl)
+def parse_set_event(uzbl, args):
+ '''Parse `VARIABLE_SET <var> <type> <value>` event and load the
+ (key, value) pair into the `uzbl.config` dict.'''
- return UZBLS[uzbl]
+ (key, type, raw_value) = (args.split(' ', 2) + ['',])[:3]
+ assert valid_key(key)
+ assert type in types
-def variable_set(uzbl, args):
- config = get_config(uzbl)
+ new_value = types[type](raw_value)
+ old_value = uzbl.config.get(key, None)
- key, type, value = list(args.split(' ', 2) + ['',])[:3]
- old = config[key] if key in config else None
- value = TYPECONVERT[type](value)
+ # Update new value.
+ uzbl.config.data[key] = new_value
- dict.__setitem__(config, key, value)
+ if old_value != new_value:
+ uzbl.event('CONFIG_CHANGED', key, new_value)
- if old != value:
- uzbl.event("CONFIG_CHANGED", key, value)
+ # Cleanup null config values.
+ if type == 'str' and not new_value:
+ del uzbl.config.data[key]
+# plugin init hook
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,
- })
+ export(uzbl, 'config', Config(uzbl))
+ connect(uzbl, 'VARIABLE_SET', parse_set_event)
+
+# plugin cleanup hook
+def cleanup(uzbl):
+ uzbl.config.data.clear()