Skip to content

Commit

Permalink
Refactor SDF handling logic for non-existing string paths
Browse files Browse the repository at this point in the history
  • Loading branch information
flferretti committed Nov 11, 2024
1 parent b4a89fb commit 73420ab
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/rod/sdf/sdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<robot" in sdf_string

# Case 4: Raise an error for unsupported types
# Case 2: Handle pathlib.Path and string paths.
case pathlib.Path() | str():
path = pathlib.Path(sdf)
sdf_string = path.read_text(encoding="utf-8")
is_urdf = path.suffix == ".urdf"

# Case 3: Raise an error for unsupported types.
case _:
raise TypeError(f"Unsupported type for 'sdf': {type(sdf)}")

Expand Down

0 comments on commit 73420ab

Please sign in to comment.