summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ext-plugins/lib-general/filters/stringlist2stringset.py13
-rw-r--r--ext-plugins/lib-general/formats/string_iter.py40
-rw-r--r--filter.py3
-rw-r--r--tests/formats/string_iter.py31
-rw-r--r--utils.py2
5 files changed, 87 insertions, 2 deletions
diff --git a/ext-plugins/lib-general/filters/stringlist2stringset.py b/ext-plugins/lib-general/filters/stringlist2stringset.py
new file mode 100644
index 0000000..eff86fa
--- /dev/null
+++ b/ext-plugins/lib-general/filters/stringlist2stringset.py
@@ -0,0 +1,13 @@
+# -*- coding: UTF-8 -*-
+# Copyright 2014 Red Hat, Inc.
+# Part of clufter project
+# Licensed under GPLv2+ (a copy included | http://gnu.org/licenses/gpl-2.0.txt)
+"""stringlist2stringset filter"""
+__author__ = "Jan Pokorný <jpokorny @at@ Red Hat .dot. com>"
+
+from ..filter import Filter
+
+@Filter.deco('string-iter', 'string-set')
+def stringlist2stringset(flt_ctxt, in_obj):
+ """Downgrades list to a set (order not preserved)"""
+ return ('stringset', set(in_obj('stringiter', protect_safe=True)))
diff --git a/ext-plugins/lib-general/formats/string_iter.py b/ext-plugins/lib-general/formats/string_iter.py
new file mode 100644
index 0000000..20e42ba
--- /dev/null
+++ b/ext-plugins/lib-general/formats/string_iter.py
@@ -0,0 +1,40 @@
+# -*- coding: UTF-8 -*-
+# Copyright 2014 Red Hat, Inc.
+# Part of clufter project
+# Licensed under GPLv2+ (a copy included | http://gnu.org/licenses/gpl-2.0.txt)
+"""Format representing a list of strings (new-line delimited)"""
+__author__ = "Jan Pokorný <jpokorny @at@ Red Hat .dot. com>"
+
+from ..format import SimpleFormat
+from ..protocol import Protocol
+
+
+class string_iter(SimpleFormat):
+ native_protocol = STRINGITER = Protocol('stringiter')
+ BYTESTRING = SimpleFormat.BYTESTRING
+
+ @SimpleFormat.producing(BYTESTRING, chained=True)
+ def get_bytestring(self, *protodecl):
+ """Return command as canonical single string"""
+ # chained fallback
+ return '\n'.join(s for s in self.STRINGITER(protect_safe=True) if s)
+
+ @SimpleFormat.producing(STRINGITER, protect=True)
+ def get_stringiter(self, *protodecl):
+ return (s.strip() for s in (self.BYTESTRING().rstrip('\n')).split('\n'))
+
+
+class string_list(string_iter):
+ native_protocol = STRINGLIST = Protocol('stringlist')
+
+ @SimpleFormat.producing(STRINGLIST, protect=True)
+ def get_stringlist(self, *protodecl):
+ return list(self.STRINGITER(protect_safe=True))
+
+
+class string_set(string_iter):
+ native_protocol = STRINGSET = Protocol('stringset')
+
+ @SimpleFormat.producing(STRINGSET, protect=True)
+ def get_stringset(self, *protodecl):
+ return set(self.STRINGITER(protect_self=True))
diff --git a/filter.py b/filter.py
index 359c668..3fad855 100644
--- a/filter.py
+++ b/filter.py
@@ -25,7 +25,7 @@ from . import package_name
from .error import ClufterError, ClufterPlainError
from .format import CompositeFormat, XML
from .plugin_registry import MetaPlugin, PluginRegistry
-from .utils import args2tuple, \
+from .utils import args2tuple, arg2wrapped, \
filterdict_keep, filterdict_pop, \
head_tail, hybridproperty, \
tuplist
@@ -161,6 +161,7 @@ class Filter(object):
flt_ctxt = cmd_ctxt.ensure_filter(self)
outdecl = self._fnc(flt_ctxt, in_obj)
outdecl_head, outdecl_tail = head_tail(outdecl)
+ outdecl_tail = arg2wrapped(outdecl_tail)
fmt_kws = filterdict_keep(flt_ctxt, *self.out_format.context, **fmt_kws)
return self.out_format(outdecl_head, *outdecl_tail, **fmt_kws)
diff --git a/tests/formats/string_iter.py b/tests/formats/string_iter.py
new file mode 100644
index 0000000..8cad3b4
--- /dev/null
+++ b/tests/formats/string_iter.py
@@ -0,0 +1,31 @@
+# -*- coding: UTF-8 -*-
+# Copyright 2014 Red Hat, Inc.
+# Part of clufter project
+# Licensed under GPLv2+ (a copy included | http://gnu.org/licenses/gpl-2.0.txt)
+"""Testing string_{iter,list,set} formats"""
+__author__ = "Jan Pokorný <jpokorny @at@ Red Hat .dot. com>"
+
+from os.path import join, dirname as d; execfile(join(d(d((__file__))), '_go'))
+
+from unittest import TestCase
+
+from .format_manager import FormatManager
+string_list = FormatManager.init_lookup('string-list').formats['string-list']
+
+
+class FormatsStringIterTestCase(TestCase):
+ def testBytestringToStringListSingle(self):
+ sl = string_list('bytestring', "abcd")
+ self.assertEqual(sl.STRINGLIST(), ['abcd'])
+ def testBytestringToStringList(self):
+ sl = string_list('bytestring', """\
+a
+b
+c
+b
+a
+""")
+ self.assertEqual(sl.STRINGLIST(), ['a', 'b', 'c', 'b', 'a'])
+
+
+from os.path import join, dirname as d; execfile(join(d(d(__file__)), '_gone'))
diff --git a/utils.py b/utils.py
index c3d1b83..9ffa96e 100644
--- a/utils.py
+++ b/utils.py
@@ -17,7 +17,7 @@ args2sgpl = \
lambda x=(), *y: x if not y and tuplist(x) else (x, ) + y
arg2wrapped = \
lambda x=(), *y: \
- x if not y and isinstance(x, tuple) and len(x) > 1 else (x, ) + y
+ x if not y and isinstance(x, tuple) else (x, ) + y
args2unwrapped = \
lambda x=None, *y: x if not y else (x, ) + y
# turn args into tuple unconditionally