Skip to content

Commit

Permalink
Merge pull request databio#10 from databio/dev
Browse files Browse the repository at this point in the history
0.2.1
  • Loading branch information
vreuter committed Jul 2, 2019
2 parents 951a07c + 9477ba6 commit 8b9e904
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
7 changes: 7 additions & 0 deletions docs/changelog.md
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion logmuse/_version.py
@@ -1 +1 @@
__version__ = "0.2.0"
__version__ = "0.2.1"
20 changes: 12 additions & 8 deletions logmuse/est.py
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -100,7 +101,9 @@ def logger_via_cli(opts, **kwargs):
try:
optval = getattr(opts, name)
except AttributeError:
raise AbsentOptionException(optname)
if strict:
raise AbsentOptionException(optname)
continue
else:
# Translate the option name if needed (i.e., for discordance
# between the CLI version and the logger setup signature).
Expand Down Expand Up @@ -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 = DEFAULT_STREAM
stream_loc = stream
elif stream in [sys.stderr, sys.stdout]:
stream_loc = stream
else:
try:
Expand Down
1 change: 0 additions & 1 deletion requirements/requirements-dev.txt
@@ -1,3 +1,2 @@
pytest==3.10.1
hypothesis

13 changes: 10 additions & 3 deletions tests/test_logger_via_cli.py
Expand Up @@ -10,6 +10,7 @@
LOGGING_CLI_OPTDATA, SILENCE_LOGS_OPTNAME, VERBOSITY_OPTNAME, \
_MIN_VERBOSITY, _MAX_VERBOSITY


__author__ = "Vince Reuter"
__email__ = "vreuter@virginia.edu"

Expand All @@ -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):
logger_via_cli(opts)
def create_logger():
return logger_via_cli(opts, strict=strict)
if strict:
with pytest.raises(AbsentOptionException):
create_logger()
else:
assert isinstance(create_logger(), logging.Logger)


def test_repeat_parser_configuration_is_exceptional(parser):
Expand Down

0 comments on commit 8b9e904

Please sign in to comment.