Skip to content

Commit

Permalink
fix: rich live
Browse files Browse the repository at this point in the history
  • Loading branch information
NTFSvolume committed Nov 8, 2024
1 parent 9cfcef5 commit 4f277b3
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 36 deletions.
4 changes: 2 additions & 2 deletions cyberdrop_dl/clients/hash_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ async def hash_item_during_download(self, media_item: MediaItem) -> None:
log(f"After hash processing failed: {media_item.complete_file} with error {e}", 40, exc_info=True)

async def cleanup_dupes(self) -> None:
with self.manager.live_manager.get_hash_live():
with self.manager.live_manager.get_hash_live(stop=True):
if not self.manager.config_manager.global_settings_data["Dupe_Cleanup_Options"]["delete_after_download"]:
return
file_hashes_dict = await self.get_file_hashes_dict()
async with self.manager.live_manager.get_remove_file_via_hash_live():
with self.manager.live_manager.get_remove_file_via_hash_live(stop=True):
final_candiates_dict = self.get_candiate_per_group(file_hashes_dict)
await self.final_dupe_cleanup(final_candiates_dict)

Expand Down
52 changes: 32 additions & 20 deletions cyberdrop_dl/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,22 @@ def startup() -> Manager:
After this function returns, the manager will be ready to use and scraping / downloading can begin.
"""

manager = Manager()
manager.startup()
try:
manager = Manager()
manager.startup()

if not manager.args_manager.immediate_download:
program_ui(manager)
if not manager.args_manager.immediate_download:
program_ui(manager)

return manager
return manager

except InvalidYamlError as e:
print_to_console(e.message_rich)
exit(1)

except KeyboardInterrupt:
print_to_console("Exiting...")
exit(0)


async def runtime(manager: Manager) -> None:
Expand Down Expand Up @@ -173,9 +182,19 @@ async def director(manager: Manager) -> None:
log_spacer(20)
log("Starting CDL...\n", 20)

with manager.live_manager.get_main_live(stop=True):
await runtime(manager)
await post_runtime(manager)
try:
with manager.live_manager.get_main_live(stop=True):
await runtime(manager)
await post_runtime(manager)
except Exception as e:
log_with_color(
f"An error occurred, please report this to the developer: {e}",
"bold red",
50,
show_in_stats=False,
exc_info=True,
)
sys.exit(1)

log_spacer(20)
manager.progress_manager.print_stats(start_time)
Expand All @@ -198,25 +217,18 @@ def main() -> None:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
exit_code = 1
manager = startup()
with contextlib.suppress(RuntimeError):
try:
manager = startup()
asyncio.run(director(manager))
exit_code = 0
except InvalidYamlError as e:
print_to_console(e.message_rich)
except KeyboardInterrupt:
print_to_console("Trying to Exit ...")
with contextlib.suppress(Exception):
print_to_console("\nTrying to Exit...")
asyncio.run(manager.close())
except Exception as e:
log_with_color(
f"An error occurred, please report this to the developer: {e}",
"bold red",
50,
show_in_stats=False,
exc_info=True,
)
exit(1)
loop.close()
sys.exit(0)

loop.close()
sys.exit(exit_code)
Expand Down
30 changes: 18 additions & 12 deletions cyberdrop_dl/managers/live_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from contextlib import contextmanager
from typing import TYPE_CHECKING, Generator

from rich.console import Console
from rich.live import Live
from rich.progress import Progress, SpinnerColumn, TextColumn

from cyberdrop_dl.utils.logger import console, log
from cyberdrop_dl.utils.logger import console

if TYPE_CHECKING:
from rich.layout import Layout
Expand All @@ -21,23 +21,29 @@ def __init__(self, manager: Manager) -> None:
auto_refresh=True,
refresh_per_second=self.manager.config_manager.global_settings_data["UI_Options"]["refresh_rate"],
console=console,
transient=True,
)

self.placeholder = Progress(
SpinnerColumn(style="green", spinner_name="dots"), TextColumn("Running Cyberdrop-DL")
)
self.placeholder.add_task("running with no UI", total=100, completed=0)

@contextmanager
def get_live(self, layout: Layout, stop: bool = False) -> Generator[Live]:
show = self.placeholder if self.manager.args_manager.no_ui else layout
try:
if self.manager.args_manager.no_ui:
yield
else:
self.live.start()
self.live.update(layout, refresh=True)
yield self.live
self.live.start()
self.live.update(show, refresh=True)
yield self.live

except Exception as e:
msg = f"Issue with rich live {e}"
raise Exception(msg) from e

finally:
if stop:
self.live.stop()
if not self.manager.args_manager.no_ui:
Console().clear()
except Exception as e:
log(f"Issue with rich live {e}", level=10, exc_info=True)

@contextmanager
def get_main_live(self, stop: bool = False) -> Generator[Live]:
Expand Down
2 changes: 1 addition & 1 deletion cyberdrop_dl/utils/sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async def sort(self) -> None:
log_with_color("Download Directory does not exist", "red", 40)
return

download_folders = await self.get_download_folder()
download_folders: list[Path] = await self.get_download_folder()
with self.manager.live_manager.get_sort_live(stop=True):
all_scan_folders = list(filter(lambda x: x.is_dir(), self.download_dir.iterdir()))
queue_length = len(all_scan_folders)
Expand Down
2 changes: 1 addition & 1 deletion cyberdrop_dl/utils/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def check_latest_pypi(log_to_console: bool = True, call_from_ui: bool = False) -
message = Text.from_markup(message)
else:
message = Text("You are currently on the latest version of Cyberdrop-DL")
level = 10
level = 20

if call_from_ui:
rich.print(message)
Expand Down

0 comments on commit 4f277b3

Please sign in to comment.