diff options
author | Jan Pokorný <jpokorny@redhat.com> | 2014-12-19 11:28:19 +0100 |
---|---|---|
committer | Jan Pokorný <jpokorny@redhat.com> | 2015-01-09 10:21:21 +0100 |
commit | 567e5686fc5713ba8163f42400fee9c9346851d6 (patch) | |
tree | 90b57dae746924a1a24d80197fd920488d945386 /ext-plugins | |
parent | 573b466c3973021fccca745d2e2ce4921d1ba852 (diff) | |
download | clufter-567e5686fc5713ba8163f42400fee9c9346851d6.tar.gz clufter-567e5686fc5713ba8163f42400fee9c9346851d6.tar.xz clufter-567e5686fc5713ba8163f42400fee9c9346851d6.zip |
ext-plugins/lib-general: string_{list,set} + conversion filter
+ start respective unit tests
+ fix faulty format passing logic as observed with the new plugins
Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
Diffstat (limited to 'ext-plugins')
-rw-r--r-- | ext-plugins/lib-general/filters/stringlist2stringset.py | 13 | ||||
-rw-r--r-- | ext-plugins/lib-general/formats/string_iter.py | 40 |
2 files changed, 53 insertions, 0 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)) |