1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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))
|