Skip to content

Commit 330afde

Browse files
committed
handle keyboard interrupt ctr-c properly
1 parent ca24366 commit 330afde

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

firedm/FireDM.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import sys
1717
import argparse
1818
import re
19+
import signal
1920

2021
# This code should stay on top to handle relative imports in case of direct call of FireDM.py
2122
if __package__ is None:
@@ -32,8 +33,8 @@
3233
from .controller import Controller, set_option
3334
from .tkview import MainWindow
3435
from .cmdview import CmdView
35-
from .utils import parse_urls, parse_bytes
36-
from .setting import get_user_settings, load_setting
36+
from .utils import parse_urls, parse_bytes, size_format
37+
from .setting import load_setting
3738
from .version import __version__
3839

3940

@@ -249,8 +250,9 @@ def speed(txt):
249250
help='Number of retries to download a file, default=%(default)s.')
250251
downloader.add_argument(
251252
'-l', '--speed-limit', dest='speed_limit',
252-
type=speed, metavar='LIMIT', default=get_default("speed_limit"),
253-
help='download speed limit, in bytes per second (e.g. 100K or 5M), zero means no limit, default=%(default)s.')
253+
type=speed, metavar='LIMIT', default=config.speed_limit,
254+
help=f'download speed limit, in bytes per second (e.g. 100K or 5M), zero means no limit, '
255+
f'current value={size_format(config.speed_limit)}/s.')
254256
downloader.add_argument(
255257
'--concurrent', dest='max_concurrent_downloads',
256258
type=int, metavar='NUMBER', default=get_default("max_concurrent_downloads"),
@@ -350,9 +352,29 @@ def getversion(mod):
350352
# update config module with custom settings
351353
config.__dict__.update(custom_settings)
352354

355+
guimode = True if len(sys.argv) == 1 or args.gui else False
356+
controller = None
357+
358+
def cleanup():
359+
if guimode or args.persistent:
360+
setting.save_setting()
361+
controller.quit()
362+
363+
def signal_handler(signum, frame):
364+
print('\n\nuser interrupt operation, cleanup ...')
365+
signal.signal(signum, signal.SIG_IGN) # ignore additional signals
366+
cleanup()
367+
sys.exit(0)
368+
369+
signal.signal(signal.SIGINT, signal_handler)
370+
353371
# ------------------------------------------------------------------------------------------------------------------
354372
# if running application without arguments will start the gui, otherwise will run application in cmdline
355-
if len(sys.argv) > 1 and not args.gui:
373+
if guimode:
374+
# GUI
375+
controller = Controller(view_class=MainWindow, custom_settings=custom_settings)
376+
controller.run()
377+
else:
356378
config.log_level = 1
357379
controller = Controller(view_class=CmdView, custom_settings=custom_settings)
358380

@@ -376,13 +398,7 @@ def getversion(mod):
376398

377399
config.shutdown = True
378400

379-
if args.persistent:
380-
setting.save_setting()
381-
else:
382-
# GUI
383-
controller = Controller(view_class=MainWindow, custom_settings=custom_settings)
384-
controller.run()
385-
setting.save_setting()
401+
cleanup()
386402

387403

388404
if __name__ == '__main__':

firedm/cmdview.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ def run(self):
2929
"""intended to be used in gui as a mainloop not in terminal views"""
3030
pass
3131

32+
def quit(self):
33+
"""intended to be used in gui as a mainloop not in terminal views"""
34+
pass
35+
3236
def update_view(self, **kwargs):
3337
"""update view"""
3438

firedm/controller.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,8 @@ def quit(self):
18141814
for d in self.d_map.values():
18151815
self.stop_download(d.uid)
18161816

1817+
self.view.quit()
1818+
18171819
self._save_settings()
18181820

18191821
def reset(self):

firedm/view.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ def run(self):
2020
"""run view mainloop if any"""
2121
pass
2222

23+
@abstractmethod
24+
def quit(self):
25+
"""quit view mainloop if any"""
26+
pass
27+
2328
@abstractmethod
2429
def update_view(self, **kwargs):
2530
"""update view, it will be called automatically by controller, when a model changes

0 commit comments

Comments
 (0)