mirror of
https://github.com/spalencsar/bearhub.git
synced 2026-07-07 00:04:15 +02:00
refactoring
This commit is contained in:
@@ -1,16 +1,12 @@
|
||||
import argparse
|
||||
import os
|
||||
import pkgutil
|
||||
import sys
|
||||
|
||||
import requests
|
||||
from PyQt5.QtGui import QIcon
|
||||
from PyQt5.QtWidgets import QApplication
|
||||
from colorama import Fore
|
||||
from fpakman_api.util.cache import Cache
|
||||
from fpakman_api.util.disk import DiskCacheLoaderFactory
|
||||
|
||||
from fpakman import __version__, __app_name__, ROOT_DIR
|
||||
from fpakman import __version__, __app_name__, app_args
|
||||
from fpakman.core import resource, extensions
|
||||
from fpakman.core.controller import GenericApplicationManager
|
||||
from fpakman.util import util
|
||||
@@ -19,89 +15,22 @@ from fpakman.view.qt import dialog
|
||||
from fpakman.view.qt.systray import TrayIcon
|
||||
from fpakman.view.qt.window import ManageWindow
|
||||
|
||||
|
||||
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 API data expiration time in SECONDS. Default: %(default)s')
|
||||
parser.add_argument('-ie', '--icon-exp', action="store", default=int(os.getenv('FPAKMAN_ICON_EXPIRATION', 60 * 5)), type=int, help='cached icons 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", choices=[0, 1], default=os.getenv('FPAKMAN_UPDATE_NOTIFICATION', 1), type=int, help='Enables / disables system notifications for new updates. Default: %(default)s')
|
||||
parser.add_argument('-dc', '--disk-cache', action="store", choices=[0, 1], default=os.getenv('FPAKMAN_DISK_CACHE', 1), type=int, help='Enables / disables disk cache. When disk cache is enabled, the installed applications data are loaded faster. Default: %(default)s')
|
||||
parser.add_argument('-di', '--download-icons', action="store", choices=[0, 1], default=os.getenv('FPAKMAN_DOWNLOAD_ICONS', 1), type=int, help='Enables / disables app icons download. It may improve the application speed, depending of how applications data are retrieved by their extensions.')
|
||||
parser.add_argument('-co', '--check-packaging-once', action="store", default=os.getenv('FPAKMAN_CHECK_PACKAGING_ONCE', 0), choices=[0, 1], type=int, help='If the available supported packaging types should be checked ONLY once. It improves the application speed if enabled, but can generate errors if you uninstall any packaging technology while using it, and every time a supported packaging type is installed it will only be available after a restart. Default: %(default)s')
|
||||
parser.add_argument('--tray', action="store", default=os.getenv('FPAKMAN_TRAY', 1), choices=[0, 1], type=int, help='If the tray icon and update-check daemon should be created. Default: %(default)s')
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.cache_exp < 0:
|
||||
log_msg("'cache-exp' set to '{}': cache will not expire.".format(args.cache_exp), Fore.YELLOW)
|
||||
|
||||
if args.icon_exp < 0:
|
||||
log_msg("'icon-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)
|
||||
|
||||
if args.download_icons == 0:
|
||||
log_msg("'download-icons' is disabled", Fore.YELLOW)
|
||||
|
||||
if args.check_packaging_once == 1:
|
||||
log_msg("'check-packaging-once' is enabled", Fore.YELLOW)
|
||||
args = app_args.read()
|
||||
|
||||
locale_keys = util.get_locale_keys(args.locale)
|
||||
|
||||
|
||||
http_session = requests.Session()
|
||||
caches, cache_map = [], {}
|
||||
managers = None
|
||||
|
||||
managers = extensions.load_managers(caches=caches,
|
||||
cache_map=cache_map,
|
||||
locale_keys=locale_keys,
|
||||
http_session=http_session,
|
||||
app_args=args)
|
||||
|
||||
icon_cache = Cache(expiration_time=args.icon_exp)
|
||||
caches.append(icon_cache)
|
||||
|
||||
disk_loader_factory = DiskCacheLoaderFactory(disk_cache=args.disk_cache, cache_map=cache_map)
|
||||
|
||||
for m in pkgutil.iter_modules():
|
||||
if m.ispkg and m.name and m.name != 'fpakman_api' and m.name.startswith('fpakman_'):
|
||||
module = pkgutil.find_loader(m.name).load_module()
|
||||
|
||||
manager = extensions.find_manager(module)
|
||||
|
||||
if manager:
|
||||
locale_path = '{}/resources/locale'.format(module.__path__[0])
|
||||
|
||||
if os.path.exists(locale_path):
|
||||
locale_keys.update(util.get_locale_keys(args.locale, locale_path))
|
||||
|
||||
if not managers:
|
||||
managers = []
|
||||
|
||||
app_cache = Cache(expiration_time=args.cache_exp)
|
||||
man = manager(app_args=args,
|
||||
fpakman_root_dir=ROOT_DIR,
|
||||
http_session=http_session,
|
||||
locale_keys=locale_keys,
|
||||
app_cache=app_cache)
|
||||
cache_map[man.get_app_type()] = app_cache
|
||||
caches.append(app_cache)
|
||||
managers.append(man)
|
||||
|
||||
|
||||
manager = GenericApplicationManager(managers, disk_loader_factory=disk_loader_factory, app_args=args)
|
||||
|
||||
app = QApplication(sys.argv)
|
||||
@@ -109,14 +38,12 @@ app.setApplicationName(__app_name__)
|
||||
app.setApplicationVersion(__version__)
|
||||
app.setWindowIcon(QIcon(resource.get_path('img/logo.svg')))
|
||||
|
||||
screen_size = app.primaryScreen().size()
|
||||
|
||||
manage_window = ManageWindow(locale_keys=locale_keys,
|
||||
manager=manager,
|
||||
icon_cache=icon_cache,
|
||||
disk_cache=args.disk_cache,
|
||||
download_icons=bool(args.download_icons),
|
||||
screen_size=screen_size)
|
||||
screen_size=app.primaryScreen().size())
|
||||
|
||||
if args.tray:
|
||||
trayIcon = TrayIcon(locale_keys=locale_keys,
|
||||
@@ -131,12 +58,12 @@ else:
|
||||
manage_window.refresh_apps()
|
||||
manage_window.show()
|
||||
|
||||
CacheCleaner(caches).start()
|
||||
|
||||
warnings = manager.list_warnings()
|
||||
|
||||
if warnings:
|
||||
for warning in warnings:
|
||||
dialog.show_warning(title=locale_keys['warning'].capitalize(), body=warning)
|
||||
|
||||
CacheCleaner(caches).start()
|
||||
|
||||
sys.exit(app.exec_())
|
||||
|
||||
69
fpakman/app_args.py
Normal file
69
fpakman/app_args.py
Normal file
@@ -0,0 +1,69 @@
|
||||
import argparse
|
||||
import os
|
||||
from argparse import Namespace
|
||||
|
||||
from colorama import Fore
|
||||
|
||||
from fpakman import __app_name__, __version__
|
||||
|
||||
|
||||
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))
|
||||
|
||||
|
||||
def read() -> Namespace:
|
||||
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 API data expiration time in SECONDS. Default: %(default)s')
|
||||
parser.add_argument('-ie', '--icon-exp', action="store", default=int(os.getenv('FPAKMAN_ICON_EXPIRATION', 60 * 5)),
|
||||
type=int, help='cached icons 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", choices=[0, 1],
|
||||
default=os.getenv('FPAKMAN_UPDATE_NOTIFICATION', 1), type=int,
|
||||
help='Enables / disables system notifications for new updates. Default: %(default)s')
|
||||
parser.add_argument('-dc', '--disk-cache', action="store", choices=[0, 1],
|
||||
default=os.getenv('FPAKMAN_DISK_CACHE', 1), type=int,
|
||||
help='Enables / disables disk cache. When disk cache is enabled, the installed applications data are loaded faster. Default: %(default)s')
|
||||
parser.add_argument('-di', '--download-icons', action="store", choices=[0, 1],
|
||||
default=os.getenv('FPAKMAN_DOWNLOAD_ICONS', 1), type=int,
|
||||
help='Enables / disables app icons download. It may improve the application speed, depending of how applications data are retrieved by their extensions.')
|
||||
parser.add_argument('-co', '--check-packaging-once', action="store",
|
||||
default=os.getenv('FPAKMAN_CHECK_PACKAGING_ONCE', 0), choices=[0, 1], type=int,
|
||||
help='If the available supported packaging types should be checked ONLY once. It improves the application speed if enabled, but can generate errors if you uninstall any packaging technology while using it, and every time a supported packaging type is installed it will only be available after a restart. Default: %(default)s')
|
||||
parser.add_argument('--tray', action="store", default=os.getenv('FPAKMAN_TRAY', 1), choices=[0, 1], type=int,
|
||||
help='If the tray icon and update-check daemon should be created. Default: %(default)s')
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.cache_exp < 0:
|
||||
log_msg("'cache-exp' set to '{}': cache will not expire.".format(args.cache_exp), Fore.YELLOW)
|
||||
|
||||
if args.icon_exp < 0:
|
||||
log_msg("'icon-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)
|
||||
|
||||
if args.download_icons == 0:
|
||||
log_msg("'download-icons' is disabled", Fore.YELLOW)
|
||||
|
||||
if args.check_packaging_once == 1:
|
||||
log_msg("'check-packaging-once' is enabled", Fore.YELLOW)
|
||||
|
||||
return args
|
||||
@@ -1,11 +1,53 @@
|
||||
import inspect
|
||||
import os
|
||||
import pkgutil
|
||||
from argparse import Namespace
|
||||
from typing import Set, List, Dict
|
||||
|
||||
from fpakman_api.util.cache import Cache
|
||||
|
||||
from fpakman import ROOT_DIR
|
||||
from fpakman.util import util
|
||||
|
||||
ignore_modules = {'fpakman_api'}
|
||||
|
||||
|
||||
def find_manager(member):
|
||||
def find_manager(member, ):
|
||||
if inspect.isclass(member) and inspect.getmro(member)[1].__name__ == 'ApplicationManager':
|
||||
return member
|
||||
elif inspect.ismodule(member):
|
||||
return member
|
||||
elif inspect.ismodule(member) and member.__name__ not in ignore_modules:
|
||||
for name, mod in inspect.getmembers(member):
|
||||
manager_found = find_manager(mod)
|
||||
if manager_found:
|
||||
return manager_found
|
||||
|
||||
|
||||
def load_managers(caches: List[Cache], cache_map: Dict[type, Cache], locale_keys: Dict[str, str], app_args: Namespace, http_session):
|
||||
managers = None
|
||||
|
||||
for m in pkgutil.iter_modules():
|
||||
if m.ispkg and m.name and m.name not in ignore_modules and m.name.startswith('fpakman_'):
|
||||
module = pkgutil.find_loader(m.name).load_module()
|
||||
|
||||
manager = find_manager(module)
|
||||
|
||||
if manager:
|
||||
locale_path = '{}/resources/locale'.format(module.__path__[0])
|
||||
|
||||
if os.path.exists(locale_path):
|
||||
locale_keys.update(util.get_locale_keys(app_args.locale, locale_path))
|
||||
|
||||
if not managers:
|
||||
managers = []
|
||||
|
||||
app_cache = Cache(expiration_time=app_args.cache_exp)
|
||||
man = manager(app_args=app_args,
|
||||
fpakman_root_dir=ROOT_DIR,
|
||||
http_session=http_session,
|
||||
locale_keys=locale_keys,
|
||||
app_cache=app_cache)
|
||||
cache_map[man.get_app_type()] = app_cache
|
||||
caches.append(app_cache)
|
||||
managers.append(man)
|
||||
|
||||
return managers
|
||||
|
||||
Reference in New Issue
Block a user