-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.py
94 lines (79 loc) · 4.05 KB
/
settings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
Copyright © 2023 by BGEO. All rights reserved.
The program is free software: you can redistribute it and/or modify it under the terms of the GNU
General Public License as published by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
"""
# -*- coding: utf-8 -*-
import configparser, os, sys, glob, importlib
# Pointer to the module object instance itself
this = sys.modules[__name__]
# we can explicitly make assignments on it
this.giswater_folder = None
this.tools_db = None
this.tools_log = None
this.tools_os = None
this.tools_qgis = None
this.tools_qt = None
this.tools_gw = None
this.dialog = None
this.toolbox = None
this.task = None
this.gw_global_vars = None
this.lib_vars = None
def init_plugin():
if this.giswater_folder is not None:
print("Variable giswater_folder already set")
return
this.giswater_folder = get_giswater_folder()
if this.giswater_folder is None:
print("Giswater plugin folder not found")
return
# Define imports from Giswater modules
try:
this.tools_db = importlib.import_module('.tools_db', package=f'{this.giswater_folder}.lib')
this.tools_log = importlib.import_module('.tools_log', package=f'{this.giswater_folder}.lib')
this.tools_os = importlib.import_module('.tools_os', package=f'{this.giswater_folder}.lib')
this.tools_qgis = importlib.import_module('.tools_qgis', package=f'{this.giswater_folder}.lib')
this.tools_qt = importlib.import_module('.tools_qt', package=f'{this.giswater_folder}.lib')
except ImportError:
this.tools_db = importlib.import_module('.tools_db', package=f'{this.giswater_folder}.libs')
this.tools_log = importlib.import_module('.tools_log', package=f'{this.giswater_folder}.libs')
this.tools_os = importlib.import_module('.tools_os', package=f'{this.giswater_folder}.libs')
this.tools_qgis = importlib.import_module('.tools_qgis', package=f'{this.giswater_folder}.libs')
this.tools_qt = importlib.import_module('.tools_qt', package=f'{this.giswater_folder}.libs')
this.lib_vars = importlib.import_module('.lib_vars', package=f'{this.giswater_folder}.libs')
this.tools_gw = importlib.import_module('.tools_gw', package=f'{this.giswater_folder}.core.utils')
this.dialog = importlib.import_module('.dialog', package=f'{this.giswater_folder}.core.toolbars')
this.toolbox = importlib.import_module('.toolbox_btn', package=f'{this.giswater_folder}.core.toolbars.utilities')
this.task = importlib.import_module('.task', package=f'{this.giswater_folder}.core.threads')
this.gw_global_vars = importlib.import_module('.global_vars', package=f'{this.giswater_folder}')
def get_giswater_folder(filename_to_find='metadata.txt', get_full_path=False):
""" Find and return Giswater plugin folder name """
# Get QGIS plugin root folder from environment variables
qgis_plugin_root_folder = None
try:
if sys.platform == "win32":
qgis_plugin_root_folder = os.environ['QGIS_PLUGINPATH']
elif sys.platform == "linux":
qgis_plugin_root_folder = os.environ['QGIS_PLUGINPATH']
elif sys.platform == "darwin":
qgis_plugin_root_folder = os.environ['QGIS_PLUGINPATH']
except KeyError:
# Key not found
pass
# Get QGIS plugin root folder from qgis plugin path
if qgis_plugin_root_folder is None:
qgis_plugin_root_folder = os.path.dirname(os.path.dirname(__file__))
# Find @filename recursively inside this folder
for filename in glob.glob(f"{qgis_plugin_root_folder}/**/{filename_to_find}", recursive=True):
parser = configparser.ConfigParser()
parser.read(filename)
if not parser.has_section('general'): continue
if not parser.has_option('general', 'name'): continue
if parser['general']['name'] == 'giswater':
if get_full_path:
return os.path.dirname(filename)
giswater_folder_name = os.path.basename(os.path.dirname(filename))
return giswater_folder_name
return None