diff options
Diffstat (limited to 'lib/python/debian_linux/kconfig.py')
-rw-r--r-- | lib/python/debian_linux/kconfig.py | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/lib/python/debian_linux/kconfig.py b/lib/python/debian_linux/kconfig.py new file mode 100644 index 0000000..c5192d1 --- /dev/null +++ b/lib/python/debian_linux/kconfig.py @@ -0,0 +1,74 @@ +from __future__ import absolute_import + +from .utils import SortedDict + +__all__ = ( + "KconfigFile", +) + +class EntryString(object): + __slots__ = "name", "value" + + def __init__(self, name, value): + self.name = name + self.value = value + + def __str__(self): + return "CONFIG_%s=%s" % (self.name, self.value) + +class EntryTristate(object): + __slots__ = "name", "value" + + VALUE_NO = 0 + VALUE_YES = 1 + VALUE_MOD = 2 + + def __init__(self, name, value = None): + self.name = 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(SortedDict): + 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 = EntryTristate(option, value) + else: + entry = EntryString(option, value) + self[option] = entry + elif line.startswith("# CONFIG_"): + option = line[9:-11] + self[option] = EntryTristate(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) + |