Skip to content

Commit

Permalink
Added indented logger to improve console output
Browse files Browse the repository at this point in the history
  • Loading branch information
coordt committed Dec 15, 2023
1 parent 4e68214 commit d1d19e3
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 88 deletions.
16 changes: 14 additions & 2 deletions bumpversion/bump.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Version changing methods."""
import logging
import shlex
from pathlib import Path
from typing import TYPE_CHECKING, ChainMap, List, Optional
Expand All @@ -12,9 +11,10 @@
from bumpversion.config.files import update_config_file
from bumpversion.config.files_legacy import update_ini_config_file
from bumpversion.exceptions import ConfigurationError
from bumpversion.ui import get_indented_logger
from bumpversion.utils import get_context, key_val_string

logger = logging.getLogger("bumpversion")
logger = get_indented_logger(__name__)


def get_next_version(
Expand All @@ -36,14 +36,18 @@ def get_next_version(
ConfigurationError: If it can't generate the next version.
"""
if new_version:
logger.info("Attempting to set new version '%s'", new_version)
logger.indent()
next_version = config.version_config.parse(new_version)
elif version_part:
logger.info("Attempting to increment part '%s'", version_part)
logger.indent()
next_version = current_version.bump(version_part, config.version_config.order)
else:
raise ConfigurationError("Unable to get the next version.")

logger.info("Values are now: %s", key_val_string(next_version.values))
logger.dedent()
return next_version


