From ed21fdc80ee5f9dc85fcfe781a5ffed98f214bce Mon Sep 17 00:00:00 2001 From: Faik Uygur Date: Tue, 22 May 2007 08:22:31 +0000 Subject: [PATCH] - get, set and write_config methods added for manipulation of pisi.conf file from Comar to solve bug #5668. --- pisi/__init__.py | 2 +- pisi/configfile.py | 142 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 138 insertions(+), 6 deletions(-) diff --git a/pisi/__init__.py b/pisi/__init__.py index 3df7f41f..f6b72007 100644 --- a/pisi/__init__.py +++ b/pisi/__init__.py @@ -17,7 +17,7 @@ __version__ = "1.1_20070305" __dbversion__ = "1.1_20070305" __filesdbversion__ = "1.0.5" # yes, this is the real bottleneck -__all__ = [ 'api', 'config', 'packagedb', 'installdb', 'search' ] +__all__ = [ 'api', 'config', 'configfile', 'packagedb', 'installdb', 'search' ] # FIXME: Exception shadows builtin Exception. This is no good. class Exception(Exception): diff --git a/pisi/configfile.py b/pisi/configfile.py index 27a74aa9..e8187bef 100644 --- a/pisi/configfile.py +++ b/pisi/configfile.py @@ -45,6 +45,8 @@ #qt_dir = /usr/qt/3 import os +import re +import StringIO import ConfigParser import gettext @@ -137,25 +139,155 @@ class ConfigurationSection(object): class ConfigurationFile(object): """Parse and get configuration values from the configuration file""" def __init__(self, filePath): - parser = ConfigParser.ConfigParser() + self.parser = ConfigParser.ConfigParser() self.filePath = filePath - parser.read(self.filePath) + self.parser.read(self.filePath) try: - generalitems = parser.items("general") + generalitems = self.parser.items("general") except ConfigParser.NoSectionError: generalitems = [] self.general = ConfigurationSection("general", generalitems) try: - builditems = parser.items("build") + builditems = self.parser.items("build") except ConfigParser.NoSectionError: builditems = [] self.build = ConfigurationSection("build", builditems) try: - dirsitems = parser.items("directories") + dirsitems = self.parser.items("directories") except ConfigParser.NoSectionError: dirsitems = [] self.dirs = ConfigurationSection("directories", dirsitems) + + # get, set and write_config methods added for manipulation of pisi.conf file from Comar to solve bug #5668. + # Current ConfigParser does not keep the comments and white spaces, which we do not want for pisi.conf. There + # are patches floating in the python sourceforge to add this feature. The write_config code is from python + # sourceforge tracker id: #1410680, modified a little to make it turn into a function. + + def get(self, section, option): + return self.parser.get(section, option) + + def set(self, section, option, value): + self.parser.set(section, option, value) + + def write_config(self, add_missing=True): + sections = {} + current = StringIO.StringIO() + replacement = [current] + sect = None + opt = None + written = [] + optcre = re.compile( + r'(?P