-
-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathcustomize.py
More file actions
73 lines (55 loc) · 2.42 KB
/
customize.py
File metadata and controls
73 lines (55 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from __future__ import annotations
from pathlib import Path
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from collections.abc import Mapping
from jinja2 import Template
from commitizen.config import BaseConfig
from commitizen.question import CzQuestion
else:
try:
from jinja2 import Template
except ImportError:
from string import Template
from commitizen import defaults
from commitizen.cz.base import BaseCommitizen
from commitizen.exceptions import MissingCzCustomizeConfigError
__all__ = ["CustomizeCommitsCz"]
class CustomizeCommitsCz(BaseCommitizen):
bump_pattern = defaults.BUMP_PATTERN
bump_map = defaults.BUMP_MAP
bump_map_major_version_zero = defaults.BUMP_MAP_MAJOR_VERSION_ZERO
change_type_order = defaults.CHANGE_TYPE_ORDER
def __init__(self, config: BaseConfig) -> None:
super().__init__(config)
if "customize" not in self.config.settings:
raise MissingCzCustomizeConfigError()
self.custom_settings = self.config.settings["customize"]
for attr_name in [
"bump_pattern",
"bump_map",
"bump_map_major_version_zero",
"change_type_order",
"commit_parser",
"changelog_pattern",
"change_type_map",
]:
if value := self.custom_settings.get(attr_name):
setattr(self, attr_name, value)
def questions(self) -> list[CzQuestion]:
return self.custom_settings.get("questions", [{}]) # type: ignore[return-value]
def message(self, answers: Mapping[str, Any]) -> str:
message_template = Template(self.custom_settings.get("message_template", ""))
if getattr(Template, "substitute", None):
return message_template.substitute(**answers) # type: ignore[attr-defined,no-any-return] # pragma: no cover # TODO: check if we can fix this
return message_template.render(**answers)
def example(self) -> str:
return self.custom_settings.get("example") or ""
def schema_pattern(self) -> str:
return self.custom_settings.get("schema_pattern") or ""
def schema(self) -> str:
return self.custom_settings.get("schema") or ""
def info(self) -> str:
if info_path := self.custom_settings.get("info_path"):
return Path(info_path).read_text(encoding=self.config.settings["encoding"])
return self.custom_settings.get("info") or ""