Skip to content

Commit 8d05b9e

Browse files
refactor: simplify code with pathlib Path object (#1840)
1 parent 0731f76 commit 8d05b9e

File tree

12 files changed

+35
-44
lines changed

12 files changed

+35
-44
lines changed

commitizen/changelog_formats/base.py

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

3-
import os
43
from abc import ABCMeta
4+
from pathlib import Path
55
from typing import IO, TYPE_CHECKING, Any, ClassVar
66

77
from commitizen.changelog import IncrementalMergeInfo, Metadata
@@ -36,12 +36,11 @@ def __init__(self, config: BaseConfig) -> None:
3636
)
3737

3838
def get_metadata(self, filepath: str) -> Metadata:
39-
if not os.path.isfile(filepath):
39+
file = Path(filepath)
40+
if not file.is_file():
4041
return Metadata()
4142

42-
with open(
43-
filepath, encoding=self.config.settings["encoding"]
44-
) as changelog_file:
43+
with file.open(encoding=self.config.settings["encoding"]) as changelog_file:
4544
return self.get_metadata_from_file(changelog_file)
4645

4746
def get_metadata_from_file(self, file: IO[Any]) -> Metadata:
@@ -74,12 +73,11 @@ def get_metadata_from_file(self, file: IO[Any]) -> Metadata:
7473
return meta
7574

7675
def get_latest_full_release(self, filepath: str) -> IncrementalMergeInfo:
77-
if not os.path.isfile(filepath):
76+
file = Path(filepath)
77+
if not file.is_file():
7878
return IncrementalMergeInfo()
7979

80-
with open(
81-
filepath, encoding=self.config.settings["encoding"]
82-
) as changelog_file:
80+
with file.open(encoding=self.config.settings["encoding"]) as changelog_file:
8381
return self.get_latest_full_release_from_file(changelog_file)
8482

8583
def get_latest_full_release_from_file(self, file: IO[Any]) -> IncrementalMergeInfo:

commitizen/commands/changelog.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from __future__ import annotations
22

3-
import os
4-
import os.path
53
from difflib import SequenceMatcher
64
from operator import itemgetter
75
from pathlib import Path
@@ -66,7 +64,7 @@ def __init__(self, config: BaseConfig, arguments: ChangelogArgs) -> None:
6664
f"or the setting `changelog_file` in {self.config.path}"
6765
)
6866
self.file_name = (
69-
os.path.join(str(self.config.path.parent), changelog_file_name)
67+
Path(self.config.path.parent, changelog_file_name).as_posix()
7068
if self.config.path is not None
7169
else changelog_file_name
7270
)
@@ -282,9 +280,10 @@ def __call__(self) -> None:
282280
raise DryRunExit()
283281

284282
lines = []
285-
if self.incremental and os.path.isfile(self.file_name):
286-
with open(
287-
self.file_name, encoding=self.config.settings["encoding"]
283+
changelog_path = Path(self.file_name)
284+
if self.incremental and changelog_path.is_file():
285+
with changelog_path.open(
286+
encoding=self.config.settings["encoding"]
288287
) as changelog_file:
289288
lines = changelog_file.readlines()
290289

commitizen/commands/commit.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ def _read_backup_message(self) -> str | None:
6161
return None
6262

6363
# Read commit message from backup
64-
with open(
65-
self.backup_file_path, encoding=self.config.settings["encoding"]
66-
) as f:
64+
with self.backup_file_path.open(encoding=self.config.settings["encoding"]) as f:
6765
return f.read().strip()
6866

6967
def _get_message_by_prompt_commit_questions(self) -> str:

commitizen/commands/init.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,12 @@ def _get_config_data(self) -> dict[str, Any]:
288288
],
289289
}
290290

291-
if not Path(".pre-commit-config.yaml").is_file():
291+
pre_commit_config_path = Path(self._PRE_COMMIT_CONFIG_PATH)
292+
if not pre_commit_config_path.is_file():
292293
return {"repos": [CZ_HOOK_CONFIG]}
293294

