[config] new general config file

This commit is contained in:
Vinícius Moreira
2019-12-19 19:39:17 -03:00
parent ef6175172a
commit b819b1ec82
12 changed files with 148 additions and 129 deletions

View File

@@ -1,34 +1,86 @@
import json
import logging
import os
import traceback
from pathlib import Path
from typing import List
from bauh import __app_name__
from bauh.api.constants import HOME_PATH
import yaml
CONFIG_PATH = '{}/.config/{}'.format(HOME_PATH, __app_name__)
FILE_PATH = '{}/config.json'.format(CONFIG_PATH)
from bauh import __app_name__
from bauh.commons.config import read_config as read
CONFIG_PATH = '{}/.config/{}'.format(Path.home(), __app_name__)
FILE_PATH = '{}/config.yml'.format(CONFIG_PATH)
# TODO stopped here
class UpdatesSettings:
def __init__(self, check_interval: int, **kwargs):
self.check_interval = check_interval
class CacheSettings:
def __init__(self, data_expiration: int, icon_expiration: int, **kwargs):
self.data_expiration = data_expiration
self.icon_expiration = icon_expiration
class Configuration:
def __init__(self, enabled_gems: List[str] = None, style: str = None):
self.enabled_gems = enabled_gems
def __init__(self, gems: List[str], style: str, cache: dict, locale: str,
updates: dict, system_notifications: bool, disk_cache: bool, download_icons: bool,
check_packaging_once: bool, suggestions: bool, max_displayed: int, download_mthread: bool,
**kwargs):
self.gems = gems
self.style = style
self.cache = CacheSettings(**cache)
self.locale = locale
self.updates = UpdatesSettings(**updates)
self.system_notifications = system_notifications
self.disk_cache = disk_cache
self.download_icons = download_icons
self.check_packaging_once = check_packaging_once
self.suggestions = suggestions
self.max_displayed = max_displayed
self.download_mthread = download_mthread
def read() -> Configuration:
if os.path.exists(FILE_PATH):
with open(FILE_PATH) as f:
config_file = f.read()
return Configuration(**json.loads(config_file))
return Configuration()
def read_config(update_file: bool = False) -> Configuration:
default = {
'gems': None,
'style': None,
'cache_exp': 60 * 60,
'icon_exp': 60 * 5,
'locale': None,
'updates': {
'check_interval': 60
},
'system_notifications': True,
'disk_cache': True,
'download_icons': True,
'check_packaging_once': False,
'suggestions': True,
'max_displayed': 50, # table
'download_mthread': True # downloads
}
obj = read(FILE_PATH, default, update_file=update_file, update_async=True)
return Configuration(**obj)
def save(config: Configuration):
Path(CONFIG_PATH).mkdir(parents=True, exist_ok=True)
with open(FILE_PATH, 'w+') as f:
f.write(json.dumps(config.__dict__, indent=2))
f.write(yaml.safe_dump(config.__dict__))
def remove_old_config(logger: logging.Logger):
old_file = FILE_PATH.replace('.yml', '.json')
if os.path.exists(old_file):
try:
os.remove(old_file)
logger.info('Old configuration file {} deleted'.format(old_file))
except:
logger.error('Could not delete the old configuration file {}'.format(old_file))
traceback.print_exc()

View File

@@ -11,6 +11,7 @@ from bauh.api.abstract.handler import ProcessWatcher
from bauh.api.abstract.model import SoftwarePackage, PackageUpdate, PackageHistory, PackageSuggestion, PackageAction
from bauh.api.exception import NoInternetException
from bauh.commons import internet
from bauh.view.core.config import Configuration
RE_IS_URL = re.compile(r'^https?://.+')
@@ -19,11 +20,11 @@ SUGGESTIONS_LIMIT = 10
class GenericSoftwareManager(SoftwareManager):
def __init__(self, managers: List[SoftwareManager], context: ApplicationContext, app_args: Namespace):
def __init__(self, managers: List[SoftwareManager], context: ApplicationContext, config: Configuration):
super(GenericSoftwareManager, self).__init__(context=context)
self.managers = managers
self.map = {t: m for m in self.managers for t in m.get_managed_types()}
self._available_cache = {} if app_args.check_packaging_once else None
self._available_cache = {} if config.check_packaging_once else None
self.thread_prepare = None
self.i18n = context.i18n
self.disk_loader_factory = context.disk_loader_factory

