-
-
Notifications
You must be signed in to change notification settings - Fork 299
refactor(BaseConfig): update docstring, extract factory method and re… #1601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bearomorphism
wants to merge
1
commit into
commitizen-tools:v4-9-2
Choose a base branch
from
bearomorphism:refactor-config
base: v4-9-2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,46 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import Generator | ||
from pathlib import Path | ||
|
||
from commitizen import defaults, git | ||
from commitizen.config.factory import create_config | ||
from commitizen.exceptions import ConfigFileIsEmpty, ConfigFileNotFound | ||
|
||
from .base_config import BaseConfig | ||
from .json_config import JsonConfig | ||
from .toml_config import TomlConfig | ||
from .yaml_config import YAMLConfig | ||
|
||
|
||
def read_cfg(filepath: str | None = None) -> BaseConfig: | ||
conf = BaseConfig() | ||
|
||
def _resolve_config_paths(filepath: str | None = None) -> Generator[Path, None, None]: | ||
if filepath is not None: | ||
if not Path(filepath).exists(): | ||
out_path = Path(filepath) | ||
if not out_path.exists(): | ||
raise ConfigFileNotFound() | ||
|
||
cfg_paths = (path for path in (Path(filepath),)) | ||
else: | ||
git_project_root = git.find_git_project_root() | ||
cfg_search_paths = [Path(".")] | ||
if git_project_root: | ||
cfg_search_paths.append(git_project_root) | ||
yield out_path | ||
return | ||
|
||
cfg_paths = ( | ||
path / Path(filename) | ||
for path in cfg_search_paths | ||
for filename in defaults.CONFIG_FILES | ||
) | ||
git_project_root = git.find_git_project_root() | ||
cfg_search_paths = [Path(".")] | ||
if git_project_root: | ||
cfg_search_paths.append(git_project_root) | ||
|
||
for filename in cfg_paths: | ||
if not filename.exists(): | ||
continue | ||
for path in cfg_search_paths: | ||
for filename in defaults.CONFIG_FILES: | ||
out_path = path / Path(filename) | ||
if out_path.exists(): | ||
yield out_path | ||
|
||
_conf: TomlConfig | JsonConfig | YAMLConfig | ||
|
||
def read_cfg(filepath: str | None = None) -> BaseConfig: | ||
for filename in _resolve_config_paths(filepath): | ||
with open(filename, "rb") as f: | ||
data: bytes = f.read() | ||
|
||
if "toml" in filename.suffix: | ||
_conf = TomlConfig(data=data, path=filename) | ||
elif "json" in filename.suffix: | ||
_conf = JsonConfig(data=data, path=filename) | ||
elif "yaml" in filename.suffix: | ||
_conf = YAMLConfig(data=data, path=filename) | ||
conf = create_config(data=data, path=filename) | ||
if not conf.is_empty_config: | ||
return conf | ||
|
||
if filepath is not None and _conf.is_empty_config: | ||
if filepath is not None: | ||
raise ConfigFileIsEmpty() | ||
elif _conf.is_empty_config: | ||
continue | ||
else: | ||
conf = _conf | ||
break | ||
|
||
return conf | ||
return BaseConfig() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,8 +17,8 @@ | |
|
||
class BaseConfig: | ||
def __init__(self) -> None: | ||
self.is_empty_config = False | ||
self._settings: Settings = DEFAULT_SETTINGS.copy() | ||
self.encoding = self.settings["encoding"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why this member variable exists. It will be stale if |
||
self._path: Path | None = None | ||
|
||
@property | ||
|
@@ -30,7 +30,7 @@ def path(self) -> Path: | |
return self._path # type: ignore[return-value] | ||
|
||
@path.setter | ||
def path(self, path: str | Path) -> None: | ||
def path(self, path: Path) -> None: | ||
self._path = Path(path) | ||
|
||
def set_key(self, key: str, value: Any) -> Self: | ||
|
@@ -48,4 +48,8 @@ def _parse_setting(self, data: bytes | str) -> None: | |
raise NotImplementedError() | ||
|
||
def init_empty_config_content(self) -> None: | ||
"""Create a config file with the empty config content. | ||
|
||
The implementation is different for each config file type. | ||
""" | ||
raise NotImplementedError() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from __future__ import annotations | ||
|
||
from pathlib import Path | ||
|
||
from commitizen.config.base_config import BaseConfig | ||
from commitizen.config.json_config import JsonConfig | ||
from commitizen.config.toml_config import TomlConfig | ||
from commitizen.config.yaml_config import YAMLConfig | ||
|
||
|
||
def create_config(*, data: bytes | str | None = None, path: Path) -> BaseConfig: | ||
if "toml" in path.suffix: | ||
return TomlConfig(data=data or "", path=path) | ||
if "json" in path.suffix: | ||
return JsonConfig(data=data or "{}", path=path) | ||
if "yaml" in path.suffix: | ||
return YAMLConfig(data=data or "", path=path) | ||
|
||
# Should be unreachable. See the constant CONFIG_FILES. | ||
raise ValueError( | ||
f"Unsupported config file: {path.name} due to unknown file extension" | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be an abstract class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, a
Protocol
, but it doesn't hurt like thisUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I found it odd that we can create
BaseConfig
objects and it's sometimes confusing. I attempted to refactor it on another branch (not published yet) but many tests would be affected.