| 17 |
# $Id$ |
# $Id$ |
| 18 |
|
|
| 19 |
from logging import * |
from logging import * |
| 20 |
from ConfigParser import ConfigParser, DEFAULTSECT |
from ConfigParser import ConfigParser |
| 21 |
from sys import exit |
from sys import exit |
| 22 |
|
|
| 23 |
|
class InvalidOption(Exception): |
| 24 |
|
""" |
| 25 |
|
Exception called when a invalid option is found in configuration |
| 26 |
|
file. |
| 27 |
|
|
| 28 |
|
Attributes: |
| 29 |
|
section -- Where the invalid option was set; |
| 30 |
|
option -- The name of invalid option. |
| 31 |
|
""" |
| 32 |
|
def __init__(self, section, option): |
| 33 |
|
self.section = section |
| 34 |
|
self.option = option |
| 35 |
|
|
| 36 |
class Config(dict): |
class Config(dict): |
| 37 |
""" |
""" |
| 38 |
Store the configurations used by our system. |
Store the configurations used by our system. |
| 71 |
|
|
| 72 |
# Read the default section. |
# Read the default section. |
| 73 |
self['DEFAULT'] = {} |
self['DEFAULT'] = {} |
| 74 |
for item, value in conf.items(DEFAULTSECT): |
for item, value in conf.items('DEFAULT'): |
| 75 |
if item not in Config.required_in_default: |
if item not in Config.required_in_default: |
| 76 |
error("[%s] is not allowed in default section of configuration file." % (item)) |
debug("[%s] is not allowed in default section." % (item)) |
| 77 |
exit(1) |
raise InvalidOption('DEFAULT', item) |
| 78 |
self['DEFAULT'][item] = value |
self['DEFAULT'][item] = value |
| 79 |
|
|
| 80 |
for section in conf.sections(): |
for section in conf.sections(): |
| 81 |
|
if section == 'DEFAULT': continue # already parsed |
| 82 |
self[section] = {} |
self[section] = {} |
| 83 |
for item, value in conf.items(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 |
|
| 84 |
if item not in Config.allowed_in_backend: |
if item not in Config.allowed_in_backend: |
| 85 |
actually_its_ok = False |
actually_its_ok = False |
| 86 |
for allowed_key in Config.allowed_in_backend: |
for allowed_key in Config.allowed_in_backend: |
| 87 |
# check the allowed_in_backend keys with @VARIABLES@ |
# check the allowed_in_backend keys with |
| 88 |
|
# @VARIABLES@ |
| 89 |
if allowed_key.find('@') != -1: |
if allowed_key.find('@') != -1: |
| 90 |
nonvar_length = allowed_key.find('@') |
nonvar_length = allowed_key.find('@') |
| 91 |
if allowed_key[:nonvar_length] == item[:nonvar_length]: |
if allowed_key[:nonvar_length] == item[:nonvar_length]: |
| 93 |
actually_its_ok = True |
actually_its_ok = True |
| 94 |
break |
break |
| 95 |
if not actually_its_ok: |
if not actually_its_ok: |
| 96 |
error("[%s] is not allowed in a backend section of a config file (it was found in [%s])." |
debug("[%s] is not allowed in a backend section (it was found in [%s])." |
| 97 |
% (item, section)) |
% (item, section)) |
| 98 |
exit(1) |
raise InvalidOption(section, item) |
| 99 |
self[section][item] = value |
self[section][item] = value |
| 100 |
|
|
| 101 |
def getValue(self, key, section='DEFAULT'): |
def getValue(self, key, section='DEFAULT'): |
| 102 |
# get a config value for a certain section. if it's not specified, |
# get a config value for a certain section. if it's not |
| 103 |
# fall back to DEFAULT |
# specified, fall back to DEFAULT |
| 104 |
if not self.has_key(section): |
if not self.has_key(section): |
| 105 |
error("no config section called [%s]." % (section)) |
error("no config section called [%s]." % (section)) |
| 106 |
exit(1) |
exit(1) |