mirror of
https://github.com/spalencsar/bearhub.git
synced 2026-07-09 13:14:15 +02:00
improvement -> all suported types now display a 'Checking configuration file' task during the initialization process
This commit is contained in:
@@ -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