mirror of
https://github.com/spalencsar/bearhub.git
synced 2026-07-09 16:44:15 +02:00
improvement -> all suported types now display a 'Checking configuration file' task during the initialization process
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
from bauh.commons.config import read_config as read
|
||||
from bauh.commons.config import YAMLConfigManager
|
||||
from bauh.gems.web import CONFIG_FILE
|
||||
|
||||
|
||||
def read_config(update_file: bool = False) -> dict:
|
||||
default_config = {
|
||||
'environment': {
|
||||
'system': False,
|
||||
'electron': {'version': None},
|
||||
'cache_exp': 1440
|
||||
class WebConfigManager(YAMLConfigManager):
|
||||
|
||||
def __init__(self):
|
||||
super(WebConfigManager, self).__init__(config_file_path=CONFIG_FILE)
|
||||
|
||||
def get_default_config(self) -> dict:
|
||||
return {
|
||||
'environment': {
|
||||
'system': False,
|
||||
'electron': {'version': None},
|
||||
'cache_exp': 1440
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return read(CONFIG_FILE, default_config, update_file)
|
||||
|
||||
|
||||
|
||||
@@ -28,15 +28,17 @@ from bauh.api.abstract.view import MessageType, MultipleSelectComponent, InputOp
|
||||
SelectViewType, TextInputComponent, FormComponent, FileChooserComponent, ViewComponent, PanelComponent
|
||||
from bauh.api.constants import DESKTOP_ENTRIES_DIR
|
||||
from bauh.commons import resource
|
||||
from bauh.commons.config import save_config
|
||||
from bauh.commons.boot import CreateConfigFile
|
||||
from bauh.commons.html import bold
|
||||
from bauh.commons.system import ProcessHandler, get_dir_size, get_human_size_str, SimpleProcess
|
||||
from bauh.gems.web import INSTALLED_PATH, nativefier, DESKTOP_ENTRY_PATH_PATTERN, URL_FIX_PATTERN, ENV_PATH, UA_CHROME, \
|
||||
SEARCH_INDEX_FILE, SUGGESTIONS_CACHE_FILE, ROOT_DIR, CONFIG_FILE, TEMP_PATH, FIXES_PATH, ELECTRON_PATH
|
||||
from bauh.gems.web.config import read_config
|
||||
SEARCH_INDEX_FILE, SUGGESTIONS_CACHE_FILE, ROOT_DIR, TEMP_PATH, FIXES_PATH, ELECTRON_PATH, \
|
||||
get_icon_path
|
||||
from bauh.gems.web.config import WebConfigManager
|
||||
from bauh.gems.web.environment import EnvironmentUpdater, EnvironmentComponent
|
||||
from bauh.gems.web.model import WebApplication
|
||||
from bauh.gems.web.worker import SuggestionsDownloader, SearchIndexGenerator
|
||||
from bauh.gems.web.worker import SuggestionsManager, UpdateEnvironmentSettings, \
|
||||
SuggestionsLoader, SearchIndexGenerator
|
||||
|
||||
try:
|
||||
from bs4 import BeautifulSoup, SoupStrainer
|
||||
@@ -58,7 +60,7 @@ RE_SYMBOLS_SPLIT = re.compile(r'[\-|_\s:.]')
|
||||
|
||||
class WebApplicationManager(SoftwareManager):
|
||||
|
||||
def __init__(self, context: ApplicationContext, suggestions_downloader: Thread = None):
|
||||
def __init__(self, context: ApplicationContext, suggestions_loader: Optional[SuggestionsLoader] = None):
|
||||
super(WebApplicationManager, self).__init__(context=context)
|
||||
self.http_client = context.http_client
|
||||
self.env_updater = EnvironmentUpdater(logger=context.logger, http_client=context.http_client,
|
||||
@@ -67,8 +69,9 @@ class WebApplicationManager(SoftwareManager):
|
||||
self.i18n = context.i18n
|
||||
self.logger = context.logger
|
||||
self.env_thread = None
|
||||
self.suggestions_downloader = suggestions_downloader
|
||||
self.suggestions_loader = suggestions_loader
|
||||
self.suggestions = {}
|
||||
self.configman = WebConfigManager()
|
||||
self.custom_actions = [CustomSoftwareAction(i18n_label_key='web.custom_action.clean_env',
|
||||
i18n_status_key='web.custom_action.clean_env.status',
|
||||
manager=self,
|
||||
@@ -254,8 +257,8 @@ class WebApplicationManager(SoftwareManager):
|
||||
|
||||
index = self._read_search_index()
|
||||
|
||||
if not index and self.suggestions_downloader and self.suggestions_downloader.is_alive():
|
||||
self.suggestions_downloader.join()
|
||||
if not index and self.suggestions_loader and self.suggestions_loader.is_alive():
|
||||
self.suggestions_loader.join()
|
||||
index = self._read_search_index()
|
||||
|
||||
if index:
|
||||
@@ -634,7 +637,7 @@ class WebApplicationManager(SoftwareManager):
|
||||
watcher.change_substatus(self.i18n['web.env.checking'])
|
||||
handler = ProcessHandler(watcher)
|
||||
|
||||
web_config = read_config()
|
||||
web_config = self.configman.get_config()
|
||||
env_settings = self.env_updater.read_settings(web_config=web_config)
|
||||
|
||||
if web_config['environment']['system'] and not nativefier.is_available():
|
||||
@@ -796,7 +799,7 @@ class WebApplicationManager(SoftwareManager):
|
||||
|
||||
def can_work(self) -> bool:
|
||||
if BS4_AVAILABLE and LXML_AVAILABLE:
|
||||
config = read_config(update_file=True)
|
||||
config = self.configman.get_config()
|
||||
use_system_env = config['environment']['system']
|
||||
|
||||
if not use_system_env:
|
||||
@@ -809,28 +812,37 @@ class WebApplicationManager(SoftwareManager):
|
||||
def requires_root(self, action: SoftwareAction, pkg: SoftwarePackage) -> bool:
|
||||
return False
|
||||
|
||||
def _download_suggestions(self, downloader: SuggestionsDownloader):
|
||||
self.suggestions = downloader.download()
|
||||
|
||||
if self.suggestions:
|
||||
index_gen = SearchIndexGenerator(logger=self.logger)
|
||||
Thread(target=index_gen.generate_index, args=(self.suggestions,), daemon=True).start()
|
||||
def _assign_suggestions(self, suggestions: dict):
|
||||
self.suggestions = suggestions
|
||||
|
||||
def prepare(self, task_manager: TaskManager, root_password: str, internet_available: bool):
|
||||
create_config = CreateConfigFile(taskman=task_manager, configman=self.configman, i18n=self.i18n,
|
||||
task_icon_path=get_icon_path(), logger=self.logger)
|
||||
create_config.start()
|
||||
|
||||
if internet_available:
|
||||
downloader = SuggestionsDownloader(logger=self.logger, http_client=self.http_client,
|
||||
i18n=self.i18n, taskman=task_manager)
|
||||
downloader.register_task()
|
||||
UpdateEnvironmentSettings(env_updater=EnvironmentUpdater(logger=self.context.logger,
|
||||
i18n=self.i18n,
|
||||
http_client=self.http_client,
|
||||
file_downloader=self.context.file_downloader,
|
||||
taskman=task_manager),
|
||||
taskman=task_manager,
|
||||
create_config=create_config,
|
||||
i18n=self.i18n).start()
|
||||
|
||||
self.suggestions_downloader = Thread(target=self._download_suggestions, args=(downloader,), daemon=True)
|
||||
self.suggestions_downloader.start()
|
||||
self.suggestions_loader = SuggestionsLoader(manager=SuggestionsManager(logger=self.logger,
|
||||
http_client=self.http_client,
|
||||
i18n=self.i18n),
|
||||
logger=self.logger,
|
||||
i18n=self.i18n,
|
||||
taskman=task_manager,
|
||||
suggestions_callback=self._assign_suggestions)
|
||||
self.suggestions_loader.start()
|
||||
|
||||
web_config = read_config()
|
||||
|
||||
if self.env_updater.should_download_settings(web_config):
|
||||
self.env_updater.register_task_read_settings(taskman=task_manager)
|
||||
self.env_thread = Thread(target=self.env_updater.read_settings, args=(web_config, False, task_manager), daemon=True)
|
||||
self.env_thread.start()
|
||||
SearchIndexGenerator(taskman=task_manager,
|
||||
suggestions_loader=self.suggestions_loader,
|
||||
i18n=self.i18n,
|
||||
logger=self.logger).start()
|
||||
|
||||
def list_updates(self, internet_available: bool) -> List[PackageUpdate]:
|
||||
pass
|
||||
@@ -893,7 +905,7 @@ class WebApplicationManager(SoftwareManager):
|
||||
return PackageSuggestion(priority=SuggestionPriority(suggestion['priority']), package=app)
|
||||
|
||||
def _fill_config_async(self, output: dict):
|
||||
output.update(read_config())
|
||||
output.update(self.configman.get_config())
|
||||
|
||||
def list_suggestions(self, limit: int, filter_installed: bool) -> List[PackageSuggestion]:
|
||||
web_config = {}
|
||||
@@ -903,14 +915,14 @@ class WebApplicationManager(SoftwareManager):
|
||||
|
||||
if self.suggestions:
|
||||
suggestions = self.suggestions
|
||||
elif self.suggestions_downloader:
|
||||
self.suggestions_downloader.join(5)
|
||||
elif self.suggestions_loader:
|
||||
self.suggestions_loader.join(5)
|
||||
suggestions = self.suggestions
|
||||
else:
|
||||
suggestions = SuggestionsDownloader(logger=self.logger, http_client=self.http_client, i18n=self.i18n).download()
|
||||
suggestions = SuggestionsManager(logger=self.logger, http_client=self.http_client, i18n=self.i18n).download()
|
||||
|
||||
# cleaning memory
|
||||
self.suggestions_downloader = None
|
||||
self.suggestions_loader = None
|
||||
self.suggestions = None
|
||||
|
||||
if suggestions:
|
||||
@@ -981,7 +993,7 @@ class WebApplicationManager(SoftwareManager):
|
||||
traceback.print_exc()
|
||||
|
||||
def get_settings(self, screen_width: int, screen_height: int) -> ViewComponent:
|
||||
web_config = read_config()
|
||||
web_config = self.configman.get_config()
|
||||
max_width = floor(screen_width * 0.15)
|
||||
|
||||
input_electron = TextInputComponent(label=self.i18n['web.settings.electron.version.label'],
|
||||
@@ -1018,7 +1030,7 @@ class WebApplicationManager(SoftwareManager):
|
||||
return PanelComponent([form_env])
|
||||
|
||||
def save_settings(self, component: PanelComponent) -> Tuple[bool, Optional[List[str]]]:
|
||||
web_config = read_config()
|
||||
web_config = self.configman.get_config()
|
||||
|
||||
form_env = component.components[0]
|
||||
|
||||
@@ -1036,7 +1048,7 @@ class WebApplicationManager(SoftwareManager):
|
||||
web_config['environment']['cache_exp'] = form_env.get_component('web_cache_exp').get_int_value()
|
||||
|
||||
try:
|
||||
save_config(web_config, CONFIG_FILE)
|
||||
self.configman.save_config(web_config)
|
||||
return True, None
|
||||
except:
|
||||
return False, [traceback.format_exc()]
|
||||
|
||||
@@ -41,12 +41,13 @@ class EnvironmentComponent:
|
||||
|
||||
class EnvironmentUpdater:
|
||||
|
||||
def __init__(self, logger: logging.Logger, http_client: HttpClient, file_downloader: FileDownloader, i18n: I18n):
|
||||
def __init__(self, logger: logging.Logger, http_client: HttpClient, file_downloader: FileDownloader, i18n: I18n, taskman: Optional[TaskManager] = None):
|
||||
self.logger = logger
|
||||
self.file_downloader = file_downloader
|
||||
self.i18n = i18n
|
||||
self.http_client = http_client
|
||||
self.task_read_settings_id = 'web_read_settings'
|
||||
self.taskman = taskman
|
||||
|
||||
def _download_and_install(self, version: str, version_url: str, watcher: ProcessWatcher) -> bool:
|
||||
self.logger.info("Downloading NodeJS {}: {}".format(version, version_url))
|
||||
@@ -267,10 +268,10 @@ class EnvironmentUpdater:
|
||||
|
||||
return res
|
||||
|
||||
def _finish_task_download_settings(self, task_man: TaskManager):
|
||||
if task_man:
|
||||
task_man.update_progress(self.task_read_settings_id, 100, None)
|
||||
task_man.finish_task(self.task_read_settings_id)
|
||||
def _finish_task_download_settings(self):
|
||||
if self.taskman:
|
||||
self.taskman.update_progress(self.task_read_settings_id, 100, None)
|
||||
self.taskman.finish_task(self.task_read_settings_id)
|
||||
|
||||
def should_download_settings(self, web_config: dict) -> bool:
|
||||
try:
|
||||
@@ -302,7 +303,14 @@ class EnvironmentUpdater:
|
||||
self.logger.error("Could not parse environment settings file timestamp: {}".format(env_ts_str))
|
||||
return True
|
||||
|
||||
return env_timestamp + timedelta(minutes=settings_exp) <= datetime.utcnow()
|
||||
expired = env_timestamp + timedelta(minutes=settings_exp) <= datetime.utcnow()
|
||||
|
||||
if expired:
|
||||
self.logger.info("Environment settings file has expired. It should be re-downloaded")
|
||||
return True
|
||||
else:
|
||||
self.logger.info("Cached environment settings file is up to date")
|
||||
return False
|
||||
|
||||
def read_cached_settings(self, web_config: dict) -> Optional[dict]:
|
||||
if not self.should_download_settings(web_config):
|
||||
@@ -314,32 +322,33 @@ class EnvironmentUpdater:
|
||||
except yaml.YAMLError:
|
||||
self.logger.error('Could not parse the cache environment settings file: {}'.format(cached_settings_str))
|
||||
|
||||
def register_task_read_settings(self, taskman: TaskManager):
|
||||
taskman.register_task(self.task_read_settings_id, self.i18n['web.task.download_settings'], get_icon_path())
|
||||
def read_settings(self, web_config: dict, cache: bool = True) -> Optional[dict]:
|
||||
if self.taskman:
|
||||
self.taskman.register_task(self.task_read_settings_id, self.i18n['web.task.download_settings'], get_icon_path())
|
||||
self.taskman.update_progress(self.task_read_settings_id, 1, None)
|
||||
|
||||
def read_settings(self, web_config: dict, cache: bool = True, taskman: Optional[TaskManager] = None) -> Optional[dict]:
|
||||
cached_settings = self.read_cached_settings(web_config) if cache else None
|
||||
|
||||
if cached_settings:
|
||||
return cached_settings
|
||||
|
||||
try:
|
||||
if taskman:
|
||||
taskman.update_progress(self.task_read_settings_id, 10, None)
|
||||
if self.taskman:
|
||||
self.taskman.update_progress(self.task_read_settings_id, 10, None)
|
||||
|
||||
self.logger.info("Downloading environment settings")
|
||||
res = self.http_client.get(URL_ENVIRONMENT_SETTINGS)
|
||||
|
||||
if not res:
|
||||
self.logger.warning('Could not retrieve the environments settings from the cloud')
|
||||
self._finish_task_download_settings(taskman)
|
||||
self._finish_task_download_settings()
|
||||
return
|
||||
|
||||
try:
|
||||
settings = yaml.safe_load(res.content)
|
||||
except yaml.YAMLError:
|
||||
self.logger.error('Could not parse environment settings: {}'.format(res.text))
|
||||
self._finish_task_download_settings(taskman)
|
||||
self._finish_task_download_settings()
|
||||
return
|
||||
|
||||
self.logger.info("Caching environment settings to disk")
|
||||
@@ -350,7 +359,7 @@ class EnvironmentUpdater:
|
||||
except OSError:
|
||||
self.logger.error("Could not create Web cache directory: {}".format(cache_dir))
|
||||
self.logger.info('Finished')
|
||||
self._finish_task_download_settings(taskman)
|
||||
self._finish_task_download_settings()
|
||||
return
|
||||
|
||||
cache_timestamp = datetime.utcnow().timestamp()
|
||||
@@ -360,12 +369,12 @@ class EnvironmentUpdater:
|
||||
with open(ENVIRONMENT_SETTINGS_TS_FILE, 'w+') as f:
|
||||
f.write(str(cache_timestamp))
|
||||
|
||||
self._finish_task_download_settings(taskman)
|
||||
self._finish_task_download_settings()
|
||||
self.logger.info("Finished")
|
||||
return settings
|
||||
|
||||
except requests.exceptions.ConnectionError:
|
||||
self._finish_task_download_settings(taskman)
|
||||
self._finish_task_download_settings()
|
||||
return
|
||||
|
||||
def _check_and_fill_electron(self, pkg: WebApplication, env: dict, local_config: dict, x86_x64: bool, widevine: bool, output: List[EnvironmentComponent]):
|
||||
|
||||
@@ -73,7 +73,9 @@ web.settings.nativefier.system.tooltip=The Nativefier version installed on your
|
||||
web.settings.nativefier.system=system
|
||||
web.settings.nativefier.tip=Defines which Nativefier version should be used to generate the Web applications
|
||||
web.task.download_settings=Updating environment settings
|
||||
web.task.suggestions=Downloading applications suggestions list
|
||||
web.task.search_index=Indexing suggestions
|
||||
web.task.suggestions=Downloading suggestions
|
||||
web.task.suggestions.saving=Saving to disk
|
||||
web.uninstall.error.install_dir.not_found=The installation directory {} was not found
|
||||
web.uninstall.error.remove_dir=It was not possible to remove {}
|
||||
web.waiting.env_updater=Updating environment
|
||||
@@ -73,7 +73,9 @@ web.settings.nativefier.system.tooltip=The Nativefier version installed on your
|
||||
web.settings.nativefier.system=system
|
||||
web.settings.nativefier.tip=Defines which Nativefier version should be used to generate the Web applications
|
||||
web.task.download_settings=Updating environment settings
|
||||
web.task.suggestions=Downloading applications suggestions list
|
||||
web.task.search_index=Indexing suggestions
|
||||
web.task.suggestions=Downloading suggestions
|
||||
web.task.suggestions.saving=Saving to disk
|
||||
web.uninstall.error.install_dir.not_found=The installation directory {} was not found
|
||||
web.uninstall.error.remove_dir=It was not possible to remove {}
|
||||
web.waiting.env_updater=Updating environment
|
||||
@@ -73,7 +73,9 @@ web.settings.nativefier.system.tooltip=Se utilizará la versión de Nativefier i
|
||||
web.settings.nativefier.system=sistema
|
||||
web.settings.nativefier.tip=Define qué versión de Nativefier debe usarse para generar las aplicaciones Web
|
||||
web.task.download_settings=Actualizando configuraciones de ambiente
|
||||
web.task.suggestions=Descargando la lista de sugerencias de aplicaciones
|
||||
web.task.search_index=Indexando sugerencias
|
||||
web.task.suggestions=Descargando sugerencias
|
||||
web.task.suggestions.saving=Guardando en disco
|
||||
web.uninstall.error.install_dir.not_found=No se encontró el directorio de instalación {}
|
||||
web.uninstall.error.remove_dir=No fue posible eliminar {}
|
||||
web.waiting.env_updater=Actualizando el ambiente
|
||||
@@ -73,7 +73,9 @@ web.settings.nativefier.system.tooltip=La version de Nativefier installée sur v
|
||||
web.settings.nativefier.system=système
|
||||
web.settings.nativefier.tip=Definit quelle version de Nativefier doit être utilisée pour géner les applications Web
|
||||
web.task.download_settings=Mise à jour des paramètres d'environnement
|
||||
web.task.suggestions=Téléchargement de la liste de suggestions d'applications
|
||||
web.task.search_index=Indexing suggestions
|
||||
web.task.suggestions=Téléchargement de suggestions
|
||||
web.task.suggestions.saving=Saving to disk
|
||||
web.uninstall.error.install_dir.not_found=Impossible de trouver le dossier d'installation {}
|
||||
web.uninstall.error.remove_dir=Impossible d'enlever {}
|
||||
web.waiting.env_updater=Mise à jour de l'environnement
|
||||
|
||||
@@ -73,7 +73,9 @@ web.settings.nativefier.system.tooltip=The Nativefier version installed on your
|
||||
web.settings.nativefier.system=system
|
||||
web.settings.nativefier.tip=Defines which Nativefier version should be used to generate the Web applications
|
||||
web.task.download_settings=Updating environment settings
|
||||
web.task.suggestions=Downloading applications suggestions list
|
||||
web.task.search_index=Indexing suggestions
|
||||
web.task.suggestions=Downloading suggestions
|
||||
web.task.suggestions.saving=Saving to disk
|
||||
web.uninstall.error.install_dir.not_found=The installation directory {} was not found
|
||||
web.uninstall.error.remove_dir=It was not possible to remove {}
|
||||
web.waiting.env_updater=Updating environment
|
||||
@@ -72,7 +72,9 @@ web.settings.nativefier.system.tooltip=A versão do Nativefier instalada no seu
|
||||
web.settings.nativefier.system=sistema
|
||||
web.settings.nativefier.tip=Define qual versão do Nativefier será utilizada para gerar os aplicativos Web
|
||||
web.task.download_settings=Atualizando configurações de ambiente
|
||||
web.task.suggestions=Baixando lista de sugestões de aplicativos
|
||||
web.task.search_index=Indexando sugestões
|
||||
web.task.suggestions=Baixando sugestões
|
||||
web.task.suggestions.saving=Salvando em disco
|
||||
web.uninstall.error.install_dir.not_found=O diretório de instalação {} não foi encontrado
|
||||
web.uninstall.error.remove=Não foi possível remover {}
|
||||
web.waiting.env_updater=Atualizando ambiente
|
||||
|
||||
@@ -73,7 +73,9 @@ web.settings.nativefier.system=Из системы
|
||||
web.settings.nativefier.system.tooltip=Будет использоваться версия Nativefier установленная в Вашей системе
|
||||
web.settings.nativefier.tip=Определяет, какие Nativefier версии должны быдут использованы для установки веб-приложений
|
||||
web.task.download_settings=Updating environment settings
|
||||
web.task.suggestions=Downloading applications suggestions list
|
||||
web.task.search_index=Indexing suggestions
|
||||
web.task.suggestions=Downloading suggestions
|
||||
web.task.suggestions.saving=Saving to disk
|
||||
web.uninstall.error.install_dir.not_found=Каталог установки {} не найден
|
||||
web.uninstall.error.remove_dir=Не удалось удалить {}
|
||||
web.waiting.env_updater=Обнавляется Веб-среда
|
||||
@@ -73,7 +73,9 @@ web.settings.nativefier.system.tooltip=Sisteminizde yüklü olan Nativefier sür
|
||||
web.settings.nativefier.system=sistem
|
||||
web.settings.nativefier.tip=Web uygulamalarını oluşturmak için hangi Nativefier sürümünün kullanılması gerektiğini tanımlar
|
||||
web.task.download_settings=Ortam ayarları güncelleniyor
|
||||
web.task.suggestions=Uygulama öneri listesi indiriliyor
|
||||
web.task.search_index=Indexing suggestions
|
||||
web.task.suggestions=Downloading suggestions
|
||||
web.task.suggestions.saving=Saving to disk
|
||||
web.uninstall.error.install_dir.not_found={} kurulum dizini bulunamadı
|
||||
web.uninstall.error.remove_dir={} kaldırmak mümkün olmadı
|
||||
web.waiting.env_updater=Ortam güncelleniyor
|
||||
|
||||
59
bauh/gems/web/suggestions.py
Normal file
59
bauh/gems/web/suggestions.py
Normal file
@@ -0,0 +1,59 @@
|
||||
import os
|
||||
import traceback
|
||||
from logging import Logger
|
||||
from pathlib import Path
|
||||
|
||||
import requests
|
||||
import yaml
|
||||
|
||||
from bauh.api.http import HttpClient
|
||||
from bauh.gems.web import URL_SUGGESTIONS, SUGGESTIONS_CACHE_FILE
|
||||
from bauh.view.util.translation import I18n
|
||||
|
||||
|
||||
class SuggestionsManager:
|
||||
|
||||
def __init__(self, http_client: HttpClient, logger: Logger, i18n: I18n):
|
||||
self.http_client = http_client
|
||||
self.logger = logger
|
||||
self.i18n = i18n
|
||||
|
||||
def download(self) -> dict:
|
||||
self.logger.info("Reading suggestions from {}".format(URL_SUGGESTIONS))
|
||||
try:
|
||||
suggestions = self.http_client.get_yaml(URL_SUGGESTIONS, session=False)
|
||||
|
||||
if suggestions:
|
||||
self.logger.info("{} suggestions successfully read".format(len(suggestions)))
|
||||
else:
|
||||
self.logger.warning("Could not read suggestions from {}".format(URL_SUGGESTIONS))
|
||||
|
||||
except (requests.exceptions.ConnectionError, requests.exceptions.ConnectTimeout):
|
||||
self.logger.warning("Internet seems to be off: it was not possible to retrieve the suggestions")
|
||||
suggestions = {}
|
||||
|
||||
self.logger.info("Finished")
|
||||
return suggestions
|
||||
|
||||
def save_to_disk(self, suggestions: dict):
|
||||
if not suggestions:
|
||||
return
|
||||
|
||||
self.logger.info('Caching {} suggestions to the disk'.format(len(suggestions)))
|
||||
suggestions_file_dir = os.path.dirname(SUGGESTIONS_CACHE_FILE)
|
||||
try:
|
||||
Path(suggestions_file_dir).mkdir(parents=True, exist_ok=True)
|
||||
except OSError:
|
||||
self.logger.error("Could not generate the directory {}".format(suggestions_file_dir))
|
||||
traceback.print_exc()
|
||||
return
|
||||
|
||||
try:
|
||||
with open(SUGGESTIONS_CACHE_FILE, 'w+') as f:
|
||||
f.write(yaml.safe_dump(suggestions))
|
||||
|
||||
self.logger.info("{} suggestions successfully cached to file '{}'".format(len(suggestions), SUGGESTIONS_CACHE_FILE))
|
||||
except:
|
||||
self.logger.error("Could write to {}".format(SUGGESTIONS_CACHE_FILE))
|
||||
traceback.print_exc()
|
||||
return
|
||||
@@ -1,87 +1,85 @@
|
||||
import logging
|
||||
import time
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
from threading import Thread
|
||||
from typing import Optional
|
||||
|
||||
import requests
|
||||
import yaml
|
||||
|
||||
from bauh.api.abstract.handler import TaskManager
|
||||
from bauh.api.http import HttpClient
|
||||
from bauh.gems.web import URL_SUGGESTIONS, TEMP_PATH, SEARCH_INDEX_FILE, SUGGESTIONS_CACHE_FILE, get_icon_path
|
||||
from bauh.commons.boot import CreateConfigFile
|
||||
from bauh.commons.html import bold
|
||||
from bauh.gems.web import SEARCH_INDEX_FILE, get_icon_path
|
||||
from bauh.gems.web.environment import EnvironmentUpdater
|
||||
from bauh.gems.web.suggestions import SuggestionsManager
|
||||
from bauh.view.util.translation import I18n
|
||||
|
||||
|
||||
class SuggestionsDownloader:
|
||||
class SuggestionsLoader(Thread):
|
||||
|
||||
def __init__(self, http_client: HttpClient, logger: logging.Logger, i18n: I18n, taskman: TaskManager = None):
|
||||
super(SuggestionsDownloader, self).__init__()
|
||||
self.http_client = http_client
|
||||
self.logger = logger
|
||||
def __init__(self, taskman: TaskManager, manager: SuggestionsManager,
|
||||
i18n: I18n, logger: logging.Logger, suggestions_callback, suggestions: Optional[dict] = None):
|
||||
super(SuggestionsLoader, self).__init__(daemon=True)
|
||||
self.taskman = taskman
|
||||
self.i18n = i18n
|
||||
self.task_id = 'web_sugs'
|
||||
self._task_registered = False
|
||||
self._task_finished = False
|
||||
self.manager = manager
|
||||
self.suggestions_callback = suggestions_callback
|
||||
self.i18n = i18n
|
||||
self.logger = logger
|
||||
self.suggestions = suggestions
|
||||
self.task_name = self.i18n['web.task.suggestions']
|
||||
self.taskman.register_task(self.task_id, self.task_name, get_icon_path())
|
||||
|
||||
def _finish_task(self):
|
||||
if self.taskman:
|
||||
def run(self):
|
||||
ti = time.time()
|
||||
self.taskman.update_progress(self.task_id, 10, None)
|
||||
try:
|
||||
self.suggestions = self.manager.download()
|
||||
|
||||
if self.suggestions_callback:
|
||||
self.suggestions_callback(self.suggestions)
|
||||
|
||||
if self.suggestions:
|
||||
self.taskman.update_progress(self.task_id, 50, self.i18n['web.task.suggestions.saving'])
|
||||
self.manager.save_to_disk(self.suggestions)
|
||||
|
||||
except:
|
||||
self.logger.error("Unexpected exception")
|
||||
traceback.print_exc()
|
||||
finally:
|
||||
self.taskman.update_progress(self.task_id, 100, None)
|
||||
self.taskman.finish_task(self.task_id)
|
||||
self._task_finished = True
|
||||
self.logger.info("Finished")
|
||||
|
||||
def register_task(self):
|
||||
if self.taskman and not self._task_registered and not self._task_finished:
|
||||
self.taskman.register_task(self.task_id, self.i18n['web.task.suggestions'], get_icon_path())
|
||||
self._task_registered = True
|
||||
|
||||
def download(self) -> dict:
|
||||
if self.taskman:
|
||||
self.taskman.update_progress(self.task_id, 10, None)
|
||||
|
||||
self.logger.info("Reading suggestions from {}".format(URL_SUGGESTIONS))
|
||||
try:
|
||||
suggestions = self.http_client.get_yaml(URL_SUGGESTIONS, session=False)
|
||||
|
||||
if suggestions:
|
||||
self.logger.info("{} suggestions successfully read".format(len(suggestions)))
|
||||
else:
|
||||
self.logger.warning("Could not read suggestions from {}".format(URL_SUGGESTIONS))
|
||||
|
||||
except (requests.exceptions.ConnectionError, requests.exceptions.ConnectTimeout):
|
||||
self.logger.warning("Internet seems to be off: it was not possible to retrieve the suggestions")
|
||||
self._finish_task()
|
||||
return {}
|
||||
|
||||
self._finish_task()
|
||||
return suggestions
|
||||
tf = time.time()
|
||||
self.logger.info("Finished. Took {0:.2f} seconds".format(tf - ti))
|
||||
|
||||
|
||||
class SearchIndexGenerator:
|
||||
class SearchIndexGenerator(Thread):
|
||||
|
||||
def __init__(self, logger: logging.Logger):
|
||||
def __init__(self, taskman: TaskManager, suggestions_loader: SuggestionsLoader, i18n: I18n, logger: logging.Logger):
|
||||
super(SearchIndexGenerator, self).__init__(daemon=True)
|
||||
self.taskman = taskman
|
||||
self.i18n = i18n
|
||||
self.logger = logger
|
||||
self.suggestions_loader = suggestions_loader
|
||||
self.task_id = 'web_idx_gen'
|
||||
self.taskman.register_task(self.task_id, self.i18n['web.task.search_index'], get_icon_path())
|
||||
|
||||
def run(self):
|
||||
ti = time.time()
|
||||
self.taskman.update_progress(self.task_id, 0, self.i18n['task.waiting_task'].format(bold(self.suggestions_loader.task_name)))
|
||||
self.suggestions_loader.join()
|
||||
|
||||
if self.suggestions_loader.suggestions:
|
||||
self.generate_index(self.suggestions_loader.suggestions)
|
||||
else:
|
||||
self.taskman.update_progress(self.task_id, 100, None)
|
||||
self.taskman.finish_task(self.task_id)
|
||||
|
||||
tf = time.time()
|
||||
self.logger.info("Finished. Took {0:.2f} seconds".format(tf - ti))
|
||||
|
||||
def generate_index(self, suggestions: dict):
|
||||
self.logger.info('Caching {} suggestions to the disk'.format(len(suggestions)))
|
||||
|
||||
try:
|
||||
Path(TEMP_PATH).mkdir(parents=True, exist_ok=True)
|
||||
except:
|
||||
self.logger.error("Could not generate the directory {}".format(TEMP_PATH))
|
||||
traceback.print_exc()
|
||||
return
|
||||
|
||||
try:
|
||||
with open(SUGGESTIONS_CACHE_FILE, 'w+') as f:
|
||||
f.write(yaml.safe_dump(suggestions))
|
||||
|
||||
self.logger.info('{} successfully cached to the disk as {}'.format(len(suggestions), SUGGESTIONS_CACHE_FILE))
|
||||
except:
|
||||
self.logger.error("Could write to {}".format(SUGGESTIONS_CACHE_FILE))
|
||||
traceback.print_exc()
|
||||
return
|
||||
|
||||
self.taskman.update_progress(self.task_id, 1, None)
|
||||
self.logger.info('Indexing suggestions')
|
||||
index = {}
|
||||
|
||||
@@ -102,6 +100,7 @@ class SearchIndexGenerator:
|
||||
mapped.add(key)
|
||||
|
||||
if index:
|
||||
self.taskman.update_progress(self.task_id, 50, self.i18n['web.task.suggestions.saving'])
|
||||
self.logger.info('Preparing search index for writing')
|
||||
|
||||
for key in index.keys():
|
||||
@@ -115,3 +114,29 @@ class SearchIndexGenerator:
|
||||
except:
|
||||
self.logger.error("Could not write the seach index to {}".format(SEARCH_INDEX_FILE))
|
||||
traceback.print_exc()
|
||||
|
||||
self.taskman.update_progress(self.task_id, 100, None)
|
||||
self.taskman.finish_task(self.task_id)
|
||||
|
||||
|
||||
class UpdateEnvironmentSettings(Thread):
|
||||
|
||||
def __init__(self, env_updater: EnvironmentUpdater, taskman: TaskManager, i18n: I18n, create_config: CreateConfigFile):
|
||||
super(UpdateEnvironmentSettings, self).__init__(daemon=True)
|
||||
self.env_updater = env_updater
|
||||
self.taskman = taskman
|
||||
self.create_config = create_config
|
||||
self.task_id = env_updater.task_read_settings_id
|
||||
self.i18n = i18n
|
||||
|
||||
def run(self):
|
||||
self.taskman.register_task(self.task_id, self.i18n['web.task.download_settings'], get_icon_path())
|
||||
self.taskman.update_progress(self.task_id, 1, self.i18n['task.waiting_task'].format(bold(self.create_config.task_name)))
|
||||
self.create_config.join()
|
||||
|
||||
web_config = self.create_config.config
|
||||
if self.env_updater.should_download_settings(web_config):
|
||||
self.env_updater.read_settings(web_config=web_config, cache=False)
|
||||
else:
|
||||
self.taskman.update_progress(self.task_id, 100, None)
|
||||
self.taskman.finish_task(self.task_id)
|
||||
|
||||
Reference in New Issue
Block a user