File: kconfig.py

package info (click to toggle)
linux-2.6 2.6.18.dfsg.1-26etch2
  • links: PTS
  • area: main
  • in suites: etch
  • size: 289,112 kB
  • ctags: 831,064
  • sloc: ansic: 4,700,345; asm: 205,595; makefile: 13,019; perl: 3,208; python: 2,735; yacc: 2,632; sh: 2,313; cpp: 2,107; lex: 1,510; lisp: 218; awk: 99; pascal: 41
file content (77 lines) | stat: -rw-r--r-- 2,121 bytes parent folder | download | duplicates (2)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
__all__ = (
    "kconfigfile",
)

class _entry(object):
    __slots__ = "name"

    def __init__(self, name):
        self.name = name

class _entry_string(_entry):
    __slots__ = "value"

    def __init__(self, name, value):
        super(_entry_string, self).__init__(name)
        self.value = value

    def __str__(self):
        return "CONFIG_%s=%s" % (self.name, self.value)

class _entry_tristate(_entry):
    __slots__ = "value"

    VALUE_NO = 0
    VALUE_YES = 1
    VALUE_MOD = 2

    def __init__(self, name, value = None):
        super(_entry_tristate, self).__init__(name)

        if value == 'n' or value is None:
            self.value = self.VALUE_NO
        elif value == 'y':
            self.value = self.VALUE_YES
        elif value == 'm':
            self.value = self.VALUE_MOD

    def __str__(self):
        conf = "CONFIG_%s" % self.name
        if self.value == self.VALUE_NO:
            return "# %s is not set" % conf
        elif self.value == self.VALUE_YES:
            return "%s=y" % conf
        elif self.value == self.VALUE_MOD:
            return "%s=m" % conf

class kconfigfile(dict):
    def __str__(self):
        ret = []
        for i in self.str_iter():
            ret.append(i)
        return '\n'.join(ret) + '\n'

    def read(self, f):
        for line in iter(f.readlines()):
            line = line.strip()
            if line.startswith("CONFIG_"):
                i = line.find('=')
                option = line[7:i]
                value = line[i+1:]
                if value in ('y', 'm'):
                    entry = _entry_tristate(option, value)
                else:
                    entry = _entry_string(option, value)
                self[option] = entry
            elif line.startswith("# CONFIG_"):
                option = line[9:-11]
                self[option] = _entry_tristate(option)
            elif line.startswith("#") or not line:
                pass
            else:
                raise RuntimeError, "Can't recognize %s" % line

    def str_iter(self):
        for key, value in self.iteritems():
            yield str(value)