From 70360a4cdda0df658a7298b5f787a009e74bdfa8 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Wed, 18 Oct 2023 08:06:52 -0400 Subject: [PATCH] fix: configure logging correctly for all of scriv but just scriv --- src/scriv/cli.py | 3 +-- src/scriv/collect.py | 5 +---- src/scriv/create.py | 4 +--- src/scriv/exceptions.py | 16 ---------------- src/scriv/ghrel.py | 6 ++---- src/scriv/util.py | 28 ++++++++++++++++++++++++++++ tests/conftest.py | 1 - 7 files changed, 33 insertions(+), 30 deletions(-) diff --git a/src/scriv/cli.py b/src/scriv/cli.py index 6fc3ff6..f121b46 100644 --- a/src/scriv/cli.py +++ b/src/scriv/cli.py @@ -10,8 +10,7 @@ from .create import create from .ghrel import github_release -# Configure our logger, so all logging works, but only from our code. -click_log.basic_config(logging.getLogger("scriv")) +click_log.basic_config(logging.getLogger()) @click.group( diff --git a/src/scriv/collect.py b/src/scriv/collect.py index 1c5b03c..1fb39b3 100644 --- a/src/scriv/collect.py +++ b/src/scriv/collect.py @@ -5,12 +5,10 @@ from typing import Optional import click -import click_log -from .exceptions import scriv_command from .gitinfo import git_add, git_config_bool, git_edit, git_rm from .scriv import Scriv -from .util import Version +from .util import Version, scriv_command logger = logging.getLogger(__name__) @@ -35,7 +33,6 @@ @click.option( "--version", default=None, help="The version name to use for this entry." ) -@click_log.simple_verbosity_option() @scriv_command def collect( add: Optional[bool], diff --git a/src/scriv/create.py b/src/scriv/create.py index f82fecd..f30f86a 100644 --- a/src/scriv/create.py +++ b/src/scriv/create.py @@ -5,11 +5,10 @@ from typing import Optional import click -import click_log -from .exceptions import scriv_command from .gitinfo import git_add, git_config_bool, git_edit from .scriv import Scriv +from .util import scriv_command logger = logging.getLogger(__name__) @@ -23,7 +22,6 @@ default=None, help="Open the created file in your text editor.", ) -@click_log.simple_verbosity_option() @scriv_command def create(add: Optional[bool], edit: Optional[bool]) -> None: """ diff --git a/src/scriv/exceptions.py b/src/scriv/exceptions.py index 4d0b8ff..60fd24a 100644 --- a/src/scriv/exceptions.py +++ b/src/scriv/exceptions.py @@ -1,21 +1,5 @@ """Specialized exceptions for scriv.""" -import functools -import sys - class ScrivException(Exception): """Any exception raised by scriv.""" - - -def scriv_command(func): - """Decorate a command so that ScrivException ends cleanly (no traceback).""" - - @functools.wraps(func) - def _wrapped(*args, **kwargs): - try: - func(*args, **kwargs) - except ScrivException as exc: - sys.exit(str(exc)) - - return _wrapped diff --git a/src/scriv/ghrel.py b/src/scriv/ghrel.py index 95362c9..a700a21 100644 --- a/src/scriv/ghrel.py +++ b/src/scriv/ghrel.py @@ -5,16 +5,15 @@ from typing import Optional import click -import click_log import jinja2 -from .exceptions import ScrivException, scriv_command +from .exceptions import ScrivException from .github import create_release, get_releases, update_release from .gitinfo import get_github_repos from .linkcheck import check_markdown_links from .scriv import Scriv from .shell import run_simple_command -from .util import Version +from .util import Version, scriv_command logger = logging.getLogger(__name__) @@ -45,7 +44,6 @@ "--repo", help="The GitHub repo (owner/reponame) to create the release in.", ) -@click_log.simple_verbosity_option() @scriv_command def github_release( all_entries: bool, diff --git a/src/scriv/util.py b/src/scriv/util.py index 407940c..89d2251 100644 --- a/src/scriv/util.py +++ b/src/scriv/util.py @@ -3,9 +3,16 @@ from __future__ import annotations import collections +import functools +import logging import re +import sys from typing import Dict, Optional, Sequence, Tuple, TypeVar +import click_log + +from .exceptions import ScrivException + T = TypeVar("T") K = TypeVar("K") @@ -107,3 +114,24 @@ def is_prerelease(self) -> bool: # noqa: D400 m = re.fullmatch(VERSION_REGEX, self.vtext) assert m # the version must be a valid version return bool(m["pre"]) + + +def scriv_command(func): + """ + Decorate scriv commands to provide scriv-specific behavior. + + - ScrivExceptions don't show tracebacks + + - Set the log level from the command line for all of scriv. + + """ + + @functools.wraps(func) + @click_log.simple_verbosity_option(logging.getLogger("scriv")) + def _wrapped(*args, **kwargs): + try: + func(*args, **kwargs) + except ScrivException as exc: + sys.exit(str(exc)) + + return _wrapped diff --git a/tests/conftest.py b/tests/conftest.py index 1455987..18f3830 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -24,7 +24,6 @@ from .faker import FakeGit, FakeRunCommand - # Pytest will rewrite assertions in test modules, but not elsewhere. # This tells pytest to also rewrite assertions in these files: pytest.register_assert_rewrite("tests.helpers")