Improve path handling.

This commit is contained in:
2026-11-12 15:01:23 -06:00
parent ad5f4293d1
commit 8189444231
5 changed files with 64 additions and 45 deletions

View File

@@ -13,17 +13,20 @@ from pathlib import Path
import yaml import yaml
CONFIG_PATH = "../../config.yaml" REPO_PATH = "../.."
CONFIG_PATH = "config.yaml"
safe: bool = True safe: bool = True
"""Default to disallowing evaluating Python from YAML files. Can be overridden """Default to disallowing evaluating Python from YAML files. Can be overridden
when calling AuDaGe from the command line with the argument `--unsafe`. when calling AuDaGe from the command line with the argument `--unsafe`.
""" """
config_path = resources.files().joinpath(CONFIG_PATH) # ty: ignore[invalid-argument-type]
config_path = Path(resources.files().joinpath(REPO_PATH, CONFIG_PATH)).resolve()
with config_path.open("r") as config_file: with config_path.open("r") as config_file:
folders: list[Path] = [ folders: list[Path] = [
Path(folder).resolve() # ty: ignore[invalid-argument-type]
Path(resources.files().joinpath(REPO_PATH, folder)).resolve()
for folder in yaml.safe_load(config_file.read())["folders"] for folder in yaml.safe_load(config_file.read())["folders"]
] ]

View File

