Skip to content
Browse files
Merge pull request databio#10 from databio/dev
  • Loading branch information
vreuter committed Jul 2, 2019
2 parents 951a07c + 9477ba6 commit 8b9e90442a8ebc74ff1f5a0c963aaac7794e7b65
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
@@ -1,5 +1,12 @@
# Changelog

## [0.2.1] -- (2019-07-02)
### Added
- Control for strictness of requirement for client code to have called `add_logging_options` when
prior to using `logger_via_cli`
### Fixed
- Remove erroneous printing of invalid stream location message.

## [0.2.0] -- (2019-06-02)

### Changed
@@ -1 +1 @@
__version__ = "0.2.0"
__version__ = "0.2.1"
@@ -74,7 +74,7 @@ def add_logging_options(parser):
return parser

def logger_via_cli(opts, **kwargs):
def logger_via_cli(opts, strict=True, **kwargs):
Convenience function creating a logger.
@@ -84,11 +84,12 @@ def logger_via_cli(opts, **kwargs):
lack of burden, parsing values for the options supplied herein.
:param argparse.Namespace opts: command-line options/arguments.
:param bool strict: whether to raise an exception
:return logging.Logger: configured logger instance.
:raise pararead.logs.AbsentOptionException: if one of the expected options
isn't available in the given Namespace. Such a case suggests that a
client application didn't use this module to add the expected logging
options to a parser.
isn't available in the given Namespace, and the argument to the strict
parameter is True. Such a case suggests that a client application
didn't use this module to add the expected logging options to a parser.
# Within the key, translate the option name if needed. If it's not
# present within the translations mapping, use the original optname.
@@ -100,7 +101,9 @@ def logger_via_cli(opts, **kwargs):
optval = getattr(opts, name)
except AttributeError:
raise AbsentOptionException(optname)
if strict:
raise AbsentOptionException(optname)
# Translate the option name if needed (i.e., for discordance
# between the CLI version and the logger setup signature).
@@ -218,9 +221,10 @@ def init_logger(
# Create and add the handler, overwriting rather than appending.
handlers.append(logging.FileHandler(logfile, mode='w'))
if stream or not logfile:
stream = stream or DEFAULT_STREAM
# Deal with possible argument types.
if stream in [sys.stderr, sys.stdout]:
if not stream:
stream_loc = stream
elif stream in [sys.stderr, sys.stdout]:
stream_loc = stream
@@ -1,3 +1,2 @@

@@ -10,6 +10,7 @@

__author__ = "Vince Reuter"
__email__ = ""

@@ -21,14 +22,20 @@ def parser():

@pytest.mark.parametrize("missing", list(LOGGING_CLI_OPTDATA.keys()))
def test_opts_not_added(parser, missing):
@pytest.mark.parametrize("strict", [False, True])
def test_opts_not_added(parser, missing, strict):
""" Special exception occurs when it appears that log opts are absent. """
opts = parser.parse_args([])
assert all(hasattr(opts, _rawopt(n)) for n in LOGGING_CLI_OPTDATA)
delattr(opts, _rawopt(missing))
assert not hasattr(opts, _rawopt(missing))
with pytest.raises(AbsentOptionException):
def create_logger():
return logger_via_cli(opts, strict=strict)
if strict:
with pytest.raises(AbsentOptionException):
assert isinstance(create_logger(), logging.Logger)

def test_repeat_parser_configuration_is_exceptional(parser):

0 comments on commit 8b9e904

Please sign in to comment.