Skip to content

Commit 2f78ffd

Browse files
chriswmackeyChris Mackey
authored and
Chris Mackey
committed
fix(cli): Write contents to stdout when no output file is specified
1 parent b8e95e8 commit 2f78ffd

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

ladybug_vtk/cli/__init__.py

+28-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import sys
44
import os
55
import logging
6+
import base64
7+
import tempfile
8+
import uuid
69

710
from ladybug_display.visualization import VisualizationSet
811
from ladybug.cli import main
@@ -26,8 +29,9 @@ def vtk():
2629
'with the vtkjs file embedded within it. ',
2730
type=str, default='vtkjs', show_default=True)
2831
@click.option(
29-
'--output-file', help='File to output the result. Default: vis_set',
30-
type=click.File('w'), default='vis_set', show_default=True)
32+
'--output-file', help='Optional file to output the JSON string of '
33+
'the config object. By default, it will be printed out to stdout.',
34+
type=click.File('w'), default='-', show_default=True)
3135
def vis_set_to_vtk(vis_file, output_format, output_file):
3236
"""Translate a VisualizationSet file (.vsf) to VTK formats.
3337
@@ -36,17 +40,36 @@ def vis_set_to_vtk(vis_file, output_format, output_file):
3640
vis_file: Full path to a Ladybug Display Visualization Set (VSF) file.
3741
"""
3842
try:
43+
# load the visualization set from the file
3944
vis_set = VisualizationSet.from_file(vis_file)
4045
output_format = output_format.lower()
41-
out_folder, out_file = os.path.split(output_file.name)
42-
if out_file.endswith('.vtkjs'):
46+
# set up the output folder and file
47+
if output_file.name == '<stdout>': # get a temporary file
48+
out_file = str(uuid.uuid4())[:6]
49+
out_folder = tempfile.gettempdir()
50+
else:
51+
out_folder, out_file = os.path.split(output_file.name)
52+
if out_file.lower().endswith('.vtkjs'):
4353
out_file = out_file[:-6]
44-
elif out_file.endswith('.html'):
54+
elif out_file.lower().endswith('.html'):
4555
out_file = out_file[:-5]
56+
# create the output file
4657
if output_format == 'vtkjs':
4758
vis_set.to_vtkjs(output_folder=out_folder, file_name=out_file)
4859
if output_format == 'html':
4960
vis_set.to_html(output_folder=out_folder, file_name=out_file)
61+
if output_file.name == '<stdout>': # load file contents to stdout
62+
out_file_ext = out_file + '.' + output_format
63+
out_file_path = os.path.join(out_folder, out_file_ext)
64+
if output_format == 'html':
65+
with open(out_file_path, encoding='utf-8') as of:
66+
f_contents = of.read()
67+
else: # vtkjs can only be read as binary
68+
with open(out_file_path, 'rb') as of:
69+
f_contents = of.read()
70+
b = base64.b64encode(f_contents)
71+
base64_string = b.decode('utf-8')
72+
output_file.write(base64_string)
5073
except Exception as e:
5174
_logger.exception('Failed to translate VisualizationSet to VTK.\n{}'.format(e))
5275
sys.exit(1)

0 commit comments

Comments
 (0)