Statistics
| Branch: | Revision:

mobicen / util / inherit_config_parser.py @ c16015e2

History | View | Annotate | Download (3.09 KB)

1 1eca333b LoreBz
"""
2
Stachl
3
LICENSE
4
Permission is hereby granted, free of charge, to any person obtaining a copy
5
of this software and associated documentation files (the "Software"), to deal
6
in the Software without restriction, including without limitation the rights
7
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
copies of the Software, and to permit persons to whom the Software is
9
furnished to do so, subject to the following conditions:
10
The above copyright notice and this permission notice shall be included in
11
all copies or substantial portions of the Software.
12
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
18
THE SOFTWARE.
19
@copyright: Copyright (c) 2011, w3agency.net, Leonardo Maccari
20
@author: Thomas Stachl <t.stachl@w3agency.net>, Leonardo Maccari leonardo.maccari@disi.unitn.it
21
@since: Mar 28, 2011
22
""" 
23
24
25
from ConfigParser import SafeConfigParser, NoOptionError, NoSectionError
26
27
class InheritConfigParser(SafeConfigParser):
28
    def get(self, section, option=None, default=""):
29
        try:
30
            return SafeConfigParser.get(self, section, option)
31
        except NoSectionError:
32
            for s in self._sections:
33
                if s.find(section) != -1:
34
                    try:
35
                        return SafeConfigParser.get(self, s, option)
36
                    except NoOptionError:
37
                        return SafeConfigParser.get(self, s.split(":")[1].strip(), option) 
38
39
    def sections(self):
40
        real_sections = SafeConfigParser.sections(self)
41
        parsed_sections = []
42
        for section in real_sections:
43
            if ":" not in section:
44
                parsed_sections.append(section)
45
            else:
46
                parsed_sections.append(section.split(":")[0])
47
        return parsed_sections
48
49
    def items(self, section):
50
        try:
51
            return SafeConfigParser.items(self, section)
52
        except NoSectionError:
53
            for s in self._sections:
54
                if s.find(section) != -1:
55
                    # this section name includes the section name we're looking
56
                    # for
57
                    base_step = []
58
                    try:
59
                        # is it an existing section name? yes, then we
60
                        # navigated to the highest ancstor, return the contents
61
                        # of the stanza
62
                        return SafeConfigParser.items(self, section)
63
                    except NoSectionError:
64
                        # no section is named like this, add the content of the
65
                        # current stanza and climb to the father
66
                        base_step = SafeConfigParser.items(self, s)
67
                        return self.items(
68
                                s.split(":")[1].strip()) + base_step
69
70
71