-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command line interface for file parsing and conversion
- Loading branch information
1 parent
c67a77d
commit c53cd9e
Showing
1 changed file
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import argparse | ||
import importlib.metadata | ||
import logging | ||
import sys | ||
|
||
from rod import logging as rodlogging | ||
|
||
|
||
def main() -> None: | ||
""" | ||
Main function of the ROD command line interface. | ||
""" | ||
parser = argparse.ArgumentParser( | ||
prog="rod", | ||
description="ROD: The ultimate Python tool for RObot Descriptions processing.", | ||
usage="%(prog)s [options] file", | ||
) | ||
|
||
# Version. | ||
parser.add_argument( | ||
"-V", | ||
"--version", | ||
action="version", | ||
version=f"%(prog)s {importlib.metadata.version('rod')}", | ||
) | ||
|
||
# Verbose output. | ||
parser.add_argument( | ||
"-vv", | ||
"--verbose", | ||
action="store_true", | ||
help="enable verbose output.", | ||
) | ||
|
||
# File to parse. | ||
parser.add_argument( | ||
"-f", | ||
"--file", | ||
type=str, | ||
help="path to the file to parse.", | ||
required=False, | ||
) | ||
|
||
# Option to display the parser file attributes. | ||
parser.add_argument( | ||
"-s", | ||
"--show", | ||
action="store_true", | ||
help="show the robot model attributes.", | ||
) | ||
|
||
# Option to output a URDF or SDF file. | ||
parser.add_argument( | ||
"-o", | ||
"--output", | ||
type=str, | ||
help="Output file path.", | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
log_level = "DEBUG" if args.verbose else "INFO" | ||
|
||
logging.basicConfig(level=log_level) | ||
|
||
from rod.urdf.exporter import UrdfExporter | ||
|
||
# Ensure file argument is provided if output or `show` is specified. | ||
if not args.file and (args.output or args.show): | ||
parser.error("The following arguments are required: FILE.") | ||
|
||
# Show the file attributes if no output file is specified. | ||
if args.file and not (args.output or args.show): | ||
args.show = True | ||
|
||
# Display the parsed file attributes. | ||
if args.file: | ||
from rod import Sdf | ||
|
||
try: | ||
sdf = Sdf.load(sdf=args.file) | ||
except Exception as e: | ||
rodlogging.error(f"Error loading file: {e}") | ||
sys.exit(1) | ||
|
||
if args.show: | ||
print(sdf.to_string()) | ||
|
||
# Output the URDF or SDF file. | ||
if args.output: | ||
|
||
try: | ||
if args.output.endswith(".urdf"): | ||
with open(args.output, "w") as file: | ||
file.write(UrdfExporter(pretty=True).to_urdf_string(sdf=sdf)) | ||
|
||
elif args.output.endswith(".sdf"): | ||
with open(args.output, "w") as file: | ||
file.write(sdf.serialize(pretty=True)) | ||
|
||
else: | ||
rodlogging.error( | ||
f"Unsupported output file extension for '{args.output}'. Supported extensions are '.urdf' and '.sdf'." | ||
) | ||
sys.exit(1) | ||
|
||
except Exception as e: | ||
rodlogging.exception(f"Error writing output file: {e}") | ||
sys.exit(1) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |