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/scripts/uzbl-event-manager | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'examples/data/scripts/uzbl-event-manager') 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