From 96a3960c180e49d06bdd4e362672428662e4addf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Metin?= Date: Sat, 2 Jul 2005 02:31:22 +0000 Subject: [PATCH] =?UTF-8?q?config=20parser=20i=C5=9Flerini=20ayr=C4=B1=20b?= =?UTF-8?q?ir=20mod=C3=BCle=20ta=C5=9F=C4=B1.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pisi/config.py | 66 +-------------------------------------- pisi/configfile.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 65 deletions(-) create mode 100644 pisi/configfile.py diff --git a/pisi/config.py b/pisi/config.py index 63f02cfb..e3b6db61 100644 --- a/pisi/config.py +++ b/pisi/config.py @@ -8,72 +8,8 @@ #/etc/pisi/conf?) and this module will provide access to those #configuration parameters. -import os -from ConfigParser import ConfigParser, NoSectionError from constants import const - -class ConfigException(Exception): - pass - -class GeneralDefaults: - destinationDirectory = os.getcwd()+"/tmp" # FOR ALPHA - -class BuildDefaults: - host = "i686-pc-linux-gnu" - CFLAGS = "-mcpu=i686 -O2 -pipe -fomit-frame-pointer" - CXXFLAGS= "-mcpu=i686 -O2 -pipe -fomit-frame-pointer" - -class ConfigurationSection(object): - def __init__(self, section, items=[]): - self.items = items - - if section == "general": - self.defaults = GeneralDefaults - elif section == "build": - self.defaults = BuildDefaults - else: - e = "No section by name '%s'" % section - raise ConfigException, e - - self.section = section - - def __getattr__(self, attr): - if not self.items: - if hasattr(self.defaults, attr): - return getattr(self.defaults, attr) - return "" - for item in self.items: - if item[0] == attr: - return item[1] - return "" - -# should we move this class to its own module? -class ConfigurationFile(object): - def __init__(self, filePath): - parser = ConfigParser() - self.filePath = filePath - #/etc/pisi/pisi.conf - #[general] - #destinationDirectory = /tmp - #[build] - #host = i686-pc-linux-gnu - #CFLAGS= -mcpu=i686 -O2 -pipe -fomit-frame-pointer - #CXXFLAGS= -mcpu=i686 -O2 -pipe -fomit-frame-pointer - - parser.read(self.filePath) - - try: - generalitems = parser.items("general") - except NoSectionError: - generalitems = [] - self.general = ConfigurationSection("general", generalitems) - - try: - builditems = parser.items("build") - except NoSectionError: - builditems = [] - self.build = ConfigurationSection("build", builditems) - +from configfile import ConfigurationFile class Config(object): """Config Singleton""" diff --git a/pisi/configfile.py b/pisi/configfile.py new file mode 100644 index 00000000..ccabcc5c --- /dev/null +++ b/pisi/configfile.py @@ -0,0 +1,78 @@ +# PISI Configuration File module, obviously, is used to read from the +# configuration file. Module also defines default values for +# configuration parameters. + +# Configuration file is located in /etc/pisi/pisi.conf by default, +# having an INI like format like below. +# +#[general] +#destinationDirectory = /tmp +# +#[build] +#host = i686-pc-linux-gnu +#CFLAGS= -mcpu=i686 -O2 -pipe -fomit-frame-pointer +#CXXFLAGS= -mcpu=i686 -O2 -pipe -fomit-frame-pointer + +import os +from ConfigParser import ConfigParser, NoSectionError + + +class ConfigException(Exception): + pass + +class GeneralDefaults: + """Default values for [general] section""" + destinationDirectory = os.getcwd()+"/tmp" # FOR ALPHA + +class BuildDefaults: + """Default values for [build] section""" + host = "i686-pc-linux-gnu" + CFLAGS = "-mcpu=i686 -O2 -pipe -fomit-frame-pointer" + CXXFLAGS= "-mcpu=i686 -O2 -pipe -fomit-frame-pointer" + + +class ConfigurationSection(object): + """ConfigurationSection class defines a section in the configuration + file, using defaults (above) as a fallback.""" + def __init__(self, section, items=[]): + self.items = items + + if section == "general": + self.defaults = GeneralDefaults + elif section == "build": + self.defaults = BuildDefaults + else: + e = "No section by name '%s'" % section + raise ConfigException, e + + self.section = section + + def __getattr__(self, attr): + if not self.items: + if hasattr(self.defaults, attr): + return getattr(self.defaults, attr) + return "" + for item in self.items: + if item[0] == attr: + return item[1] + return "" + + +class ConfigurationFile(object): + def __init__(self, filePath): + parser = ConfigParser() + self.filePath = filePath + + parser.read(self.filePath) + + try: + generalitems = parser.items("general") + except NoSectionError: + generalitems = [] + self.general = ConfigurationSection("general", generalitems) + + try: + builditems = parser.items("build") + except NoSectionError: + builditems = [] + self.build = ConfigurationSection("build", builditems)