diff --git a/bauh/app_args.py b/bauh/app_args.py index a9a74792..3200f022 100644 --- a/bauh/app_args.py +++ b/bauh/app_args.py @@ -1,22 +1 @@ -import argparse -from argparse import Namespace - -from bauh import __app_name__, __version__ - - -def read() -> Namespace: - parser = argparse.ArgumentParser(prog=__app_name__, description="GUI for Linux software management") - parser.add_argument('-v', '--version', action='version', version='%(prog)s {}'.format(__version__)) - parser.add_argument('--logs', action="store_true", help='It activates {} logs.'.format(__app_name__)) - parser.add_argument('--offline', action="store_true", help='It assumes the internet connection is off') - parser.add_argument('--suggestions', action="store_true", - help='It forces loading software suggestions after the initialization process') - - exclusive_args = parser.add_mutually_exclusive_group() - exclusive_args.add_argument('--tray', action="store_true", help='If {} should be attached to the system tray.'.format(__app_name__)) - exclusive_args.add_argument('--settings', action="store_true", help="Display only the settings panel") - exclusive_args.add_argument('--reset', action="store_true", help='Remove all configuration and cache files') - - parser.add_argument_group(exclusive_args) - - return parser.parse_args() +from bearhub.app_args import * # noqa: F401,F403 diff --git a/bauh/context.py b/bauh/context.py index 5443c9a3..cf17acfa 100644 --- a/bauh/context.py +++ b/bauh/context.py @@ -1,104 +1 @@ -import os -import sys -from logging import Logger -from typing import Tuple - -from PyQt5.QtCore import QCoreApplication -from PyQt5.QtWidgets import QApplication - -from bauh import __app_name__, __version__ -from bauh.stylesheet import process_theme, read_default_themes, read_user_themes, read_theme_metada -from bauh.view.util import util, translation -from bauh.view.util.translation import I18n - -DEFAULT_I18N_KEY = 'en' -PROPERTY_HARDCODED_STYLESHEET = 'hcqss' - - -def new_qt_application(app_config: dict, logger: Logger, quit_on_last_closed: bool = False, name: str = None) -> QApplication: - app = QApplication(sys.argv) - app.setQuitOnLastWindowClosed(quit_on_last_closed) # otherwise windows opened through the tray icon kill the application when closed - app.setApplicationName(name if name else __app_name__) - app.setApplicationVersion(__version__) - app.setWindowIcon(util.get_default_icon()[1]) - - if app_config['ui']['qt_style']: - app.setStyle(str(app_config['ui']['qt_style'])) - else: - app.setStyle('fusion') - - app.setProperty('qt_style', app.style().objectName().lower()) - - theme_key = app_config['ui']['theme'].strip() if app_config['ui']['theme'] else None - set_theme(theme_key=theme_key, app=app, logger=logger) - - if not app_config['ui']['system_theme']: - app.setPalette(app.style().standardPalette()) - - return app - - -def _gen_i18n_data(app_config: dict, locale_dir: str) -> Tuple[str, dict, str, dict]: - i18n_key, current_i18n = translation.get_locale_keys(app_config['locale'], locale_dir=locale_dir) - default_i18n = translation.get_locale_keys(DEFAULT_I18N_KEY, locale_dir=locale_dir)[1] if i18n_key != DEFAULT_I18N_KEY else {} - return i18n_key, current_i18n, DEFAULT_I18N_KEY, default_i18n - - -def generate_i18n(app_config: dict, locale_dir: str) -> I18n: - return I18n(*_gen_i18n_data(app_config, locale_dir)) - - -def update_i18n(app_config, locale_dir: str, i18n: I18n) -> I18n: - cur_key, cur_dict, def_key, def_dict = _gen_i18n_data(app_config, locale_dir) - - if i18n.current_key == cur_key: - i18n.current.update(cur_dict) - - i18n.default.update(def_dict) - return i18n - - -def set_theme(theme_key: str, app: QCoreApplication, logger: Logger): - if not theme_key: - logger.warning("config: no theme defined") - else: - available_themes = {} - default_themes = read_default_themes() - available_themes.update(default_themes) - - theme_file = None - - if '/' in theme_key: - if os.path.isfile(theme_key): - user_sheets = read_user_themes() - - if user_sheets: - available_themes.update(user_sheets) - - if theme_key in user_sheets: - theme_file = theme_key - else: - theme_file = default_themes.get(theme_key) - - if theme_file: - with open(theme_file) as f: - theme_str = f.read() - - if not theme_str: - logger.warning("theme file '{}' has no content".format(theme_file)) - else: - base_metadata = read_theme_metada(key=theme_key, file_path=theme_file) - - if base_metadata.abstract: - logger.warning("theme file '{}' is abstract (abstract = true) and cannot be loaded".format(theme_file)) - else: - processed = process_theme(file_path=theme_file, - metadata=base_metadata, - theme_str=theme_str, - available_themes=available_themes) - - if processed: - app.setStyleSheet(processed[0]) - logger.info("theme file '{}' loaded".format(theme_file)) - else: - logger.warning("theme file '{}' could not be interpreted and processed".format(theme_file)) +from bearhub.context import * # noqa: F401,F403 diff --git a/bearhub/app_args.py b/bearhub/app_args.py new file mode 100644 index 00000000..a2136802 --- /dev/null +++ b/bearhub/app_args.py @@ -0,0 +1,22 @@ +import argparse +from argparse import Namespace + +from bearhub import __app_name__, __version__ + + +def read() -> Namespace: + parser = argparse.ArgumentParser(prog=__app_name__, description="GUI for Linux software management") + parser.add_argument('-v', '--version', action='version', version='%(prog)s {}'.format(__version__)) + parser.add_argument('--logs', action="store_true", help='It activates {} logs.'.format(__app_name__)) + parser.add_argument('--offline', action="store_true", help='It assumes the internet connection is off') + parser.add_argument('--suggestions', action="store_true", + help='It forces loading software suggestions after the initialization process') + + exclusive_args = parser.add_mutually_exclusive_group() + exclusive_args.add_argument('--tray', action="store_true", help='If {} should be attached to the system tray.'.format(__app_name__)) + exclusive_args.add_argument('--settings', action="store_true", help="Display only the settings panel") + exclusive_args.add_argument('--reset', action="store_true", help='Remove all configuration and cache files') + + parser.add_argument_group(exclusive_args) + + return parser.parse_args() diff --git a/bearhub/context.py b/bearhub/context.py new file mode 100644 index 00000000..40f89508 --- /dev/null +++ b/bearhub/context.py @@ -0,0 +1,104 @@ +import os +import sys +from logging import Logger +from typing import Tuple + +from PyQt5.QtCore import QCoreApplication +from PyQt5.QtWidgets import QApplication + +from bearhub import __app_name__, __version__ +from bearhub.stylesheet import process_theme, read_default_themes, read_user_themes, read_theme_metada +from bauh.view.util import util, translation +from bauh.view.util.translation import I18n + +DEFAULT_I18N_KEY = 'en' +PROPERTY_HARDCODED_STYLESHEET = 'hcqss' + + +def new_qt_application(app_config: dict, logger: Logger, quit_on_last_closed: bool = False, name: str = None) -> QApplication: + app = QApplication(sys.argv) + app.setQuitOnLastWindowClosed(quit_on_last_closed) # otherwise windows opened through the tray icon kill the application when closed + app.setApplicationName(name if name else __app_name__) + app.setApplicationVersion(__version__) + app.setWindowIcon(util.get_default_icon()[1]) + + if app_config['ui']['qt_style']: + app.setStyle(str(app_config['ui']['qt_style'])) + else: + app.setStyle('fusion') + + app.setProperty('qt_style', app.style().objectName().lower()) + + theme_key = app_config['ui']['theme'].strip() if app_config['ui']['theme'] else None + set_theme(theme_key=theme_key, app=app, logger=logger) + + if not app_config['ui']['system_theme']: + app.setPalette(app.style().standardPalette()) + + return app + + +def _gen_i18n_data(app_config: dict, locale_dir: str) -> Tuple[str, dict, str, dict]: + i18n_key, current_i18n = translation.get_locale_keys(app_config['locale'], locale_dir=locale_dir) + default_i18n = translation.get_locale_keys(DEFAULT_I18N_KEY, locale_dir=locale_dir)[1] if i18n_key != DEFAULT_I18N_KEY else {} + return i18n_key, current_i18n, DEFAULT_I18N_KEY, default_i18n + + +def generate_i18n(app_config: dict, locale_dir: str) -> I18n: + return I18n(*_gen_i18n_data(app_config, locale_dir)) + + +def update_i18n(app_config, locale_dir: str, i18n: I18n) -> I18n: + cur_key, cur_dict, def_key, def_dict = _gen_i18n_data(app_config, locale_dir) + + if i18n.current_key == cur_key: + i18n.current.update(cur_dict) + + i18n.default.update(def_dict) + return i18n + + +def set_theme(theme_key: str, app: QCoreApplication, logger: Logger): + if not theme_key: + logger.warning("config: no theme defined") + else: + available_themes = {} + default_themes = read_default_themes() + available_themes.update(default_themes) + + theme_file = None + + if '/' in theme_key: + if os.path.isfile(theme_key): + user_sheets = read_user_themes() + + if user_sheets: + available_themes.update(user_sheets) + + if theme_key in user_sheets: + theme_file = theme_key + else: + theme_file = default_themes.get(theme_key) + + if theme_file: + with open(theme_file) as f: + theme_str = f.read() + + if not theme_str: + logger.warning("theme file '{}' has no content".format(theme_file)) + else: + base_metadata = read_theme_metada(key=theme_key, file_path=theme_file) + + if base_metadata.abstract: + logger.warning("theme file '{}' is abstract (abstract = true) and cannot be loaded".format(theme_file)) + else: + processed = process_theme(file_path=theme_file, + metadata=base_metadata, + theme_str=theme_str, + available_themes=available_themes) + + if processed: + app.setStyleSheet(processed[0]) + logger.info("theme file '{}' loaded".format(theme_file)) + else: + logger.warning("theme file '{}' could not be interpreted and processed".format(theme_file)) diff --git a/bearhub/stylesheet.py b/bearhub/stylesheet.py new file mode 100644 index 00000000..1af7a6cd --- /dev/null +++ b/bearhub/stylesheet.py @@ -0,0 +1,5 @@ +""" +Temporary namespace migration bridge for stylesheet utilities. +""" + +from bauh.stylesheet import * # noqa: F401,F403