Parsersο
URDF, XACRO, and SRDF parsers for converting files into Python objects.
URDF Parserο
URDFParser Classο
- class linkforge.core.URDFParser(max_file_size=104857600, sandbox_root=None, resource_resolver=None)[source]ο
Bases:
RobotXMLParser[Robot]Refined URDF Parser using a class-based interface.
- Parameters:
max_file_size (
int)resource_resolver (
IResourceResolver|None)
- __init__(max_file_size=104857600, sandbox_root=None, resource_resolver=None)[source]ο
Initialize parser.
- Parameters:
max_file_size (
int) β Maximum allowed file size in bytes (default: 100MB)sandbox_root (
Path|None) β Optional root directory for security sandboxresource_resolver (
IResourceResolver|None) β Optional resolver for URIs
- parse(filepath, **_kwargs)[source]ο
Parse URDF file into a Robot model using iterative parsing.
This implementation uses iterparse to maintain O(1) memory complexity even for massive URDF files.
- Parameters:
- Return type:
- Returns:
The generic Robot model (Intermediate Representation)
- Raises:
RobotParserIOError β If file cannot be read or exceeds size limit
XacroDetectedError β If a XACRO file is passed instead of URDF
RobotParserXMLRootError β If root tag is not <robot>
RobotParserUnexpectedError β If XML is malformed or internal error occurs
- parse_string(content, source_directory=None, **_kwargs)[source]ο
Parse URDF from string.
- Parameters:
- Return type:
- Returns:
The generic Robot model (Intermediate Representation)
- Raises:
RobotParserIOError β If string exceeds size limit
XacroDetectedError β If string contains XACRO markers
RobotParserXMLRootError β If root tag is not <robot>
RobotParserUnexpectedError β If XML parsing fails
XACRO Parserο
XACROParser Classο
The XACROParser provides native, pure-Python resolution of XACRO files. It handles macros, properties, math expressions, and conditional blocks without external ROS dependencies.
- class linkforge.core.XACROParser[source]ο
Bases:
objectIndependent XACRO resolution utility.
This class acts as a pre-processor for URDF/SRDF files that use the XACRO templating system. It resolves templates into plain XML strings.
- resolve(filepath, **kwargs)[source]ο
Resolve a XACRO file into a plain XML string.
- Parameters:
- Return type:
- Returns:
The fully resolved XML as a string.
- Raises:
RobotXacroError β If resolution fails.
RobotXacroRecursionError β If circular includes are detected.
XacroResolver Class (Internal)ο
The internal engine used by XACROParser for hierarchical property resolution and macro substitution.
- class linkforge.core.XacroResolver(search_paths=None, max_depth=2000, start_dir=None)[source]ο
Bases:
objectLightweight XACRO resolver with macro and math support.
- __init__(search_paths=None, max_depth=2000, start_dir=None)[source]ο
Initialize the XACRO resolver.
- resolve_file(filepath)[source]ο
Resolve a XACRO file and return the final XML string.
- Parameters:
filepath (
Path) β Path to the XACRO file to resolve.- Return type:
- Returns:
The fully resolved XML as a string.
- Raises:
RobotXacroError β If resolution fails or internal error occurs
RobotXacroRecursionError β If circular dependencies are found
- resolve_string(xml_string)[source]ο
Resolve a XACRO string and return the final XML string.
- Parameters:
xml_string (
str) β The XACRO XML content as a string.- Return type:
- Returns:
The fully resolved XML as a string.
- Raises:
RobotXacroError β If XML is malformed or resolution fails
- resolve_element(element)[source]ο
Process a single element recursively, tracking depth.
- Parameters:
element (
Element) β The XML element to resolve.- Return type:
- Returns:
The resolved XML element or a container.
- Raises:
RobotXacroRecursionError β If maximum recursion depth is exceeded
Note
Structural Caching: XacroResolver implements a two-phase approach for
large modular robot cascades. In the Structural Phase, all xacro:include tags are
resolved once into an in-memory template tree. In the Evaluation Phase, arguments and
conditional blocks are injected into the cached tree. This means a single Xacro file can
be evaluated many times with different parameters (e.g., different prefix= values for
two arms) without re-reading or re-parsing any files.
RobotXMLParser Class (Base)ο
- class linkforge.core.parsers.xml_base.RobotXMLParser(max_file_size=104857600, sandbox_root=None, resource_resolver=None)[source]ο
Bases:
RobotParser[T],Generic[T]Abstract base class for XML-based robotics format parsers.
- Parameters:
max_file_size (
int)resource_resolver (
IResourceResolver|None)
- __init__(max_file_size=104857600, sandbox_root=None, resource_resolver=None)[source]ο
Initialize base XML parser.
- Parameters:
max_file_size (
int) β Maximum allowed file size in bytessandbox_root (
Path|None) β Optional root directory for security sandboxresource_resolver (
IResourceResolver|None) β Optional resolver for URIs
- parse_xacro(filepath, **kwargs)[source]ο
Resolve XACRO then parse the resulting XML string.
This is a convenience wrapper around XACROParser.resolve() + parse_string().
- Parameters:
- Return type:
TypeVar(T)- Returns:
The parsed robot model (T).
- Raises:
RobotXacroError β If XACRO resolution fails.
RobotParserError β If XML parsing fails.
SRDF Parserο
The SRDF parser is documented with the rest of the SRDF layer (models, parser, generator) on the dedicated SRDF reference page.
Usage Examplesο
Parse XACRO Fileο
To resolve a XACRO file into a plain XML string (format-agnostic):
from linkforge.core import XACROParser
from pathlib import Path
# Returns a plain XML string
xml_string = XACROParser().resolve(Path("robot.urdf.xacro"))
To parse a XACRO file directly into a Robot model (canonical usage):
from linkforge.core import URDFParser
from pathlib import Path
# Natively resolves XACRO then parses URDF
robot = URDFParser().parse_xacro(Path("robot.urdf.xacro"))
print(f"Loaded robot: {robot.name}")
Parse URDF Fileο
from linkforge.core import URDFParser
from pathlib import Path
robot = URDFParser().parse(Path("my_robot.urdf"))
print(f"Loaded robot: {robot.name}")
Parse URDF Stringο
from linkforge.core import URDFParser
urdf_content = """<?xml version="1.0"?>
<robot name="simple_robot">
<link name="base_link"/>
</robot>"""
robot = URDFParser().parse_string(urdf_content)
Robustness & Securityο
The parser includes professional-grade protections for production robotics:
Duplicate Name Resolution: Re-names conflicting link/joint names (e.g.,
link_duplicate_1) to preserve kinematic tree integrity while alerting the user.DoS Protection: Enforces a maximum XML depth of 2,000 levels and file size (100 MB) to prevent βXML Bombβ attacks.
O(1) Memory Efficiency: All core parsers use iterative processing to handle massive robot descriptions with a constant, low memory footprint.
Path Sandboxing: Validates all mesh paths to prevent directory traversal and ensure assets remain within authorized project folders.
Secured Math Environment: XACRO expressions are evaluated in a hardened sandbox that prevents access to dangerous Python built-ins or private
__dunder__methods.XACRO Debugging Support: Natively evaluates and routes
xacro.warning(),xacro.error(),xacro.fatal(), andxacro.message()calls to the LinkForge Python logger.Resilient Skip: Malformed geometry or broken joint references are logged as warnings, allowing the rest of the robot to load successfully.