Skip to content

Commit 8874696

Browse files
committed
[processing] added support for old model file format
1 parent c7fb837 commit 8874696

File tree

5 files changed

+139
-14
lines changed

5 files changed

+139
-14
lines changed

python/plugins/processing/core/Processing.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* *
1717
***************************************************************************
1818
"""
19-
from processing.modeler.ModelerUtils import ModelerUtils
19+
2020

2121
__author__ = 'Victor Olaya'
2222
__date__ = 'August 2012'
@@ -32,6 +32,7 @@
3232
from qgis.core import *
3333
import processing
3434
from qgis.utils import iface
35+
from processing.modeler.ModelerUtils import ModelerUtils
3536
from processing.core.ProcessingConfig import ProcessingConfig
3637
from processing.core.GeoAlgorithm import GeoAlgorithm
3738
from processing.core.ProcessingLog import ProcessingLog
@@ -138,7 +139,7 @@ def initialize():
138139
Processing.addProvider(Grass7AlgorithmProvider())
139140
Processing.addProvider(ScriptAlgorithmProvider())
140141
Processing.addProvider(TauDEMAlgorithmProvider())
141-
Processing.addProvider(ModelerAlgorithmProvider())
142+
Processing.addProvider(Processing.modeler)
142143
Processing.modeler.initializeSettings()
143144

144145
# And initialize
@@ -166,7 +167,8 @@ def loadFromProviders():
166167

167168
@staticmethod
168169
def updateProviders():
169-
for provider in Processing.providers:
170+
providers = [p for p in Processing.providers if p.getName() != "model"]
171+
for provider in providers:
170172
provider.loadAlgorithms()
171173

172174
@staticmethod
@@ -188,7 +190,8 @@ def fireAlgsListHasChanged():
188190
def loadAlgorithms():
189191
Processing.algs = {}
190192
Processing.updateProviders()
191-
for provider in Processing.providers:
193+
providers = [p for p in Processing.providers if p.getName() != "model"]
194+
for provider in providers:
192195
providerAlgs = provider.algs
193196
algs = {}
194197
for alg in providerAlgs:
@@ -198,9 +201,18 @@ def loadAlgorithms():
198201
provs = {}
199202
for provider in Processing.providers:
200203
provs[provider.getName()] = provider
204+
201205
ModelerUtils.allAlgs = Processing.algs
202206
ModelerUtils.providers = provs
203207

208+
Processing.modeler.loadAlgorithms()
209+
210+
algs = {}
211+
for alg in Processing.modeler.algs:
212+
algs[alg.commandLineName()] = alg
213+
Processing.algs[Processing.modeler.getName()] = algs
214+
215+
204216
@staticmethod
205217
def loadActions():
206218
for provider in Processing.providers:

python/plugins/processing/modeler/AddModelFromFileAction.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def execute(self):
4747
'*.model')
4848
if filename:
4949
try:
50-
ModelerAlgorithm.fromJsonFile(filename)
50+
ModelerAlgorithm.fromFile(filename)
5151
except WrongModelException:
5252
QtGui.QMessageBox.warning(self.toolbox, "Error reading model", "The selected file does not contain a valid model")
5353
return

python/plugins/processing/modeler/ModelerAlgorithm.py

+117
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"""
1919

2020

21+
2122
__author__ = 'Victor Olaya'
2223
__date__ = 'August 2012'
2324
__copyright__ = '(C) 2012, Victor Olaya'
@@ -31,6 +32,8 @@
3132
import copy
3233
import time
3334
import json
35+
import codecs
36+
import traceback
3437
from PyQt4 import QtCore, QtGui
3538
from qgis.core import *
3639
from processing.core.GeoAlgorithm import GeoAlgorithm
@@ -41,6 +44,8 @@
4144
from processing.modeler.ModelerUtils import ModelerUtils
4245
from processing.core.parameters import *
4346
from processing.tools import dataobjects
47+
from processing.core.parameters import getParameterFromString
48+
4449

4550

4651
class ModelerParameter():
@@ -524,3 +529,115 @@ def fromJsonFile(filename):
524529
alg.descriptionFile = filename
525530
return alg
526531

