diff --git a/.gitignore b/.gitignore index 110bd02..0fa2eb7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -pg_service_parser/libs \ No newline at end of file +pg_service_parser/libs +.idea diff --git a/LICENCE b/LICENSE similarity index 100% rename from LICENCE rename to LICENSE diff --git a/pg_service_parser/__init__.py b/pg_service_parser/__init__.py new file mode 100644 index 0000000..b0ec41b --- /dev/null +++ b/pg_service_parser/__init__.py @@ -0,0 +1,5 @@ +from .pg_service_parser_plugin import PGServiceParserPlugin + + +def classFactory(iface): + return PGServiceParserPlugin(iface) diff --git a/pg_service_parser/metadata.txt b/pg_service_parser/metadata.txt new file mode 100644 index 0000000..366d50f --- /dev/null +++ b/pg_service_parser/metadata.txt @@ -0,0 +1,17 @@ +[general] +name=pg_service parser +description=Copy or override PG service entries +about=Copy or override PG service entries for connection to PostgreSQL databases +version=0.0.1 +qgisMinimumVersion=3.00 +qgisMaximumVersion=3.99 +category=Database +author=OPENGIS.CH +email=german@opengis.ch +tags=postgresql,connection,db,database,service +homepage=https://github.com/opengisch/qgis-pg-service-parser-plugin +tracker=https://github.com/opengisch/qgis-pg-service-parser-plugin/issues +repository=https://github.com/opengisch/qgis-pg-service-parser-plugin +icon=images/icon.png +experimental=False +deprecated=False \ No newline at end of file diff --git a/pg_service_parser/pg_service_parser_plugin.py b/pg_service_parser/pg_service_parser_plugin.py new file mode 100644 index 0000000..444916a --- /dev/null +++ b/pg_service_parser/pg_service_parser_plugin.py @@ -0,0 +1,19 @@ +from qgis.PyQt.QtWidgets import QAction + + +class PGServiceParserPlugin(): + def __init__(self, iface): + self.iface = iface + self.action = None + + def initGui(self): + self.action = QAction('Go!', self.iface.mainWindow()) + self.action.triggered.connect(self.run) + self.iface.addToolBarIcon(self.action) + + def unload(self): + self.iface.removeToolBarIcon(self.action) + del self.action + + def run(self): + print("Eureka!") diff --git a/pg_service_parser/pg_service_parser_wrapper.py b/pg_service_parser/pg_service_parser_wrapper.py new file mode 100644 index 0000000..4a80dae --- /dev/null +++ b/pg_service_parser/pg_service_parser_wrapper.py @@ -0,0 +1,88 @@ +import pgserviceparser +from typing import (List, + Optional) + + +def service_names(conf_file_path: Optional[str] = None) -> List[str]: + return pgserviceparser.service_names(conf_file_path) + + +def service_config(service_name: str, conf_file_path: Optional[str] = None) -> dict: + return pgserviceparser.service_config(service_name, conf_file_path) + + +def write_service_settings( + 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 + + +def create_service(service_name: str, settings: dict, conf_file_path: Optional[str] = None) -> bool: + config = pgserviceparser.full_config(conf_file_path) + if service_name in config: + return False + + config.add_section(service_name) + with open(conf_file_path or pgserviceparser.conf_path(), 'w') as f: + config.write(f) + + if service_name in config: + return write_service_settings(service_name, settings) + + 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) + else: + res = create_service(target_service_name, settings, conf_file_path) + + return res + + +if __name__ == '__main__': + assert service_names() == [] + + # Add new service + _settings = {'host': 'localhost', 'port': '5432', 'user': 'postgres', 'password': 'secret', + 'dbname': 'qgis_test_db'} + assert create_service("qgis-test", _settings) + assert service_names() == ["qgis-test"] + + # Clone existing service + assert copy_service_settings("qgis-test", "qgis-demo") + assert service_names() == ["qgis-test", "qgis-demo"] + assert service_config("qgis-demo") == _settings + + # Add new service + _settings = {'host': 'localhost', 'port': '5433', 'user': 'admin', 'password': 'secret', + 'dbname': 'qgis_test_db2'} + assert create_service("qgis-new-test", _settings) + assert service_names() == ["qgis-test", "qgis-demo", "qgis-new-test"] + assert service_config("qgis-new-test") == _settings + + # Override existing qgis-test + assert copy_service_settings("qgis-new-test", "qgis-test") + assert service_names() == ["qgis-test", "qgis-demo", "qgis-new-test"] + assert service_config("qgis-test") == _settings