Skip to content

Commit

Permalink
fix: configure logging correctly for all of scriv but just scriv
Browse files Browse the repository at this point in the history
  • Loading branch information
Ned Batchelder committed Oct 18, 2023
1 parent a19026e commit 70360a4
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 30 deletions.
3 changes: 1 addition & 2 deletions src/scriv/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 1 addition & 4 deletions src/scriv/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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],
Expand Down
4 changes: 1 addition & 3 deletions src/scriv/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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:
"""
Expand Down
16 changes: 0 additions & 16 deletions src/scriv/exceptions.py
Original file line number Diff line number Diff line change
@@ -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
6 changes: 2 additions & 4 deletions src/scriv/ghrel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions src/scriv/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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
1 change: 0 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 70360a4

Please sign in to comment.