-
-
Notifications
You must be signed in to change notification settings - Fork 302
refactor(BaseConfig): update docstring, extract factory method and remove unnecessary variable assignment #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
Merged
Merged
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
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.
Uh oh!
There was an error while loading. Please reload this page.