Skip to content

Commit

Permalink
Adapt more python code to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 5, 2017
1 parent 6517470 commit 0c3ad14
Show file tree
Hide file tree
Showing 53 changed files with 157 additions and 637 deletions.
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/gdal/translate.py
Expand Up @@ -109,7 +109,7 @@ def getConsoleCommands(self, parameters):
outsize = str(self.getParameterValue(self.OUTSIZE))
outsizePerc = str(self.getParameterValue(self.OUTSIZE_PERC))
noData = self.getParameterValue(self.NO_DATA)
expand = self.getParameterFromName(self.EXPAND).options[self.getParameterValue(self.EXPAND)][1]
expand = parameters[self.EXPAND].options[self.getParameterValue(self.EXPAND)][1]
projwin = str(self.getParameterValue(self.PROJWIN))
if not projwin:
projwin = QgsProcessingUtils.combineLayerExtents([inLayer])
Expand Down
41 changes: 14 additions & 27 deletions python/plugins/processing/algs/grass7/ext/i.py
Expand Up @@ -33,6 +33,7 @@
from processing.tools.system import isWindows
from ..Grass7Utils import Grass7Utils
from os import path
from copy import deepcopy


def multipleOutputDir(alg, field, basename=None):
Expand Down Expand Up @@ -112,58 +113,44 @@ def regroupRasters(alg, parameters, field, groupField, subgroupField=None, extFi
:param parameters:
"""
# List of rasters names

new_parameters = deepcopy(parameters)

rasters = alg.getParameterFromName(field)
rastersList = rasters.value.split(';')
alg.parameters.remove(rasters)
del new_parameters[field]

# Insert a i.group command
group = getParameterFromString("ParameterString|{}|group of rasters|None|False|False".format(groupField))
group.value = alg.getTempFilename()
alg.addParameter(group)
new_parameters[group.name()] = alg.getTempFilename()

if subgroupField:
subgroup = getParameterFromString("ParameterString|{}|subgroup of rasters|None|False|False".format(subgroupField))
subgroup.value = alg.getTempFilename()
alg.addParameter(subgroup)
new_parameters[subgroup.name()] = alg.getTempFilename()

command = 'i.group group={}{} input={}'.format(
group.value,
' subgroup={}'.format(subgroup.value) if subgroupField else '',
new_parameters[group.name()],
' subgroup={}'.format(new_parameters[subgroup.name()]) if subgroupField else '',
','.join([alg.exportedLayers[f] for f in rastersList])
)
alg.commands.append(command)

# Handle external files
origExtParams = {}
if subgroupField and extFile:
for ext in list(extFile.keys()):
extFileName = alg.getParameterValue(ext)
extFileName = new_parameters[ext]
if extFileName:
shortFileName = path.basename(extFileName)
destPath = path.join(Grass7Utils.grassMapsetFolder(),
'PERMANENT',
'group', group.value,
'subgroup', subgroup.value,
'group', new_parameters[group.name()],
'subgroup', new_parameters[subgroup.name()],
extFile[ext], shortFileName)
copyFile(alg, extFileName, destPath)
origExtParams[ext] = extFileName
alg.setParameterValue(ext, shortFileName)
new_parameters[ext] = shortFileName

# modify parameters values
alg.processCommand()

# Re-add input rasters
alg.addParameter(rasters)

# replace external files value with original value
for param in list(origExtParams.keys()):
alg.setParameterValue(param, origExtParams[param])

# Delete group:
alg.parameters.remove(group)
if subgroupField:
alg.parameters.remove(subgroup)
return group.value, subgroup.value
alg.processCommand(new_parameters)

return group.value

Expand Down
22 changes: 10 additions & 12 deletions python/plugins/processing/algs/grass7/ext/r_colors.py
Expand Up @@ -27,6 +27,7 @@
__revision__ = '$Format:%H$'

import os
from copy import deepcopy


def checkParameterValuesBeforeExecuting(alg):
Expand Down Expand Up @@ -74,32 +75,29 @@ def processInputs(alg):

def processCommand(alg, parameters):
# remove output before processCommand
new_parameters = deepcopy(parameters)

output = alg.getOutputFromName('output_dir')
alg.removeOutputFromName('output_dir')
color = alg.getParameterFromName('color')
if color.value == 0:
alg.parameters.remove(color)
if new_parameters[color.name()] == 0:
del new_parameters[color.name()]

# Handle rules
txtRules = alg.getParameterFromName('rules_txt')
if txtRules.value:
if new_parameters[txtRules.name()]:
# Creates a temporary txt file
tempRulesName = alg.getTempFilename()

# Inject rules into temporary txt file
with open(tempRulesName, "w") as tempRules:
tempRules.write(txtRules.value)
tempRules.write(new_parameters[txtRules.name()])

# Use temporary file as rules file
alg.setParameterValue('rules', tempRulesName)
alg.parameters.remove(txtRules)

alg.processCommand()
new_parameters['rules'] = tempRulesName
del new_parameters[textRules.name()]

# re-add the previous output
alg.addOutput(output)
alg.addParameter(color)
alg.addParameter(txtRules)
alg.processCommand(new_parameters)


def processOutputs(alg):
Expand Down
34 changes: 17 additions & 17 deletions python/plugins/processing/algs/grass7/ext/r_li.py
Expand Up @@ -29,6 +29,7 @@
from processing.tools.system import isWindows, mkdir
from processing.core.parameters import getParameterFromString
import os
from copy import deepcopy

# for MS-Windows users who have MBCS chars in their name:
if os.name == 'nt':
Expand Down Expand Up @@ -83,29 +84,33 @@ def checkMovingWindow(alg, outputTxt=False):
return None


def configFile(alg, outputTxt=False):
""" Handle inline configuration """
def configFile(alg, parameters, outputTxt=False):
""" Handle inline configuration
:param parameters:
"""
# Where is the GRASS7 user directory ?

new_parameters = deepcopy(parameters)

userGrass7Path = rliPath()
if not os.path.isdir(userGrass7Path):
mkdir(userGrass7Path)
if not os.path.isdir(os.path.join(userGrass7Path, 'output')):
mkdir(os.path.join(userGrass7Path, 'output'))
origConfigFile = alg.getParameterValue('config')
origConfigFile = new_parameters['config']

# Handle inline configuration
configTxt = alg.getParameterFromName('config_txt')
if configTxt.value:
if new_parameters['config_txt']:
# Creates a temporary txt file in user r.li directory
tempConfig = alg.getTempFilename()
configFilePath = os.path.join(userGrass7Path, tempConfig)
# Inject rules into temporary txt file
with open(configFilePath, "w") as f:
f.write(configTxt.value)
f.write(new_parameters['config_txt'])

# Use temporary file as rules file
alg.setParameterValue('config', os.path.basename(configFilePath))
alg.parameters.remove(configTxt)
new_parameters['config'] = os.path.basename(configFilePath)
del new_parameters['config_txt']

# If we have a configuration file, we need to copy it into user dir
if origConfigFile:
Expand All @@ -114,26 +119,21 @@ def configFile(alg, outputTxt=False):
shutil.copy(origConfigFile, configFilePath)

# Change the parameter value
alg.setParameterValue('config', os.path.basename(configFilePath))
new_parameters['config'] = os.path.basename(configFilePath)

origOutput = alg.getOutputFromName('output')
if outputTxt:
if new_parameters['output']:
param = getParameterFromString("ParameterString|output|txt output|None|False|True")
param.value = os.path.basename(origOutput.value)
alg.addParameter(param)
new_parameters[param.name()] = origOutput.value
alg.removeOutputFromName('output')

alg.processCommand()
alg.processCommand(new_parameters)

# Remove Config file:
removeConfigFile(alg)

# re-add configTxt
alg.addParameter(configTxt)
alg.setParameterValue('config', origConfigFile)
if outputTxt:
for param in [f for f in alg.parameters if f.name == 'output']:
alg.parameters.remove(param)
alg.addOutput(origOutput)


Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/ext/r_li_cwed.py
Expand Up @@ -34,4 +34,4 @@ def checkParameterValuesBeforeExecuting(alg):


def processCommand(alg, parameters):
configFile(alg)
configFile(alg, parameters)
Expand Up @@ -34,7 +34,7 @@ def checkParameterValuesBeforeExecuting(alg):


def processCommand(alg, parameters):
configFile(alg, True)
configFile(alg, parameters, True)


def processOutputs(alg):
Expand Down
Expand Up @@ -34,4 +34,4 @@ def checkParameterValuesBeforeExecuting(alg):


def processCommand(alg, parameters):
configFile(alg)
configFile(alg, parameters)
Expand Up @@ -34,7 +34,7 @@ def checkParameterValuesBeforeExecuting(alg):


def processCommand(alg, parameters):
configFile(alg, True)
configFile(alg, parameters, True)


def processOutputs(alg):
Expand Down
Expand Up @@ -34,4 +34,4 @@ def checkParameterValuesBeforeExecuting(alg):


def processCommand(alg, parameters):
configFile(alg)
configFile(alg, parameters)
Expand Up @@ -34,7 +34,7 @@ def checkParameterValuesBeforeExecuting(alg):


def processCommand(alg, parameters):
configFile(alg, True)
configFile(alg, parameters, True)


def processOutputs(alg):
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/ext/r_li_mpa.py
Expand Up @@ -34,4 +34,4 @@ def checkParameterValuesBeforeExecuting(alg):


def processCommand(alg, parameters):
configFile(alg)
configFile(alg, parameters)
Expand Up @@ -34,7 +34,7 @@ def checkParameterValuesBeforeExecuting(alg):


def processCommand(alg, parameters):
configFile(alg, True)
configFile(alg, parameters, True)


def processOutputs(alg):
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/ext/r_li_mps.py
Expand Up @@ -34,4 +34,4 @@ def checkParameterValuesBeforeExecuting(alg):


def processCommand(alg, parameters):
configFile(alg)
configFile(alg, parameters)
Expand Up @@ -34,7 +34,7 @@ def checkParameterValuesBeforeExecuting(alg):


def processCommand(alg, parameters):
configFile(alg, True)
configFile(alg, parameters, True)


def processOutputs(alg):
Expand Down
28 changes: 14 additions & 14 deletions python/plugins/processing/algs/grass7/ext/r_li_orig.py
Expand Up @@ -28,6 +28,7 @@
import shutil
from processing.tools.system import isWindows, userFolder, mkdir
from os import path
from copy import deepcopy


def rliPath():
Expand Down Expand Up @@ -73,26 +74,29 @@ def checkMovingWindow(alg):
return None


def configFile(alg, outputTxt=False):
""" Handle inline configuration """
def configFile(alg, parameters, outputTxt=False):
""" Handle inline configuration
:param parameters:
"""
new_parameters = deepcopy(parameters)

# Where is the GRASS7 user directory ?
userGrass7Path = rliPath()
mkdir(userGrass7Path)
origConfigFile = alg.getParameterValue('config')
origConfigFile = new_parameters['config']

# Handle inline configuration
configTxt = alg.getParameterFromName('config_txt')
if configTxt.value:
if new_parameters['config_txt']:
# Creates a temporary txt file in user r.li directory
tempConfig = alg.getTempFilename()
configFilePath = path.join(userGrass7Path, tempConfig)
# Inject rules into temporary txt file
with open(configFilePath, "w") as f:
f.write(configTxt.value)
f.write(new_parameters['config_txt'])

# Use temporary file as rules file
alg.setParameterValue('config', configFilePath)
alg.parameters.remove(configTxt)
new_parameters['config'] = configFilePath
del new_parameters['config_txt']

# If we have a configuration file, we need to copy it into user dir
if origConfigFile:
Expand All @@ -101,21 +105,17 @@ def configFile(alg, outputTxt=False):
shutil.copy(origConfigFile, configFilePath)

# Change the parameter value
alg.setParameterValue('config', configFilePath)
new_parameters['config'] = configFilePath

if outputTxt:
origOutput = alg.getOutputValue('output')
alg.setOutputValue('output', path.basename(origOutput))

alg.processCommand()
alg.processCommand(new_parameters)

# Remove Config file:
removeConfigFile(alg)

# re-add configTxt
alg.addParameter(configTxt)
alg.setParameterValue('config', origConfigFile)


def moveOutputTxtFile(alg):
# Find output file name:
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/ext/r_li_padcv.py
Expand Up @@ -34,4 +34,4 @@ def checkParameterValuesBeforeExecuting(alg):


def processCommand(alg, parameters):
configFile(alg)
configFile(alg, parameters)
Expand Up @@ -34,7 +34,7 @@ def checkParameterValuesBeforeExecuting(alg):


def processCommand(alg, parameters):
configFile(alg, True)
configFile(alg, parameters, True)


def processOutputs(alg):
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/ext/r_li_padrange.py
Expand Up @@ -34,4 +34,4 @@ def checkParameterValuesBeforeExecuting(alg):


def processCommand(alg, parameters):
configFile(alg)
configFile(alg, parameters)
Expand Up @@ -34,7 +34,7 @@ def checkParameterValuesBeforeExecuting(alg):


def processCommand(alg, parameters):
configFile(alg, True)
configFile(alg, parameters, True)


def processOutputs(alg):
Expand Down

0 comments on commit 0c3ad14

Please sign in to comment.