From 713c9bb02b9bab9956f8e5439a0d47e7dedfaf24 Mon Sep 17 00:00:00 2001 From: keis Date: Sat, 15 Jan 2011 22:16:42 +0100 Subject: grok quoted strings in a select places in the em --- examples/data/plugins/config.py | 10 +++++++++- examples/data/plugins/cookies.py | 11 +++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/data/plugins/config.py b/examples/data/plugins/config.py index ed2d761..dde4ae1 100644 --- a/examples/data/plugins/config.py +++ b/examples/data/plugins/config.py @@ -2,8 +2,16 @@ from re import compile from types import BooleanType from UserDict import DictMixin +_unquote = compile("'(.*?)'|\"(.*?)\"") +def unquote(s): + m = _unquote.match(s) + if m is not None: + return unicode(m.group(1)).decode('string_escape') + return unicode(s).decode('string_escape') + + valid_key = compile('^[A-Za-z0-9_\.]+$').match -types = {'int': int, 'float': float, 'str': unicode} +types = {'int': int, 'float': float, 'str': unquote} escape = lambda s: unicode(s).replace('\n', '\\n') class Config(DictMixin): diff --git a/examples/data/plugins/cookies.py b/examples/data/plugins/cookies.py index 6ee8798..05a8901 100644 --- a/examples/data/plugins/cookies.py +++ b/examples/data/plugins/cookies.py @@ -7,9 +7,16 @@ import os, re # these are symbolic names for the components of the cookie tuple symbolic = {'domain': 0, 'path':1, 'name':2, 'value':3, 'scheme':4, 'expires':5} -_splitquoted = re.compile("( |\\\".*?\\\"|'.*?')") +_unquote = re.compile("'(.*?)'|\"(.*?)\"") +def unquote(s): + m = _unquote.match(s) + if m is not None: + return unicode(m.group(1)).decode('string_escape') + return unicode(s).decode('string_escape') + +_splitquoted = re.compile("( |\"(?:\\\\.|[^\"])*?\"|'(?:\\\\.|[^'])*?')") def splitquoted(text): - return [str(p.strip('\'"')) for p in _splitquoted.split(text) if p.strip()] + return [unquote(p) for p in _splitquoted.split(text) if p.strip()] # allows for partial cookies # ? allow wildcard in key -- cgit v1.2.3 From 3594f35e565ebf210b583bcc3c4df042e0dc4191 Mon Sep 17 00:00:00 2001 From: keis Date: Sun, 23 Jan 2011 21:57:16 +0100 Subject: move common function related to escaping to Plugin --- examples/data/plugins/bind.py | 9 --------- examples/data/plugins/config.py | 14 +++----------- examples/data/plugins/cookies.py | 11 ----------- examples/data/scripts/uzbl-event-manager | 20 +++++++++++++++++++- 4 files changed, 22 insertions(+), 32 deletions(-) (limited to 'examples') diff --git a/examples/data/plugins/bind.py b/examples/data/plugins/bind.py index 41f96c5..69fd863 100644 --- a/examples/data/plugins/bind.py +++ b/examples/data/plugins/bind.py @@ -164,15 +164,6 @@ def split_glob(glob): return (mods, glob) -def unquote(str): - '''Remove quotation marks around string.''' - - if str and str[0] == str[-1] and str[0] in ['"', "'"]: - str = str[1:-1] - - return str - - class Bind(object): # Class attribute to hold the number of Bind classes created. diff --git a/examples/data/plugins/config.py b/examples/data/plugins/config.py index dde4ae1..b7ecf42 100644 --- a/examples/data/plugins/config.py +++ b/examples/data/plugins/config.py @@ -2,17 +2,7 @@ from re import compile from types import BooleanType from UserDict import DictMixin -_unquote = compile("'(.*?)'|\"(.*?)\"") -def unquote(s): - m = _unquote.match(s) - if m is not None: - return unicode(m.group(1)).decode('string_escape') - return unicode(s).decode('string_escape') - - valid_key = compile('^[A-Za-z0-9_\.]+$').match -types = {'int': int, 'float': float, 'str': unquote} -escape = lambda s: unicode(s).replace('\n', '\\n') class Config(DictMixin): def __init__(self, uzbl): @@ -57,7 +47,7 @@ class Config(DictMixin): value = int(value) else: - value = escape(value) + value = value.encode('unicode_escape') if not force and key in self and self[key] == value: return @@ -90,6 +80,8 @@ def parse_set_event(uzbl, args): # plugin init hook def init(uzbl): + global types + types = {'int': int, 'float': float, 'str': unquote} export(uzbl, 'config', Config(uzbl)) connect(uzbl, 'VARIABLE_SET', parse_set_event) diff --git a/examples/data/plugins/cookies.py b/examples/data/plugins/cookies.py index 05a8901..e29ee36 100644 --- a/examples/data/plugins/cookies.py +++ b/examples/data/plugins/cookies.py @@ -7,17 +7,6 @@ import os, re # these are symbolic names for the components of the cookie tuple symbolic = {'domain': 0, 'path':1, 'name':2, 'value':3, 'scheme':4, 'expires':5} -_unquote = re.compile("'(.*?)'|\"(.*?)\"") -def unquote(s): - m = _unquote.match(s) - if m is not None: - return unicode(m.group(1)).decode('string_escape') - return unicode(s).decode('string_escape') - -_splitquoted = re.compile("( |\"(?:\\\\.|[^\"])*?\"|'(?:\\\\.|[^'])*?')") -def splitquoted(text): - return [unquote(p) for p in _splitquoted.split(text) if p.strip()] - # allows for partial cookies # ? allow wildcard in key def match(key, cookie): diff --git a/examples/data/scripts/uzbl-event-manager b/examples/data/scripts/uzbl-event-manager index 8ad3af7..d47318a 100755 --- a/examples/data/scripts/uzbl-event-manager +++ b/examples/data/scripts/uzbl-event-manager @@ -34,6 +34,7 @@ import socket import sys import time import weakref +import re from collections import defaultdict from functools import partial from glob import glob @@ -169,13 +170,16 @@ class EventHandler(object): self.callback(uzbl, *args, **kwargs) + + + class Plugin(object): '''Plugin module wrapper object.''' # Special functions exported from the Plugin instance to the # plugin namespace. special_functions = ['require', 'export', 'export_dict', 'connect', - 'connect_dict', 'logger'] + 'connect_dict', 'logger', 'unquote', 'splitquoted'] def __init__(self, parent, name, path, plugin): @@ -291,6 +295,20 @@ class Plugin(object): assert plugin in self.parent.plugins, self.logger.critical( 'plugin %r required by plugin %r' (plugin, self.name)) + @classmethod + def unquote(cls, s): + '''Removes quotation marks around strings if any and interprets + \\-escape sequences using `unicode_escape`''' + if s and s[0] == s[-1] and s[0] in ['"', "'"]: + s = s[1:-1] + return unicode(s).decode('unicode_escape') + + _splitquoted = re.compile("( |\"(?:\\\\.|[^\"])*?\"|'(?:\\\\.|[^'])*?')") + @classmethod + def splitquoted(cls, text): + '''Splits string on whitespace while respecting quotations''' + return [cls.unquote(p) for p in cls._splitquoted.split(text) if p.strip()] + class Uzbl(object): def __init__(self, parent, child_socket): -- cgit v1.2.3 From e2e1f6203fa8e9bdbae567cfbec1ee341c10282b Mon Sep 17 00:00:00 2001 From: keis Date: Mon, 24 Jan 2011 18:59:17 +0100 Subject: don't escape in Config.set escapes should be done by caller checks for plain newline as these would break stuff --- examples/data/plugins/config.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/data/plugins/config.py b/examples/data/plugins/config.py index b7ecf42..c9bdf67 100644 --- a/examples/data/plugins/config.py +++ b/examples/data/plugins/config.py @@ -47,7 +47,8 @@ class Config(DictMixin): value = int(value) else: - value = value.encode('unicode_escape') + value = unicode(value) + assert '\n' not in value if not force and key in self and self[key] == value: return -- cgit v1.2.3 From 70b9ba7440c5164c90f61ddf4d988f32019a5ecc Mon Sep 17 00:00:00 2001 From: keis Date: Mon, 7 Feb 2011 11:11:41 +0100 Subject: use string_escape to remove \-escapes unicode_escape fails on non-ascii chars. --- examples/data/scripts/uzbl-event-manager | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/data/scripts/uzbl-event-manager b/examples/data/scripts/uzbl-event-manager index d47318a..cb462c7 100755 --- a/examples/data/scripts/uzbl-event-manager +++ b/examples/data/scripts/uzbl-event-manager @@ -298,10 +298,10 @@ class Plugin(object): @classmethod def unquote(cls, s): '''Removes quotation marks around strings if any and interprets - \\-escape sequences using `unicode_escape`''' + \\-escape sequences using `string_escape`''' if s and s[0] == s[-1] and s[0] in ['"', "'"]: s = s[1:-1] - return unicode(s).decode('unicode_escape') + return s.encode('utf-8').decode('string_escape').decode('utf-8') _splitquoted = re.compile("( |\"(?:\\\\.|[^\"])*?\"|'(?:\\\\.|[^'])*?')") @classmethod @@ -987,3 +987,5 @@ if __name__ == "__main__": daemon_actions[action]() logger.debug('process CPU time: %f' % time.clock()) + +# vi: set et ts=4: -- cgit v1.2.3 From 3cb9af3b4457f5c6aec1eae2acf552aad8a3e6db Mon Sep 17 00:00:00 2001 From: keis Date: Sun, 23 Jan 2011 22:06:33 +0100 Subject: support quoted events in a few more places * downloads * on_event handlers --- examples/data/plugins/downloads.py | 18 +++++++++++++----- examples/data/plugins/on_event.py | 6 ++++++ 2 files changed, 19 insertions(+), 5 deletions(-) (limited to 'examples') diff --git a/examples/data/plugins/downloads.py b/examples/data/plugins/downloads.py index 7bf32d7..8d796ce 100644 --- a/examples/data/plugins/downloads.py +++ b/examples/data/plugins/downloads.py @@ -31,7 +31,11 @@ def update_download_section(uzbl): if uzbl.config.get('downloads', '') != result: uzbl.config['downloads'] = result -def download_started(uzbl, destination_path): +def download_started(uzbl, args): + # parse the arguments + args = splitquoted(args) + destination_path = args[0] + # add to the list of active downloads global ACTIVE_DOWNLOADS ACTIVE_DOWNLOADS[destination_path] = (0.0,) @@ -41,9 +45,9 @@ def download_started(uzbl, destination_path): def download_progress(uzbl, args): # parse the arguments - s = args.rindex(' ') - destination_path = args[:s] - progress = float(args[s+1:]) + args = splitquoted(args) + destination_path = args[0] + progress = float(args[1]) # update the progress global ACTIVE_DOWNLOADS @@ -52,7 +56,11 @@ def download_progress(uzbl, args): # update the status bar variable update_download_section(uzbl) -def download_complete(uzbl, destination_path): +def download_complete(uzbl, args): + # parse the arguments + args = splitquoted(args) + destination_path = args[0] + # remove from the list of active downloads global ACTIVE_DOWNLOADS del ACTIVE_DOWNLOADS[destination_path] diff --git a/examples/data/plugins/on_event.py b/examples/data/plugins/on_event.py index 5142275..32f09e2 100644 --- a/examples/data/plugins/on_event.py +++ b/examples/data/plugins/on_event.py @@ -24,6 +24,10 @@ def event_handler(uzbl, *args, **kargs): '''This function handles all the events being watched by various on_event definitions and responds accordingly.''' + # Could be connected to a EM internal event that can use anything as args + if len(args) == 1 and isinstance(args[0], basestring): + args = splitquoted(args[0]) + events = uzbl.on_events event = kargs['on_event'] if event not in events: @@ -80,3 +84,5 @@ def cleanup(uzbl): del handlers[:] uzbl.on_events.clear() + +# vi: set et ts=4: -- cgit v1.2.3