From 7f4f2c805cda2e908d3f104256c9ea869c25727c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Moreira?= Date: Sat, 9 May 2020 14:52:01 -0300 Subject: [PATCH] [feature] new 'restore' custom action --- CHANGELOG.md | 5 ++++- bauh/api/abstract/controller.py | 2 +- bauh/api/abstract/model.py | 4 +++- bauh/gems/appimage/controller.py | 14 ++++++++----- bauh/gems/web/controller.py | 13 +++++++----- bauh/view/core/controller.py | 34 ++++++++++++++++++++++++++++---- bauh/view/qt/thread.py | 4 ++-- bauh/view/qt/window.py | 3 ++- bauh/view/resources/locale/ca | 4 ++++ bauh/view/resources/locale/de | 4 ++++ bauh/view/resources/locale/en | 4 ++++ bauh/view/resources/locale/es | 4 ++++ bauh/view/resources/locale/it | 4 ++++ bauh/view/resources/locale/pt | 4 ++++ bauh/view/resources/locale/ru | 4 ++++ bauh/view/resources/locale/tr | 4 ++++ bauh/view/util/util.py | 22 ++++++++++++++------- 17 files changed, 106 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41d296ce..c5047b6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). -## [0.9.3] 2020 +## [0.9.3] 2020-05 +### Features +- new **restore** action to restore bauh settings and cache through the 'custom actions' button (**+**). It is equivalent to the command `bauh --reset`. + ### Fixes - Arch - 'clean cache' operation was not working in some scenarios diff --git a/bauh/api/abstract/controller.py b/bauh/api/abstract/controller.py index abf08b5d..e1aa32b8 100644 --- a/bauh/api/abstract/controller.py +++ b/bauh/api/abstract/controller.py @@ -312,7 +312,7 @@ class SoftwareManager(ABC): """ pass - def clear_data(self): + def clear_data(self, logs: bool = True): """ Removes all data created by the SoftwareManager instance """ diff --git a/bauh/api/abstract/model.py b/bauh/api/abstract/model.py index f8f75913..d594e376 100644 --- a/bauh/api/abstract/model.py +++ b/bauh/api/abstract/model.py @@ -7,7 +7,7 @@ from bauh.api.constants import CACHE_PATH class CustomSoftwareAction: - def __init__(self, i18_label_key: str, i18n_status_key: str, icon_path: str, manager_method: str, requires_root: bool, manager: "SoftwareManager" = None, backup: bool = False): + def __init__(self, i18_label_key: str, i18n_status_key: str, icon_path: str, manager_method: str, requires_root: bool, manager: "SoftwareManager" = None, backup: bool = False, refresh: bool = True): """ :param i18_label_key: the i18n key that will be used to display the action name :param i18n_status_key: the i18n key that will be used to display the action name being executed @@ -16,6 +16,7 @@ class CustomSoftwareAction: :param manager: the instance that will execute the action ( optional ) :param backup: if a system backup should be performed before executing the action :param requires_root: + :param refresh: if the a full app refresh should be done if the action succeeds """ self.i18_label_key = i18_label_key self.i18n_status_key = i18n_status_key @@ -24,6 +25,7 @@ class CustomSoftwareAction: self.requires_root = requires_root self.manager = manager self.backup = backup + self.refresh = refresh def __hash__(self): return self.i18_label_key.__hash__() + self.i18n_status_key.__hash__() + self.manager_method.__hash__() diff --git a/bauh/gems/appimage/controller.py b/bauh/gems/appimage/controller.py index 6e85bf6c..bee22356 100644 --- a/bauh/gems/appimage/controller.py +++ b/bauh/gems/appimage/controller.py @@ -613,15 +613,19 @@ class AppImageManager(SoftwareManager): return [] - def clear_data(self): + def clear_data(self, logs: bool = True): for f in glob.glob('{}/*.db'.format(LOCAL_PATH)): try: - print('[bauh][appimage] Deleting {}'.format(f)) + if logs: + print('[bauh][appimage] Deleting {}'.format(f)) os.remove(f) - print('{}[bauh][appimage] {} deleted{}'.format(Fore.YELLOW, f, Fore.RESET)) + + if logs: + print('{}[bauh][appimage] {} deleted{}'.format(Fore.YELLOW, f, Fore.RESET)) except: - print('{}[bauh][appimage] An exception has happened when deleting {}{}'.format(Fore.RED, f, Fore.RESET)) - traceback.print_exc() + if logs: + print('{}[bauh][appimage] An exception has happened when deleting {}{}'.format(Fore.RED, f, Fore.RESET)) + traceback.print_exc() def get_settings(self, screen_width: int, screen_height: int) -> ViewComponent: config = read_config() diff --git a/bauh/gems/web/controller.py b/bauh/gems/web/controller.py index b4fd6088..94996ce4 100644 --- a/bauh/gems/web/controller.py +++ b/bauh/gems/web/controller.py @@ -944,16 +944,19 @@ class WebApplicationManager(SoftwareManager): def get_screenshots(self, pkg: SoftwarePackage) -> List[str]: pass - def clear_data(self): + def clear_data(self, logs: bool = True): if os.path.exists(ENV_PATH): - print('[bauh][web] Deleting directory {}'.format(ENV_PATH)) + if logs: + print('[bauh][web] Deleting directory {}'.format(ENV_PATH)) try: shutil.rmtree(ENV_PATH) - print('{}[bauh][web] Directory {} deleted{}'.format(Fore.YELLOW, ENV_PATH, Fore.RESET)) + if logs: + print('{}[bauh][web] Directory {} deleted{}'.format(Fore.YELLOW, ENV_PATH, Fore.RESET)) except: - print('{}[bauh][web] An exception has happened when deleting {}{}'.format(Fore.RED, ENV_PATH, Fore.RESET)) - traceback.print_exc() + if logs: + print('{}[bauh][web] An exception has happened when deleting {}{}'.format(Fore.RED, ENV_PATH, Fore.RESET)) + traceback.print_exc() def get_settings(self, screen_width: int, screen_height: int) -> ViewComponent: config = read_config() diff --git a/bauh/view/core/controller.py b/bauh/view/core/controller.py index f3f819e5..662510b8 100755 --- a/bauh/view/core/controller.py +++ b/bauh/view/core/controller.py @@ -14,8 +14,11 @@ from bauh.api.abstract.model import SoftwarePackage, PackageUpdate, PackageHisto from bauh.api.abstract.view import ViewComponent, TabGroupComponent from bauh.api.exception import NoInternetException from bauh.commons import internet +from bauh.commons.html import bold from bauh.view.core.settings import GenericSettingsManager from bauh.view.core.update import check_for_update +from bauh.view.util import resource +from bauh.view.util.util import clean_app_files, restart_app RE_IS_URL = re.compile(r'^https?://.+') @@ -47,6 +50,13 @@ class GenericSoftwareManager(SoftwareManager): self.config = config self.settings_manager = settings_manager self.http_client = context.http_client + self.extra_actions = [CustomSoftwareAction(i18_label_key='action.reset', + i18n_status_key='action.reset.status', + manager_method='reset', + manager=self, + icon_path=resource.get_path('img/logo.svg'), + requires_root=False, + refresh=False)] def reset_cache(self): if self._available_cache is not None: @@ -473,10 +483,25 @@ class GenericSoftwareManager(SoftwareManager): return res - def get_custom_actions(self) -> List[CustomSoftwareAction]: - if self.managers: - actions = [] + def reset(self, root_password: str, watcher: ProcessWatcher) -> bool: + body = '