View File

@@ -44,10 +44,10 @@ def load_managers(locale: str, context: ApplicationContext, config: Configuratio
man = manager_class(context=context)
if config.enabled_gems is None:
if config.gems is None:
man.set_enabled(man.is_default_enabled())
else:
man.set_enabled(f.name in config.enabled_gems)
man.set_enabled(f.name in config.gems)
managers.append(man)

View File

@@ -34,7 +34,7 @@ class StylesComboBox(QComboBox):
self.last_index = idx
style = self.styles[idx]
user_config = config.read()
user_config = config.read_config()
user_config.style = style
config.save(user_config)

View File

@@ -51,10 +51,8 @@ class ManageWindow(QWidget):
signal_user_res = pyqtSignal(bool)
signal_table_update = pyqtSignal()
def __init__(self, i18n: I18n, icon_cache: MemoryCache, manager: SoftwareManager, disk_cache: bool,
download_icons: bool, screen_size, suggestions: bool, display_limit: int, config: Configuration,
context: ApplicationContext, notifications: bool, http_client: HttpClient, logger: logging.Logger,
tray_icon=None):
def __init__(self, i18n: I18n, icon_cache: MemoryCache, manager: SoftwareManager, screen_size, config: Configuration,
context: ApplicationContext, http_client: HttpClient, logger: logging.Logger, tray_icon=None):
super(ManageWindow, self).__init__()
self.i18n = i18n
self.logger = logger
@@ -64,14 +62,11 @@ class ManageWindow(QWidget):
self.pkgs = [] # packages current loaded in the table
self.pkgs_available = [] # all packages loaded in memory
self.pkgs_installed = [] # cached installed packages
self.display_limit = display_limit
self.display_limit = config.max_displayed
self.icon_cache = icon_cache
self.disk_cache = disk_cache
self.download_icons = download_icons
self.screen_size = screen_size
self.config = config
self.context = context
self.notifications = notifications
self.http_client = http_client
self.icon_app = QIcon(resource.get_path('img/logo.svg'))
@@ -213,7 +208,7 @@ class ManageWindow(QWidget):
self.layout.addWidget(self.toolbar)
self.table_apps = AppsTable(self, self.icon_cache, disk_cache=self.disk_cache, download_icons=self.download_icons)
self.table_apps = AppsTable(self, self.icon_cache, disk_cache=self.config.disk_cache, download_icons=self.config.download_icons)
self.table_apps.change_headers_policy()
self.layout.addWidget(self.table_apps)
@@ -265,7 +260,7 @@ class ManageWindow(QWidget):
self.thread_apply_filters.signal_table.connect(self._update_table_and_upgrades)
self.signal_table_update.connect(self.thread_apply_filters.stop_waiting)
self.thread_install = InstallPackage(manager=self.manager, disk_cache=self.disk_cache, icon_cache=self.icon_cache, i18n=self.i18n)
self.thread_install = InstallPackage(manager=self.manager, disk_cache=self.config.disk_cache, icon_cache=self.icon_cache, i18n=self.i18n)
self._bind_async_action(self.thread_install, finished_call=self._finish_install)
self.thread_animate_progress = AnimateProgress()
@@ -313,7 +308,7 @@ class ManageWindow(QWidget):
self.types_changed = False
self.dialog_about = None
self.first_refresh = suggestions
self.first_refresh = config.suggestions
self.thread_warnings = ListWarnings(man=manager, i18n=i18n)
self.thread_warnings.signal_warnings.connect(self._show_warnings)
@@ -545,7 +540,7 @@ class ManageWindow(QWidget):
self.checkbox_console.setChecked(True)
def _can_notify_user(self):
return self.notifications and (self.isHidden() or self.isMinimized())
return self.config.system_notifications and (self.isHidden() or self.isMinimized())
def _finish_downgrade(self, res: dict):
self.finish_action()