summaryrefslogtreecommitdiff
path: root/Config.py
blob: 47009f210a3cbe7d61446544bfad1c9d262da6ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

import os
import ConfigParser

__config = None
SECTION = "main"

class Error(Exception):
	"""Configuration error"""
	def __init__(self, value):
		self.value = value
		self.str = repr(self.value)
	def __str__(self):
		return self.str

def load(filename):
	global __config
	conf = ConfigParser.ConfigParser()
	conf.read(filename)
	if not conf.has_section(SECTION):
		raise Error, "invalid or missing config file: %s" % filename
	__config = conf

def require(key):
	result = option(key)
	if not result:
		raise Error, "missing conf option '%s' in section '%s'" % (key, SECTION)
	return result

def option(key, default = None):
	assert __config is not None, "configuration not loaded"
	if not __config.has_option(SECTION, key):
		return default
	return __config.get(SECTION, key)