Skip to content

Commit 0fe602d

Browse files
committed
refactor: simplify code with pathlib Path object
1 parent bc1542c commit 0fe602d

File tree

13 files changed

+37
-46
lines changed

13 files changed

+37
-46
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: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,14 @@ def _resolve_config_candidates() -> list[BaseConfig]:
3434

3535

3636
def _create_config_from_path(path: Path) -> BaseConfig:
37-
with open(path, "rb") as f:
38-
data: bytes = f.read()
39-
40-
return create_config(data=data, path=path)
37+
with path.open("rb") as f:
38+
return create_config(data=f.read(), path=path)
4139

4240

4341
def read_cfg(filepath: str | None = None) -> BaseConfig:
4442
if filepath is not None:
4543
conf_path = Path(filepath)
46-
if not conf_path.exists():
44+
if not conf_path.is_file():
4745
raise ConfigFileNotFound()
4846
conf = _create_config_from_path(conf_path)
4947
if conf.is_empty_config:

commitizen/config/json_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def init_empty_config_content(self) -> None:
3232
json.dump({"commitizen": {}}, json_file)
3333

3434
def set_key(self, key: str, value: object) -> Self:
35-
with open(self.path, "rb") as f:
35+
with self.path.open("rb") as f:
3636
config_doc = json.load(f)
3737

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

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
@@ -28,25 +27,25 @@ def __init__(self, *, data: bytes | str, path: Path) -> None:
2827

2928
def init_empty_config_content(self) -> None:
3029
config_doc = TOMLDocument()
31-
if os.path.isfile(self.path):
32-
with open(self.path, "rb") as input_toml_file:
30+
if self.path.is_file():
31+
with self.path.open("rb") as input_toml_file:
3332
config_doc = parse(input_toml_file.read())
3433

3534
if config_doc.get("tool") is None:
3635
config_doc["tool"] = table()
3736
config_doc["tool"]["commitizen"] = table() # type: ignore[index]
3837

39-
with open(self.path, "wb") as output_toml_file:
38+
with self.path.open("wb") as output_toml_file:
4039
output_toml_file.write(
4140
config_doc.as_string().encode(self._settings["encoding"])
4241
)
4342

4443
def set_key(self, key: str, value: object) -> Self:
45-
with open(self.path, "rb") as f:
44+
with self.path.open("rb") as f:
4645
config_doc = parse(f.read())
4746

4847
config_doc["tool"]["commitizen"][key] = value # type: ignore[index]
49-
with open(self.path, "wb") as f:
48+
with self.path.open("wb") as f:
5049
f.write(config_doc.as_string().encode(self._settings["encoding"]))
5150

5251
return self

commitizen/config/yaml_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def _parse_setting(self, data: bytes | str) -> None:
5353
self.is_empty_config = True
5454

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

5959
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()

0 commit comments

Comments
 (0)