From 73420ab743fa5c890157fe474a8c606cdfaad084 Mon Sep 17 00:00:00 2001 From: Filippo Luca Ferretti <102977828+flferretti@users.noreply.github.com> Date: Mon, 11 Nov 2024 15:34:18 +0100 Subject: [PATCH] Refactor SDF handling logic for non-existing string paths --- src/rod/sdf/sdf.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/rod/sdf/sdf.py b/src/rod/sdf/sdf.py index 77cc680..74e133d 100644 --- a/src/rod/sdf/sdf.py +++ b/src/rod/sdf/sdf.py @@ -60,32 +60,31 @@ def load(sdf: pathlib.Path | str, is_urdf: bool | None = None) -> Sdf: The parsed SDF file. """ - # Handle the max path length depending on the OS + # Handle the max path length depending on the OS. try: from ctypes.wintypes import MAX_PATH except ValueError: MAX_PATH = os.pathconf("/", "PC_PATH_MAX") + # Check if the input is a string path. + is_str_path = getattr(sdf, "__len__", lambda: MAX_PATH + 1)() <= MAX_PATH + if is_urdf is not None: sdf_string = sdf else: match sdf: - # Case 1: It's a Path object - case pathlib.Path(): - sdf_string = sdf.read_text() - is_urdf = sdf.suffix == ".urdf" - - # Case 2: It's a string with a path - case str() if len(sdf) <= MAX_PATH and pathlib.Path(sdf).is_file(): - sdf_string = pathlib.Path(sdf).read_text(encoding="utf-8") - is_urdf = pathlib.Path(sdf).suffix == ".urdf" - - # Case 3: It's an SDF/URDF string - case str(): + # Case 1: Handle SDF/URDF string. + case str() if not is_str_path: sdf_string = sdf is_urdf = "