mirror of
https://github.com/spalencsar/bearhub.git
synced 2026-07-07 03:34:15 +02:00
refactoring to use ApplicationContext
This commit is contained in:
19
bauh/app.py
19
bauh/app.py
@@ -2,11 +2,12 @@ import sys
|
||||
|
||||
from PyQt5.QtGui import QIcon
|
||||
from PyQt5.QtWidgets import QApplication
|
||||
from bauh_api.abstract.controller import ApplicationContext
|
||||
from bauh_api.util.cache import Cache
|
||||
from bauh_api.util.disk import DiskCacheLoaderFactory
|
||||
from bauh_api.util.http import HttpClient
|
||||
|
||||
from bauh import __version__, __app_name__, app_args
|
||||
from bauh import __version__, __app_name__, app_args, ROOT_DIR
|
||||
from bauh.core import resource, extensions
|
||||
from bauh.core.controller import GenericSoftwareManager
|
||||
from bauh.util import util
|
||||
@@ -16,22 +17,18 @@ from bauh.view.qt.window import ManageWindow
|
||||
|
||||
args = app_args.read()
|
||||
|
||||
locale_keys = util.get_locale_keys(args.locale)
|
||||
http_client = HttpClient()
|
||||
i18n = util.get_locale_keys(args.locale)
|
||||
caches, cache_map = [], {}
|
||||
|
||||
managers = extensions.load_managers(caches=caches,
|
||||
cache_map=cache_map,
|
||||
locale_keys=locale_keys,
|
||||
http_client=http_client,
|
||||
app_args=args)
|
||||
context = ApplicationContext(i18n=i18n, http_client=HttpClient(), args=args, app_root_dir=ROOT_DIR)
|
||||
managers = extensions.load_managers(caches=caches, cache_map=cache_map, context=context)
|
||||
|
||||
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)
|
||||
|
||||
manager = GenericSoftwareManager(managers, disk_loader_factory=disk_loader_factory, app_args=args, locale_keys=locale_keys)
|
||||
manager = GenericSoftwareManager(managers, disk_loader_factory=disk_loader_factory, context=context)
|
||||
manager.prepare()
|
||||
|
||||
app = QApplication(sys.argv)
|
||||
@@ -39,7 +36,7 @@ app.setApplicationName(__app_name__)
|
||||
app.setApplicationVersion(__version__)
|
||||
app.setWindowIcon(QIcon(resource.get_path('img/logo.svg')))
|
||||
|
||||
manage_window = ManageWindow(locale_keys=locale_keys,
|
||||
manage_window = ManageWindow(locale_keys=i18n,
|
||||
manager=manager,
|
||||
icon_cache=icon_cache,
|
||||
disk_cache=args.disk_cache,
|
||||
@@ -48,7 +45,7 @@ manage_window = ManageWindow(locale_keys=locale_keys,
|
||||
suggestions=args.sugs)
|
||||
|
||||
if args.tray:
|
||||
trayIcon = TrayIcon(locale_keys=locale_keys,
|
||||
trayIcon = TrayIcon(locale_keys=i18n,
|
||||
manager=manager,
|
||||
manage_window=manage_window,
|
||||
check_interval=args.check_interval,
|
||||
|
||||
@@ -16,7 +16,7 @@ def log_msg(msg: str, color: int = None):
|
||||
|
||||
|
||||
def read() -> Namespace:
|
||||
parser = argparse.ArgumentParser(prog=__app_name__, description="GUI for Flatpak applications management")
|
||||
parser = argparse.ArgumentParser(prog=__app_name__, description="GUI for Linux packages 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('BAUH_CACHE_EXPIRATION', 60 * 60)), type=int,
|
||||
@@ -35,13 +35,13 @@ def read() -> Namespace:
|
||||
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('BAUH_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.')
|
||||
help='Enables / disables package 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('BAUH_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('BAUH_TRAY', 0), choices=[0, 1], type=int,
|
||||
help='If the tray icon and update-check daemon should be created. Default: %(default)s')
|
||||
parser.add_argument('--sugs', action="store", default=os.getenv('BAUH_SUGGESTIONS', 1), choices=[0, 1], type=int, help='If app suggestions should be displayed if no app is installed (runtimes do not count as apps). Default: %(default)s')
|
||||
parser.add_argument('--sugs', action="store", default=os.getenv('BAUH_SUGGESTIONS', 1), choices=[0, 1], type=int, help='If app suggestions should be displayed if no application package is installed (runtimes / libraries do not count as apps). Default: %(default)s')
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.cache_exp < 0:
|
||||
|
||||
@@ -1,27 +1,25 @@
|
||||
from argparse import Namespace
|
||||
from threading import Thread
|
||||
from typing import List, Set, Type
|
||||
|
||||
from bauh_api.abstract.controller import SoftwareManager, SearchResult
|
||||
from bauh_api.abstract.controller import SoftwareManager, SearchResult, ApplicationContext
|
||||
from bauh_api.abstract.handler import ProcessWatcher
|
||||
from bauh_api.abstract.model import SoftwarePackage, PackageUpdate, PackageHistory
|
||||
from bauh_api.util.disk import DiskCacheLoader
|
||||
from bauh_api.util.disk import DiskCacheLoaderFactory
|
||||
|
||||
from bauh import ROOT_DIR
|
||||
|
||||
SUGGESTIONS_LIMIT = 6
|
||||
|
||||
|
||||
class GenericSoftwareManager(SoftwareManager):
|
||||
|
||||
def __init__(self, managers: List[SoftwareManager], disk_loader_factory: DiskCacheLoaderFactory, app_args: Namespace, locale_keys: dict):
|
||||
super(GenericSoftwareManager, self).__init__(app_args=app_args, app_cache=None, locale_keys=locale_keys, app_root_dir=ROOT_DIR, http_client=None)
|
||||
def __init__(self, managers: List[SoftwareManager], context: ApplicationContext, disk_loader_factory: DiskCacheLoaderFactory):
|
||||
super(GenericSoftwareManager, self).__init__(context=context, app_cache=None)
|
||||
self.managers = managers
|
||||
self.map = {t: m for m in self.managers for t in m.get_managed_types()}
|
||||
self.disk_loader_factory = disk_loader_factory
|
||||
self._enabled_map = {} if app_args.check_packaging_once else None
|
||||
self._enabled_map = {} if context.args.check_packaging_once else None
|
||||
self.thread_prepare = None
|
||||
self.i18n = context.i18n
|
||||
|
||||
def _sort(self, apps: List[SoftwarePackage], word: str) -> List[SoftwarePackage]:
|
||||
|
||||
@@ -179,8 +177,8 @@ class GenericSoftwareManager(SoftwareManager):
|
||||
if man:
|
||||
return man.get_history(app)
|
||||
|
||||
def get_managed_types(self) -> Set["type"]:
|
||||
return None
|
||||
def get_managed_types(self) -> Set[Type[SoftwarePackage]]:
|
||||
pass
|
||||
|
||||
def is_enabled(self):
|
||||
return True
|
||||
@@ -247,7 +245,7 @@ class GenericSoftwareManager(SoftwareManager):
|
||||
|
||||
warnings.extend(man_warnings)
|
||||
else:
|
||||
warnings.append(self.locale_keys['warning.no_managers'])
|
||||
warnings.append(self.i18n['warning.no_managers'])
|
||||
|
||||
return warnings
|
||||
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
import inspect
|
||||
import os
|
||||
import pkgutil
|
||||
from argparse import Namespace
|
||||
from typing import List, Dict
|
||||
|
||||
from bauh_api.abstract.controller import SoftwareManager
|
||||
from bauh_api.abstract.controller import SoftwareManager, ApplicationContext
|
||||
from bauh_api.util.cache import Cache
|
||||
from bauh_api.util.http import HttpClient
|
||||
|
||||
from bauh import ROOT_DIR, __app_name__
|
||||
from bauh import __app_name__
|
||||
from bauh.util import util
|
||||
|
||||
ignore_modules = {'{}_api'.format(__app_name__)}
|
||||
@@ -26,7 +24,7 @@ def find_manager(member):
|
||||
return manager_found
|
||||
|
||||
|
||||
def load_managers(caches: List[Cache], cache_map: Dict[type, Cache], locale_keys: Dict[str, str], app_args: Namespace, http_client: HttpClient) -> List[SoftwareManager]:
|
||||
def load_managers(caches: List[Cache], cache_map: Dict[type, Cache], context: ApplicationContext) -> List[SoftwareManager]:
|
||||
managers = []
|
||||
|
||||
for m in pkgutil.iter_modules():
|
||||
@@ -40,14 +38,10 @@ def load_managers(caches: List[Cache], cache_map: Dict[type, Cache], locale_keys
|
||||
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))
|
||||
context.i18n.update(util.get_locale_keys(context.args.locale, locale_path))
|
||||
|
||||
app_cache = Cache(expiration_time=app_args.cache_exp)
|
||||
man = manager_class(app_args=app_args,
|
||||
app_root_dir=ROOT_DIR,
|
||||
http_client=http_client,
|
||||
locale_keys=locale_keys,
|
||||
app_cache=app_cache)
|
||||
app_cache = Cache(expiration_time=context.args.cache_exp)
|
||||
man = manager_class(context=context, app_cache=app_cache)
|
||||
|
||||
for t in man.get_managed_types():
|
||||
cache_map[t] = app_cache
|
||||
|
||||
Reference in New Issue
Block a user