return cls._instance
def __init__(self, fpath=None):
+ self._cfgfiles = []
self._cfgparsers = []
- self.reset_from_conf(fpath)
+ if fpath:
+ if not os.path.exists(fpath):
+ raise errors.ConfigError('Configuration file %s does not '\
+ 'exist' % fpath)
+ self._cfgfiles.append(fpath)
+
+ # find the default path
+ fpaths = self._lookfor_confs()
+ if not fpaths:
+ self._new_conf()
+ fpaths = self._lookfor_confs()
+ self._cfgfiles.extend(fpaths)
+
+ self.load_confs()
def _create_default_parser(self):
'create a default parser that handle DEFAULTS values'
parser.set(sec, key, val)
return parser
- def reset_from_conf(self, fpath):
+ def load_confs(self):
'reset all config values by files passed in'
- if fpath:
- if not os.path.exists(fpath):
- raise errors.ConfigError('Configuration file %s does not '\
- 'exist' % fpath)
- fpaths = [fpath]
- else:
- # use the default path
- fpaths = self._lookfor_confs()
- if not fpaths:
- self._new_conf()
- fpaths = self._lookfor_confs()
self._cfgparsers = []
- for fpath in fpaths:
+ for fpath in self._cfgfiles:
cfgparser = BrainConfigParser()
try:
cfgparser.read_one(fpath)
self._check_passwd()
+ def add_conf(self, fpath):
+ """ Add new config to configmgr, and new added config file has
+ highest priority
+ """
+ if not fpath:
+ return
+ if not os.path.exists(fpath):
+ raise errors.ConfigError('Configuration file %s does not '\
+ 'exist' % fpath)
+ # new added conf has highest priority
+ self._cfgfiles.insert(0, fpath)
+ # reload config files
+ self.load_confs()
+
@staticmethod
def _lookfor_confs():
"""Look for available config files following the order:
reload(gitbuildsys.conf)
return gitbuildsys.conf.configmgr.get(option, section)
+ @staticmethod
+ def add_conf(fpath):
+ '''get section.option from config'''
+ # configmgr is a global variable, reload to recreate it
+ # otherwise fixtures only take effect in the first time
+ reload(gitbuildsys.conf)
+ return gitbuildsys.conf.configmgr.add_conf(fpath)
+
+
@Fixture(project='project1.ini')
def test_no_such_section(self):
'''test no such section'''
'test interpolation is supported'
self.assertEquals('abc/def', self.get('remote', 'target'))
+ @Fixture(home='home1.ini')
+ def test_addconf(self):
+ '''value can be inherit from two levels'''
+ self.add_conf(os.path.join(FILE_DIRNAME, 'testdata', 'ini', 'project1.ini'))
+ self.assertEqual('homev2', self.get('section', 'home_only_key'))
+
if __name__ == '__main__':
- unittest.main()
\ No newline at end of file
+ unittest.main()
# Process configuration file if --conf is used
if args.conf:
from gitbuildsys.conf import configmgr
- configmgr.reset_from_conf(args.conf)
+ configmgr.add_conf(args.conf)
# Import target module and call 'main' from it
module = __import__("gitbuildsys.%s" % args.module, fromlist=[args.module])