Expand All @@ -66,8 +70,13 @@ def do_bump(
"""
from bumpversion.files import modify_files, resolve_file_config

logger.indent()

ctx = get_context(config)
logger.info("Parsing current version '%s'", config.current_version)
logger.indent()
version = config.version_config.parse(config.current_version)
logger.dedent()
next_version = get_next_version(version, config, version_part, new_version)
next_version_str = config.version_config.serialize(next_version, ctx)
logger.info("New version will be '%s'", next_version_str)
Expand All @@ -76,6 +85,8 @@ def do_bump(
logger.info("Version is already '%s'", next_version_str)
return

logger.dedent()

if dry_run:
logger.info("Dry run active, won't touch any files.")

Expand All @@ -91,6 +102,7 @@ def do_bump(
ctx = get_context(config, version, next_version)
ctx["new_version"] = next_version_str
commit_and_tag(config, config_file, configured_files, ctx, dry_run)
logger.info("Done.")


def commit_and_tag(
Expand Down
6 changes: 3 additions & 3 deletions bumpversion/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""bump-my-version Command line interface."""
import logging
from typing import List, Optional

import rich_click as click
Expand All @@ -12,10 +11,10 @@
from bumpversion.config.files import find_config_file
from bumpversion.files import ConfiguredFile, modify_files
from bumpversion.show import do_show, log_list
from bumpversion.ui import print_warning, setup_logging
from bumpversion.ui import get_indented_logger, print_warning, setup_logging
from bumpversion.utils import get_context, get_overrides

logger = logging.getLogger(__name__)
logger = get_indented_logger(__name__)


@click.group(
Expand Down Expand Up @@ -307,6 +306,7 @@ def bump(
config.add_files(files)
config.included_paths = files

logger.dedent()
do_bump(version_part, new_version, config, found_config_file, dry_run)


Expand Down
9 changes: 7 additions & 2 deletions bumpversion/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
"""Configuration management."""
from __future__ import annotations

import logging
from typing import TYPE_CHECKING, Union

from bumpversion.config.files import read_config_file
from bumpversion.config.models import Config
from bumpversion.exceptions import ConfigurationError
from bumpversion.ui import get_indented_logger

if TYPE_CHECKING: # pragma: no-coverage
from pathlib import Path

logger = logging.getLogger(__name__)
logger = get_indented_logger(__name__)

DEFAULTS = {
"current_version": None,
Expand Down Expand Up @@ -49,6 +49,9 @@ def get_configuration(config_file: Union[str, Path, None] = None, **overrides) -
from bumpversion.config.utils import get_all_file_configs, get_all_part_configs
from bumpversion.scm import SCMInfo, SourceCodeManager, get_scm_info # noqa: F401

logger.info("Reading configuration")
logger.indent()

config_dict = DEFAULTS.copy()
parsed_config = read_config_file(config_file) if config_file else {}

Expand All @@ -75,6 +78,8 @@ def get_configuration(config_file: Union[str, Path, None] = None, **overrides) -
# Update and verify the current_version
config.current_version = check_current_version(config)

logger.dedent()

return config


Expand Down
14 changes: 8 additions & 6 deletions bumpversion/config/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

from __future__ import annotations

import logging
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, MutableMapping, Union

from bumpversion.config.files_legacy import read_ini_file
from bumpversion.ui import print_warning
from bumpversion.ui import get_indented_logger, print_warning

if TYPE_CHECKING: # pragma: no-coverage
from bumpversion.config.models import Config
from bumpversion.version_part import Version

logger = logging.getLogger(__name__)
logger = get_indented_logger(__name__)

CONFIG_FILE_SEARCH_ORDER = (
".bumpversion.cfg",
Expand Down Expand Up @@ -67,7 +66,7 @@ def read_config_file(config_file: Union[str, Path, None] = None) -> Dict[str, An
logger.info("Configuration file not found: %s.", config_path)
return {}

logger.info("Reading config file %s:", config_file)
logger.info("Reading config file: %s", config_file)

if config_path.suffix == ".cfg":
print_warning("The .cfg file format is deprecated. Please use .toml instead.")
Expand Down Expand Up @@ -120,9 +119,11 @@ def update_config_file(
from bumpversion.files import DataFileUpdater

if not config_file:
logger.info("No configuration file found to update.")
logger.info("\n%sNo configuration file found to update.", logger.indent_str)
return

else:
logger.info("\n%sProcessing config file: %s", logger.indent_str, config_file)
logger.indent()
config_path = Path(config_file)
if config_path.suffix != ".toml":
logger.info("You must have a `.toml` suffix to update the config file: %s.", config_path)
Expand All @@ -142,3 +143,4 @@ def update_config_file(

updater = DataFileUpdater(datafile_config, config.version_config.part_configs)
updater.update_file(current_version, new_version, context, dry_run)
logger.dedent()
5 changes: 3 additions & 2 deletions bumpversion/config/files_legacy.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""This module handles the legacy config file format."""
from __future__ import annotations

import logging
import re
from difflib import context_diff
from pathlib import Path
from typing import Any, Dict, Union

logger = logging.getLogger(__name__)
from bumpversion.ui import get_indented_logger

logger = get_indented_logger(__name__)


def read_ini_file(file_path: Path) -> Dict[str, Any]: # noqa: C901
Expand Down
7 changes: 7 additions & 0 deletions bumpversion/indented_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ def __init__(
if reset:
self.reset()

@property
def current_indent(self) -> int:
"""
The current indent level.
"""
return CURRENT_INDENT.get()

def indent(self, amount: int = 1) -> None:
"""
Increase the indent level by `amount`.
Expand Down
6 changes: 4 additions & 2 deletions bumpversion/scm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Version control system management."""

import logging
import os
import re
import subprocess
Expand All @@ -9,14 +8,15 @@
from tempfile import NamedTemporaryFile
from typing import TYPE_CHECKING, ClassVar, List, MutableMapping, Optional, Type, Union

from bumpversion.ui import get_indented_logger
from bumpversion.utils import extract_regex_flags

if TYPE_CHECKING: # pragma: no-coverage
from bumpversion.config import Config

from bumpversion.exceptions import DirtyWorkingDirectoryError, SignedTagsError

logger = logging.getLogger(__name__)
logger = get_indented_logger(__name__)


@dataclass
Expand Down Expand Up @@ -145,6 +145,7 @@ def commit_to_scm(
"Preparing" if do_commit else "Would prepare",
cls.__name__,
)
logger.indent()
for path in files:
logger.info(
"%s changes in file '%s' to %s",
Expand All @@ -171,6 +172,7 @@ def commit_to_scm(
new_version=context["new_version"],
extra_args=extra_args,
)
logger.dedent()

@classmethod
def tag_in_scm(cls, config: "Config", context: MutableMapping, dry_run: bool = False) -> None:
Expand Down
15 changes: 10 additions & 5 deletions bumpversion/version_part.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Module for managing Versions and their internal parts."""
import logging
import re
import string
from copy import copy
Expand All @@ -10,9 +9,10 @@
from bumpversion.config.models import VersionPartConfig
from bumpversion.exceptions import FormattingError, InvalidVersionPartError, MissingValueError
from bumpversion.functions import NumericFunction, PartFunction, ValuesFunction
from bumpversion.ui import get_indented_logger
from bumpversion.utils import key_val_string, labels_for_format

logger = logging.getLogger(__name__)
logger = get_indented_logger(__name__)


class VersionPart:
Expand Down Expand Up @@ -183,6 +183,7 @@ def parse(self, version_string: Optional[str] = None) -> Optional[Version]:
version_string,
regexp_one_line,
)
logger.indent()

match = self.parse_regex.search(version_string)

Expand All @@ -202,6 +203,7 @@ def parse(self, version_string: Optional[str] = None) -> Optional[Version]:
v = Version(_parsed, version_string)

logger.info("Parsed the following values: %s", key_val_string(v.values))
logger.dedent()

return v

Expand Down Expand Up @@ -268,8 +270,8 @@ def _serialize(
def _choose_serialize_format(self, version: Version, context: MutableMapping) -> str:
chosen = None

logger.debug("Available serialization formats: '%s'", "', '".join(self.serialize_formats))

logger.debug("Evaluating serialization formats")
logger.indent()
for serialize_format in self.serialize_formats:
try:
self._serialize(version, serialize_format, context, raise_if_incomplete=True)
Expand All @@ -291,7 +293,7 @@ def _choose_serialize_format(self, version: Version, context: MutableMapping) ->

if not chosen:
raise KeyError("Did not find suitable serialization format")

logger.dedent()
logger.debug("Selected serialization format '%s'", chosen)

return chosen
Expand All @@ -307,6 +309,9 @@ def serialize(self, version: Version, context: MutableMapping) -> str:
Returns:
The serialized version as a string
"""
logger.debug("Serializing version '%s'", version)
logger.indent()
serialized = self._serialize(version, self._choose_serialize_format(version, context), context)
logger.debug("Serialized to '%s'", serialized)
logger.dedent()
return serialized
Loading

0 comments on commit d1d19e3

Please sign in to comment.