From c1a3947359b0c4b734785d3eefa99204d601059b Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 4 Apr 2017 21:44:17 +0100 Subject: [PATCH] Setup variable verbosity --- md_pdf/args.py | 2 +- md_pdf/cli.py | 5 ++--- md_pdf/config/validate.py | 1 - md_pdf/logging.py | 18 ++++++++++++++++++ md_pdf/utils.py | 7 +++++++ 5 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 md_pdf/logging.py diff --git a/md_pdf/args.py b/md_pdf/args.py index 5da4175..ac54656 100644 --- a/md_pdf/args.py +++ b/md_pdf/args.py @@ -3,7 +3,7 @@ import argparse def parse_args(): parser = argparse.ArgumentParser() - parser.add_argument("-v", "--verbose", help="Display verbose output", action="store_true") + parser.add_argument("-v", "--verbose", help="Display verbose output", action="count", default=0) parser.add_argument("--update-csl", help="Update CSL files", action="store_true") parser.add_help = True return parser.parse_args() diff --git a/md_pdf/cli.py b/md_pdf/cli.py index 9eea044..d7c7965 100644 --- a/md_pdf/cli.py +++ b/md_pdf/cli.py @@ -1,17 +1,16 @@ import logging from md_pdf.args import parse_args from md_pdf.exceptions import PrematureExit, BaseException +from md_pdf.logging import set_verbosity from md_pdf.build import build from md_pdf.config.read import load_config from md_pdf.config.validate import validate_config from md_pdf.csl import check_csl, download_csl -FORMAT = "[%(levelname)s]: %(message)s" - def cli(): args = parse_args() - logging.basicConfig(format=FORMAT, level=logging.WARN if args.verbose else logging.INFO) + set_verbosity(args) try: if args.update_csl: download_csl() diff --git a/md_pdf/config/validate.py b/md_pdf/config/validate.py index 07096d5..3a66882 100644 --- a/md_pdf/config/validate.py +++ b/md_pdf/config/validate.py @@ -69,7 +69,6 @@ def validate_context(config): def validate_config(config): - logger.info("Validating Config...") for validator in [ check_required_keys, test_input, diff --git a/md_pdf/logging.py b/md_pdf/logging.py new file mode 100644 index 0000000..72733a2 --- /dev/null +++ b/md_pdf/logging.py @@ -0,0 +1,18 @@ +import logging +from md_pdf.utils import safe_list_get + + +FORMAT = "[%(levelname)s]: %(message)s" +LOG_LEVELS = [ + logging.WARN, + logging.INFO, + logging.DEBUG, + logging.NOTSET +] + + +def set_verbosity(args): + level = min(args.verbose, len(LOG_LEVELS) - 1) + verbosity = safe_list_get(LOG_LEVELS, level, logging.NOTSET) + logging.basicConfig(format=FORMAT, level=verbosity) + diff --git a/md_pdf/utils.py b/md_pdf/utils.py index 1009166..69a0e0b 100644 --- a/md_pdf/utils.py +++ b/md_pdf/utils.py @@ -12,3 +12,10 @@ def remove_dir(dir): os.rmdir(dir) except FileNotFoundError: pass + + +def safe_list_get(l, idx, default): + try: + return l[idx] + except IndexError: + return default