r127 - branches/rewrite/src
Otavio Salvador
partial-mirror-devel@lists.alioth.debian.org
Thu, 01 Jul 2004 22:22:36 -0600
Author: otavio
Date: Thu Jul 1 22:22:35 2004
New Revision: 127
Modified:
branches/rewrite/src/Config.py
Log:
Rewrite the dump method and make the code more simple.
Modified: branches/rewrite/src/Config.py
==============================================================================
--- branches/rewrite/src/Config.py (original)
+++ branches/rewrite/src/Config.py Thu Jul 1 22:22:35 2004
@@ -17,7 +17,7 @@
# $Id$
from logging import *
-from ConfigParser import ConfigParser, DEFAULTSECT
+from ConfigParser import ConfigParser
from sys import exit
class InvalidOption(Exception):
@@ -38,7 +38,7 @@
Store the configurations used by our system.
"""
- required_in_default = [
+ required_in_global = [
'mirror_dir',
'architectures',
'sections',
@@ -69,61 +69,52 @@
conf = ConfigParser()
conf.read(filename)
- # Read the default section.
- self['DEFAULT'] = {}
- for item, value in conf.items(DEFAULTSECT):
- if item not in Config.required_in_default:
- debug("[%s] is not allowed in default section." % (item))
- raise InvalidOption('DEFAULT', item)
- self['DEFAULT'][item] = value
-
+ # Read the global section.
+ self['GLOBAL'] = {}
+ for item, value in conf.items('GLOBAL'):
+ if item not in self.required_in_global:
+ debug("[%s] is not allowed in global section." % (item))
+ raise InvalidOption('GLOBAL', item)
+ self['GLOBAL'][item] = value
+
for section in conf.sections():
+ if section == 'GLOBAL': continue
self[section] = {}
for item, value in conf.items(section):
- # is this item from the default section? if so, go on to the
- # next item
- if self['DEFAULT'].has_key(item):
- if self['DEFAULT'][item] == value:
- continue
- if item not in Config.allowed_in_backend:
- actually_its_ok = False
- for allowed_key in Config.allowed_in_backend:
+ if item not in self.allowed_in_backend:
+ for allowed_key in self.allowed_in_backend:
# check the allowed_in_backend keys with
# @VARIABLES@
if allowed_key.find('@') != -1:
nonvar_length = allowed_key.find('@')
+ print allowed_key[:nonvar_length], item[:nonvar_length]
if allowed_key[:nonvar_length] == item[:nonvar_length]:
# found it!
- actually_its_ok = True
break
- if not actually_its_ok:
+ else:
debug("[%s] is not allowed in a backend section (it was found in [%s])."
% (item, section))
raise InvalidOption(section, item)
self[section][item] = value
- def getValue(self, key, section='DEFAULT'):
+ def getOption(self, key, section='GLOBAL'):
# get a config value for a certain section. if it's not
- # specified, fall back to DEFAULT
+ # specified, fall back to GLOBAL
if not self.has_key(section):
error("no config section called [%s]." % (section))
exit(1)
if self[section].has_key(key):
return self[section][key]
- if not self['DEFAULT'].has_key(key):
- error("[%s] is not present in section [%s] or the default section of config file." % (key, section))
+ if not self['GLOBAL'].has_key(key):
+ error("[%s] is not present in section [%s] or the global section of config file." % (key, section))
exit(1)
- return self['DEFAULT'][key]
+ return self['GLOBAL'][key]
def dump(self):
- print 'mirror_dir =', self.getValue('mirror_dir')
- print 'architectures =', self.getValue('architectures')
- print 'sections = ', self.getValue('sections')
- print 'distributions =', self.getValue('distributions')
- print 'get_provides = ', self.getValue('get_provides')
- print 'get_recommends = ', self.getValue('get_recommends')
- print 'get_suggests = ', self.getValue('get_suggests')
-
+ for section, options in self.items():
+ print section
+ for item, value in options.items():
+ print " %s = %s" %(item, value)
getLogger().setLevel(DEBUG)
conf = Config('../etc/debpartial-mirror.conf')