{}

{}

'.format(self.i18n['action.reset.body_1'].format(bold(self.context.app_name)), + self.i18n['action.reset.body_2']) + if watcher.request_confirmation(title=self.i18n['action.reset'], + body=body, + confirmation_label=self.i18n['proceed'].capitalize(), + deny_label=self.i18n['cancel'].capitalize()): + try: + clean_app_files(managers=self.managers, logs=False) + restart_app() + except: + return False + + return True + + def get_custom_actions(self) -> List[CustomSoftwareAction]: + actions = [] + if self.managers: working_managers = [] for man in self.managers: @@ -492,7 +517,8 @@ class GenericSoftwareManager(SoftwareManager): if man_actions: actions.extend(man_actions) - return actions + actions.extend(self.extra_actions) + return actions def _fill_sizes(self, man: SoftwareManager, pkgs: List[SoftwarePackage]): ti = time.time() diff --git a/bauh/view/qt/thread.py b/bauh/view/qt/thread.py index f88a33fa..ff995cfe 100644 --- a/bauh/view/qt/thread.py +++ b/bauh/view/qt/thread.py @@ -874,7 +874,7 @@ class CustomAction(AsyncAction): if self.custom_action.backup: app_config = read_config() if not self.request_backup(app_config, None, self.i18n, self.root_pwd): - self.notify_finished({'success': False, 'pkg': self.pkg}) + self.notify_finished({'success': False, 'pkg': self.pkg, 'action': self.custom_action}) self.pkg = None self.custom_action = None self.root_pwd = None @@ -889,7 +889,7 @@ class CustomAction(AsyncAction): success = False self.signal_output.emit(self.i18n['internet.required']) - self.notify_finished({'success': success, 'pkg': self.pkg}) + self.notify_finished({'success': success, 'pkg': self.pkg, 'action': self.custom_action}) self.pkg = None self.custom_action = None self.root_pwd = None diff --git a/bauh/view/qt/window.py b/bauh/view/qt/window.py index d23560f9..f16053a3 100755 --- a/bauh/view/qt/window.py +++ b/bauh/view/qt/window.py @@ -1226,7 +1226,8 @@ class ManageWindow(QWidget): def _finish_custom_action(self, res: dict): self.finish_action() if res['success']: - self.refresh_packages(pkg_types={res['pkg'].model.__class__} if res['pkg'] else None) + if res['action'].refresh: + self.refresh_packages(pkg_types={res['pkg'].model.__class__} if res['pkg'] else None) else: self.checkbox_console.setChecked(True) diff --git a/bauh/view/resources/locale/ca b/bauh/view/resources/locale/ca index 021d6285..18fbdbd9 100644 --- a/bauh/view/resources/locale/ca +++ b/bauh/view/resources/locale/ca @@ -60,6 +60,10 @@ action.history.no_history.title=No history action.info.tooltip=Feu clic aquí per a veure informació sobre aquesta aplicació action.not_allowed=Action not allowed action.request_reboot.title=System restart +action.reset=Restore +action.reset.status=Restoring +action.reset.body_1=This operation will restore {} default settings and clean its cache. +action.reset.body_2=It will be restarted after the operation is finished. action.run.tooltip=Feu clic aquí per a iniciar l’aplicació action.screenshots.tooltip=Feu clic aquí per a veure imatges d’aquesta aplicació action.settings.tooltip=Feu clic aquí per a obrir accions addicionals diff --git a/bauh/view/resources/locale/de b/bauh/view/resources/locale/de index 699d993e..50081463 100644 --- a/bauh/view/resources/locale/de +++ b/bauh/view/resources/locale/de @@ -60,6 +60,10 @@ action.history.no_history.title=No history action.info.tooltip=Informationen über diese Anwendung anzeigen action.not_allowed=Action not allowed action.request_reboot.title=System restart +action.reset=Restore +action.reset.status=Restoring +action.reset.body_1=This operation will restore {} default settings and clean its cache. +action.reset.body_2=It will be restarted after the operation is finished. action.run.tooltip=Die Anwendung ausführen action.screenshots.tooltip=Fotos der Anwendung anzeigen action.settings.tooltip=Weitere Optionen anzeigen diff --git a/bauh/view/resources/locale/en b/bauh/view/resources/locale/en index d5ad1881..8b6a9b07 100644 --- a/bauh/view/resources/locale/en +++ b/bauh/view/resources/locale/en @@ -60,6 +60,10 @@ action.history.no_history.title=No history action.info.tooltip=Click here to see information about this application action.not_allowed=Action not allowed action.request_reboot.title=System restart +action.reset=Restore +action.reset.status=Restoring +action.reset.body_1=This operation will restore {} default settings and clean its cache. +action.reset.body_2=It will be restarted after the operation is finished. action.run.tooltip=Click here to launch this application action.screenshots.tooltip=Click here to see some pictures of this application action.settings.tooltip=Click here to open extra actions diff --git a/bauh/view/resources/locale/es b/bauh/view/resources/locale/es index 763dbccc..e6e3c361 100644 --- a/bauh/view/resources/locale/es +++ b/bauh/view/resources/locale/es @@ -60,6 +60,10 @@ action.history.no_history.title=No history action.info.tooltip=Pulse aquí para ver información sobre esta aplicación action.not_allowed=Acción no permitida action.request_reboot.title=Reinicio de sistema +action.reset=Restaurar +action.reset.status=Restaurando +action.reset.body_1=Esta operación restaurará las configuraciónes de {} y limpiará su caché. +action.reset.body_2=Él será reiniciado una vez que la operación esté terminada. action.run.tooltip=Pulse aquí para iniciar la aplicación action.screenshots.tooltip=Pulse aquí para ver algunas fotos de esta aplicación action.settings.tooltip=Pulse aquí para abrir acciones adicionales diff --git a/bauh/view/resources/locale/it b/bauh/view/resources/locale/it index e21ed34b..d22b8a24 100644 --- a/bauh/view/resources/locale/it +++ b/bauh/view/resources/locale/it @@ -60,6 +60,10 @@ action.history.no_history.title=No history action.info.tooltip=Clicca qui per vedere informazioni su questa applicazione action.not_allowed=Action not allowed action.request_reboot.title=System restart +action.reset=Restore +action.reset.status=Restoring +action.reset.body_1=This operation will restore {} default settings and clean its cache. +action.reset.body_2=It will be restarted after the operation is finished. action.run.tooltip=Fai clic qui per avviare questa applicazione action.screenshots.tooltip=Clicca qui per vedere alcune immagini di questa applicazione action.settings.tooltip=Fai clic qui per aprire ulteriori azioni diff --git a/bauh/view/resources/locale/pt b/bauh/view/resources/locale/pt index 27e6aef2..18f4f2df 100644 --- a/bauh/view/resources/locale/pt +++ b/bauh/view/resources/locale/pt @@ -60,6 +60,10 @@ action.history.no_history.title=Sem histórico action.info.tooltip=Clique aqui para ver informações sobre este aplicativo action.not_allowed=Ação não permitida action.request_reboot.title=Reinicialização de sistema +action.reset=Restaurar +action.reset.status=Restaurando +action.reset.body_1=Essa operação restaurará as configurações padrões e limpará o cache do {}. +action.reset.body_2=Ele será reinicializado após a operação ser concluída. action.run.tooltip=Clique aqui para iniciar esse aplicativo action.screenshots.tooltip=Clique aqui para ver algumas fotos desse aplicativo action.settings.tooltip=Clique aqui para abrir ações adicionais diff --git a/bauh/view/resources/locale/ru b/bauh/view/resources/locale/ru index 713642f2..18ab565c 100644 --- a/bauh/view/resources/locale/ru +++ b/bauh/view/resources/locale/ru @@ -60,6 +60,10 @@ action.history.no_history.title=No history action.info.tooltip=Нажмите здесь, чтобы посмотреть информацию об этом приложении action.not_allowed=Действие не допускается action.request_reboot.title=System restart +action.reset=Restore +action.reset.status=Restoring +action.reset.body_1=This operation will restore {} default settings and clean its cache. +action.reset.body_2=It will be restarted after the operation is finished. action.run.tooltip=Нажмите здесь, чтобы запустить это приложение action.screenshots.tooltip=Нажмите здесь, чтобы увидеть некоторые скриншоты этого приложения action.settings.tooltip=Нажмите здесь, чтобы открыть дополнительные действия diff --git a/bauh/view/resources/locale/tr b/bauh/view/resources/locale/tr index ee068ef8..c9c4cf3a 100644 --- a/bauh/view/resources/locale/tr +++ b/bauh/view/resources/locale/tr @@ -60,6 +60,10 @@ action.history.no_history.title=Geçmiş yok action.info.tooltip=Uygulama bilgilerini görmek için buraya tıkla action.not_allowed=Eyleme izin verilmiyor action.request_reboot.title=Sistemi yeniden başlat +action.reset=Restore +action.reset.status=Restoring +action.reset.body_1=This operation will restore {} default settings and clean its cache. +action.reset.body_2=It will be restarted after the operation is finished. action.run.tooltip=Uygulamayı başlatmak için burayı tıkla action.screenshots.tooltip=Bu uygulamanın bazı resimlerini görmek için buraya tıklayın action.settings.tooltip=Extra eylemleri açmak için buraya tıkla diff --git a/bauh/view/util/util.py b/bauh/view/util/util.py index e13e21e6..333716da 100644 --- a/bauh/view/util/util.py +++ b/bauh/view/util/util.py @@ -56,20 +56,28 @@ def get_distro(): return 'unknown' -def clean_app_files(managers: List[SoftwareManager]): - print('[bauh] Cleaning configuration and cache files') +def clean_app_files(managers: List[SoftwareManager], logs: bool = True): + + if logs: + print('[bauh] Cleaning configuration and cache files') + for path in (CACHE_PATH, CONFIG_PATH): - print('[bauh] Deleting directory {}'.format(path)) + if logs: + print('[bauh] Deleting directory {}'.format(path)) + if os.path.exists(path): try: shutil.rmtree(path) - print('{}[bauh] Directory {} deleted{}'.format(Fore.YELLOW, path, Fore.RESET)) + if logs: + print('{}[bauh] Directory {} deleted{}'.format(Fore.YELLOW, path, Fore.RESET)) except: - print('{}[bauh] An exception has happened when deleting {}{}'.format(Fore.RED, path, Fore.RESET)) - traceback.print_exc() + if logs: + print('{}[bauh] An exception has happened when deleting {}{}'.format(Fore.RED, path, Fore.RESET)) + traceback.print_exc() if managers: for m in managers: m.clear_data() - print('[bauh] Cleaning finished') + if logs: + print('[bauh] Cleaning finished')