Skip to content

Commit 03abf0d

Browse files
committed
refactor(BaseFormat): merge ChangelogFormat into BaseFormat
1 parent 1bb40c7 commit 03abf0d

File tree

8 files changed

+68
-85
lines changed

8 files changed

+68
-85
lines changed
Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,28 @@
11
from __future__ import annotations
22

33
import sys
4-
from typing import Callable, ClassVar, Protocol
4+
from typing import Callable
5+
6+
from commitizen.changelog_formats.base import BaseFormat
57

68
if sys.version_info >= (3, 10):
79
from importlib import metadata
810
else:
911
import importlib_metadata as metadata
1012

11-
from commitizen.changelog import Metadata
1213
from commitizen.config.base_config import BaseConfig
1314
from commitizen.exceptions import ChangelogFormatUnknown
1415

1516
CHANGELOG_FORMAT_ENTRYPOINT = "commitizen.changelog_format"
16-
TEMPLATE_EXTENSION = "j2"
17-
18-
19-
class ChangelogFormat(Protocol):
20-
extension: ClassVar[str]
21-
"""Standard known extension associated with this format"""
22-
23-
alternative_extensions: ClassVar[set[str]]
24-
"""Known alternatives extensions for this format"""
25-
26-
config: BaseConfig
27-
28-
def __init__(self, config: BaseConfig) -> None:
29-
self.config = config
30-
31-
@property
32-
def ext(self) -> str:
33-
"""Dotted version of extensions, as in `pathlib` and `os` modules"""
34-
return f".{self.extension}"
35-
36-
@property
37-
def template(self) -> str:
38-
"""Expected template name for this format"""
39-
return f"CHANGELOG.{self.extension}.{TEMPLATE_EXTENSION}"
40-
41-
@property
42-
def default_changelog_file(self) -> str:
43-
return f"CHANGELOG.{self.extension}"
44-
45-
def get_metadata(self, filepath: str) -> Metadata:
46-
"""
47-
Extract the changelog metadata.
48-
"""
49-
raise NotImplementedError
5017

5118

52-
KNOWN_CHANGELOG_FORMATS: dict[str, type[ChangelogFormat]] = {
19+
KNOWN_CHANGELOG_FORMATS: dict[str, type[BaseFormat]] = {
5320
ep.name: ep.load()
5421
for ep in metadata.entry_points(group=CHANGELOG_FORMAT_ENTRYPOINT)
5522
}
5623

5724

58-
def get_changelog_format(
59-
config: BaseConfig, filename: str | None = None
60-
) -> ChangelogFormat:
25+
def get_changelog_format(config: BaseConfig, filename: str | None = None) -> BaseFormat:
6126
"""
6227
Get a format from its name
6328
@@ -74,7 +39,7 @@ def get_changelog_format(
7439
return format(config)
7540

7641

77-
def _guess_changelog_format(filename: str | None) -> type[ChangelogFormat] | None:
42+
def _guess_changelog_format(filename: str | None) -> type[BaseFormat] | None:
7843
"""
7944
Try guessing the file format from the filename.
8045
@@ -92,7 +57,7 @@ def _guess_changelog_format(filename: str | None) -> type[ChangelogFormat] | Non
9257
return None
9358

9459

95-
def __getattr__(name: str) -> Callable[[str], type[ChangelogFormat] | None]:
60+
def __getattr__(name: str) -> Callable[[str], type[BaseFormat] | None]:
9661
if name == "guess_changelog_format":
9762
return _guess_changelog_format
9863
raise AttributeError(f"module {__name__} has no attribute {name}")

commitizen/changelog_formats/base.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
from __future__ import annotations
22

33
import os
4-
from abc import ABCMeta
4+
from abc import ABCMeta, abstractmethod
55
from typing import IO, Any, ClassVar
66

77
from commitizen.changelog import Metadata
88
from commitizen.config.base_config import BaseConfig
99
from commitizen.tags import TagRules, VersionTag
1010
from commitizen.version_schemes import get_version_scheme
1111

12-
from . import ChangelogFormat
12+
TEMPLATE_EXTENSION = "j2"
1313

1414

15-
class BaseFormat(ChangelogFormat, metaclass=ABCMeta):
15+
class BaseFormat(metaclass=ABCMeta):
1616
"""
1717
Base class to extend to implement a changelog file format.
1818
"""
1919

2020
extension: ClassVar[str] = ""
21+
"""Standard known extension associated with this format"""
22+
2123
alternative_extensions: ClassVar[set[str]] = set()
24+
"""Known alternatives extensions for this format"""
2225

2326
def __init__(self, config: BaseConfig) -> None:
2427
# Constructor needs to be redefined because `Protocol` prevent instantiation by default
@@ -33,6 +36,20 @@ def __init__(self, config: BaseConfig) -> None:
3336
ignored_tag_formats=self.config.settings["ignored_tag_formats"],
3437
)
3538

