-
Notifications
You must be signed in to change notification settings - Fork 6
IFC-1811: Replace toml package with tomllib and tomli optionally #551
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
Changes from 1 commit
f942609
b792772
db2e183
2ea0aeb
4ebe000
1b7e6a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,11 +4,15 @@ | |||||||||||||
|
|
||||||||||||||
| from pathlib import Path | ||||||||||||||
|
|
||||||||||||||
| import toml | ||||||||||||||
| import typer | ||||||||||||||
| from pydantic import Field, ValidationError, field_validator | ||||||||||||||
| from pydantic_settings import BaseSettings, SettingsConfigDict | ||||||||||||||
|
|
||||||||||||||
| try: | ||||||||||||||
| import tomllib | ||||||||||||||
| except ModuleNotFoundError: | ||||||||||||||
| import tomli as tomllib | ||||||||||||||
|
|
||||||||||||||
| DEFAULT_CONFIG_FILE = "infrahubctl.toml" | ||||||||||||||
| ENVVAR_CONFIG_FILE = "INFRAHUBCTL_CONFIG" | ||||||||||||||
| INFRAHUB_REPO_CONFIG_FILE = ".infrahub.yml" | ||||||||||||||
|
|
@@ -59,7 +63,7 @@ def load(self, config_file: str | Path = "infrahubctl.toml", config_data: dict | | |||||||||||||
|
|
||||||||||||||
| if config_file.is_file(): | ||||||||||||||
| config_string = config_file.read_text(encoding="utf-8") | ||||||||||||||
| config_tmp = toml.loads(config_string) | ||||||||||||||
| config_tmp = tomllib.loads(config_string) | ||||||||||||||
|
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. Handle TOML parse errors and fix doc typos. Currently TOML decode errors bubble unhandled; load_and_exit only catches ValidationError. Catch tomllib.TOMLDecodeError (works for both tomllib and tomli alias) and show a clear message. Also fix typos and align parameter names; prefer DEFAULT_CONFIG_FILE constant in signatures. Apply: - def load(self, config_file: str | Path = "infrahubctl.toml", config_data: dict | None = None) -> None:
+ def load(self, config_file: str | Path = DEFAULT_CONFIG_FILE, config_data: dict | None = None) -> None:
@@
- if config_file.is_file():
- config_string = config_file.read_text(encoding="utf-8")
- config_tmp = tomllib.loads(config_string)
+ if config_file.is_file():
+ config_string = config_file.read_text(encoding="utf-8")
+ try:
+ config_tmp = tomllib.loads(config_string)
+ except tomllib.TOMLDecodeError as exc:
+ print(f"Configuration file parse error: {config_file} — {exc}") # consider typer.secho
+ raise typer.Abort()
@@
- def load_and_exit(self, config_file: str | Path = "infrahubctl.toml", config_data: dict | None = None) -> None:
+ def load_and_exit(self, config_file: str | Path = DEFAULT_CONFIG_FILE, config_data: dict | None = None) -> None:
@@
- """Calls load, but wraps it in a try except block.
+ """Calls load, but wraps it in a try/except block.
@@
- This is done to handle a ValidationErorr which is raised when settings are specified but invalid.
+ This handles a ValidationError when settings are specified but invalid.
@@
- config_file_name (str, optional): [description]. Defaults to "pyprojectctl.toml".
+ config_file (str | Path, optional): Path to TOML config. Defaults to "infrahubctl.toml".
@@
- except ValidationError as exc:
+ except ValidationError as exc:
print(f"Configuration not valid, found {len(exc.errors())} error(s)")
for error in exc.errors():
loc_str = [str(item) for item in error["loc"]]
print(f" {'/'.join(loc_str)} | {error['msg']} ({error['type']})")
raise typer.Abort()Optional: replace print with typer.echo/secho for CLI UX. 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||
|
|
||||||||||||||
| self._settings = Settings(**config_tmp) | ||||||||||||||
| return | ||||||||||||||
|
|
||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.