Init confobj before using it

only read config file once
This commit is contained in:
Marcus Stoegbauer 2013-01-13 21:30:48 +00:00
parent 79452ebf10
commit 748fef4403

View File

@ -10,15 +10,16 @@ cfgfile.py
Created by Marcus Stoegbauer on 2013-01-12. Created by Marcus Stoegbauer on 2013-01-12.
Copyright (c) 2013 __MyCompanyName__. All rights reserved. Copyright (c) 2013 __MyCompanyName__. All rights reserved.
""" """
from ConfigParser import ConfigParser import ConfigParser
class Conf(object): class Conf(object):
confobj = ConfigParser() confobj = ConfigParser.RawConfigParser()
cfgfile = '' cfgfile = ''
def __init__(self, filename = None): def __init__(self, filename = None):
"""if filename is set, open config file and initialize the ConfigParser """if filename is set, open config file and initialize the ConfigParser
""" """
self.confobj = ConfigParser.RawConfigParser()
if filename: if filename:
self.setfilename(filename) self.setfilename(filename)
# if filename # if filename
@ -27,7 +28,8 @@ class Conf(object):
def setfilename(self, filename): def setfilename(self, filename):
"""initialize the ConfigParser """initialize the ConfigParser
""" """
if len(self.confobj.read(filename)) == 0 or self.confobj.read(filename)[0] != filename: ret = self.confobj.read(filename)
if len(ret) == 0 or ret[0] != filename:
raise Exception('Cannot read config file ' + filename) raise Exception('Cannot read config file ' + filename)
# if cannot read # if cannot read
self.cfgfile = filename self.cfgfile = filename