@@ -10,6 +10,7 @@ from termcolor import cprint
import AuDaGe import AuDaGe
from AuDaGe.converter import Converter from AuDaGe.converter import Converter
from AuDaGe.file_utilities import breadcrumb_to_path, find_file
UNSAFE_WARNING = ( UNSAFE_WARNING = (
"You have chosen the option `unsafe`. Be aware (BEWARE!) that this allows " "You have chosen the option `unsafe`. Be aware (BEWARE!) that this allows "
@@ -91,22 +92,12 @@ def convert(args):
if args.unsafe: if args.unsafe:
AuDaGe.safe = False AuDaGe.safe = False
print(UNSAFE_WARNING) print(UNSAFE_WARNING)
for folder in AuDaGe.folders: input_format_path = find_file(
input_format_path = ( breadcrumb_to_path(args.input_format, ".yaml")
Path(folder, args.input_format.replace(".", "/"))
.with_suffix(".yaml")
.resolve()
) )
if input_format_path.is_file(): output_format_path = find_file(
break breadcrumb_to_path(args.output_format, ".yaml")
for folder in AuDaGe.folders:
output_format_path = (
Path(folder, args.output_format.replace(".", "/"))
.with_suffix(".yaml")
.resolve()
) )
if output_format_path.is_file():
break
input_converter = Converter(input_format_path) input_converter = Converter(input_format_path)
input_frame = input_converter.from_file(args.input_file.resolve()) input_frame = input_converter.from_file(args.input_file.resolve())
af = input_converter.to_AF(input_frame) af = input_converter.to_AF(input_frame)

View File

@@ -0,0 +1,24 @@
from os import PathLike, getcwd
from pathlib import Path
import AuDaGe
def breadcrumb_to_path(breadcrumb: str, extension: str = "") -> Path:
return Path(breadcrumb.replace(".", "/")).with_suffix(extension)
def find_file(
filepath: PathLike | Path | str, folders: list[Path] | None = None
) -> Path:
path = Path(filepath)
folders = [] if folders is None else folders
allfolders = AuDaGe.folders + folders + [Path(getcwd())]
for folder in allfolders:
fullpath = Path(folder, path).resolve()
if fullpath.exists():
return fullpath
raise FileNotFoundError(
f"The file {filepath} was not found in any of the configured folders "
f"({allfolders})."
)

View File

@@ -14,17 +14,19 @@ from collections.abc import Iterable
from dataclasses import dataclass from dataclasses import dataclass
from enum import IntEnum, StrEnum, auto from enum import IntEnum, StrEnum, auto
from importlib import resources from importlib import resources
from importlib.resources.abc import Traversable
from itertools import pairwise from itertools import pairwise
from pathlib import Path
import pandas as pd import pandas as pd
from numpy import nan from numpy import nan
import AuDaGe
from AuDaGe import frame, iec from AuDaGe import frame, iec
from AuDaGe.better_enum import BetterIntEnum from AuDaGe.better_enum import BetterIntEnum
from AuDaGe.debug_tools import MeasureTime, TimeUnit from AuDaGe.debug_tools import MeasureTime, TimeUnit
from AuDaGe.file_utilities import find_file
ASSETS_PATH = "AuDaGe.formats.controlexpert.assets" ASSETS_PATH = "../../extensions/controlexpert/assets"
MAX_RECURSION = 5 MAX_RECURSION = 5
@@ -35,22 +37,22 @@ class XSYBlock(frame.AuDaGeFrame):
from_file class method. from_file class method.
""" """
DEFAULT_SS_PATH = resources.files(ASSETS_PATH).joinpath( DEFAULT_SS_PATH = resources.files(AuDaGe).joinpath(
"import_datablock.xslt" ASSETS_PATH, "import_datablock.xslt"
) )
@classmethod @classmethod
def from_file( def from_file(
cls, cls,
path: Traversable, path: Path,
stylesheet_path: Traversable | None = None, stylesheet_path: Path | None = None,
name_ext: str = "", name_ext: str = "",
) -> XSYBlock: ) -> XSYBlock:
"""Load an XSY file into a dataframe using the specified stylesheet.""" """Load an XSY file into a dataframe using the specified stylesheet."""
cls.stylesheet_path = ( cls.stylesheet_path = (
cls.DEFAULT_SS_PATH if stylesheet_path is None else stylesheet_path cls.DEFAULT_SS_PATH if stylesheet_path is None else stylesheet_path
) )
cls.xsy_path = path cls.xsy_path = find_file(path)
with ( with (
resources.as_file(cls.xsy_path) as xsy_file, resources.as_file(cls.xsy_path) as xsy_file,
resources.as_file(cls.stylesheet_path) as stylesheet_file, resources.as_file(cls.stylesheet_path) as stylesheet_file,
@@ -85,11 +87,13 @@ class XSYBlock(frame.AuDaGeFrame):
class Definitions(XSYBlock): class Definitions(XSYBlock):
"""Read and parse the DDTSource nodes of an XSY file.""" """Read and parse the DDTSource nodes of an XSY file."""
DEFAULT_SS_PATH = resources.files(ASSETS_PATH).joinpath( # ty: ignore[invalid-argument-type]
"import_definitions.xslt" DEFAULT_SS_PATH = Path(
resources.files(AuDaGe).joinpath(ASSETS_PATH, "import_definitions.xslt")
) )
DEFAULT_BUILTINS_PATH = resources.files(ASSETS_PATH).joinpath( # ty: ignore[invalid-argument-type]
"builtins.xsy" DEFAULT_BUILTINS_PATH = Path(
resources.files(AuDaGe).joinpath(ASSETS_PATH, "builtins.xsy")
) )
class BoolParents(BetterIntEnum): class BoolParents(BetterIntEnum):
@@ -103,10 +107,10 @@ class Definitions(XSYBlock):
@classmethod @classmethod
def from_file( def from_file(
cls, cls,
path: Traversable, path: Path,
stylesheet_path: Traversable | None = None, stylesheet_path: Path | None = None,
name_ext: str = "Definitions", name_ext: str = "Definitions",
definition_paths: Iterable[Traversable] | None = None, definition_paths: Iterable[Path] | None = None,
include_builtins: bool = True, include_builtins: bool = True,
) -> Definitions: ) -> Definitions:
"""Read the DDTSource nodes of an XSY file into a DataFrame.""" """Read the DDTSource nodes of an XSY file into a DataFrame."""
@@ -544,9 +548,9 @@ class XSYReader:
def __init__( def __init__(
self, self,
definition_paths: Iterable[Traversable] | None = None, definition_paths: Iterable[Path] | None = None,
datablock_ss_path: Traversable | None = None, datablock_ss_path: Path | None = None,
definitions_ss_path: Traversable | None = None, definitions_ss_path: Path | None = None,
even_addresses: bool = True, even_addresses: bool = True,
): ):
"""Initialize XSYReader object with default definition files and """Initialize XSYReader object with default definition files and
@@ -574,10 +578,7 @@ class XSYReader:
self.even_addresses = even_addresses self.even_addresses = even_addresses
def read( def read(
self, self, path: Path, expand: bool = True, converters=None, **kwargs
path: Traversable,
expand: bool = True,
converters=None,
) -> XSYBlock: ) -> XSYBlock:
"""Read the dataBlock and definitions of an XSY file into an XSY """Read the dataBlock and definitions of an XSY file into an XSY
object. object.

View File

@@ -10,7 +10,7 @@ desc: Extensions to DataFrame for AuDaGe purposes.
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from collections.abc import Callable, Iterable from collections.abc import Callable, Iterable
from importlib.resources.abc import Traversable from pathlib import Path
from typing import Any from typing import Any
import pandas as pd import pandas as pd
@@ -181,7 +181,7 @@ class Frame(pd.DataFrame, metaclass=MetaFrame):
return cls.translate(cls, frame, translators, dropna_columns) return cls.translate(cls, frame, translators, dropna_columns)
@classmethod @classmethod
def from_file(cls, path: Traversable) -> Frame: def from_file(cls, path: Path) -> Frame:
"""Minimal implementation of the `from_file` method. Reads `path` as a """Minimal implementation of the `from_file` method. Reads `path` as a
CSV file into a dataframe and returns the latter. CSV file into a dataframe and returns the latter.
""" """
@@ -200,7 +200,7 @@ class Frame(pd.DataFrame, metaclass=MetaFrame):
AuDaGeFrame, self, translators, AuDaGeFrame.dropna_columns AuDaGeFrame, self, translators, AuDaGeFrame.dropna_columns
) )
def to_file(self, path: Traversable): def to_file(self, path: Path):
"""Minimal implementation of `to_file` method. Writes `frame` to `path` """Minimal implementation of `to_file` method. Writes `frame` to `path`
as a CSV. as a CSV.
""" """
@@ -240,9 +240,9 @@ class Reader(ABC):
""" """
@abstractmethod @abstractmethod
def read(self, path: Traversable) -> Frame: def read(self, path: Path) -> Frame:
"""Read a file into a Frame.""" """Read a file into a Frame."""
@abstractmethod @abstractmethod
def write(self, path: Traversable): def write(self, path: Path):
"""Write a Frame to a file.""" """Write a Frame to a file."""