3
3
import sys
4
4
import os
5
5
import logging
6
+ import base64
7
+ import tempfile
8
+ import uuid
6
9
7
10
from ladybug_display .visualization import VisualizationSet
8
11
from ladybug .cli import main
@@ -26,8 +29,9 @@ def vtk():
26
29
'with the vtkjs file embedded within it. ' ,
27
30
type = str , default = 'vtkjs' , show_default = True )
28
31
@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 )
31
35
def vis_set_to_vtk (vis_file , output_format , output_file ):
32
36
"""Translate a VisualizationSet file (.vsf) to VTK formats.
33
37
@@ -36,17 +40,36 @@ def vis_set_to_vtk(vis_file, output_format, output_file):
36
40
vis_file: Full path to a Ladybug Display Visualization Set (VSF) file.
37
41
"""
38
42
try :
43
+ # load the visualization set from the file
39
44
vis_set = VisualizationSet .from_file (vis_file )
40
45
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' ):
43
53
out_file = out_file [:- 6 ]
44
- elif out_file .endswith ('.html' ):
54
+ elif out_file .lower (). endswith ('.html' ):
45
55
out_file = out_file [:- 5 ]
56
+ # create the output file
46
57
if output_format == 'vtkjs' :
47
58
vis_set .to_vtkjs (output_folder = out_folder , file_name = out_file )
48
59
if output_format == 'html' :
49
60
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 )
50
73
except Exception as e :
51
74
_logger .exception ('Failed to translate VisualizationSet to VTK.\n {}' .format (e ))
52
75
sys .exit (1 )
0 commit comments