-
Notifications
You must be signed in to change notification settings - Fork 4
/
wnt_config_toolkit.py
148 lines (120 loc) · 4.74 KB
/
wnt_config_toolkit.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# -*- coding: utf-8 -*-
"""
/***************************************************************************
WaterNetworkTools
A QGIS plugin
Water Network Modelling Utilities
-------------------
begin : 2019-07-19
copyright : (C) 2019 by Andrés García Martínez
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
__author__ = 'Andrés García Martínez'
__date__ = '2019-07-19'
__copyright__ = '(C) 2019 by Andrés García Martínez'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import sys
import configparser
from PyQt5.QtCore import QCoreApplication
from qgis.core import (QgsProcessing,
QgsProcessingAlgorithm,
QgsProcessingParameterFile)
class ConfigToolkitAlgorithm(QgsProcessingAlgorithm):
"""
Set epanet lib path in tookit.ini file.
"""
# DEFINE CONSTANTS
INPUT = 'INPUT'
def tr(self, string):
"""
Returns a translatable string with the self.tr() function.
"""
return QCoreApplication.translate('Processing', string)
def createInstance(self):
"""
Create a instance and return a new copy of algorithm.
"""
return ConfigToolkitAlgorithm()
def name(self):
"""
Returns the unique algorithm name, used for identifying the algorithm.
"""
return 'config_toolkit'
def displayName(self):
"""
Returns the translated algorithm name, which should be used for any
user-visible display of the algorithm name.
"""
return self.tr('Configure epanet lib')
def group(self):
"""
Returns the name of the group this algorithm belongs to.
"""
return self.tr('Import')
def groupId(self):
"""
Returns the unique ID of the group this algorithm belongs to.
"""
return 'import'
def shortHelpString(self):
"""
Returns a localised short helper string for the algorithm.
"""
return self.tr('''Set the epanet lib path.
Note: The path is store in the toolkit.ini file.
Epanet lib can be download from:
https://www.epa.gov/water-research/epanet#tab-2 (only win 32),
or
https://github.com/andresgciamtez/entoolkit/tree/master/entoolkit/epanet
===
Define la ruta de acceso a la biblioteca de epanet.
Nota: La ruta de acceso se almacena en el archivo toolkit.ini.
La biblioteca de epanet puede ser descargada desde:
- https://www.epa.gov/water-research/epanet#tab-2 (only win 32),
o
- https://github.com/andresgciamtez/entoolkit/tree/master/entoolkit/epanet
''')
def initAlgorithm(self, config=None):
"""
Define the inputs and outputs of the algorithm.
"""
# ADD A FILE DESTINATION
self.addParameter(
QgsProcessingParameterFile(
self.INPUT,
self.tr('Epanet lib')
)
)
def processAlgorithm(self, parameters, context, feedback):
"""
RUN PROCESS
"""
# OUTPUT
lib_file = self.parameterAsFile(parameters, self.INPUT, context)
init_file = sys.path[0] + '/toolkit.ini'
feedback.pushInfo(init_file)
config = configparser.ConfigParser()
config.read(init_file)
config['EPANET']['lib'] = lib_file
with open(init_file, 'w') as configfile:
config.write(configfile)
# SHOW INFO
feedback.pushInfo('='*40)
msg = 'Epanet toolkit library: {}'.format(lib_file)
feedback.pushInfo(f'Epanet toolkit library: {lib_file}')
feedback.pushInfo('='*40)
# PROCCES CANCELED
if feedback.isCanceled():
return {}
# OUTPUT
return {}