294-
with open(
295-
self._PRE_COMMIT_CONFIG_PATH, encoding=self.config.settings["encoding"]
295+
with pre_commit_config_path.open(
296+
encoding=self.config.settings["encoding"]
296297
) as config_file:
297298
config_data: dict[str, Any] = yaml.safe_load(config_file) or {}
298299

commitizen/config/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ def _resolve_config_candidates() -> list[BaseConfig]:
2828

2929

3030
def _create_config_from_path(path: Path) -> BaseConfig:
31-
with open(path, "rb") as f:
32-
data: bytes = f.read()
33-
34-
return create_config(data=data, path=path)
31+
with path.open("rb") as f:
32+
return create_config(data=f.read(), path=path)
3533

3634

3735
def read_cfg(filepath: str | None = None) -> BaseConfig:

commitizen/config/toml_config.py

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

3-
import os
43
from typing import TYPE_CHECKING
54

65
from tomlkit import TOMLDocument, exceptions, parse, table
@@ -33,25 +32,25 @@ def contains_commitizen_section(self) -> bool:
3332

3433
def init_empty_config_content(self) -> None:
3534
config_doc = TOMLDocument()
36-
if os.path.isfile(self.path):
37-
with open(self.path, "rb") as input_toml_file:
35+
if self.path.is_file():
36+
with self.path.open("rb") as input_toml_file:
3837
config_doc = parse(input_toml_file.read())
3938

4039
if config_doc.get("tool") is None:
4140
config_doc["tool"] = table()
4241
config_doc["tool"]["commitizen"] = table() # type: ignore[index]
4342

44-
with open(self.path, "wb") as output_toml_file:
43+
with self.path.open("wb") as output_toml_file:
4544
output_toml_file.write(
4645
config_doc.as_string().encode(self._settings["encoding"])
4746
)
4847

4948
def set_key(self, key: str, value: object) -> Self:
50-
with open(self.path, "rb") as f:
49+
with self.path.open("rb") as f:
5150
config_doc = parse(f.read())
5251

5352
config_doc["tool"]["commitizen"][key] = value # type: ignore[index]
54-
with open(self.path, "wb") as f:
53+
with self.path.open("wb") as f:
5554
f.write(config_doc.as_string().encode(self._settings["encoding"]))
5655

5756
return self

commitizen/config/yaml_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def _parse_setting(self, data: bytes | str) -> None:
5656
pass
5757

5858
def set_key(self, key: str, value: object) -> Self:
59-
with open(self.path, "rb") as yaml_file:
59+
with self.path.open("rb") as yaml_file:
6060
config_doc = yaml.load(yaml_file, Loader=yaml.FullLoader)
6161

6262
config_doc["commitizen"][key] = value

commitizen/cz/conventional_commits/conventional_commits.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
import os
3+
from pathlib import Path
44
from typing import TYPE_CHECKING, TypedDict
55

66
from commitizen import defaults
@@ -214,7 +214,6 @@ def schema_pattern(self) -> str:
214214
)
215215

216216
def info(self) -> str:
217-
dir_path = os.path.dirname(os.path.realpath(__file__))
218-
filepath = os.path.join(dir_path, "conventional_commits_info.txt")
219-
with open(filepath, encoding=self.config.settings["encoding"]) as f:
217+
filepath = Path(__file__).parent / "conventional_commits_info.txt"
218+
with filepath.open(encoding=self.config.settings["encoding"]) as f:
220219
return f.read()

commitizen/cz/jira/jira.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
import os
3+
from pathlib import Path
44
from typing import TYPE_CHECKING
55

66
from commitizen.cz.base import BaseCommitizen
@@ -73,7 +73,6 @@ def schema_pattern(self) -> str:
7373
return r".*[A-Z]{2,}\-[0-9]+( #| .* #).+( #.+)*"
7474

7575
def info(self) -> str:
76-
dir_path = os.path.dirname(os.path.realpath(__file__))
77-
filepath = os.path.join(dir_path, "jira_info.txt")
78-
with open(filepath, encoding=self.config.settings["encoding"]) as f:
76+
filepath = Path(__file__).parent / "jira_info.txt"
77+
with filepath.open(encoding=self.config.settings["encoding"]) as f:
7978
return f.read()

commitizen/providers/cargo_provider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def set(self, document: TOMLDocument, version: str) -> None:
3939

4040
def set_version(self, version: str) -> None:
4141
super().set_version(version)
42-
if self.lock_file.exists():
42+
if self.lock_file.is_file():
4343
self.set_lock_version(version)
4444

4545
def set_lock_version(self, version: str) -> None:

0 commit comments

Comments
 (0)