532+
533+
############LEGACY METHOD TO SUPPORT OLD FORMAT###########
534+
535+
LINE_BREAK_STRING = '%%%'
536+
537+
@staticmethod
538+
def fromFile(filename):
539+
try:
540+
alg = ModelerAlgorithm.fromJsonFile(filename)
541+
return alg
542+
except WrongModelException, e:
543+
alg = ModelerAlgorithm.fromOldFormatFile(filename)
544+
return alg
545+
546+
547+
@staticmethod
548+
def fromOldFormatFile(filename):
549+
hardcodedValues = {}
550+
modelParameters = []
551+
modelAlgs = []
552+
model = ModelerAlgorithm()
553+
model.descriptionFile = filename
554+
lines = codecs.open(filename, 'r', encoding='utf-8')
555+
line = lines.readline().strip('\n').strip('\r')
556+
try:
557+
while line != '':
558+
if line.startswith('PARAMETER:'):
559+
paramLine = line[len('PARAMETER:'):]
560+
param = getParameterFromString(paramLine)
561+
if param:
562+
pass
563+
else:
564+
raise WrongModelException('Error in parameter line: '
565+
+ line)
566+
line = lines.readline().strip('\n')
567+
tokens = line.split(',')
568+
model.addParameter(ModelerParameter(param, QtCore.QPointF(
569+
float(tokens[0]), float(tokens[1]))))
570+
modelParameters.append(param.name)
571+
elif line.startswith('VALUE:'):
572+
valueLine = line[len('VALUE:'):]
573+
tokens = valueLine.split('===')
574+
name = tokens[0]
575+
value = tokens[1].replace(ModelerAlgorithm.LINE_BREAK_STRING, '\n')
576+
hardcodedValues[name] = value
577+
elif line.startswith('NAME:'):
578+
model.name = line[len('NAME:'):]
579+
elif line.startswith('GROUP:'):
580+
model.group = line[len('GROUP:'):]
581+
elif line.startswith('ALGORITHM:'):
582+
algLine = line[len('ALGORITHM:'):]
583+
alg = ModelerUtils.getAlgorithm(algLine)
584+
if alg is not None:
585+
modelAlg = Algorithm(alg.commandLineName())
586+
modelAlg.description = alg.name
587+
posline = lines.readline().strip('\n').strip('\r')
588+
tokens = posline.split(',')
589+
modelAlg.pos = QtCore.QPointF(float(tokens[0]), float(tokens[1]))
590+
dependenceline = lines.readline().strip('\n').strip('\r') #unused
591+
for param in alg.parameters:
592+
if not param.hidden:
593+
line = lines.readline().strip('\n').strip('\r')
594+
if line == str(None):
595+
modelAlg.params[param.name] = None
596+
else:
597+
tokens = line.split('|')
598+
algIdx = int(tokens[0])
599+
if algIdx == -1:
600+
if tokens[1] in modelParameters:
601+
modelAlg.params[param.name] = ValueFromInput(tokens[1])
602+
else:
603+
modelAlg.params[param.name] = hardcodedValues[tokens[1]]
604+
else:
605+
modelAlg.params[param.name] = ValueFromOutput(algIdx, tokens[1])
606+
607+
for out in alg.outputs:
608+
if not out.hidden:
609+
line = lines.readline().strip('\n').strip('\r')
610+
if str(None) != line:
611+
if '|' in line:
612+
tokens = line.split('|')
613+
name = tokens[0]
614+
tokens = tokens[1].split(',')
615+
pos = QtCore.QPointF(
616+
float(tokens[0]), float(tokens[1]))
617+
else:
618+
name = line
619+
pos = None
620+
modelerOutput = ModelerOutput(name)
621+
modelerOutput.pos = pos
622+
modelAlg.outputs[out.name] = modelerOutput
623+
624+
model.addAlgorithm(modelAlg)
625+
modelAlgs.append(modelAlg.name)
626+
else:
627+
raise WrongModelException('Error in algorithm name: '
628+
+ algLine)
629+
line = lines.readline().strip('\n').strip('\r')
630+
for modelAlg in model.algs.values():
631+
for name, value in modelAlg.params.iteritems():
632+
if isinstance(value, ValueFromOutput):
633+
value.alg = modelAlgs[value.alg]
634+
return model
635+
except Exception, e:
636+
if isinstance(e, WrongModelException):
637+
raise e
638+
else:
639+
raise WrongModelException('Error in model definition line:'
640+
+ line.strip() + ' : ' + traceback.format_exc())
641+
642+
643+

python/plugins/processing/modeler/ModelerAlgorithmProvider.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,9 @@ def loadFromFolder(self, folder):
8181
if descriptionFile.endswith('model'):
8282
try:
8383
fullpath = os.path.join(path, descriptionFile)
84-
alg = ModelerAlgorithm.fromJsonFile(fullpath)
85-
if alg:
86-
alg.provider = self
87-
self.algs.append(alg)
84+
alg = ModelerAlgorithm.fromFile(fullpath)
85+
alg.provider = self
86+
self.algs.append(alg)
8887
except WrongModelException, e:
8988
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
9089
'Could not load model ' + descriptionFile + '\n'

python/plugins/processing/modeler/ModelerDialog.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -316,16 +316,13 @@ def openModel(self):
316316
self.tr('Processing models (*.model)')))
317317
if filename:
318318
try:
319-
alg = ModelerAlgorithm.fromJsonFile(filename)
319+
alg = ModelerAlgorithm.fromFile(filename)
320320
self.alg = alg
321321
self.alg.setModelerView(self)
322322
self.textGroup.setText(alg.group)
323323
self.textName.setText(alg.name)
324324
self.repaintModel()
325-
#===============================================================
326-
# if self.scene.getLastAlgorithmItem():
327-
# self.view.ensureVisible(self.scene.getLastAlgorithmItem())
328-
#===============================================================
325+
329326
self.view.centerOn(0, 0)
330327
self.hasChanged = False
331328
except WrongModelException, e:

0 commit comments

Comments
 (0)