aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar keis <keijser@gmail.com>2011-01-23 21:57:16 +0100
committerGravatar keis <keijser@gmail.com>2011-02-15 21:17:16 +0100
commit3594f35e565ebf210b583bcc3c4df042e0dc4191 (patch)
tree614eab8b78bc09cc16a642783064ae119ee573c9
parentbed2433630f6d0d78e8b829c46b00c3ab301b512 (diff)
move common function related to escaping to Plugin
-rw-r--r--examples/data/plugins/bind.py9
-rw-r--r--examples/data/plugins/config.py14
-rw-r--r--examples/data/plugins/cookies.py11
-rwxr-xr-xexamples/data/scripts/uzbl-event-manager20
4 files changed, 22 insertions, 32 deletions
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):