in rostran/cli/__main__.py [0:0]
def _format_file(path: Path, replace: bool = False, check_suffix=True):
suffix = path.suffix
if suffix == ".json":
file_format = FileFormat.Json
try:
source = json.loads(path.read_text())
except json.JSONDecodeError:
raise exceptions.InvalidTemplateFormat(path=path, format="json")
elif suffix in (".yaml", ".yml"):
file_format = FileFormat.Yaml
try:
source = yaml.load(path.read_text())
except YAMLError:
raise exceptions.InvalidTemplateFormat(path=path, format="yaml")
else:
if check_suffix:
raise exceptions.TemplateFormatNotSupport(path=path, format=suffix)
return
typer.secho(f"Formatting {path}.", fg="green")
template = RosTemplate.initialize(source)
data = template.as_dict(format=True)
if file_format == FileFormat.Json:
content = json.dumps(data, indent=2, ensure_ascii=False)
else:
s = StringIO()
yaml.dump(data, s)
content = s.getvalue()
if replace:
with path.open("w") as f:
f.write(content)
else:
typer.secho(content)
typer.echo()
return path