Skip to content

Commit

Permalink
Handle a configuration file specified on the command line.
Browse files Browse the repository at this point in the history
 * pyrseas/cmdargs.py (cmd_parser): Initialize global _cfg here.
   (parse_args): If config file specified, load it and merge it.
 * pyrseas/config.py (Config.__init__): Use new merge method.
   (Config.merge): New method to merge configuration dicts.
 * tests/test_config.py: New test.
  • Loading branch information
jmafc committed Aug 23, 2013
1 parent c1d4263 commit 3227634
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
10 changes: 9 additions & 1 deletion pyrseas/cmdargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
from argparse import ArgumentParser, FileType
import getpass

import yaml

from pyrseas.config import Config

_cfg = Config()
_cfg = None

HELP_TEXT = {
'host': "database server host or socket directory",
Expand All @@ -30,9 +32,13 @@ def cmd_parser(description, version):
:param version: version of the caller
:return: the created parser
"""
global _cfg

parent = ArgumentParser(add_help=False)
parent.add_argument('dbname', help='database name')
group = parent.add_argument_group('Connection options')
if _cfg is None:
_cfg = Config()
cfg = _cfg['database'] if 'database' in _cfg else {}
group.add_argument('-H', '--host', **_help_dflt('host', cfg))
group.add_argument('-p', '--port', type=int, **_help_dflt('port', cfg))
Expand Down Expand Up @@ -70,5 +76,7 @@ def parse_args(parser):
for key in ['output', 'config']:
_cfg['files'][key] = args[key]
del args[key]
if 'config' in _cfg['files'] and _cfg['files']['config']:
_cfg.merge(yaml.safe_load(_cfg['files']['config']))
_cfg['options'] = arg_opts
return _cfg
17 changes: 9 additions & 8 deletions pyrseas/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ def __init__(self, sys_only=False):
os.path.join(os.path.dirname(__file__), '..', 'config'))))
if sys_only:
return
cfg = _load_cfg("PYRSEAS_USER_CONFIG",
os.path.join(_home_dir(), 'pyrseas'))
for key, val in list(cfg.items()):
if key in self:
self[key].update(val)
else:
self[key] = val
cfg = _load_cfg("PYRSEAS_REPO_DIR", None)
self.merge(_load_cfg("PYRSEAS_USER_CONFIG",
os.path.join(_home_dir(), 'pyrseas')))
self.merge(_load_cfg("PYRSEAS_REPO_DIR", None))

def merge(self, cfg):
"""Merge extra configuration
:param cfg: extra configuration (dict)
"""
for key, val in list(cfg.items()):
if key in self:
self[key].update(val)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,15 @@ def test_repo_config(tmpdir):
os.environ["PYRSEAS_REPO_DIR"] = tmpdir.strpath
cfg = Config()
assert cfg['schema public'] == CFG_TABLE_DATA


def test_cmd_parser(tmpdir):
"Test parsing a configuration file specified on the command line"
f = tmpdir.join(CFG_FILE)
f.write(yamldump(CFG_DATA))
sys.argv = ['testprog', 'testdb', '--config', f.strpath]
os.environ["PYRSEAS_USER_CONFIG"] = ''
os.environ["PYRSEAS_REPO_DIR"] = ''
parser = cmd_parser("Test description", '0.0.1')
cfg = parse_args(parser)
assert cfg['schema public'] == CFG_TABLE_DATA

0 comments on commit 3227634

Please sign in to comment.