-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial wrapper code and plugin python skeleton
- Loading branch information
1 parent
95b34a5
commit ff52c23
Showing
6 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pg_service_parser/libs | ||
pg_service_parser/libs | ||
.idea |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from .pg_service_parser_plugin import PGServiceParserPlugin | ||
|
||
|
||
def classFactory(iface): | ||
return PGServiceParserPlugin(iface) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 protected] | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |