From c1a94c716cee8ecfe124136ff7aa51f7e454fed6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Germ=C3=A1n=20Carrillo?= Date: Tue, 16 Apr 2024 15:49:22 +0200 Subject: [PATCH] Update base lib and adapt the plugin to it --- pg_service_parser/config.py | 3 -- .../core/pg_service_parser_wrapper.py | 42 ++++++------------- pg_service_parser/gui/dlg_pg_service.py | 28 ++++++------- pg_service_parser/utils.py | 11 ----- 4 files changed, 26 insertions(+), 58 deletions(-) delete mode 100644 pg_service_parser/config.py diff --git a/pg_service_parser/config.py b/pg_service_parser/config.py deleted file mode 100644 index d294428..0000000 --- a/pg_service_parser/config.py +++ /dev/null @@ -1,3 +0,0 @@ -import os.path - -DEFAULT_PG_SERVICE_PATH = os.path.expanduser("~/.pg_service.conf") diff --git a/pg_service_parser/core/pg_service_parser_wrapper.py b/pg_service_parser/core/pg_service_parser_wrapper.py index 9af4000..9895c45 100644 --- a/pg_service_parser/core/pg_service_parser_wrapper.py +++ b/pg_service_parser/core/pg_service_parser_wrapper.py @@ -1,12 +1,12 @@ -import os.path +from pathlib import Path from typing import List, Optional from pg_service_parser.libs import pgserviceparser -def conf_path() -> str: +def conf_path() -> Path: path = pgserviceparser.conf_path() - return path if os.path.exists(path) else None + return path if path.exists() else None def service_names(conf_file_path: Optional[str] = None) -> List[str]: @@ -17,26 +17,12 @@ def service_config(service_name: str, conf_file_path: Optional[str] = None) -> d return pgserviceparser.service_config(service_name, conf_file_path) -def write_service_settings( +def write_service( service_name: str, settings: dict, conf_file_path: Optional[str] = None, -) -> bool: - """Returns true if it could write the settings to the file. - - :param str service_name: service's name - :param dict settings: Settings dict defining the service - :param str conf_file_path: path to the pg_service.conf. If None the `conf_path()` is used, defaults to None - - :return bool: True if the setting has been successfully written - """ - config = pgserviceparser.full_config(conf_file_path) - if service_name in config: - config[service_name] = settings.copy() - with open(conf_file_path or pgserviceparser.conf_path(), "w") as configfile: - config.write(configfile) - return True - return False +): + pgserviceparser.write_service(service_name, settings, conf_file_path) def create_service( @@ -51,24 +37,22 @@ def create_service( config.write(f) if service_name in config: - return write_service_settings(service_name, settings) + pgserviceparser.write_service(service_name, settings) + return True return False def copy_service_settings( source_service_name: str, target_service_name: str, conf_file_path: Optional[str] = None -) -> bool: +): settings = pgserviceparser.service_config(source_service_name, conf_file_path) - config = pgserviceparser.full_config(conf_file_path) - res = False + if target_service_name in config: - res = write_service_settings(target_service_name, settings, conf_file_path) + pgserviceparser.write_service(target_service_name, settings, conf_file_path) else: - res = create_service(target_service_name, settings, conf_file_path) - - return res + create_service(target_service_name, settings, conf_file_path) if __name__ == "__main__": @@ -86,7 +70,7 @@ def copy_service_settings( assert service_names() == ["qgis-test"] # Clone existing service - assert copy_service_settings("qgis-test", "qgis-demo") + copy_service_settings("qgis-test", "qgis-demo") assert service_names() == ["qgis-test", "qgis-demo"] assert service_config("qgis-demo") == _settings diff --git a/pg_service_parser/gui/dlg_pg_service.py b/pg_service_parser/gui/dlg_pg_service.py index 484924c..ae62a86 100644 --- a/pg_service_parser/gui/dlg_pg_service.py +++ b/pg_service_parser/gui/dlg_pg_service.py @@ -8,7 +8,7 @@ copy_service_settings, service_config, service_names, - write_service_settings, + write_service, ) from pg_service_parser.utils import get_ui_class @@ -35,7 +35,7 @@ def __init__(self, parent): self.__edit_model = None - self.txtConfFile.setText(conf_file_path) + self.txtConfFile.setText(str(conf_file_path)) self.lblWarning.setVisible(False) self.radOverwrite.toggled.connect(self.__update_target_controls) @@ -99,7 +99,10 @@ def __copy_service(self): return elif self.txtNewService.text().strip() in service_names(): self.bar.pushWarning( - "PG service", "Service name already exists! Change it and try again." + "PG service", + "Service name '{}' already exists! Change it and try again.".format( + self.txtNewService.text().strip() + ), ) return elif self.radOverwrite.isChecked(): @@ -113,12 +116,10 @@ def __copy_service(self): else self.txtNewService.text().strip() ) - if copy_service_settings(self.cboSourceService.currentText(), target_service): - self.bar.pushSuccess("PG service", f"PG service copied to '{target_service}'!") - if self.radCreate.isChecked(): - self.__initialize_copy_services() # Reflect the newly added service - else: - self.bar.pushWarning("PG service", "There was a problem copying the service!") + copy_service_settings(self.cboSourceService.currentText(), target_service) + self.bar.pushSuccess("PG service", f"PG service copied to '{target_service}'!") + if self.radCreate.isChecked(): + self.__initialize_copy_services() # Reflect the newly added service @pyqtSlot(int) def __current_tab_changed(self, index): @@ -157,11 +158,8 @@ def __edit_service_changed(self, index): def __update_service_clicked(self): if self.__edit_model and self.__edit_model.is_dirty(): target_service = self.cboEditService.currentText() - res = write_service_settings(target_service, self.__edit_model.service_config()) - if res: - self.bar.pushSuccess("PG service", f"PG service '{target_service}' updated!") - self.__edit_model.set_not_dirty() - else: - self.bar.pushWarning("PG service", "There was a problem updating the service!") + write_service(target_service, self.__edit_model.service_config()) + self.bar.pushSuccess("PG service", f"PG service '{target_service}' updated!") + self.__edit_model.set_not_dirty() else: self.bar.pushInfo("PG service", "Edit the service configuration and try again.") diff --git a/pg_service_parser/utils.py b/pg_service_parser/utils.py index 9c7f47f..fc0bbec 100644 --- a/pg_service_parser/utils.py +++ b/pg_service_parser/utils.py @@ -2,8 +2,6 @@ from qgis.PyQt.uic import loadUiType -from pg_service_parser.config import DEFAULT_PG_SERVICE_PATH - def get_ui_class(ui_file): """Get UI Python class from .ui file. @@ -20,12 +18,3 @@ def get_ui_file_path(ui_file) -> str: ui_file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "ui", ui_file)) return ui_file_path - - -def get_pg_service_path() -> str: - """ - Path to pg_service.conf file, where data will be read from. - - :return: String. pg_service file path. - """ - return DEFAULT_PG_SERVICE_PATH