Skip to content
This repository has been archived by the owner on Jan 27, 2024. It is now read-only.

Commit

Permalink
Frontend fixes
Browse files Browse the repository at this point in the history
Fix #98 + print exceptions from frontend (debug of #97).
  • Loading branch information
setnicka committed Aug 10, 2022
1 parent adfb10a commit 937bcd7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 2 additions & 0 deletions uldlib/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def run():

# Register sigint handler
def sigint_handler(sig, frame):
if d.terminating:
return # Already terminating
d.terminate()
print('Program terminated.')
sys.exit(1)
Expand Down
2 changes: 1 addition & 1 deletion uldlib/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def download(self, url, parts=10, target_dir="", conn_timeout=DEFAULT_CONN_TIMEO

self.frontend_thread = threading.Thread(
target=self.frontend.run,
args=(info, downloads, self.stop_frontend)
args=(info, downloads, self.stop_frontend, self.terminate)
)
self.frontend_thread.start()

Expand Down
21 changes: 18 additions & 3 deletions uldlib/frontend.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

from abc import abstractmethod
from datetime import timedelta
from traceback import print_exc
import colors
import os
import sys
Expand Down Expand Up @@ -41,7 +41,7 @@ def prompt(self, msg: str, level: LogLevel = LogLevel.INFO) -> str:
pass

@abstractmethod
def run(self, parts: List[DownloadPart], stop_event: threading.Event):
def run(self, parts: List[DownloadPart], stop_event: threading.Event, terminate_func):
pass


Expand Down Expand Up @@ -106,7 +106,20 @@ def _color(text: str, level: LogLevel) -> str:
return colors.green(text)
return text

def run(self, info: DownloadInfo, parts: List[DownloadPart], stop_event: threading.Event):
def run(self, info: DownloadInfo, parts: List[DownloadPart], stop_event: threading.Event, terminate_func):
try:
self._loop(info, parts, stop_event)
except Exception:
if self.cli_initialized:
y = info.parts + CLI_STATUS_STARTLINE + 4
sys.stdout.write("\033[{};{}H".format(y, 0))
sys.stdout.write("\033[?25h") # show cursor
self.cli_initialized = False
print("")
print_exc()
terminate_func()

def _loop(self, info: DownloadInfo, parts: List[DownloadPart], stop_event: threading.Event):
os.system('cls' if os.name == 'nt' else 'clear')
sys.stdout.write("\033[?25l") # hide cursor
self.cli_initialized = True
Expand All @@ -125,6 +138,8 @@ def run(self, info: DownloadInfo, parts: List[DownloadPart], stop_event: threadi
s_start += size
last_bps = [(s_start, t_start)]

y = 0

while True:
if stop_event.is_set():
break
Expand Down

0 comments on commit 937bcd7

Please sign in to comment.