from .app import PpApplication
-__version__ = '0.3.1'
+__version__ = '0.3.2'
LOG = logging.getLogger(__name__)
self, appname=None, verbose=0, version=__version__, base_dir=None,
initialized=None, usage=None, description=None,
argparse_epilog=None, argparse_prefix_chars='-', env_prefix=None,
- cfg_dir=None, cfg_stem=None, cfg_encoding='utf-8', need_config_file=False):
+ cfg_dir=None, cfg_stems=None, cfg_encoding='utf-8', need_config_file=False):
self._cfg_encoding = cfg_encoding
self._need_config_file = bool(need_config_file)
self.cfg = RecursiveDictionary()
self._cfg_dir = None
- self._cfg_stem = None
+ self.cfg_stems = []
self.cfg_files = []
super(PpConfigApplication, self).__init__(
)
if cfg_dir is None:
- self._cfg_dir = self.appname
+ self._cfg_dir = 'pixelpark'
else:
d = str(cfg_dir).strip()
if d == '':
else:
self._cfg_dir = d
- if cfg_stem:
- s = str(cfg_stem).strip()
- if not s:
- msg = "Invalid configuration stem {!r} given.".format(cfg_stem)
- raise PbCfgAppError(msg)
- self._cfg_stem = s
+ if cfg_stems:
+ if isinstance(cfg_stems, list):
+ for stem in cfg_stems:
+ s = str(stem).strip()
+ if not s:
+ msg = "Invalid configuration stem {!r} given.".format(stem)
+ raise PbCfgAppError(msg)
+ self.cfg_stems.append(s)
+ else:
+ s = str(cfg_stems).strip()
+ if not s:
+ msg = "Invalid configuration stem {!r} given.".format(cfg_stems)
+ raise PbCfgAppError(msg)
+ self.cfg_stems.append(s)
else:
- self._cfg_stem = self.appname
+ self.cfg_stems = self.appname
self.init_cfgfiles()
"""The directory containing the configuration files."""
return self._cfg_dir
- # -----------------------------------------------------------
- @property
- def cfg_stem(self):
- """The basename of the configuration file without any file extension."""
- return self._cfg_stem
-
# -------------------------------------------------------------------------
def as_dict(self, short=True):
"""
res['need_config_file'] = self.need_config_file
res['cfg_encoding'] = self.cfg_encoding
res['cfg_dir'] = self.cfg_dir
- res['cfg_stem'] = self.cfg_stem
return res
"""
self.arg_parser.add_argument(
"-C", "--cfgfile", "--cfg-file", "--config",
- metavar="FILE",
- dest="cfg_file",
- help="Configuration file to use additional to the standard configuration files.",
+ metavar="FILE", nargs='+', dest="cfg_file",
+ help="Configuration files to use additional to the standard configuration files.",
)
self.arg_parser.add_argument(
"--cfg-encoding",
- metavar="ENCODING",
- dest="cfg_encoding",
- default=self.cfg_encoding,
+ metavar="ENCODING", dest="cfg_encoding", default=self.cfg_encoding,
help=("The encoding character set of the configuration files "
"(default: %(default)r)."),
)
"""Method to generate the self.cfg_files list."""
self.cfg_files = []
- cfg_basename = '{}.ini'.format(self.cfg_stem)
+
+ cfg_basenames = []
+ for stem in self.cfg_stems:
+ cfg_basename = '{}.ini'.format(stem)
+ cfg_basenames.append(cfg_basename)
# add /etc/app/app.ini or $VIRTUAL_ENV/etc/app/app.ini
etc_dir = os.sep + 'etc'
if 'VIRTUAL_ENV' in os.environ:
etc_dir = os.path.join(os.environ['VIRTUAL_ENV'], 'etc')
- syscfg_fn = None
- if self.cfg_dir:
- syscfg_fn = os.path.join(etc_dir, self.cfg_dir, cfg_basename)
- else:
- syscfg_fn = os.path.join(etc_dir, cfg_basename)
- self.cfg_files.append(syscfg_fn)
+ for cfg_basename in cfg_basenames:
+ syscfg_fn = None
+ if self.cfg_dir:
+ syscfg_fn = os.path.join(etc_dir, self.cfg_dir, cfg_basename)
+ else:
+ syscfg_fn = os.path.join(etc_dir, cfg_basename)
+ self.cfg_files.append(syscfg_fn)
# add <WORKDIR>/etc/app.ini
mod_dir = os.path.dirname(__file__)
work_etc_dir = os.path.join(work_dir, 'etc')
if self.verbose > 1:
LOG.debug("Searching for {!r} ...".format(work_etc_dir))
- self.cfg_files.append(os.path.join(work_etc_dir, cfg_basename))
+ for cfg_basename in cfg_basenames:
+ self.cfg_files.append(os.path.join(work_etc_dir, cfg_basename))
# add $HOME/.config/app.ini
usercfg_fn = None
user_cfg_dir = os.path.join(user_cfg_dir, self.cfg_dir)
if self.verbose > 1:
LOG.debug("user_cfg_dir: {!r}".format(user_cfg_dir))
- usercfg_fn = os.path.join(user_cfg_dir, cfg_basename)
- self.cfg_files.append(usercfg_fn)
+ for cfg_basename in cfg_basenames:
+ usercfg_fn = os.path.join(user_cfg_dir, cfg_basename)
+ self.cfg_files.append(usercfg_fn)
# add a configfile given on command line with --cfg-file
cmdline_cfg = getattr(self.args, 'cfg_file', None)
if cmdline_cfg:
- self.cfg_files.append(cmdline_cfg)
+ for usercfg_fn in cmdline_cfg:
+ self.cfg_files.append(usercfg_fn)
# -------------------------------------------------------------------------
def _read_config(self):
with open(cfg_file, 'r', **open_opts) as fh:
stream = StringIO("[default]\n" + fh.read())
if six.PY2:
- config.readfp(stram)
+ config.readfp(stream)
else:
- config.read_file(stram)
+ config.read_file(stream)
except ConfigParseError as e:
msg = "Wrong configuration in {!r} found: ".format(cfg_file)
msg += str(e)