From 5d4d0c3a3373cee80ad3d134e4f6799d68ea7198 Mon Sep 17 00:00:00 2001 From: Mikan <72257910+Mikan-DS@users.noreply.github.com> Date: Thu, 19 Nov 2026 21:28:45 +0400 Subject: [PATCH] Fix window icon, absolute logging paths, and added CLI test tool --- cli.py | 105 +++++++++++++++++++++++++++++++++++++++ main.py | 48 +++++++++++++----- src/gui/main_window.py | 1 + src/gui/style_manager.py | 18 ++++--- 4 files changed, 153 insertions(+), 19 deletions(-) create mode 100644 cli.py diff --git a/cli.py b/cli.py new file mode 100644 index 0000000..4dc1e1b --- /dev/null +++ b/cli.py @@ -0,0 +1,105 @@ +import sys +import os +import argparse +from src.core.torrent_session import TorrentSession +from src.data.database_manager import DatabaseManager + +def get_app_dir(): + if getattr(sys, 'frozen', False): + return os.path.dirname(sys.executable) + return os.path.dirname(os.path.abspath(__file__)) + +def main(): + parser = argparse.ArgumentParser(description="Citorrent CLI Testing Tool") + subparsers = parser.add_subparsers(dest="command", help="Available commands") + + # Add command + add_parser = subparsers.add_parser("add", help="Add a torrent (magnet or file path)") + add_parser.add_argument("source", help="Magnet URI or path to .torrent file") + + # List command + subparsers.add_parser("list", help="List all torrents in database") + + # Status command + status_parser = subparsers.add_parser("status", help="Get status of a specific torrent") + status_parser.add_argument("hash", help="Info hash of the torrent") + + args = parser.parse_args() + + app_dir = get_app_dir() + db_path = os.path.join(app_dir, "citorrent.db") + save_path = os.path.join(app_dir, "downloads") + os.makedirs(save_path, exist_ok=True) + + session = TorrentSession(save_path=save_path) + db = DatabaseManager(db_path) + + if args.command == "add": + print(f"Adding torrent: {args.source}") + try: + handle = None + if args.source.startswith("magnet:"): + handle = session.add_magnet(args.source) + else: + handle = session.add_torrent_file(args.source) + + if handle: + info_hash = handle.info_hash().to_string() + # Simplified adding to DB for CLI tool + db.add_torrent({ + "info_hash": info_hash, + "name": "Fetching...", + "save_path": save_path, + "status": "Starting", + "progress": 0, + "source": args.source + }) + print(f"Successfully added torrent with hash: {info_hash}") + else: + print("Failed to add torrent.") + except Exception as e: + print(f"Error adding torrent: {e}") + + elif args.command == "list": + torrents = db.get_all_torrents() + if not torrents: + print("No torrents found in database.") + return + + print(f"{'Hash':<40} {'Name':<30} {'Status':<15} {'Progress':<10}") + print("-" * 95) + for t in torrents: + print(f"{t.get('info_hash', 'N/A'):<40} {t.get('name', 'N/A'):<30} {t.get('status', 'N/A'):<15} {t.get('progress', 0):<10}") + + elif args.command == "status": + # For status, we need to check if it's currently in the session + # Since CLI is short-lived, session only has what was just added or resumed + # In a real CLI tool we would resume all from DB first. + print("Resuming torrents from DB to check status...") + existing = db.get_all_torrents() + for t in existing: + source = t.get("source", "") + if source.startswith("magnet:"): + session.add_magnet(source) + elif os.path.exists(source): + session.add_torrent_file(source) + + # Now check specific hash + found = False + for handle in session.handles(): + if handle.info_hash().to_string() == args.hash: + status = session.get_status(handle) + print(f"Torrent {args.hash} Status:") + print(f" Name: {status['name']}") + print(f" Progress: {status['progress']:.2%}") + print(f" State: {status['state']}") + found = True + break + if not found: + print(f"Torrent with hash {args.hash} not found in active session.") + + else: + parser.print_help() + +if __name__ == "__main__": + main() diff --git a/main.py b/main.py index afd67cc..b441ac0 100644 --- a/main.py +++ b/main.py @@ -1,30 +1,52 @@ import sys import os +import logging from PySide6.QtWidgets import QApplication from src.gui.main_window import MainWindow from src.gui.style_manager import StyleManager +# Configure Logging +def setup_logging(app_dir): + log_path = os.path.join(app_dir, "citorrent.log") + logging.basicConfig( + level=logging.DEBUG, + format='%(asctime)s [%(levelname)s] %(name)s: %(message)s', + handlers=[ + logging.FileHandler(log_path, encoding='utf-8'), + logging.StreamHandler(sys.stdout) + ] + ) + return logging.getLogger("Citorrent") + def get_app_dir(): if getattr(sys, 'frozen', False): return os.path.dirname(sys.executable) return os.path.dirname(os.path.abspath(__file__)) def main(): - app = QApplication(sys.argv) - - # Portable mode: Determine app directory for DB and config app_dir = get_app_dir() - db_path = os.path.join(app_dir, "citorrent.db") - save_path = os.path.join(app_dir, "downloads") - os.makedirs(save_path, exist_ok=True) + logger = setup_logging(app_dir) + logger.info("Application starting...") + + try: + app = QApplication(sys.argv) + + # Portable mode: Determine app directory for DB and config + db_path = os.path.join(app_dir, "citorrent.db") + save_path = os.path.join(app_dir, "downloads") + os.makedirs(save_path, exist_ok=True) + logger.info(f"App directory: {app_dir}, DB path: {db_path}, Save path: {save_path}") - # Apply the dark theme stylesheet - StyleManager.apply_stylesheet(app, "style.qss") - - window = MainWindow(db_path=db_path, save_path=save_path) - window.show() - - sys.exit(app.exec()) + # Apply the dark theme stylesheet + StyleManager.apply_stylesheet(app, "style.qss") + + window = MainWindow(db_path=db_path, save_path=save_path) + window.show() + + sys.exit(app.exec()) + except Exception as e: + logger.exception("Critical error during application execution") + sys.exit(1) if __name__ == "__main__": main() diff --git a/src/gui/main_window.py b/src/gui/main_window.py index c1b9f82..d17fe56 100644 --- a/src/gui/main_window.py +++ b/src/gui/main_window.py @@ -26,6 +26,7 @@ class MainWindow(QMainWindow): self.setObjectName("MainWindow") self.setWindowTitle("Citorrent") + self.setWindowIcon(StyleManager.get_icon("logo.png")) self.resize(800, 600) # Integration: Initialize Core and Data layers diff --git a/src/gui/style_manager.py b/src/gui/style_manager.py index e488701..7639cea 100644 --- a/src/gui/style_manager.py +++ b/src/gui/style_manager.py @@ -31,10 +31,16 @@ class StyleManager: def get_icon(icon_name): """Returns a QIcon from the assets folder.""" # Use resource path helper for PyInstaller compatibility - path = get_resource_path(os.path.join("assets", "icons", icon_name)) - if os.path.exists(path): - return QIcon(path) - else: - print(f"Warning: Icon {icon_name} not found at {path}") - return QIcon() # Empty icon as placeholder + # Try both assets/icons/ and assets/ directly + paths = [ + os.path.join("assets", "icons", icon_name), + os.path.join("assets", icon_name) + ] + for p in paths: + full_path = get_resource_path(p) + if os.path.exists(full_path): + return QIcon(full_path) + + print(f"Warning: Icon {icon_name} not found in assets or assets/icons") + return QIcon() # Empty icon as placeholder