Beispiel #1
0
    def download(self) -> dict:
        if self.taskman:
            self.taskman.register_task('web_sugs',
                                       self.i18n['web.task.suggestions'],
                                       get_icon_path())
            self.taskman.update_progress('web_sugs', 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
Beispiel #2
0
    def read_settings(self, task_man: TaskManager = None) -> dict:
        try:
            if task_man:
                task_man.register_task('web_down_sets',
                                       self.i18n['web.task.download_settings'],
                                       get_icon_path())
                task_man.update_progress('web_down_sets', 10, None)

            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(task_man)
                return

            try:
                self._finish_task_download_settings(task_man)
                return yaml.safe_load(res.content)
            except yaml.YAMLError:
                self.logger.error(
                    'Could not parse environment settings: {}'.format(
                        res.text))
                self._finish_task_download_settings(task_man)
                return
        except requests.exceptions.ConnectionError:
            self._finish_task_download_settings(task_man)
            return
Beispiel #3
0
 def __init__(self, taskman: TaskManager, idxman: SearchIndexManager,
              suggestions_loader: SuggestionsLoader, i18n: I18n,
              logger: logging.Logger):
     super(SearchIndexGenerator, self).__init__(daemon=True)
     self.taskman = taskman
     self.idxman = idxman
     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())
Beispiel #4
0
    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)
Beispiel #5
0
 def __init__(self,
              taskman: TaskManager,
              manager: SuggestionsManager,
              i18n: I18n,
              logger: logging.Logger,
              suggestions_callback,
              create_config: CreateConfigFile,
              internet_connection: bool,
              suggestions: Optional[dict] = None):
     super(SuggestionsLoader, self).__init__(daemon=True)
     self.taskman = taskman
     self.task_id = 'web_sugs'
     self.manager = manager
     self.suggestions_callback = suggestions_callback
     self.i18n = i18n
     self.logger = logger
     self.suggestions = suggestions
     self.create_config = create_config
     self.internet_connection = internet_connection
     self.task_name = self.i18n['web.task.suggestions']
     self.taskman.register_task(self.task_id, self.task_name,
                                get_icon_path())
Beispiel #6
0
    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)

        cached_settings = self.read_cached_settings(web_config) if cache else None

        if cached_settings:
            return cached_settings

        try:
            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()
                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()
                return

            self.logger.info("Caching environment settings to disk")
            cache_dir = os.path.dirname(ENVIRONMENT_SETTINGS_CACHED_FILE)

            try:
                Path(cache_dir).mkdir(parents=True, exist_ok=True)
            except OSError:
                self.logger.error("Could not create Web cache directory: {}".format(cache_dir))
                self.logger.info('Finished')
                self._finish_task_download_settings()
                return

            cache_timestamp = datetime.utcnow().timestamp()
            with open(ENVIRONMENT_SETTINGS_CACHED_FILE, 'w+') as f:
                f.write(yaml.safe_dump(settings))

            with open(ENVIRONMENT_SETTINGS_TS_FILE, 'w+') as f:
                f.write(str(cache_timestamp))

            self._finish_task_download_settings()
            self.logger.info("Finished")
            return settings

        except requests.exceptions.ConnectionError:
            self._finish_task_download_settings()
            return