Skip to content

Commit cdfd89a

Browse files
authored
autoplot -replace sys.exit by raise SystemExit and catch it (#2561)
1 parent 23120a8 commit cdfd89a

2 files changed

Lines changed: 38 additions & 22 deletions

File tree

mslib/msui/autoplot_dockwidget.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import os
3131
import json
32+
import logging
3233
from datetime import datetime
3334

3435
import click
@@ -196,9 +197,19 @@ def download_plots_cli(self, config_settings):
196197
}
197198

198199
# Invoke the main method using click from the mssautoplot
199-
ctx = click.Context(cli_tool)
200-
ctx.obj = self
201-
ctx.invoke(cli_tool, **args)
200+
try:
201+
ctx = click.Context(cli_tool)
202+
ctx.obj = self
203+
ctx.invoke(cli_tool, **args)
204+
except SystemExit as ex:
205+
logging.error("Can't find given data: %s", ex)
206+
QMessageBox.information(
207+
self,
208+
"Error",
209+
ex.args[0]
210+
)
211+
ctx.obj = None
212+
return
202213

203214
def autoplotSecsTreeWidget_selected_row(self):
204215
selected_items = self.autoplotSecsTreeWidget.selectedItems()

mslib/utils/mssautoplot.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"""
2727

2828
import os
29-
import sys
3029
import io
3130
import re
3231
import json
@@ -196,7 +195,7 @@ def get_op_id(msc_url, token, op_name):
196195

197196

198197
class Plotting:
199-
def __init__(self, cpath, msc_url=None, msc_auth_password=None, username=None, password=None):
198+
def __init__(self, cpath, msc_url=None, msc_auth_password=None, username=None, password=None, pdlg=None):
200199
"""
201200
Initialize the Plotting object with the provided parameters.
202201
@@ -208,6 +207,7 @@ def __init__(self, cpath, msc_url=None, msc_auth_password=None, username=None, p
208207
:password: User's password
209208
"""
210209
read_config_file(cpath)
210+
self.pdlg = pdlg
211211
self.config = config_loader()
212212
self.num_interpolation_points = self.config["num_interpolation_points"]
213213
self.num_labels = self.config["num_labels"]
@@ -222,21 +222,23 @@ def __init__(self, cpath, msc_url=None, msc_auth_password=None, username=None, p
222222
self.config["predefined_map_sections"][section]["CRS"].lower())
223223
except KeyError as e:
224224
print(e)
225-
sys.exit("Invalid SECTION and/or CRS")
225+
raise SystemExit("Invalid SECTION and/or CRS")
226226
self.params["basemap"].update(self.config["predefined_map_sections"][section]["map"])
227227
self.bbox_units = self.params["bbox"]
228228
if filename != "" and filename == flight:
229229
self.read_operation(flight, msc_url, msc_auth_password, username, password)
230230
elif filename != "":
231+
# Todo add the dir to the file in the mssautoplot.json
232+
dirpath = "./"
233+
file_path = os.path.join(dirpath, filename)
234+
exists = os.path.exists(file_path)
235+
if not exists:
236+
print("Filename {} doesn't exist".format(filename))
237+
self.pdlg.close()
238+
raise SystemExit("Filename {} doesn't exist".format(filename))
231239
self.read_ftml(filename)
232240

233241
def read_ftml(self, filename):
234-
dirpath = "./"
235-
file_path = os.path.join(dirpath, filename)
236-
exists = os.path.exists(file_path)
237-
if not exists:
238-
print("Filename {} doesn't exist".format(filename))
239-
sys.exit()
240242
self.wps, self.wp_model_data = load_from_ftml(filename)
241243
self.wp_lats, self.wp_lons, self.wp_locs = [[x[i] for x in self.wps] for i in [0, 1, 3]]
242244
self.wp_press = [mslib.utils.thermolib.flightlevel2pressure(wp[2] * units.hft).to("Pa").m for wp in self.wps]
@@ -262,8 +264,9 @@ def read_operation(self, op_name, msc_url, msc_auth_password, username, password
262264

263265

264266
class TopViewPlotting(Plotting):
265-
def __init__(self, cpath, msc_url, msc_auth_password, msc_username, msc_password):
266-
super(TopViewPlotting, self).__init__(cpath, msc_url, msc_auth_password, msc_username, msc_password)
267+
def __init__(self, cpath, msc_url, msc_auth_password, msc_username, msc_password, pdlg):
268+
super(TopViewPlotting, self).__init__(cpath, msc_url, msc_auth_password, msc_username, msc_password, pdlg)
269+
self.pdlg = pdlg
267270
self.myfig = qt.TopViewPlotter()
268271
self.myfig.fig.canvas.draw()
269272
self.fig, self.ax = self.myfig.fig, self.myfig.ax
@@ -342,9 +345,10 @@ def draw(self, flight, section, vertical, filename, init_time, time, url, layer,
342345

343346

344347
class SideViewPlotting(Plotting):
345-
def __init__(self, cpath, msc_url, msc_auth_password, msc_username, msc_password):
348+
def __init__(self, cpath, msc_url, msc_auth_password, msc_username, msc_password, pdlg):
346349
# ToDo Implement access to MSColab
347-
super(SideViewPlotting, self).__init__(cpath)
350+
super(SideViewPlotting, self).__init__(cpath, pdlg)
351+
self.pdlg = pdlg
348352
self.myfig = qt.SideViewPlotter()
349353
self.ax = self.myfig.ax
350354
self.fig = self.myfig.fig
@@ -384,7 +388,7 @@ def draw(self, flight, section, vertical, filename, init_time, time, url, layer,
384388
self.update_path(filename)
385389
except AttributeError as e:
386390
logging.debug(e)
387-
sys.exit("No FLIGHT Selected")
391+
raise SystemExit("No FLIGHT Selected")
388392
width, height = self.myfig.get_plot_size_in_px()
389393
p_bot, p_top = [float(x) * 100 for x in vertical.split(",")]
390394
self.bbox = tuple([x for x in (self.num_interpolation_points,
@@ -496,7 +500,11 @@ def draw(self):
496500
@click.option('--etime', default="", help='Ending time for downloading multiple plots with a fixed interval.')
497501
@click.pass_context
498502
def main(ctx, cpath, view, ftrack, itime, vtime, intv, stime, etime):
503+
def close_process_dialog(pdlg):
504+
pdlg.close()
505+
499506
if ctx.obj is not None:
507+
# ToDo find a simpler solution, on a split of the package, QT is expensive for such a progressbar
500508
pdlg = QProgressDialog("Downloading images", "Cancel", 0, 10, parent=ctx.obj)
501509
pdlg.setMinimumDuration(0)
502510
pdlg.repaint()
@@ -527,17 +535,14 @@ def main(ctx, cpath, view, ftrack, itime, vtime, intv, stime, etime):
527535

528536
# Choose view (top or side)
529537
if view == "top":
530-
top_view = TopViewPlotting(cpath, msc_url, msc_auth_password, msc_username, msc_password)
538+
top_view = TopViewPlotting(cpath, msc_url, msc_auth_password, msc_username, msc_password, pdlg)
531539
sec = "automated_plotting_hsecs"
532540
else:
533-
side_view = SideViewPlotting(cpath, msc_url, msc_auth_password, msc_username, msc_password)
541+
side_view = SideViewPlotting(cpath, msc_url, msc_auth_password, msc_username, msc_password, pdlg)
534542
sec = "automated_plotting_vsecs"
535543
if ctx.obj is not None:
536544
pdlg.setValue(2)
537545

538-
def close_process_dialog(pdlg):
539-
pdlg.close()
540-
541546
def draw(no_of_plots):
542547
try:
543548
if view == "top":

0 commit comments

Comments
 (0)