-
Notifications
You must be signed in to change notification settings - Fork 59
ITables can use an itables.toml configuration file
#432
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| """ | ||
| The ITables options can be modified through an itables.toml configuration file. | ||
|
|
||
| The location of that file can be specified through the ITABLES_CONFIG | ||
| environment variable (use an empty string for no config). Otherwise ITables will | ||
| look for an itables.toml file in the current directory and its parent | ||
| directories, for a tool.itables section in a pyproject.toml file in the | ||
| current or parent directories, and then for itables.toml in the user config directory. | ||
| """ | ||
|
|
||
| import os | ||
| from itertools import chain | ||
| from pathlib import Path | ||
| from typing import Any, Optional, cast | ||
|
|
||
| try: | ||
| import tomllib | ||
| except ImportError: | ||
| import tomli as tomllib # pyright: ignore[reportMissingImports] | ||
|
|
||
| from platformdirs import user_config_path | ||
|
|
||
| from itables.typing import ITableOptions, check_itable_arguments | ||
|
|
||
|
|
||
| def get_config_file(path: Path = Path.cwd()) -> Optional[Path]: | ||
| """Return the itables config file if found""" | ||
| if (config_file := os.getenv("ITABLES_CONFIG")) is not None: | ||
| if not config_file: | ||
| # Setting ITABLES_CONFIG to an empty string | ||
| # disables config file loading | ||
| return None | ||
|
|
||
| config_file = Path(config_file) | ||
| if not config_file.exists(): | ||
| raise FileNotFoundError( | ||
| f"ITables config file was not found: ITABLES_CONFIG={config_file}" | ||
| ) | ||
| return config_file | ||
|
|
||
| ceiling_directories = { | ||
| Path(path) | ||
| for path in os.getenv("ITABLES_CEILING_DIRECTORIES", "").split(":") | ||
| if path | ||
| } | ||
| for parent in chain([path], path.parents): | ||
| config_file = parent / "itables.toml" | ||
| if config_file.exists(): | ||
| return config_file | ||
| config_file = parent / "pyproject.toml" | ||
| if config_file.exists(): | ||
| with open(config_file, "rb") as fp: | ||
| config = tomllib.load(fp) | ||
| if "tool" not in config or "itables" not in config["tool"]: | ||
| continue | ||
| return config_file | ||
| if parent in ceiling_directories: | ||
| break | ||
|
|
||
| config_file = user_config_path("itables") / "itables.toml" | ||
| if config_file.exists(): | ||
| return config_file | ||
|
|
||
| return None | ||
|
|
||
|
|
||
| def load_config_file(config_file: Path) -> ITableOptions: | ||
| with open(config_file, "rb") as fp: | ||
| config = tomllib.load(fp) | ||
|
|
||
| if config_file.name == "pyproject.toml": | ||
| if "tool" not in config or "itables" not in config["tool"]: | ||
| raise ValueError( | ||
| f"This pyproject.toml file has no tool.itables section: {config_file}" | ||
| ) | ||
| config = config["tool"]["itables"] | ||
|
|
||
| check_itable_arguments(config, ITableOptions) | ||
| return cast(ITableOptions, config) | ||
|
|
||
|
|
||
| def set_options_from_config_file(options: dict[str, Any]) -> None: | ||
| if (config_file := get_config_file()) is not None: | ||
| config = load_config_file(config_file) | ||
| for key, value in config.items(): | ||
| options[key] = value | ||
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.
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.
platformdirsshould be added as a dependency:ImportError while loading conftest '/home/glefalher/nwork/itables/tests/conftest.py'. tests/conftest.py:4: in <module> from itables.sample_dfs import PANDAS_VERSION_MAJOR, get_dict_of_test_dfs src/itables/__init__.py:1: in <module> from itables import config, downsample, options, sample_dfs src/itables/config.py:24: in <module> from platformdirs import user_config_path E ModuleNotFoundError: No module named 'platformdirs'