Skip to content

Commit

Permalink
Fix error on output due to mismatched byte/str
Browse files Browse the repository at this point in the history
  • Loading branch information
fsimonis committed May 16, 2024
1 parent dacaa1a commit 30f838c
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions preciceconfigvisualizer/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def parse_args():
"-o",
"--outfile",
nargs="?",
type=argparse.FileType("w"),
default=sys.stdout,
type=argparse.FileType("wb"),
default=sys.stdout.buffer, # sys.stdout is always in utf-8 mode, so we need to use the underlying buffer to write bytes
help=f"The output file. Files with extensions {', '.join(SUPPORTED_FORMATS)} will be rendered using graphviz. Omit to output dot to stdout.",
)
displayChoices = ["full", "merged", "hide"]
Expand Down Expand Up @@ -77,17 +77,18 @@ def parse_args():
return parser.parse_args()


def main():
def main() -> None:
args = parse_args()
dot = configFileToDotCode(args.infile, **vars(args))
dot: str = configFileToDotCode(args.infile, **vars(args))

ext = os.path.splitext(args.outfile.name)[1].lower().lstrip(".")
ext: str = os.path.splitext(args.outfile.name)[1].lower().lstrip(".")
data: bytes
if ext in SUPPORTED_FORMATS:
g = pydot.graph_from_dot_data(dot)[0]
args.outfile.write(g.create(format=ext))
data = g.create(format=ext)
else:
# sys.stdout is always in utf-8 mode, so we do the same for the outfile
args.outfile.write(dot)
data = dot.encode()
args.outfile.write(data)


if __name__ == "__main__":
Expand Down

0 comments on commit 30f838c

Please sign in to comment.