diff options
author | Joel Andres Granados <jgranado@redhat.com> | 2008-01-02 14:32:07 +0100 |
---|---|---|
committer | Joel Andres Granados <jgranado@redhat.com> | 2008-01-02 14:32:07 +0100 |
commit | 26da06a02b8cbeea5aea9eccc2de55d33ade23bf (patch) | |
tree | 7bb10032a86f8015dcee987697c480ef24d32872 /pyfirstaidkit/configuration.py | |
parent | 403519b478ffbe5b433b1fd2fb7babe12c693af2 (diff) | |
download | firstaidkit-26da06a02b8cbeea5aea9eccc2de55d33ade23bf.tar.gz firstaidkit-26da06a02b8cbeea5aea9eccc2de55d33ade23bf.tar.xz firstaidkit-26da06a02b8cbeea5aea9eccc2de55d33ade23bf.zip |
Move from tasker to pyfirstaidkit to try to follow the standard in site-packages.
Diffstat (limited to 'pyfirstaidkit/configuration.py')
-rw-r--r-- | pyfirstaidkit/configuration.py | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/pyfirstaidkit/configuration.py b/pyfirstaidkit/configuration.py new file mode 100644 index 0000000..70a626e --- /dev/null +++ b/pyfirstaidkit/configuration.py @@ -0,0 +1,102 @@ +# First Aid Kit - diagnostic and repair tool for Linux +# Copyright (C) 2007 Martin Sivak <msivak@redhat.com> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +import ConfigParser +import os +from cStringIO import StringIO +from shlex import shlex + +if os.environ.has_key("FIRST_AID_KIT_CONF"): + cfgfile = os.environ["FIRST_AID_KIT_CONF"].split(":") +else: + cfgfile = ["/etc/firstaidkit.conf", os.environ["HOME"]+"/.firstaidkit.conf"] + +defaultconfig = """ +[system] +root = /mnt/sysimage + +[plugin] +path = /usr/lib/FirstAidKit/plugins +disabled = + +[operation] +mode = auto +help = False +verbose = False +gui = console + +[log] +method = stdout +""" + +class LockedError(Exception): + pass + +class FAKConfigSection(object): + """Proxy object for one configuration section""" + + def __init__(self, cfg, name): + self.__dict__["section_name"] = name + self.__dict__["configuration"] = cfg + + def __getattr__(self, key): + if not self.__dict__["configuration"].has_section(self.__dict__["section_name"]): + raise ConfigParser.NoSectionError(self.__dict__["section_name"]) + + if not self.__dict__["configuration"].has_option(self.__dict__["section_name"], key): + raise ConfigParser.NoOptionError(key) + + return self.__dict__["configuration"].get(self.__dict__["section_name"], key) + + def __setattr__(self, key, value): + if self.__dict__["configuration"].__dict__.has_key("_lock") and self.__dict__["configuration"].__dict__["_lock"]: + raise LockedError(key) + + if not self.__dict__["configuration"].has_section(self.__dict__["section_name"]): + self.__dict__["configuration"].add_section(self.__dict__["section_name"]) + self.__dict__["configuration"].set(self.__dict__["section_name"], key, value) + + def _list(self, key): + l = [] + lex = shlex(instream = StringIO(getattr(self, key)), posix = True) + token = lex.get_token() + while token!=lex.eof: + l.append(token) + token = lex.get_token() + return l + + +class FAKConfigMixIn(object): + """Enhance ConfigParser so we can use it in the python way (config.section.value)""" + + def __getattr__(self, section): + return FAKConfigSection(self, section) + + def lock(self): + self.__dict__["_lock"] = True + + def unlock(self): + self.__dict__["_lock"] = False + +class FAKConfig(ConfigParser.SafeConfigParser, FAKConfigMixIn): + pass + +Config = FAKConfig() +Config.readfp(StringIO(defaultconfig), "<default>") +Config.read(cfgfile) + + |