Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Apr 15, 2024
1 parent c93c6d5 commit ed53053
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 41 deletions.
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,3 @@ repos:
ci:
autofix_prs: true
autoupdate_schedule: quarterly

1 change: 0 additions & 1 deletion pg_service_parser/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os.path


DEFAULT_PG_SERVICE_PATH = os.path.expanduser("~/.pg_service.conf")
32 changes: 20 additions & 12 deletions pg_service_parser/gui/dlg_pg_service.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from qgis.PyQt.QtCore import (Qt,
pyqtSlot)
from qgis.PyQt.QtWidgets import (QDialog,
QSizePolicy)
from qgis.gui import QgsMessageBar

from pg_service_parser.pg_service_parser_wrapper import (conf_path,
copy_service_settings,
service_names)
from qgis.PyQt.QtCore import Qt, pyqtSlot
from qgis.PyQt.QtWidgets import QDialog, QSizePolicy

from pg_service_parser.pg_service_parser_wrapper import (
conf_path,
copy_service_settings,
service_names,
)
from pg_service_parser.utils import get_ui_class

DIALOG_UI = get_ui_class('pg_service_dialog.ui')
DIALOG_UI = get_ui_class("pg_service_dialog.ui")
COPY_TAB_INDEX = 0
EDIT_TAB_INDEX = 1

Expand All @@ -23,7 +23,9 @@ def __init__(self, parent):
conf_file_path = conf_path()
if not conf_file_path:
self.lblConfFile.setText("Config file not found!")
self.lblConfFile.setToolTip("Set your PGSERVICEFILE environment variable and reopen the dialog.")
self.lblConfFile.setToolTip(
"Set your PGSERVICEFILE environment variable and reopen the dialog."
)
self.txtConfFile.setVisible(False)
self.tabWidget.setEnabled(False)
return
Expand Down Expand Up @@ -79,14 +81,20 @@ def __copy_service(self):
self.bar.pushInfo("PG service", "Enter a service name and try again.")
return
elif self.txtNewService.text().strip() in service_names():
self.bar.pushWarning("PG service", "Service name already exists! Change it and try again.")
self.bar.pushWarning(
"PG service", "Service name already exists! Change it and try again."
)
return
elif self.radOverwrite.isChecked():
if not self.cboTargetService.currentText():
self.bar.pushInfo("PG service", "Select a valid target service and try again.")
return

target_service = self.cboTargetService.currentText() if self.radOverwrite.isChecked() else self.txtNewService.text().strip()
target_service = (
self.cboTargetService.currentText()
if self.radOverwrite.isChecked()
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}'!")
Expand Down
2 changes: 1 addition & 1 deletion pg_service_parser/images/warning.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion pg_service_parser/metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ 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
deprecated=False
4 changes: 2 additions & 2 deletions pg_service_parser/pg_service_parser_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
from pg_service_parser.gui.dlg_pg_service import PgServiceDialog


class PgServiceParserPlugin():
class PgServiceParserPlugin:
def __init__(self, iface):
self.iface = iface
self.action = None

def initGui(self):
self.action = QAction('Go!', self.iface.mainWindow())
self.action = QAction("Go!", self.iface.mainWindow())
self.action.triggered.connect(self.run)
self.iface.addToolBarIcon(self.action)

Expand Down
40 changes: 27 additions & 13 deletions pg_service_parser/pg_service_parser_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import os.path
from typing import List, Optional

import pgserviceparser
from typing import (List,
Optional)


def conf_path() -> str:
path = pgserviceparser.conf_path()
return path if os.path.exists(path) else None


def service_names(conf_file_path: Optional[str] = None) -> List[str]:
return pgserviceparser.service_names(conf_file_path)

Expand All @@ -18,9 +18,9 @@ def service_config(service_name: str, conf_file_path: Optional[str] = None) -> d


def write_service_settings(
service_name: str,
settings: dict,
conf_file_path: Optional[str] = None,
service_name: str,
settings: dict,
conf_file_path: Optional[str] = None,
) -> bool:
"""Returns true if it could write the settings to the file.
Expand All @@ -39,13 +39,15 @@ def write_service_settings(
return False


def create_service(service_name: str, settings: dict, conf_file_path: Optional[str] = None) -> bool:
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:
with open(conf_file_path or pgserviceparser.conf_path(), "w") as f:
config.write(f)

if service_name in config:
Expand All @@ -54,7 +56,9 @@ def create_service(service_name: str, settings: dict, conf_file_path: Optional[s
return False


def copy_service_settings(source_service_name: str, target_service_name: str, conf_file_path: Optional[str] = None) -> bool:
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)
Expand All @@ -67,12 +71,17 @@ def copy_service_settings(source_service_name: str, target_service_name: str, co
return res


if __name__ == '__main__':
if __name__ == "__main__":
assert service_names() == []

# Add new service
_settings = {'host': 'localhost', 'port': '5432', 'user': 'postgres', 'password': 'secret',
'dbname': 'qgis_test_db'}
_settings = {
"host": "localhost",
"port": "5432",
"user": "postgres",
"password": "secret",
"dbname": "qgis_test_db",
}
assert create_service("qgis-test", _settings)
assert service_names() == ["qgis-test"]

Expand All @@ -82,8 +91,13 @@ def copy_service_settings(source_service_name: str, target_service_name: str, co
assert service_config("qgis-demo") == _settings

# Add new service
_settings = {'host': 'localhost', 'port': '5433', 'user': 'admin', 'password': 'secret',
'dbname': 'qgis_test_db2'}
_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
Expand Down
14 changes: 4 additions & 10 deletions pg_service_parser/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from qgis.PyQt.uic import (loadUiType,
loadUi)

from qgis.PyQt.uic import loadUiType

from pg_service_parser.config import DEFAULT_PG_SERVICE_PATH

Expand All @@ -16,14 +16,8 @@ def get_ui_class(ui_file):


def get_ui_file_path(ui_file) -> str:
os.path.sep.join(ui_file.split('/'))
ui_file_path = os.path.abspath(
os.path.join(
os.path.dirname(__file__),
'ui',
ui_file
)
)
os.path.sep.join(ui_file.split("/"))
ui_file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "ui", ui_file))

return ui_file_path

Expand Down

0 comments on commit ed53053

Please sign in to comment.