55"""
66
77import os
8- import sys
98
109from mfc .state import ARG
10+ from mfc .common import MFCException
1111from mfc .printer import cons
1212
1313
@@ -33,10 +33,9 @@ def _parse_steps(step_arg, available_steps):
3333 return [s for s in requested if s in set (available_steps )]
3434
3535 single = int (step_arg )
36- except ValueError :
37- cons .print (f"[bold red]Error:[/bold red] Invalid --step value '{ step_arg } '. "
38- "Expected an integer, a range (start:end:stride), or 'all'." )
39- sys .exit (1 )
36+ except ValueError as exc :
37+ raise MFCException (f"Invalid --step value '{ step_arg } '. "
38+ "Expected an integer, a range (start:end:stride), or 'all'." ) from exc
4039
4140 if available_steps and single not in set (available_steps ):
4241 return []
@@ -50,28 +49,24 @@ def viz(): # pylint: disable=too-many-locals,too-many-statements,too-many-branc
5049
5150 case_dir = ARG ('input' )
5251 if case_dir is None :
53- cons .print ("[bold red]Error:[/bold red] Please specify a case directory." )
54- sys .exit (1 )
52+ raise MFCException ("Please specify a case directory." )
5553
5654 # Resolve case directory
5755 if not os .path .isdir (case_dir ):
58- cons .print (f"[bold red]Error:[/bold red] Directory not found: { case_dir } " )
59- sys .exit (1 )
56+ raise MFCException (f"Directory not found: { case_dir } " )
6057
6158 # Auto-detect or use specified format
6259 fmt_arg = ARG ('format' )
6360 if fmt_arg :
6461 if fmt_arg not in ('binary' , 'silo' ):
65- cons .print (f"[bold red]Error:[/bold red] Unknown format '{ fmt_arg } '. "
66- "Supported formats: 'binary', 'silo'." )
67- sys .exit (1 )
62+ raise MFCException (f"Unknown format '{ fmt_arg } '. "
63+ "Supported formats: 'binary', 'silo'." )
6864 fmt = fmt_arg
6965 else :
7066 try :
7167 fmt = discover_format (case_dir )
7268 except FileNotFoundError as exc :
73- cons .print (f"[bold red]Error:[/bold red] { exc } " )
74- sys .exit (1 )
69+ raise MFCException (str (exc )) from exc
7570
7671 cons .print (f"[bold]Format:[/bold] { fmt } " )
7772
@@ -98,19 +93,17 @@ def viz(): # pylint: disable=too-many-locals,too-many-statements,too-many-branc
9893 step_arg = ARG ('step' )
9994 steps = discover_timesteps (case_dir , fmt )
10095 if not steps :
101- cons .print ("[bold red]Error:[/bold red] No timesteps found." )
102- sys .exit (1 )
96+ raise MFCException ("No timesteps found." )
10397
10498 if step_arg is None or step_arg == 'all' :
10599 step = steps [0 ]
106100 cons .print (f"[dim]Using first available timestep: { step } [/dim]" )
107101 else :
108102 try :
109103 step = int (step_arg )
110- except ValueError :
111- cons .print (f"[bold red]Error:[/bold red] Invalid --step value '{ step_arg } '. "
112- "Expected an integer or 'all'." )
113- sys .exit (1 )
104+ except ValueError as exc :
105+ raise MFCException (f"Invalid --step value '{ step_arg } '. "
106+ "Expected an integer or 'all'." ) from exc
114107
115108 if fmt == 'silo' :
116109 from .silo_reader import assemble_silo # pylint: disable=import-outside-toplevel
@@ -130,27 +123,23 @@ def viz(): # pylint: disable=too-many-locals,too-many-statements,too-many-branc
130123 step_arg = ARG ('step' )
131124
132125 if varname is None :
133- cons .print ("[bold red]Error:[/bold red] --var is required for rendering. "
134- "Use --list-vars to see available variables." )
135- sys .exit (1 )
126+ raise MFCException ("--var is required for rendering. "
127+ "Use --list-vars to see available variables." )
136128
137129 if step_arg is None :
138- cons .print ("[bold red]Error:[/bold red] --step is required for rendering. "
139- "Use --list-steps to see available timesteps, or pass --step all." )
140- sys .exit (1 )
130+ raise MFCException ("--step is required for rendering. "
131+ "Use --list-steps to see available timesteps, or pass --step all." )
141132
142133 steps = discover_timesteps (case_dir , fmt )
143134 if not steps :
144- cons .print ("[bold red]Error:[/bold red] No timesteps found." )
145- sys .exit (1 )
135+ raise MFCException ("No timesteps found." )
146136
147137 requested_steps = _parse_steps (step_arg , steps )
148138 if not requested_steps :
149- cons . print ( f"[bold red]Error:[/bold red] No matching timesteps for --step { step_arg } ")
139+ msg = f" No matching timesteps for --step { step_arg } "
150140 if steps :
151- cons .print (f"[bold]Available range:[/bold] { steps [0 ]} to { steps [- 1 ]} "
152- f"({ len (steps )} timesteps)" )
153- sys .exit (1 )
141+ msg += f". Available range: { steps [0 ]} to { steps [- 1 ]} ({ len (steps )} timesteps)"
142+ raise MFCException (msg )
154143
155144 # Collect rendering options
156145 render_opts = {}
@@ -196,9 +185,8 @@ def read_step_all_vars(step):
196185 test_assembled = read_step_all_vars (requested_steps [0 ])
197186 if varname not in test_assembled .variables :
198187 avail = sorted (test_assembled .variables .keys ())
199- cons .print (f"[bold red]Error:[/bold red] Variable '{ varname } ' not found." )
200- cons .print (f"[bold]Available variables:[/bold] { ', ' .join (avail )} " )
201- sys .exit (1 )
188+ raise MFCException (f"Variable '{ varname } ' not found. "
189+ f"Available variables: { ', ' .join (avail )} " )
202190
203191 # Create output directory
204192 output_base = ARG ('output' )
@@ -216,9 +204,8 @@ def read_step_all_vars(step):
216204 if success :
217205 cons .print (f"[bold green]Done:[/bold green] { mp4_path } " )
218206 else :
219- cons .print (f"[bold red]Error:[/bold red] Failed to generate { mp4_path } . "
220- "Ensure imageio and imageio-ffmpeg are installed." )
221- sys .exit (1 )
207+ raise MFCException (f"Failed to generate { mp4_path } . "
208+ "Ensure imageio and imageio-ffmpeg are installed." )
222209 return
223210
224211 # Single or multiple PNG frames
0 commit comments