Skip to content
Permalink
Browse files

[processing] added support for old model file format

  • Loading branch information
volaya committed Aug 22, 2014
1 parent c7fb837 commit 887469643f116e6105fbfa141c2932dd9896e08b
@@ -16,7 +16,7 @@
* *
***************************************************************************
"""
from processing.modeler.ModelerUtils import ModelerUtils


__author__ = 'Victor Olaya'
__date__ = 'August 2012'
@@ -32,6 +32,7 @@
from qgis.core import *
import processing
from qgis.utils import iface
from processing.modeler.ModelerUtils import ModelerUtils
from processing.core.ProcessingConfig import ProcessingConfig
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.ProcessingLog import ProcessingLog
@@ -138,7 +139,7 @@ def initialize():
Processing.addProvider(Grass7AlgorithmProvider())
Processing.addProvider(ScriptAlgorithmProvider())
Processing.addProvider(TauDEMAlgorithmProvider())
Processing.addProvider(ModelerAlgorithmProvider())
Processing.addProvider(Processing.modeler)
Processing.modeler.initializeSettings()

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

@staticmethod
def updateProviders():
for provider in Processing.providers:
providers = [p for p in Processing.providers if p.getName() != "model"]
for provider in providers:
provider.loadAlgorithms()

@staticmethod
@@ -188,7 +190,8 @@ def fireAlgsListHasChanged():
def loadAlgorithms():
Processing.algs = {}
Processing.updateProviders()
for provider in Processing.providers:
providers = [p for p in Processing.providers if p.getName() != "model"]
for provider in providers:
providerAlgs = provider.algs
algs = {}
for alg in providerAlgs:
@@ -198,9 +201,18 @@ def loadAlgorithms():
provs = {}
for provider in Processing.providers:
provs[provider.getName()] = provider

ModelerUtils.allAlgs = Processing.algs
ModelerUtils.providers = provs

Processing.modeler.loadAlgorithms()

algs = {}
for alg in Processing.modeler.algs:
algs[alg.commandLineName()] = alg
Processing.algs[Processing.modeler.getName()] = algs


@staticmethod
def loadActions():
for provider in Processing.providers:
@@ -47,7 +47,7 @@ def execute(self):
'*.model')
if filename:
try:
ModelerAlgorithm.fromJsonFile(filename)
ModelerAlgorithm.fromFile(filename)
except WrongModelException:
QtGui.QMessageBox.warning(self.toolbox, "Error reading model", "The selected file does not contain a valid model")
return
@@ -18,6 +18,7 @@
"""



__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
@@ -31,6 +32,8 @@
import copy
import time
import json
import codecs
import traceback
from PyQt4 import QtCore, QtGui
from qgis.core import *
from processing.core.GeoAlgorithm import GeoAlgorithm
@@ -41,6 +44,8 @@
from processing.modeler.ModelerUtils import ModelerUtils
from processing.core.parameters import *
from processing.tools import dataobjects
from processing.core.parameters import getParameterFromString



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


############LEGACY METHOD TO SUPPORT OLD FORMAT###########

LINE_BREAK_STRING = '%%%'

@staticmethod
def fromFile(filename):
try:
alg = ModelerAlgorithm.fromJsonFile(filename)
return alg
except WrongModelException, e:
alg = ModelerAlgorithm.fromOldFormatFile(filename)
return alg


