From 060acaea0834bef61aadfdfe5b20ff79c0210c0f Mon Sep 17 00:00:00 2001 From: Vinicius Moreira Date: Mon, 1 Jul 2019 16:37:00 -0300 Subject: [PATCH] accepting comand line arguments --- CHANGELOG.md | 5 +++-- README.md | 4 +++- fpakman/app.py | 39 +++++++++++++++++++++++++++++++---- fpakman/core/controller.py | 5 ++++- fpakman/core/system.py | 3 +-- fpakman/core/util.py | 2 +- fpakman/view/qt/apps_table.py | 3 --- fpakman/view/qt/systray.py | 11 ++++++---- requirements.txt | 3 ++- 9 files changed, 56 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3411366c..65fdc14f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,15 +4,16 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). -## [0.3.0] - 2019-06-XX +## [0.3.0] - 2019-07-XX ### Features - Applications search - Now when you right-click a selected application you can: - retrieve its information - retrieve its commit history - downgrade - - install it + - install and uninstall it - "About" window available when right-clicking the tray icon. +- Now the application accepts arguments related to environment variables as well. Check 'README.md'. ### Improvements - Performance and memory usage diff --git a/README.md b/README.md index 3e3bbc60..3fff9e2d 100644 --- a/README.md +++ b/README.md @@ -41,11 +41,13 @@ In order to autostart the application, use your Desktop Environment settings to (P.S: the installation script currently does not do that) ### Settings -You can change some application settings via environment variables: +You can change some application settings via environment variables or arguments (type ```fpakman --help``` to get more information). - **FPAKMAN_UPDATE_NOTIFICATION**: enable or disable system updates notifications. Use **0** (disable) or **1** (enable, default). - **FPAKMAN_CHECK_INTERVAL**: define the updates check interval in seconds. Default: 60. - **FPAKMAN_LOCALE**: define a custom app translation for a given locale key (e.g: 'pt', 'en', 'es', ...). Default: system locale. - **FPAKMAN_CACHE_EXPIRATION**: define a custom expiration time in SECONDS for application data retrieved from the origin API. Default: 3600 (1 hour). + ### Roadmap - Support for other packaging technologies +- Memory and performance improvements diff --git a/fpakman/app.py b/fpakman/app.py index 7c986695..f23df03b 100755 --- a/fpakman/app.py +++ b/fpakman/app.py @@ -4,6 +4,7 @@ import sys from PyQt5.QtGui import QIcon from PyQt5.QtWidgets import QApplication +from colorama import Fore from fpakman import __version__ from fpakman.core import resource @@ -11,20 +12,50 @@ from fpakman.core import util from fpakman.core.controller import FlatpakManager from fpakman.view.qt.systray import TrayIcon -parser = argparse.ArgumentParser(prog='fpakman', description="GUI for Flatpak applications management") +app_name = 'fpakman' + + +def log_msg(msg: str, color: int = None): + + if color is None: + print('[{}] {}'.format(app_name, msg)) + else: + print('{}[{}] {}{}'.format(color, app_name, msg, Fore.RESET)) + + +parser = argparse.ArgumentParser(prog=app_name, description="GUI for Flatpak applications management") parser.add_argument('-v', '--version', action='version', version='%(prog)s {}'.format(__version__)) +parser.add_argument('-e', '--cache-exp', action="store", default=int(os.getenv('FPAKMAN_CACHE_EXPIRATION', 60 * 60)), type=int, help='cached application expiration time in SECONDS. Default: %(default)s') +parser.add_argument('-l', '--locale', action="store", default=os.getenv('FPAKMAN_LOCALE', 'en'), help='Translation key. Default: %(default)s') +parser.add_argument('-i', '--check-interval', action="store", default=int(os.getenv('FPAKMAN_CHECK_INTERVAL', 60)), type=int, help='Updates check interval in SECONDS. Default: %(default)s') +parser.add_argument('-n', '--update-notification', action="store", default=os.getenv('FPAKMAN_UPDATE_NOTIFICATION', 1), type=int, help='Enable/disable system notifications for new updates. Default: %(default)s') args = parser.parse_args() -locale_keys = util.get_locale_keys(os.getenv('FPAKMAN_LOCALE', None)) +if args.cache_exp <= 0: + log_msg("'cache-exp' set to '{}': cache will not expire.".format(args.cache_exp), Fore.YELLOW) + +if not args.locale.strip(): + log_msg("'locale' set as '{}'. You must provide a valid one. Aborting...".format(args.locale), Fore.RED) + exit(1) + +if args.check_interval <= 0: + log_msg("'check-interval' set as '{}'. It must be >= 0. Aborting...".format(args.check_interval), Fore.RED) + exit(1) + +if args.update_notification == 0: + log_msg('updates notifications are disabled', Fore.YELLOW) + +locale_keys = util.get_locale_keys(args.locale) app = QApplication(sys.argv) app.setWindowIcon(QIcon(resource.get_path('img/logo.svg'))) -manager = FlatpakManager(cache_expire=int(os.getenv('FPAKMAN_CACHE_EXPIRATION', 60 * 60))) +manager = FlatpakManager(cache_expire=args.cache_exp) trayIcon = TrayIcon(locale_keys=locale_keys, manager=manager, - check_interval=int(os.getenv('FPAKMAN_CHECK_INTERVAL', 60))) + check_interval=args.check_interval, + update_notification=bool(args.update_notification)) trayIcon.load_database() trayIcon.show() diff --git a/fpakman/core/controller.py b/fpakman/core/controller.py index 373e77d3..5e02af2d 100755 --- a/fpakman/core/controller.py +++ b/fpakman/core/controller.py @@ -53,7 +53,10 @@ class FlatpakManager: api_data = self._request_app_data(app['id']) if api_data: - api_data['expires_at'] = datetime.utcnow() + timedelta(seconds=self.cache_expire) + + if self.cache_expire > 0: + api_data['expires_at'] = datetime.utcnow() + timedelta(seconds=self.cache_expire) + self.cache_apps[app['id']] = api_data if not api_data: diff --git a/fpakman/core/system.py b/fpakman/core/system.py index ae39009d..462f9a66 100644 --- a/fpakman/core/system.py +++ b/fpakman/core/system.py @@ -15,5 +15,4 @@ def stream_cmd(cmd: List[str]): def notify_user(msg: str, icon_path: str = resource.get_path('img/logo.svg')): - if bool(os.getenv('FPAKMAN_UPDATE_NOTIFICATION', 1)): - os.system("notify-send {} '{}'".format("-i {}".format(icon_path) if icon_path else '', msg)) + os.system("notify-send {} '{}'".format("-i {}".format(icon_path) if icon_path else '', msg)) diff --git a/fpakman/core/util.py b/fpakman/core/util.py index a83daf1b..678c577d 100644 --- a/fpakman/core/util.py +++ b/fpakman/core/util.py @@ -14,7 +14,7 @@ def get_locale_keys(key: str = None): if key is None: current_locale = locale.getdefaultlocale() else: - current_locale = [key] + current_locale = [key.strip().lower()] if current_locale: current_locale = current_locale[0] diff --git a/fpakman/view/qt/apps_table.py b/fpakman/view/qt/apps_table.py index 74f64f6f..ff7618fc 100644 --- a/fpakman/view/qt/apps_table.py +++ b/fpakman/view/qt/apps_table.py @@ -69,8 +69,6 @@ class AppsTable(QTableWidget): menu_row = QMenu() if app['model']['installed']: - napps = len([app for app in self.window.apps if not app['model']['runtime']]) - action_info = QAction(self.window.locale_keys["manage_window.apps_table.row.actions.info"]) action_info.setIcon(QIcon(resource.get_path('img/info.svg'))) action_info.triggered.connect(self._get_app_info) @@ -84,7 +82,6 @@ class AppsTable(QTableWidget): action_uninstall = QAction(self.window.locale_keys["manage_window.apps_table.row.actions.uninstall"]) action_uninstall.setIcon(QIcon(resource.get_path('img/uninstall.svg'))) action_uninstall.triggered.connect(self._uninstall_app) - action_uninstall.setEnabled(not napps or not app['model']['runtime']) # only enabled for runtimes when no apps are available menu_row.addAction(action_uninstall) if not app['model']['runtime']: # not available for runtimes diff --git a/fpakman/view/qt/systray.py b/fpakman/view/qt/systray.py index 51b88b67..55627b88 100755 --- a/fpakman/view/qt/systray.py +++ b/fpakman/view/qt/systray.py @@ -49,7 +49,7 @@ class LoadDatabase(QThread): class TrayIcon(QSystemTrayIcon): - def __init__(self, locale_keys: dict, manager: FlatpakManager, check_interval: int = 60): + def __init__(self, locale_keys: dict, manager: FlatpakManager, check_interval: int = 60, update_notification: bool = True): super(TrayIcon, self).__init__() self.locale_keys = locale_keys self.manager = manager @@ -75,7 +75,7 @@ class TrayIcon(QSystemTrayIcon): self.setContextMenu(self.menu) - self.manage_window = ManageWindow(locale_keys=self.locale_keys, manager=self.manager, tray_icon=self) + self.manage_window = None self.check_thread = UpdateCheck(check_interval=check_interval, manager=self.manager) self.check_thread.signal.connect(self.notify_updates) self.check_thread.start() @@ -85,6 +85,7 @@ class TrayIcon(QSystemTrayIcon): self.thread_database = LoadDatabase(manager) self.thread_database.signal_finished.connect(self._update_menu) self.last_updates = set() + self.update_notification = update_notification def load_database(self): self.thread_database.start() @@ -105,7 +106,9 @@ class TrayIcon(QSystemTrayIcon): self.last_updates = update_keys msg = '{}: {}'.format(self.locale_keys['notification.new_updates'].format('Flatpak'), len(updates)) self.setToolTip(msg) - system.notify_user(msg) + + if self.update_notification: + system.notify_user(msg) else: new_icon = self.icon_default @@ -116,7 +119,7 @@ class TrayIcon(QSystemTrayIcon): def show_manage_window(self): - if not self.manage_window: + if self.manage_window is None: self.manage_window = ManageWindow(locale_keys=self.locale_keys, manager=self.manager, tray_icon=self) diff --git a/requirements.txt b/requirements.txt index 9ef4c21f..3415a156 100755 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ pyqt5>=5.12 -requests>=2.22 \ No newline at end of file +requests>=2.22 +colorama>=0.4.1 \ No newline at end of file