-
Notifications
You must be signed in to change notification settings - Fork 5
refactor: rewrite configuration handler #163
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,54 +1,147 @@ | ||
| import logging | ||
| from dataclasses import asdict, dataclass | ||
| from enum import Enum | ||
| from os import getcwd | ||
| from pathlib import Path | ||
| from typing import Any, Optional | ||
|
|
||
| from tomlkit import dumps, parse | ||
| from tomlkit import TOMLDocument, dumps, parse, table | ||
|
|
||
| from twyn.base.constants import DEFAULT_PROJECT_TOML_FILE | ||
| from twyn.base.constants import ( | ||
| DEFAULT_PROJECT_TOML_FILE, | ||
| DEFAULT_SELECTOR_METHOD, | ||
| AvailableLoggingLevels, | ||
| ) | ||
| from twyn.core.exceptions import ( | ||
| AllowlistPackageAlreadyExistsError, | ||
| AllowlistPackageDoesNotExistError, | ||
| TOMLError, | ||
| ) | ||
|
|
||
| logger = logging.getLogger() | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class TwynConfiguration: | ||
| """Fully resolved configuration for Twyn.""" | ||
|
|
||
| dependency_file: Optional[str] | ||
| selector_method: str | ||
| logging_level: AvailableLoggingLevels | ||
| allowlist: set[str] | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class ReadTwynConfiguration: | ||
| """Configuration for twyn as set by the user. It may have None values.""" | ||
|
|
||
| dependency_file: Optional[str] | ||
| selector_method: Optional[str] | ||
| logging_level: Optional[AvailableLoggingLevels] | ||
| allowlist: set[str] | ||
|
|
||
|
|
||
| class ConfigHandler: | ||
| """Read certain values into a central ConfigHandler object.""" | ||
| """Manage reading and writing configurations for Twyn.""" | ||
|
|
||
| def __init__(self, file_path: Optional[str] = None, enforce_file: bool = True): | ||
| self._file_path = file_path or DEFAULT_PROJECT_TOML_FILE | ||
| self._enforce_file = enforce_file | ||
| self._toml = self._get_toml_as_dict() | ||
| self._twyn_data = self._get_twyn_data() | ||
|
|
||
| self.dependency_file: Optional[str] = self._twyn_data.get("dependency_file") | ||
| self.selector_method: Optional[str] = self._twyn_data.get("selector_method") | ||
| self.logging_level: Optional[str] = self._twyn_data.get("logging_level") | ||
| self.allowlist: set[str] = set(self._twyn_data.get("allowlist", [])) | ||
| def resolve_config( | ||
| self, | ||
| selector_method: Optional[str] = None, | ||
| dependency_file: Optional[str] = None, | ||
| verbosity: AvailableLoggingLevels = AvailableLoggingLevels.none, | ||
| ) -> TwynConfiguration: | ||
| """Resolve the configuration for Twyn. | ||
|
|
||
| Given the cli flags it will return a fully resolved configuration for Twyn, | ||
| giving precedence to cli flags vs values set in the config files. | ||
|
|
||
| It will also handle default values, when appropriate. | ||
| """ | ||
| toml = self._read_toml() | ||
| twyn_config_data = self._get_twyn_data_from_toml(toml) | ||
|
|
||
| # Resolve the configuration so that it is ready to be used by Twyn, | ||
| # handling defaults etc | ||
| return TwynConfiguration( | ||
| dependency_file=dependency_file or twyn_config_data.get("dependency_file"), | ||
| selector_method=selector_method or twyn_config_data.get("selector_method", DEFAULT_SELECTOR_METHOD), | ||
| logging_level=_get_logging_level(verbosity, twyn_config_data.get("logging_level")), | ||
| allowlist=set(twyn_config_data.get("allowlist", set())), | ||
| ) | ||
|
|
||
| def add_package_to_allowlist(self, package_name: str) -> None: | ||
| if package_name in self.allowlist: | ||
| """Add a package to the allowlist configuration in the toml file.""" | ||
| toml = self._read_toml() | ||
| config = self._get_read_config(toml) | ||
| if package_name in config.allowlist: | ||
| raise AllowlistPackageAlreadyExistsError(package_name) | ||
|
|
||
| self._create_allowlist_in_toml_if_not_exists() | ||
|
|
||
| self._toml["tool"]["twyn"]["allowlist"].append(package_name) | ||
| self._write_toml() | ||
|
|
||
| logger.warning(f"Package '{package_name}' successfully added to allowlist") | ||
| new_config = ReadTwynConfiguration( | ||
| dependency_file=config.dependency_file, | ||
| selector_method=config.selector_method, | ||
| logging_level=config.logging_level, | ||
| allowlist=config.allowlist | {package_name}, | ||
| ) | ||
| self._write_config(toml, new_config) | ||
| logger.info(f"Package '{package_name}' successfully added to allowlist") | ||
|
|
||
| def remove_package_from_allowlist(self, package_name: str) -> None: | ||
| if package_name not in self.allowlist: | ||
| """Remove a package from the allowlist configuration in the toml file.""" | ||
| toml = self._read_toml() | ||
| config = self._get_read_config(toml) | ||
| if package_name not in config.allowlist: | ||
| raise AllowlistPackageDoesNotExistError(package_name) | ||
|
|
||
| self._toml["tool"]["twyn"]["allowlist"].remove(package_name) | ||
| self._write_toml() | ||
| logger.warning(f"Package '{package_name}' successfully removed from allowlist") | ||
| new_config = ReadTwynConfiguration( | ||
| dependency_file=config.dependency_file, | ||
| selector_method=config.selector_method, | ||
| logging_level=config.logging_level, | ||
| allowlist=config.allowlist - {package_name}, | ||
| ) | ||
| self._write_config(toml, new_config) | ||
| logger.info(f"Package '{package_name}' successfully removed from allowlist") | ||
|
|
||
| def _get_read_config(self, toml: TOMLDocument) -> ReadTwynConfiguration: | ||
| """Read the twyn configuration from a provided toml document.""" | ||
| twyn_config_data = self._get_twyn_data_from_toml(toml) | ||
| return ReadTwynConfiguration( | ||
| dependency_file=twyn_config_data.get("dependency_file"), | ||
| selector_method=twyn_config_data.get("selector_method"), | ||
| logging_level=twyn_config_data.get("logging_level"), | ||
| allowlist=set(twyn_config_data.get("allowlist", set())), | ||
| ) | ||
|
|
||
| def _write_config(self, toml: TOMLDocument, config: ReadTwynConfiguration) -> None: | ||
| """Write the configuration to the toml file. | ||
|
|
||
| All null values are simply omitted from the toml file. | ||
| """ | ||
| twyn_toml_data = asdict(config, dict_factory=lambda x: _serialize_config(x)) | ||
| if "tool" not in toml: | ||
| toml.add("tool", table()) | ||
| if "twyn" not in toml["tool"]: # type: ignore[operator] | ||
| toml["tool"]["twyn"] = {} # type: ignore[index] | ||
| toml["tool"]["twyn"] = twyn_toml_data # type: ignore[index] | ||
| self._write_toml(toml) | ||
|
|
||
| def _read_toml(self) -> TOMLDocument: | ||
| try: | ||
| fp = self._get_toml_file_pointer() | ||
| except FileNotFoundError: | ||
| if not self._enforce_file and self._file_path == DEFAULT_PROJECT_TOML_FILE: | ||
| return TOMLDocument() | ||
| raise TOMLError(f"Error reading toml from {self._file_path}") from None | ||
|
|
||
| with open(fp, "r") as f: | ||
| content = parse(f.read()) | ||
| return parse(dumps(content)) | ||
|
|
||
| def _get_twyn_data(self) -> dict[str, Any]: | ||
| return self._toml.get("tool", {}).get("twyn", {}) | ||
| def _get_twyn_data_from_toml(self, toml: TOMLDocument) -> dict[str, Any]: | ||
| return toml.get("tool", {}).get("twyn", {}) | ||
|
|
||
| def _get_toml_file_pointer(self) -> Path: | ||
| """Create a path for the toml file with the format <current working directory>/self.file_path.""" | ||
|
|
@@ -59,32 +152,35 @@ def _get_toml_file_pointer(self) -> Path: | |
|
|
||
| return fp | ||
|
|
||
| def _write_toml(self) -> None: | ||
| def _write_toml(self, toml: TOMLDocument) -> None: | ||
| with open(self._get_toml_file_pointer(), "w") as f: | ||
| f.write(dumps(self._toml)) | ||
|
|
||
| def _get_toml_as_dict(self) -> dict[str, Any]: | ||
| """Read TOML into a dictionary.""" | ||
| try: | ||
| fp = self._get_toml_file_pointer() | ||
| except FileNotFoundError: | ||
| if not self._enforce_file and self._file_path == DEFAULT_PROJECT_TOML_FILE: | ||
| return {} | ||
| raise | ||
|
|
||
| with open(fp, "r") as f: | ||
| content = parse(f.read()) | ||
| return parse(dumps(content)) | ||
|
|
||
| def _create_allowlist_in_toml_if_not_exists(self) -> None: | ||
| try: | ||
| isinstance(self._toml["tool"]["twyn"]["allowlist"], list) | ||
| except KeyError: | ||
| if "tool" not in self._toml: | ||
| self._toml["tool"] = {} | ||
|
|
||
| if "twyn" not in self._toml["tool"]: | ||
| self._toml["tool"]["twyn"] = {} | ||
|
|
||
| if "allowlist" not in self._toml["tool"]["twyn"]: | ||
| self._toml["tool"]["twyn"]["allowlist"] = [] | ||
| try: | ||
| f.write(dumps(toml)) | ||
| except Exception: | ||
| logger.exception("Error writing toml file") | ||
| raise TOMLError(f"Error writing toml to {self._file_path}") from None | ||
|
|
||
|
|
||
| def _get_logging_level( | ||
|
Collaborator
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. should this be inside the class?
Collaborator
Author
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. Since it does not use |
||
| cli_verbosity: AvailableLoggingLevels, | ||
| config_logging_level: Optional[str], | ||
| ) -> AvailableLoggingLevels: | ||
| """Return the appropriate logging level, considering that the one in config has less priority than the one passed directly.""" | ||
| if cli_verbosity is AvailableLoggingLevels.none: | ||
| if config_logging_level: | ||
| return AvailableLoggingLevels[config_logging_level.lower()] | ||
| else: | ||
| # default logging level | ||
| return AvailableLoggingLevels.warning | ||
| return cli_verbosity | ||
|
|
||
|
|
||
| def _serialize_config(x): | ||
| def _value_to_for_config(v): | ||
| if isinstance(v, Enum): | ||
| return v.name | ||
| elif isinstance(v, set): | ||
| return list(v) | ||
| return v | ||
|
|
||
| return {k: _value_to_for_config(v) for (k, v) in x if v is not None and v != set()} | ||
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.
I've been thinking for a while, could it be that this class' responsibilities are too broad?
For instance, right now this class both creates mechanisms to create config objects and interacting with the config file, would it make sense to split it?
Say, we have something that acts as a factory, just creating config objects, and then we have the config objects themselves, who are the ones that read and write to the file.
Maybe this is not the PR to make these changes, as some methods are mandatory both when creating the object and when interacting with the file (like
_read_toml) but this could be adapted whenever we move this class' implementation to use the FileHandler.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.
Overall agree that it would not hurt to separate the reading and the writing. Though I imagine they will need mechanisms to share stuff.
Having said that, this class has 3 public methods, and 150 lines or so. It's not too bad. I am not a fan of the config objects writing themselves though.
I do not really know how useful the filehandler will be for this, but I guess I'll see in the next PRs.