39+
@property
40+
def ext(self) -> str:
41+
"""Dotted version of extensions, as in `pathlib` and `os` modules"""
42+
return f".{self.extension}"
43+
44+
@property
45+
def template(self) -> str:
46+
"""Expected template name for this format"""
47+
return f"CHANGELOG.{self.extension}.{TEMPLATE_EXTENSION}"
48+
49+
@property
50+
def default_changelog_file(self) -> str:
51+
return f"CHANGELOG.{self.extension}"
52+
3653
def get_metadata(self, filepath: str) -> Metadata:
3754
if not os.path.isfile(filepath):
3855
return Metadata()
@@ -69,18 +86,14 @@ def get_metadata_from_file(self, file: IO[Any]) -> Metadata:
6986

7087
return meta
7188

89+
@abstractmethod
7290
def parse_version_from_title(self, line: str) -> VersionTag | None:
7391
"""
7492
Extract the version from a title line if any
7593
"""
76-
raise NotImplementedError(
77-
"Default `get_metadata_from_file` requires `parse_version_from_changelog` to be implemented"
78-
)
7994

95+
@abstractmethod
8096
def parse_title_level(self, line: str) -> int | None:
8197
"""
8298
Get the title level/type of a line if any
8399
"""
84-
raise NotImplementedError(
85-
"Default `get_metadata_from_file` requires `parse_title_type_of_line` to be implemented"
86-
)

commitizen/changelog_formats/restructuredtext.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import IO, TYPE_CHECKING, Any, Union
66

77
from commitizen.changelog import Metadata
8+
from commitizen.tags import VersionTag
89

910
from .base import BaseFormat
1011

@@ -90,3 +91,9 @@ def is_underlined_title(self, first: str, second: str) -> bool:
9091
and not second.isalnum()
9192
and all(char == second[0] for char in second[1:])
9293
)
94+
95+
def parse_version_from_title(self, line: str) -> VersionTag | None:
96+
raise NotImplementedError("parse_version_from_title is not implemented")
97+
98+
def parse_title_level(self, line: str) -> int | None:
99+
raise NotImplementedError("parse_title_level is not implemented")