@staticmethod
def fromOldFormatFile(filename):
hardcodedValues = {}
modelParameters = []
modelAlgs = []
model = ModelerAlgorithm()
model.descriptionFile = filename
lines = codecs.open(filename, 'r', encoding='utf-8')
line = lines.readline().strip('\n').strip('\r')
try:
while line != '':
if line.startswith('PARAMETER:'):
paramLine = line[len('PARAMETER:'):]
param = getParameterFromString(paramLine)
if param:
pass
else:
raise WrongModelException('Error in parameter line: '
+ line)
line = lines.readline().strip('\n')
tokens = line.split(',')
model.addParameter(ModelerParameter(param, QtCore.QPointF(
float(tokens[0]), float(tokens[1]))))
modelParameters.append(param.name)
elif line.startswith('VALUE:'):
valueLine = line[len('VALUE:'):]
tokens = valueLine.split('===')
name = tokens[0]
value = tokens[1].replace(ModelerAlgorithm.LINE_BREAK_STRING, '\n')
hardcodedValues[name] = value
elif line.startswith('NAME:'):
model.name = line[len('NAME:'):]
elif line.startswith('GROUP:'):
model.group = line[len('GROUP:'):]
elif line.startswith('ALGORITHM:'):
algLine = line[len('ALGORITHM:'):]
alg = ModelerUtils.getAlgorithm(algLine)
if alg is not None:
modelAlg = Algorithm(alg.commandLineName())
modelAlg.description = alg.name
posline = lines.readline().strip('\n').strip('\r')
tokens = posline.split(',')
modelAlg.pos = QtCore.QPointF(float(tokens[0]), float(tokens[1]))
dependenceline = lines.readline().strip('\n').strip('\r') #unused
for param in alg.parameters:
if not param.hidden:
line = lines.readline().strip('\n').strip('\r')
if line == str(None):
modelAlg.params[param.name] = None
else:
tokens = line.split('|')
algIdx = int(tokens[0])
if algIdx == -1:
if tokens[1] in modelParameters:
modelAlg.params[param.name] = ValueFromInput(tokens[1])
else:
modelAlg.params[param.name] = hardcodedValues[tokens[1]]
else:
modelAlg.params[param.name] = ValueFromOutput(algIdx, tokens[1])

for out in alg.outputs:
if not out.hidden:
line = lines.readline().strip('\n').strip('\r')
if str(None) != line:
if '|' in line:
tokens = line.split('|')
name = tokens[0]
tokens = tokens[1].split(',')
pos = QtCore.QPointF(
float(tokens[0]), float(tokens[1]))
else:
name = line
pos = None
modelerOutput = ModelerOutput(name)
modelerOutput.pos = pos
modelAlg.outputs[out.name] = modelerOutput

model.addAlgorithm(modelAlg)
modelAlgs.append(modelAlg.name)
else:
raise WrongModelException('Error in algorithm name: '
+ algLine)
line = lines.readline().strip('\n').strip('\r')
for modelAlg in model.algs.values():
for name, value in modelAlg.params.iteritems():
if isinstance(value, ValueFromOutput):
value.alg = modelAlgs[value.alg]
return model
except Exception, e:
if isinstance(e, WrongModelException):
raise e
else:
raise WrongModelException('Error in model definition line:'
+ line.strip() + ' : ' + traceback.format_exc())



@@ -81,10 +81,9 @@ def loadFromFolder(self, folder):
if descriptionFile.endswith('model'):
try:
fullpath = os.path.join(path, descriptionFile)
alg = ModelerAlgorithm.fromJsonFile(fullpath)
if alg:
alg.provider = self
self.algs.append(alg)
alg = ModelerAlgorithm.fromFile(fullpath)
alg.provider = self
self.algs.append(alg)
except WrongModelException, e:
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
'Could not load model ' + descriptionFile + '\n'
@@ -316,16 +316,13 @@ def openModel(self):
self.tr('Processing models (*.model)')))
if filename:
try:
alg = ModelerAlgorithm.fromJsonFile(filename)
alg = ModelerAlgorithm.fromFile(filename)
self.alg = alg
self.alg.setModelerView(self)
self.textGroup.setText(alg.group)
self.textName.setText(alg.name)
self.repaintModel()
#===============================================================
# if self.scene.getLastAlgorithmItem():
# self.view.ensureVisible(self.scene.getLastAlgorithmItem())
#===============================================================

self.view.centerOn(0, 0)
self.hasChanged = False
except WrongModelException, e:

0 comments on commit 8874696

Please sign in to comment.
You can’t perform that action at this time.