summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Pokorný <jpokorny@redhat.com>2015-07-02 22:17:56 +0200
committerJan Pokorný <jpokorny@redhat.com>2015-07-02 23:44:06 +0200
commit71ca12ec51eb0d02bb85edebdb63321b3661bcef (patch)
treef2ca0b43cb5e8be36f6f13290ba840edab68e506
parent4c8066aa22ac253f50416917907bd1a2181fe64d (diff)
downloadclufter-71ca12ec51eb0d02bb85edebdb63321b3661bcef.tar.gz
clufter-71ca12ec51eb0d02bb85edebdb63321b3661bcef.tar.xz
clufter-71ca12ec51eb0d02bb85edebdb63321b3661bcef.zip
filters/simpleconfig-normalize: new filter + unit test
Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
-rw-r--r--filters/simpleconfig_normalize.py57
-rw-r--r--formats/simpleconfig.py2
-rw-r--r--tests/filters/simpleconfig_normalize.py40
3 files changed, 99 insertions, 0 deletions
diff --git a/filters/simpleconfig_normalize.py b/filters/simpleconfig_normalize.py
new file mode 100644
index 0000000..5caae4d
--- /dev/null
+++ b/filters/simpleconfig_normalize.py
@@ -0,0 +1,57 @@
+# -*- coding: UTF-8 -*-
+# Copyright 2015 Red Hat, Inc.
+# Part of clufter project
+# Licensed under GPLv2+ (a copy included | http://gnu.org/licenses/gpl-2.0.txt)
+"""simpleconfig-normalize filter"""
+__author__ = "Jan Pokorný <jpokorny @at@ Red Hat .dot. com>"
+
+try:
+ from collections import OrderedDict
+except ImportError:
+ from ordereddict import OrderedDict
+from logging import getLogger
+
+log = getLogger(__name__)
+
+from ..filter import Filter
+from ..utils import selfaware
+
+
+@selfaware
+def _simpleconfig_normalize(me, parent_section):
+ # must not attempt to modify anything from parent_section in-place
+ parent_tag, parent_options, parent_sections = parent_section
+ parent_sections_new = []
+ for s in parent_sections:
+ tag, options, sections = s
+ opts = OrderedDict()
+ for n, v in options:
+ vals = opts.setdefault(n, [])
+ if v in vals:
+ log.warning("omitting duplicated `({0}, {1}}' option from"
+ " normalization".format(n, v))
+ else:
+ vals.append(v)
+ options = []
+ for n, vals in opts.iteritems():
+ options.append((n, vals[0]))
+ vals[:] = vals[1:]
+ parent_sections_new.append((tag, options, [me(s) for s in sections]))
+ for i, (n, vals) in enumerate((n, vals) for n, vals in opts.iteritems()
+ if vals):
+ if not i and sections:
+ log.warning("current section `{0}' needs normalization but"
+ " contains subsections (not expected)".format(tag))
+ for v in vals:
+ parent_sections_new.append((tag, ((n, v), ), ()))
+ return (parent_tag, parent_options, parent_sections_new)
+
+
+@Filter.deco('simpleconfig', 'simpleconfig-normalized')
+def simpleconfig_normalize(flt_ctxt, in_obj):
+ """Normalizes simpleconfig per simpleconfig-normalized format description
+
+ (Mentioned traversal is deliberately defined as post-order here.)
+ """
+ struct = in_obj('struct', protect_safe=True)
+ return ('struct', _simpleconfig_normalize(struct))
diff --git a/formats/simpleconfig.py b/formats/simpleconfig.py
index 51111e7..1d197b0 100644
--- a/formats/simpleconfig.py
+++ b/formats/simpleconfig.py
@@ -152,5 +152,7 @@ class simpleconfig_normalized(simpleconfig):
own; if the current section contains subsection,
issue a warning about that
2. continue with the next section in a defined traversal
+
+ Fair enough, we've just described the simpleconfig-normalize filter :)
"""
pass
diff --git a/tests/filters/simpleconfig_normalize.py b/tests/filters/simpleconfig_normalize.py
new file mode 100644
index 0000000..dd54cb4
--- /dev/null
+++ b/tests/filters/simpleconfig_normalize.py
@@ -0,0 +1,40 @@
+# -*- coding: UTF-8 -*-
+# Copyright 2015 Red Hat, Inc.
+# Part of clufter project
+# Licensed under GPLv2+ (a copy included | http://gnu.org/licenses/gpl-2.0.txt)
+"""Testing `simpleconfig-normalize' filter"""
+__author__ = "Jan Pokorný <jpokorny @at@ Red Hat .dot. com>"
+
+from os.path import join, dirname as d; execfile(join(d(d((__file__))), '_go'))
+
+
+from os.path import dirname, join
+from unittest import TestCase
+
+from .filter_manager import FilterManager
+from .formats.simpleconfig import simpleconfig
+
+flt = 'simpleconfig-normalize'
+simpleconfig_normalize = FilterManager.init_lookup(flt).filters[flt]
+
+
+class FiltersSimpleconfigNormalizeTestCase(TestCase):
+ def testSimpleconfigNormalize(self):
+ result = simpleconfig_normalize(simpleconfig('struct',
+ ('IGNORED',
+ [],
+ [('uidgid', [('uid', '0'), ('uid', '1000'), ('gid', '0')], [])])
+ ))
+ #print result.BYTESTRING()
+ self.assertEquals(result.BYTESTRING(), """\
+uidgid {
+ uid: 0
+ gid: 0
+}
+uidgid {
+ uid: 1000
+}
+""")
+
+
+from os.path import join, dirname as d; execfile(join(d(d(__file__)), '_gone'))