Skip to content

Commit

Permalink
Update base lib and adapt the plugin to it
Browse files Browse the repository at this point in the history
  • Loading branch information
gacarrillor committed Apr 16, 2024
1 parent 1c7f125 commit c1a94c7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 58 deletions.
3 changes: 0 additions & 3 deletions pg_service_parser/config.py

This file was deleted.

42 changes: 13 additions & 29 deletions pg_service_parser/core/pg_service_parser_wrapper.py
Original file line number Diff line number Diff line change
@@ -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]:
Expand All @@ -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(
Expand All @@ -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__":
Expand All @@ -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

Expand Down
28 changes: 13 additions & 15 deletions pg_service_parser/gui/dlg_pg_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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():
Expand All @@ -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):
Expand Down Expand Up @@ -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.")
11 changes: 0 additions & 11 deletions pg_service_parser/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

0 comments on commit c1a94c7

Please sign in to comment.