forked from wireviz/WireViz
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sgorsh
committed
Oct 31, 2024
1 parent
5467ce5
commit df3bb64
Showing
1 changed file
with
19 additions
and
4 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 |
---|---|---|
@@ -1,33 +1,48 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
import sys | ||
import sys, io | ||
import argparse | ||
import traceback | ||
import wireviz.wireviz as wv | ||
from wireviz import __version__ | ||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description= | ||
"""Wireviz Kroki wrapper. | ||
Reads YAML data from stdin and writes output to stdout. | ||
Errors and messages from WireViz are printed to stderr.""") | ||
|
||
output_formats = ["svg", "png", "harness"] | ||
|
||
parser.add_argument("--format", "-f", type=str, required=True, choices=output_formats, | ||
help=f"Supported output formats.") | ||
parser.add_argument("input", type=str, choices=["-"], help="'-' for stdin") | ||
parser.add_argument("-o", type=str, choices=["-"], help="'-' for stdout") | ||
parser.add_argument("--version", "-v", action="version", version=__version__, help="print WireViz version") | ||
|
||
args = parser.parse_args() | ||
|
||
try: | ||
# Capture stdout | ||
old_stdout = sys.stdout | ||
sys.stdout = io.StringIO() | ||
|
||
data = wv.parse(inp = sys.stdin.read(), return_types=args.format) | ||
|
||
# Restore stdout | ||
sys.stdout = old_stdout | ||
|
||
if isinstance(data, str): | ||
sys.stdout.write(data) | ||
elif isinstance(data, bytes): | ||
sys.stdout.buffer.write(data) | ||
else: | ||
raise ValueError("Unsupported data format") | ||
sys.exit(0) | ||
|
||
except Exception as e: | ||
sys.stderr.write(str(e)) | ||
sys.stderr.write(str(e)) | ||
sys.stderr.write("\n-----------\n") | ||
sys.stderr.write(traceback.format_exc()) | ||
sys.exit(1) |