From 818944423147020596fe2b8af59098d692aa3db4 Mon Sep 17 00:00:00 2001 From: Joshua Date: Thu, 12 Nov 2026 15:01:23 -0600 Subject: [PATCH] Improve path handling. --- src/AuDaGe/__init__.py | 9 ++++-- src/AuDaGe/__main__.py | 23 ++++--------- src/AuDaGe/file_utilities.py | 24 ++++++++++++++ src/AuDaGe/formats/controlexpert/xsy.py | 43 +++++++++++++------------ src/AuDaGe/frame.py | 10 +++--- 5 files changed, 64 insertions(+), 45 deletions(-) create mode 100644 src/AuDaGe/file_utilities.py diff --git a/src/AuDaGe/__init__.py b/src/AuDaGe/__init__.py index d624231..b456f3c 100644 --- a/src/AuDaGe/__init__.py +++ b/src/AuDaGe/__init__.py @@ -13,17 +13,20 @@ from pathlib import Path import yaml -CONFIG_PATH = "../../config.yaml" +REPO_PATH = "../.." +CONFIG_PATH = "config.yaml" safe: bool = True """Default to disallowing evaluating Python from YAML files. Can be overridden 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: 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"] ] diff --git a/src/AuDaGe/__main__.py b/src/AuDaGe/__main__.py index 6dfcf2f..8b12ce5 100644 --- a/src/AuDaGe/__main__.py +++ b/src/AuDaGe/__main__.py @@ -10,6 +10,7 @@ from termcolor import cprint import AuDaGe from AuDaGe.converter import Converter +from AuDaGe.file_utilities import breadcrumb_to_path, find_file UNSAFE_WARNING = ( "You have chosen the option `unsafe`. Be aware (BEWARE!) that this allows " @@ -91,22 +92,12 @@ def convert(args): if args.unsafe: AuDaGe.safe = False print(UNSAFE_WARNING) - for folder in AuDaGe.folders: - input_format_path = ( - Path(folder, args.input_format.replace(".", "/")) - .with_suffix(".yaml") - .resolve() - ) - if input_format_path.is_file(): - break - 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_format_path = find_file( + breadcrumb_to_path(args.input_format, ".yaml") + ) + output_format_path = find_file( + breadcrumb_to_path(args.output_format, ".yaml") + ) input_converter = Converter(input_format_path) input_frame = input_converter.from_file(args.input_file.resolve()) af = input_converter.to_AF(input_frame) diff --git a/src/AuDaGe/file_utilities.py b/src/AuDaGe/file_utilities.py new file mode 100644 index 0000000..6ff52df --- /dev/null +++ b/src/AuDaGe/file_utilities.py @@ -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})." + ) diff --git a/src/AuDaGe/formats/controlexpert/xsy.py b/src/AuDaGe/formats/controlexpert/xsy.py index 2c34590..13e8f78 100644 --- a/src/AuDaGe/formats/controlexpert/xsy.py +++ b/src/AuDaGe/formats/controlexpert/xsy.py @@ -14,17 +14,19 @@ from collections.abc import Iterable from dataclasses import dataclass from enum import IntEnum, StrEnum, auto from importlib import resources -from importlib.resources.abc import Traversable from itertools import pairwise +from pathlib import Path import pandas as pd from numpy import nan +import AuDaGe from AuDaGe import frame, iec from AuDaGe.better_enum import BetterIntEnum 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 @@ -35,22 +37,22 @@ class XSYBlock(frame.AuDaGeFrame): from_file class method. """ - DEFAULT_SS_PATH = resources.files(ASSETS_PATH).joinpath( - "import_datablock.xslt" + DEFAULT_SS_PATH = resources.files(AuDaGe).joinpath( + ASSETS_PATH, "import_datablock.xslt" ) @classmethod def from_file( cls, - path: Traversable, - stylesheet_path: Traversable | None = None, + path: Path, + stylesheet_path: Path | None = None, name_ext: str = "", ) -> XSYBlock: """Load an XSY file into a dataframe using the specified stylesheet.""" cls.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 ( resources.as_file(cls.xsy_path) as xsy_file, resources.as_file(cls.stylesheet_path) as stylesheet_file, @@ -85,11 +87,13 @@ class XSYBlock(frame.AuDaGeFrame): class Definitions(XSYBlock): """Read and parse the DDTSource nodes of an XSY file.""" - DEFAULT_SS_PATH = resources.files(ASSETS_PATH).joinpath( - "import_definitions.xslt" + # ty: ignore[invalid-argument-type] + DEFAULT_SS_PATH = Path( + resources.files(AuDaGe).joinpath(ASSETS_PATH, "import_definitions.xslt") ) - DEFAULT_BUILTINS_PATH = resources.files(ASSETS_PATH).joinpath( - "builtins.xsy" + # ty: ignore[invalid-argument-type] + DEFAULT_BUILTINS_PATH = Path( + resources.files(AuDaGe).joinpath(ASSETS_PATH, "builtins.xsy") ) class BoolParents(BetterIntEnum): @@ -103,10 +107,10 @@ class Definitions(XSYBlock): @classmethod def from_file( cls, - path: Traversable, - stylesheet_path: Traversable | None = None, + path: Path, + stylesheet_path: Path | None = None, name_ext: str = "Definitions", - definition_paths: Iterable[Traversable] | None = None, + definition_paths: Iterable[Path] | None = None, include_builtins: bool = True, ) -> Definitions: """Read the DDTSource nodes of an XSY file into a DataFrame.""" @@ -544,9 +548,9 @@ class XSYReader: def __init__( self, - definition_paths: Iterable[Traversable] | None = None, - datablock_ss_path: Traversable | None = None, - definitions_ss_path: Traversable | None = None, + definition_paths: Iterable[Path] | None = None, + datablock_ss_path: Path | None = None, + definitions_ss_path: Path | None = None, even_addresses: bool = True, ): """Initialize XSYReader object with default definition files and @@ -574,10 +578,7 @@ class XSYReader: self.even_addresses = even_addresses def read( - self, - path: Traversable, - expand: bool = True, - converters=None, + self, path: Path, expand: bool = True, converters=None, **kwargs ) -> XSYBlock: """Read the dataBlock and definitions of an XSY file into an XSY object. diff --git a/src/AuDaGe/frame.py b/src/AuDaGe/frame.py index 7af33ac..14284c5 100644 --- a/src/AuDaGe/frame.py +++ b/src/AuDaGe/frame.py @@ -10,7 +10,7 @@ desc: Extensions to DataFrame for AuDaGe purposes. from abc import ABC, abstractmethod from collections.abc import Callable, Iterable -from importlib.resources.abc import Traversable +from pathlib import Path from typing import Any import pandas as pd @@ -181,7 +181,7 @@ class Frame(pd.DataFrame, metaclass=MetaFrame): return cls.translate(cls, frame, translators, dropna_columns) @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 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 ) - def to_file(self, path: Traversable): + def to_file(self, path: Path): """Minimal implementation of `to_file` method. Writes `frame` to `path` as a CSV. """ @@ -240,9 +240,9 @@ class Reader(ABC): """ @abstractmethod - def read(self, path: Traversable) -> Frame: + def read(self, path: Path) -> Frame: """Read a file into a Frame.""" @abstractmethod - def write(self, path: Traversable): + def write(self, path: Path): """Write a Frame to a file."""