fix: resolve CI linting and type checking issues
Some checks failed
CI / test (push) Has been cancelled
Some checks failed
CI / test (push) Has been cancelled
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
"""TOML format converter."""
|
"""TOML format converter."""
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Dict
|
from typing import Any, Dict, Union
|
||||||
|
|
||||||
import tomlkit
|
import tomlkit
|
||||||
|
import tomlkit.exceptions
|
||||||
from tomlkit import TOMLDocument
|
from tomlkit import TOMLDocument
|
||||||
|
|
||||||
from config_converter.converters.base import BaseConverter, ConversionError
|
from config_converter.converters.base import BaseConverter, ConversionError
|
||||||
@@ -15,7 +16,7 @@ class TomlConverter(BaseConverter):
|
|||||||
FORMAT_NAME = "toml"
|
FORMAT_NAME = "toml"
|
||||||
FILE_EXTENSIONS = ["toml"]
|
FILE_EXTENSIONS = ["toml"]
|
||||||
|
|
||||||
def read(self, source: str | Path) -> Dict[str, Any]:
|
def read(self, source: Union[str, Path]) -> Dict[str, Any]:
|
||||||
"""Read and parse a TOML configuration file."""
|
"""Read and parse a TOML configuration file."""
|
||||||
try:
|
try:
|
||||||
with open(source, "r", encoding="utf-8") as f:
|
with open(source, "r", encoding="utf-8") as f:
|
||||||
@@ -27,7 +28,7 @@ class TomlConverter(BaseConverter):
|
|||||||
except PermissionError as e:
|
except PermissionError as e:
|
||||||
raise ConversionError(f"Permission denied: {source}") from e
|
raise ConversionError(f"Permission denied: {source}") from e
|
||||||
|
|
||||||
def write(self, data: Dict[str, Any], target: str | Path) -> None:
|
def write(self, data: Dict[str, Any], target: Union[str, Path]) -> None:
|
||||||
"""Write configuration data to a TOML file."""
|
"""Write configuration data to a TOML file."""
|
||||||
try:
|
try:
|
||||||
doc: TOMLDocument = tomlkit.document()
|
doc: TOMLDocument = tomlkit.document()
|
||||||
@@ -35,7 +36,7 @@ class TomlConverter(BaseConverter):
|
|||||||
doc[key] = value
|
doc[key] = value
|
||||||
with open(target, "w", encoding="utf-8") as f:
|
with open(target, "w", encoding="utf-8") as f:
|
||||||
f.write(tomlkit.dumps(doc))
|
f.write(tomlkit.dumps(doc))
|
||||||
except (OSError, TypeError, tomlkit.exceptions.TypeError) as e:
|
except (OSError, TypeError) as e:
|
||||||
raise ConversionError(f"Failed to write TOML to {target}: {e}") from e
|
raise ConversionError(f"Failed to write TOML to {target}: {e}") from e
|
||||||
|
|
||||||
def parse(self, content: str) -> Dict[str, Any]:
|
def parse(self, content: str) -> Dict[str, Any]:
|
||||||
@@ -52,5 +53,5 @@ class TomlConverter(BaseConverter):
|
|||||||
for key, value in data.items():
|
for key, value in data.items():
|
||||||
doc[key] = value
|
doc[key] = value
|
||||||
return tomlkit.dumps(doc)
|
return tomlkit.dumps(doc)
|
||||||
except (TypeError, ValueError, tomlkit.exceptions.TypeError) as e:
|
except (TypeError, ValueError) as e:
|
||||||
raise ConversionError(f"Failed to format data as TOML: {e}") from e
|
raise ConversionError(f"Failed to format data as TOML: {e}") from e
|
||||||
|
|||||||
Reference in New Issue
Block a user