fix: improve TOML error handing and docs for Windows paths (#31)

* fix: improve TOML error handing and documentation for Windows paths

* build(linting): pash ruff v2.0.260
This commit is contained in:
Nathaniel Landau
2023-03-30 10:33:51 -04:00
committed by GitHub
parent 5a4643ea8f
commit 4df10e785e
8 changed files with 42 additions and 29 deletions

View File

@@ -77,10 +77,11 @@ class Config:
VaultConfig(vault_name=key, vault_config=self.config[key]) for key in self.config
]
except TypeError as e:
log.error(f"Configuration file is invalid: '{self.config_path}'")
log.error(f"Configuration file is invalid: '{self.config_path}'\n{e}")
raise typer.Exit(code=1) from e
log.debug(f"Loaded configuration from '{self.config_path}'")
log.trace("Configuration:")
log.trace(self.config)
def __rich_repr__(self) -> rich.repr.Result: # pragma: no cover
@@ -91,10 +92,10 @@ class Config:
def _load_config(self) -> dict[str, Any]:
"""Load the configuration file."""
try:
with self.config_path.open(encoding="utf-8") as fp:
with self.config_path.open(mode="rt", encoding="utf-8") as fp:
return tomlkit.load(fp)
except tomlkit.exceptions.TOMLKitError as e:
alerts.error(f"Could not parse '{self.config_path}'")
alerts.error(f"Could not parse '{self.config_path}'\n{e}")
raise typer.Exit(code=1) from e
def _validate_config_path(self, config_path: Path | None) -> Path:
@@ -117,6 +118,8 @@ class Config:
["Vault 1"] # Name of the vault.
# Path to your obsidian vault
# Note for Windows users: Windows paths must use `\\` as the path separator due to a limitation with how TOML parses strings.
# Example: "C:\\Users\\username\\Documents\\Obsidian"
path = "{vault_path}"
# Folders within the vault to ignore when indexing metadata

View File

@@ -282,7 +282,9 @@ def remove_markdown_sections(
return text
def validate_csv_bulk_imports(csv_path: Path, note_paths: list) -> dict[str, list[dict[str, str]]]:
def validate_csv_bulk_imports( # noqa: C901
csv_path: Path, note_paths: list
) -> dict[str, list[dict[str, str]]]:
"""Validate the bulk import CSV file.
Args:

View File

@@ -319,7 +319,7 @@ class Vault:
return num_changed
def export_metadata(self, path: str, export_format: str = "csv") -> None:
def export_metadata(self, path: str, export_format: str = "csv") -> None: # noqa: C901
"""Write metadata to a csv file.
Args:
@@ -333,7 +333,7 @@ class Vault:
match export_format:
case "csv":
with export_file.open(mode="w", encoding="UTF8") as f:
with export_file.open(mode="w", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow(["Metadata Type", "Key", "Value"])
@@ -361,7 +361,7 @@ class Vault:
"tags": self.metadata.tags,
}
with export_file.open(mode="w", encoding="UTF8") as f:
with export_file.open(mode="w", encoding="utf-8") as f:
json.dump(dict_to_dump, f, indent=4, ensure_ascii=False, sort_keys=True)
def export_notes_to_csv(self, path: str) -> None:
@@ -375,7 +375,7 @@ class Vault:
alerts.error(f"Path does not exist: {export_file.parent}")
raise typer.Exit(code=1)
with export_file.open(mode="w", encoding="UTF8") as f:
with export_file.open(mode="w", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow(["path", "type", "key", "value"])