diff --git a/src/rod/sdf/sdf.py b/src/rod/sdf/sdf.py index 77cc680..a0a5e25 100644 --- a/src/rod/sdf/sdf.py +++ b/src/rod/sdf/sdf.py @@ -1,7 +1,6 @@ from __future__ import annotations import dataclasses -import os import pathlib import mashumaro @@ -60,34 +59,28 @@ 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 - try: - from ctypes.wintypes import MAX_PATH - except ValueError: - MAX_PATH = os.pathconf("/", "PC_PATH_MAX") - - 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" + path = pathlib.Path(sdf) - # Case 3: It's an SDF/URDF string - case str(): + match sdf: + # Case 1: Handle strings. + case str(): + if path.suffix: + # Assuming that if the string has a suffix, it is a path. + sdf_string = pathlib.Path(sdf).read_text(encoding="utf-8") + else: + # Otherwise, it is an SDF string. sdf_string = sdf - is_urdf = " None: with pytest.raises(RuntimeError): _ = rod.Sdf.load(sdf=urdf_path.read_text(), is_urdf=False) + # Check that it fails is is_urdf=True and the resource is a non-existing path + with pytest.raises(FileNotFoundError): + _ = rod.Sdf.load(sdf="/non/existing/path.sdf", is_urdf=False) + + # Check that it fails is is_urdf=True and the resource is an empty string + with pytest.raises(FileNotFoundError): + _ = rod.Sdf.load(sdf=pathlib.Path("/non/existing/path.sdf"), is_urdf=False) + # The following instead should succeed _ = rod.Sdf.load(sdf=urdf_path, is_urdf=None) _ = rod.Sdf.load(sdf=urdf_path, is_urdf=True) _ = rod.Sdf.load(sdf=str(urdf_path), is_urdf=None) _ = rod.Sdf.load(sdf=str(urdf_path), is_urdf=True) _ = rod.Sdf.load(sdf=urdf_path.read_text(), is_urdf=True) + _ = rod.Sdf.load( + sdf=" \ + \ + \ + \ + \ + \ + \ + \ + \ + " + ) # Load once again the urdf rod_sdf = rod.Sdf.load(sdf=urdf_path)