mirror of
https://github.com/spalencsar/bearhub.git
synced 2026-07-06 22:54:16 +02:00
adding a logger instance to the application context
This commit is contained in:
@@ -8,13 +8,15 @@ from bauh_api.util.http import HttpClient
|
||||
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
|
||||
from bauh.util import util, logs
|
||||
from bauh.util.cache import DefaultMemoryCacheFactory, CacheCleaner
|
||||
from bauh.util.disk import DefaultDiskCacheLoaderFactory
|
||||
from bauh.view.qt.systray import TrayIcon
|
||||
from bauh.view.qt.window import ManageWindow
|
||||
|
||||
args = app_args.read()
|
||||
logger = logs.new_logger(__app_name__, bool(args.logs))
|
||||
app_args.validate(args, logger)
|
||||
|
||||
i18n = util.get_locale_keys(args.locale)
|
||||
|
||||
@@ -28,7 +30,8 @@ context = ApplicationContext(i18n=i18n,
|
||||
download_icons=args.download_icons,
|
||||
app_root_dir=ROOT_DIR,
|
||||
cache_factory=cache_factory,
|
||||
disk_loader_factory=DefaultDiskCacheLoaderFactory(disk_cache_enabled=args.disk_cache))
|
||||
disk_loader_factory=DefaultDiskCacheLoaderFactory(disk_cache_enabled=args.disk_cache),
|
||||
logger=logger)
|
||||
|
||||
managers = extensions.load_managers(context=context, locale=args.locale)
|
||||
|
||||
|
||||
@@ -1,20 +1,11 @@
|
||||
import argparse
|
||||
import logging
|
||||
import os
|
||||
from argparse import Namespace
|
||||
|
||||
from colorama import Fore
|
||||
|
||||
from bauh 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 Linux packages management")
|
||||
parser.add_argument('-v', '--version', action='version', version='%(prog)s {}'.format(__version__))
|
||||
@@ -43,32 +34,39 @@ def read() -> Namespace:
|
||||
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 application package is installed (runtimes / libraries do not count as apps). Default: %(default)s')
|
||||
parser.add_argument('-md', '--max-displayed', action="store", default=os.getenv('BAUH_MAX_DISPLAYED', 100), choices=[0, 1], type=int, help='Maximum number of displayed packages in the management panel table. Default: %(default)s')
|
||||
args = parser.parse_args()
|
||||
parser.add_argument('--logs', action="store", default=int(os.getenv('BAUH_LOGS', 0)), choices=[0, 1], type=int, help='If the application logs should be displayed. Default: %(default)s')
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def validate(args: Namespace, logger: logging.Logger):
|
||||
|
||||
if args.cache_exp < 0:
|
||||
log_msg("'cache-exp' set to '{}': cache will not expire.".format(args.cache_exp), Fore.YELLOW)
|
||||
logger.info("'cache-exp' set to '{}': cache will not expire.".format(args.cache_exp))
|
||||
|
||||
if args.icon_exp < 0:
|
||||
log_msg("'icon-exp' set to '{}': cache will not expire.".format(args.cache_exp), Fore.YELLOW)
|
||||
logger.info("'icon-exp' set to '{}': cache will not expire.".format(args.cache_exp))
|
||||
|
||||
if not args.locale.strip():
|
||||
log_msg("'locale' set as '{}'. You must provide a valid one. Aborting...".format(args.locale), Fore.RED)
|
||||
logger.info("'locale' set as '{}'. You must provide a valid one. Aborting...".format(args.locale))
|
||||
exit(1)
|
||||
|
||||
if args.check_interval <= 0:
|
||||
log_msg("'check-interval' set as '{}'. It must be >= 0. Aborting...".format(args.check_interval), Fore.RED)
|
||||
logger.info("'check-interval' set as '{}'. It must be >= 0. Aborting...".format(args.check_interval))
|
||||
exit(1)
|
||||
|
||||
if args.update_notification == 0:
|
||||
log_msg('updates notifications are disabled', Fore.YELLOW)
|
||||
logger.info('updates notifications are disabled')
|
||||
|
||||
if args.download_icons == 0:
|
||||
log_msg("'download-icons' is disabled", Fore.YELLOW)
|
||||
logger.info("'download-icons' is disabled")
|
||||
|
||||
if args.check_packaging_once == 1:
|
||||
log_msg("'check-packaging-once' is enabled", Fore.YELLOW)
|
||||
logger.info("'check-packaging-once' is enabled")
|
||||
|
||||
if args.sugs == 0:
|
||||
log_msg("suggestions are disabled", Fore.YELLOW)
|
||||
logger.info("suggestions are disabled")
|
||||
|
||||
if args.logs == 1:
|
||||
logger.info("Logs are enabled")
|
||||
|
||||
return args
|
||||
|
||||
22
bauh/util/logs.py
Normal file
22
bauh/util/logs.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import logging
|
||||
from logging import INFO
|
||||
|
||||
FORMAT = '%(asctime)s %(levelname)s [%(module_path)s:%(lineno)s - %(funcName)s()] - %(message)s'
|
||||
|
||||
|
||||
class FilePathFilter(logging.Filter):
|
||||
|
||||
def filter(self, record):
|
||||
record.module_path = record.pathname.split('site-packages/')[1] if 'site-packages' in record.pathname else str(record.pathname)
|
||||
return True
|
||||
|
||||
|
||||
def new_logger(name: str, enabled: bool) -> logging.Logger:
|
||||
instance = logging.Logger(name, level=INFO)
|
||||
instance.addFilter(FilePathFilter())
|
||||
stream_handler = logging.StreamHandler()
|
||||
stream_handler.setFormatter(logging.Formatter(FORMAT))
|
||||
instance.addHandler(stream_handler)
|
||||
instance.disabled = not enabled
|
||||
|
||||
return instance
|
||||
@@ -30,15 +30,16 @@ from bauh.view.qt.view_utils import load_icon
|
||||
DARK_ORANGE = '#FF4500'
|
||||
|
||||
|
||||
def toolbar_button_style(bg: str):
|
||||
return 'QPushButton { color: white; font-weight: bold; background: ' + bg + '}'
|
||||
|
||||
|
||||
class ManageWindow(QWidget):
|
||||
__BASE_HEIGHT__ = 400
|
||||
|
||||
signal_user_res = pyqtSignal(bool)
|
||||
signal_table_update = pyqtSignal()
|
||||
|
||||
def _toolbar_button_style(self, bg: str):
|
||||
return 'QPushButton { color: white; font-weight: bold; background: ' + bg + '}'
|
||||
|
||||
def __init__(self, i18n: dict, icon_cache: MemoryCache, manager: SoftwareManager, disk_cache: bool, download_icons: bool, screen_size, suggestions: bool, display_limit: int, tray_icon=None):
|
||||
super(ManageWindow, self).__init__()
|
||||
self.i18n = i18n
|
||||
@@ -135,14 +136,14 @@ class ManageWindow(QWidget):
|
||||
self.bt_installed.setIcon(QIcon(resource.get_path('img/disk.png')))
|
||||
self.bt_installed.setText(self.i18n['manage_window.bt.installed.text'].capitalize())
|
||||
self.bt_installed.clicked.connect(self._show_installed)
|
||||
self.bt_installed.setStyleSheet(self._toolbar_button_style('#A94E0A'))
|
||||
self.bt_installed.setStyleSheet(toolbar_button_style('#A94E0A'))
|
||||
self.ref_bt_installed = self.toolbar.addWidget(self.bt_installed)
|
||||
|
||||
self.bt_refresh = QPushButton()
|
||||
self.bt_refresh.setToolTip(i18n['manage_window.bt.refresh.tooltip'])
|
||||
self.bt_refresh.setIcon(QIcon(resource.get_path('img/refresh.svg')))
|
||||
self.bt_refresh.setText(self.i18n['manage_window.bt.refresh.text'])
|
||||
self.bt_refresh.setStyleSheet(self._toolbar_button_style('#2368AD'))
|
||||
self.bt_refresh.setStyleSheet(toolbar_button_style('#2368AD'))
|
||||
self.bt_refresh.clicked.connect(lambda: self.refresh_apps(keep_console=False))
|
||||
self.ref_bt_refresh = self.toolbar.addWidget(self.bt_refresh)
|
||||
|
||||
@@ -150,7 +151,7 @@ class ManageWindow(QWidget):
|
||||
self.bt_upgrade.setToolTip(i18n['manage_window.bt.upgrade.tooltip'])
|
||||
self.bt_upgrade.setIcon(QIcon(resource.get_path('img/app_update.svg')))
|
||||
self.bt_upgrade.setText(i18n['manage_window.bt.upgrade.text'])
|
||||
self.bt_upgrade.setStyleSheet(self._toolbar_button_style('#20A435'))
|
||||
self.bt_upgrade.setStyleSheet(toolbar_button_style('#20A435'))
|
||||
self.bt_upgrade.clicked.connect(self.update_selected)
|
||||
self.ref_bt_upgrade = self.toolbar.addWidget(self.bt_upgrade)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user