2013-01-12 23:04:52 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
#
|
|
|
|
# $Id$
|
|
|
|
# $URL$
|
|
|
|
|
|
|
|
"""
|
|
|
|
cfgfile.py
|
|
|
|
|
|
|
|
Created by Marcus Stoegbauer on 2013-01-12.
|
|
|
|
Copyright (c) 2013 __MyCompanyName__. All rights reserved.
|
|
|
|
"""
|
|
|
|
from ConfigParser import ConfigParser
|
|
|
|
|
|
|
|
class Conf(object):
|
|
|
|
confobj = ConfigParser()
|
|
|
|
cfgfile = ''
|
|
|
|
|
|
|
|
def __init__(self, filename = None):
|
2013-01-12 23:06:31 +01:00
|
|
|
"""if filename is set, open config file and initialize the ConfigParser
|
|
|
|
"""
|
2013-01-12 23:04:52 +01:00
|
|
|
if filename:
|
|
|
|
self.setfilename(filename)
|
|
|
|
# if filename
|
|
|
|
# def __init__
|
|
|
|
|
|
|
|
def setfilename(self, filename):
|
2013-01-12 23:06:31 +01:00
|
|
|
"""initialize the ConfigParser
|
|
|
|
"""
|
2013-01-12 23:04:52 +01:00
|
|
|
if len(self.confobj.read(filename)) == 0 or self.confobj.read(filename)[0] != filename:
|
|
|
|
raise Exception('Cannot read config file ' + filename)
|
|
|
|
# if cannot read
|
|
|
|
self.cfgfile = filename
|
|
|
|
# def setfilename
|
|
|
|
|
|
|
|
def get(self, section, option):
|
2013-01-12 23:06:31 +01:00
|
|
|
"""returns the value of option in section
|
|
|
|
"""
|
2013-01-12 23:04:52 +01:00
|
|
|
if not self.cfgfile:
|
|
|
|
raise Exception('No config file set')
|
|
|
|
# if not cfgfile
|
|
|
|
return self.confobj.get(section, option)
|
|
|
|
# def get
|
|
|
|
|
|
|
|
def set(self, section, option, value):
|
2013-01-12 23:06:31 +01:00
|
|
|
"""docstring for update"""
|
2013-01-12 23:04:52 +01:00
|
|
|
self.confobj.set(section, option, value)
|
|
|
|
# def set
|
|
|
|
|
|
|
|
|
|
|
|
def getitems(self, section):
|
2013-01-12 23:06:31 +01:00
|
|
|
"""returns all items in section
|
|
|
|
"""
|
2013-01-12 23:04:52 +01:00
|
|
|
if not self.cfgfile:
|
|
|
|
raise Exception('No config file set')
|
|
|
|
# if not cfgfile
|
|
|
|
return self.confobj.items(section)
|
|
|
|
# def getitems
|
|
|
|
|
2013-01-12 23:06:31 +01:00
|
|
|
def check(self, section, option):
|
|
|
|
"""checks for option in section"""
|
|
|
|
return self.confobj.has_option(section, option)
|
|
|
|
# def check
|
|
|
|
# class Conf
|