Skip to content

Commit d34212b

Browse files
committed
refactor: remove self.encoding for better maintainability
1 parent a472bea commit d34212b

File tree

6 files changed

+26
-16
lines changed

6 files changed

+26
-16
lines changed

commitizen/changelog_formats/base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ def __init__(self, config: BaseConfig) -> None:
2424
# Constructor needs to be redefined because `Protocol` prevent instantiation by default
2525
# See: https://bugs.python.org/issue44807
2626
self.config = config
27-
self.encoding = self.config.settings["encoding"]
2827
self.tag_format = self.config.settings["tag_format"]
2928
self.tag_rules = TagRules(
3029
scheme=get_version_scheme(self.config.settings),
@@ -37,7 +36,9 @@ def get_metadata(self, filepath: str) -> Metadata:
3736
if not os.path.isfile(filepath):
3837
return Metadata()
3938

40-
with open(filepath, encoding=self.encoding) as changelog_file:
39+
with open(
40+
filepath, encoding=self.config.settings["encoding"]
41+
) as changelog_file:
4142
return self.get_metadata_from_file(changelog_file)
4243

4344
def get_metadata_from_file(self, file: IO[Any]) -> Metadata:

commitizen/commands/bump.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ def __init__(self, config: BaseConfig, arguments: BumpArgs) -> None:
6767
raise NotAGitProjectError()
6868

6969
self.config: BaseConfig = config
70-
self.encoding = config.settings["encoding"]
7170
self.arguments = arguments
7271
self.bump_settings = cast(
7372
BumpArgs,
@@ -335,7 +334,7 @@ def __call__(self) -> None:
335334
str(new_version),
336335
self.bump_settings["version_files"],
337336
check_consistency=self.check_consistency,
338-
encoding=self.encoding,
337+
encoding=self.config.settings["encoding"],
339338
)
340339
)
341340

commitizen/commands/changelog.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ def __init__(self, config: BaseConfig, arguments: ChangelogArgs) -> None:
6767
else changelog_file_name
6868
)
6969

70-
self.encoding = self.config.settings["encoding"]
7170
self.cz = factory.committer_factory(self.config)
7271

7372
self.start_rev = arguments.get("start_rev") or self.config.settings.get(
@@ -159,7 +158,9 @@ def _find_incremental_rev(self, latest_version: str, tags: Iterable[GitTag]) ->
159158
def _write_changelog(
160159
self, changelog_out: str, lines: list[str], changelog_meta: changelog.Metadata
161160
) -> None:
162-
with smart_open(self.file_name, "w", encoding=self.encoding) as changelog_file:
161+
with smart_open(
162+
self.file_name, "w", encoding=self.config.settings["encoding"]
163+
) as changelog_file:
163164
partial_changelog: str | None = None
164165
if self.incremental:
165166
new_lines = changelog.incremental_build(
@@ -261,7 +262,9 @@ def __call__(self) -> None:
261262

262263
lines = []
263264
if self.incremental and os.path.isfile(self.file_name):
264-
with open(self.file_name, encoding=self.encoding) as changelog_file:
265+
with open(
266+
self.file_name, encoding=self.config.settings["encoding"]
267+
) as changelog_file:
265268
lines = changelog_file.readlines()
266269

267270
self._write_changelog(changelog_out, lines, changelog_meta)

commitizen/commands/check.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ def __init__(self, config: BaseConfig, arguments: CheckArgs, *args: object) -> N
7171
self.commit_msg = sys.stdin.read()
7272

7373
self.config: BaseConfig = config
74-
self.encoding = config.settings["encoding"]
7574
self.cz = factory.committer_factory(self.config)
7675

7776
def __call__(self) -> None:
@@ -105,7 +104,9 @@ def _get_commit_message(self) -> str | None:
105104
# Get commit message from command line (--message)
106105
return self.commit_msg
107106

108-
with open(self.commit_msg_file, encoding=self.encoding) as commit_file:
107+
with open(
108+
self.commit_msg_file, encoding=self.config.settings["encoding"]
109+
) as commit_file:
109110
# Get commit message from file (--commit-msg-file)
110111
return commit_file.read()
111112

commitizen/commands/commit.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ def __init__(self, config: BaseConfig, arguments: CommitArgs) -> None:
4848
raise NotAGitProjectError()
4949

5050
self.config: BaseConfig = config
51-
self.encoding = config.settings["encoding"]
5251
self.cz = factory.committer_factory(self.config)
5352
self.arguments = arguments
5453
self.temp_file: str = get_backup_file_path()
@@ -59,7 +58,7 @@ def _read_backup_message(self) -> str | None:
5958
return None
6059

6160
# Read commit message from backup
62-
with open(self.temp_file, encoding=self.encoding) as f:
61+
with open(self.temp_file, encoding=self.config.settings["encoding"]) as f:
6362
return f.read().strip()
6463

6564
def _prompt_commit_questions(self) -> str:
@@ -146,7 +145,9 @@ def __call__(self) -> None:
146145
out.info(f"\n{m}\n")
147146

148147
if write_message_to_file:
149-
with smart_open(write_message_to_file, "w", encoding=self.encoding) as file:
148+
with smart_open(
149+
write_message_to_file, "w", encoding=self.config.settings["encoding"]
150+
) as file:
150151
file.write(m)
151152

152153
if dry_run:
@@ -160,7 +161,9 @@ def __call__(self) -> None:
160161
out.error(c.err)
161162

162163
# Create commit backup
163-
with smart_open(self.temp_file, "w", encoding=self.encoding) as f:
164+
with smart_open(
165+
self.temp_file, "w", encoding=self.config.settings["encoding"]
166+
) as f:
164167
f.write(m)
165168

166169
raise CommitError()

commitizen/commands/init.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ class Init:
121121

122122
def __init__(self, config: BaseConfig, *args: object) -> None:
123123
self.config: BaseConfig = config
124-
self.encoding = config.settings["encoding"]
125124
self.cz = factory.committer_factory(self.config)
126125
self.project_info = ProjectInfo()
127126

@@ -163,7 +162,9 @@ def __call__(self) -> None:
163162
if hook_types:
164163
config_data = self._get_config_data()
165164
with smart_open(
166-
self._PRE_COMMIT_CONFIG_PATH, "w", encoding=self.encoding
165+
self._PRE_COMMIT_CONFIG_PATH,
166+
"w",
167+
encoding=self.config.settings["encoding"],
167168
) as config_file:
168169
yaml.safe_dump(config_data, stream=config_file)
169170

@@ -355,7 +356,9 @@ def _get_config_data(self) -> dict[str, Any]:
355356
# .pre-commit-config.yaml does not exist
356357
return {"repos": [CZ_HOOK_CONFIG]}
357358

358-
with open(self._PRE_COMMIT_CONFIG_PATH, encoding=self.encoding) as config_file:
359+
with open(
360+
self._PRE_COMMIT_CONFIG_PATH, encoding=self.config.settings["encoding"]
361+
) as config_file:
359362
config_data: dict[str, Any] = yaml.safe_load(config_file) or {}
360363

361364
if not isinstance(repos := config_data.get("repos"), list):

0 commit comments

Comments
 (0)