tests/commands/test_bump_command.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import commitizen.commands.bump as bump
1515
from commitizen import cli, cmd, defaults, git, hooks
16-
from commitizen.changelog_formats import ChangelogFormat
16+
from commitizen.changelog_formats import BaseFormat
1717
from commitizen.config.base_config import BaseConfig
1818
from commitizen.cz.base import BaseCommitizen
1919
from commitizen.exceptions import (
@@ -1289,7 +1289,7 @@ def test_bump_command_version_scheme_priority_over_version_type(mocker: MockFixt
12891289
def test_bump_template_option_precedence(
12901290
mocker: MockFixture,
12911291
tmp_commitizen_project: Path,
1292-
any_changelog_format: ChangelogFormat,
1292+
any_changelog_format: BaseFormat,
12931293
arg: str,
12941294
cfg: str,
12951295
expected: str,
@@ -1331,7 +1331,7 @@ def test_bump_template_option_precedence(
13311331
def test_bump_template_extras_precedence(
13321332
mocker: MockFixture,
13331333
tmp_commitizen_project: Path,
1334-
any_changelog_format: ChangelogFormat,
1334+
any_changelog_format: BaseFormat,
13351335
mock_plugin: BaseCommitizen,
13361336
):
13371337
project_root = Path(tmp_commitizen_project)
@@ -1376,7 +1376,7 @@ def test_bump_template_extras_precedence(
13761376
def test_bump_template_extra_quotes(
13771377
mocker: MockFixture,
13781378
tmp_commitizen_project: Path,
1379-
any_changelog_format: ChangelogFormat,
1379+
any_changelog_format: BaseFormat,
13801380
):
13811381
project_root = Path(tmp_commitizen_project)
13821382
changelog_tpl = project_root / any_changelog_format.template

tests/commands/test_changelog_command.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from commitizen import __file__ as commitizen_init
1414
from commitizen import cli, git
15-
from commitizen.changelog_formats import ChangelogFormat
15+
from commitizen.changelog_formats import BaseFormat
1616
from commitizen.commands.changelog import Changelog
1717
from commitizen.config.base_config import BaseConfig
1818
from commitizen.cz.base import BaseCommitizen
@@ -77,7 +77,7 @@ def test_changelog_with_different_cz(mocker: MockFixture, capsys, file_regressio
7777

7878
@pytest.mark.usefixtures("tmp_commitizen_project")
7979
def test_changelog_from_start(
80-
mocker: MockFixture, capsys, changelog_format: ChangelogFormat, file_regression
80+
mocker: MockFixture, capsys, changelog_format: BaseFormat, file_regression
8181
):
8282
create_file_and_commit("feat: new file")
8383
create_file_and_commit("refactor: is in changelog")
@@ -103,7 +103,7 @@ def test_changelog_from_start(
103103

104104
@pytest.mark.usefixtures("tmp_commitizen_project")
105105
def test_changelog_replacing_unreleased_using_incremental(
106-
mocker: MockFixture, capsys, changelog_format: ChangelogFormat, file_regression
106+
mocker: MockFixture, capsys, changelog_format: BaseFormat, file_regression
107107
):
108108
create_file_and_commit("feat: add new output")
109109
create_file_and_commit("fix: output glitch")
@@ -1484,7 +1484,7 @@ def test_changelog_from_current_version_tag_with_nonversion_tag(
14841484
def test_changelog_template_option_precedence(
14851485
mocker: MockFixture,
14861486
tmp_commitizen_project: Path,
1487-
any_changelog_format: ChangelogFormat,
1487+
any_changelog_format: BaseFormat,
14881488
arg: str,
14891489
cfg: str,
14901490
expected: str,
@@ -1527,7 +1527,7 @@ def test_changelog_template_extras_precedence(
15271527
mocker: MockFixture,
15281528
tmp_commitizen_project: Path,
15291529
mock_plugin: BaseCommitizen,
1530-
any_changelog_format: ChangelogFormat,
1530+
any_changelog_format: BaseFormat,
15311531
):
15321532
project_root = Path(tmp_commitizen_project)
15331533
changelog_tpl = project_root / any_changelog_format.template
@@ -1800,7 +1800,7 @@ def test_changelog_ignored_tags(
18001800
def test_changelog_template_extra_quotes(
18011801
mocker: MockFixture,
18021802
tmp_commitizen_project: Path,
1803-
any_changelog_format: ChangelogFormat,
1803+
any_changelog_format: BaseFormat,
18041804
):
18051805
project_root = Path(tmp_commitizen_project)
18061806
changelog_tpl = project_root / any_changelog_format.template
@@ -1836,7 +1836,7 @@ def test_changelog_template_extra_quotes(
18361836
def test_changelog_template_extra_weird_but_valid(
18371837
mocker: MockFixture,
18381838
tmp_commitizen_project: Path,
1839-
any_changelog_format: ChangelogFormat,
1839+
any_changelog_format: BaseFormat,
18401840
extra: str,
18411841
expected,
18421842
):
@@ -1858,7 +1858,7 @@ def test_changelog_template_extra_weird_but_valid(
18581858
def test_changelog_template_extra_bad_format(
18591859
mocker: MockFixture,
18601860
tmp_commitizen_project: Path,
1861-
any_changelog_format: ChangelogFormat,
1861+
any_changelog_format: BaseFormat,
18621862
extra: str,
18631863
):
18641864
project_root = Path(tmp_commitizen_project)
@@ -1876,7 +1876,7 @@ def test_changelog_template_extra_bad_format(
18761876
def test_export_changelog_template_from_default(
18771877
mocker: MockFixture,
18781878
tmp_commitizen_project: Path,
1879-
any_changelog_format: ChangelogFormat,
1879+
any_changelog_format: BaseFormat,
18801880
):
18811881
project_root = Path(tmp_commitizen_project)
18821882
target = project_root / "changelog.jinja"
@@ -1895,7 +1895,7 @@ def test_export_changelog_template_from_plugin(
18951895
mocker: MockFixture,
18961896
tmp_commitizen_project: Path,
18971897
mock_plugin: BaseCommitizen,
1898-
changelog_format: ChangelogFormat,
1898+
changelog_format: BaseFormat,
18991899
tmp_path: Path,
19001900
):
19011901
project_root = Path(tmp_commitizen_project)

tests/conftest.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from commitizen import cmd, defaults
1313
from commitizen.changelog_formats import (
14-
ChangelogFormat,
14+
BaseFormat,
1515
get_changelog_format,
1616
)
1717
from commitizen.config import BaseConfig
@@ -265,9 +265,7 @@ def mock_plugin(mocker: MockerFixture, config: BaseConfig) -> BaseCommitizen:
265265

266266

267267
@pytest.fixture(params=SUPPORTED_FORMATS)
268-
def changelog_format(
269-
config: BaseConfig, request: pytest.FixtureRequest
270-
) -> ChangelogFormat:
268+
def changelog_format(config: BaseConfig, request: pytest.FixtureRequest) -> BaseFormat:
271269
"""For tests relying on formats specifics"""
272270
format: str = request.param
273271
config.settings["changelog_format"] = format
@@ -279,7 +277,7 @@ def changelog_format(
279277

280278

281279
@pytest.fixture
282-
def any_changelog_format(config: BaseConfig) -> ChangelogFormat:
280+
def any_changelog_format(config: BaseConfig) -> BaseFormat:
283281
"""For test not relying on formats specifics, use the default"""
284282
config.settings["changelog_format"] = defaults.CHANGELOG_FORMAT
285283
return get_changelog_format(config)

0 commit comments

Comments
 (0)