Fix MS-Windows implementation for r.li.* algorithms

This commit is contained in:
Médéric RIBREUX 2016-03-03 19:25:06 +01:00
parent 63eab874b4
commit 2275ce4166

View File

@ -28,24 +28,29 @@ __revision__ = '$Format:%H$'
import shutil
from processing.tools.system import isWindows, isMac, userFolder, mkdir
from processing.core.parameters import getParameterFromString
from os import path
import os
# for MS-Windows users who have MBCS chars in their name:
if os.name == 'nt':
import win32api
def rliPath():
"""Return r.li GRASS7 user dir"""
if isWindows():
return path.join(path.expanduser("~").decode('mcbs'), 'GRASS7', 'r.li')
homeDir = win32api.GetShortPathName(os.path.expanduser('~'))
return os.path.join(homeDir, 'AppData', 'Roaming', 'GRASS7', 'r.li')
else:
return path.join(path.expanduser("~"), '.grass7', 'r.li')
return os.path.join(os.path.expanduser("~"), '.grass7', 'r.li')
def removeConfigFile(alg):
""" Remove the r.li user dir config file """
configPath = alg.getParameterValue('config')
if isWindows():
command = "DEL {}".format(path.join(rliPath(), configPath))
command = "DEL {}".format(os.path.join(rliPath(), configPath))
else:
command = "rm {}".format(path.join(rliPath(), configPath))
command = "rm {}".format(os.path.join(rliPath(), configPath))
alg.commands.append(command)
@ -82,10 +87,10 @@ def configFile(alg, outputTxt=False):
""" Handle inline configuration """
# Where is the GRASS7 user directory ?
userGrass7Path = rliPath()
if not path.isdir(userGrass7Path):
if not os.path.isdir(userGrass7Path):
mkdir(userGrass7Path)
if not path.isdir(path.join(userGrass7Path, 'output')):
mkdir(path.join(userGrass7Path, 'output'))
if not os.path.isdir(os.path.join(userGrass7Path, 'output')):
mkdir(os.path.join(userGrass7Path, 'output'))
origConfigFile = alg.getParameterValue('config')
# Handle inline configuration
@ -93,28 +98,28 @@ def configFile(alg, outputTxt=False):
if configTxt.value:
# Creates a temporary txt file in user r.li directory
tempConfig = alg.getTempFilename()
configFilePath = path.join(userGrass7Path, tempConfig)
configFilePath = os.path.join(userGrass7Path, tempConfig)
# Inject rules into temporary txt file
with open(configFilePath, "w") as f:
f.write(configTxt.value)
# Use temporary file as rules file
alg.setParameterValue('config', path.basename(configFilePath))
alg.setParameterValue('config', os.path.basename(configFilePath))
alg.parameters.remove(configTxt)
# If we have a configuration file, we need to copy it into user dir
if origConfigFile:
configFilePath = path.join(userGrass7Path, path.basename(origConfigFile))
configFilePath = os.path.join(userGrass7Path, os.path.basename(origConfigFile))
# Copy the file
shutil.copy(origConfigFile, configFilePath)
# Change the parameter value
alg.setParameterValue('config', path.basename(configFilePath))
alg.setParameterValue('config', os.path.basename(configFilePath))
origOutput = alg.getOutputFromName('output')
if outputTxt:
param = getParameterFromString("ParameterString|output|txt output|None|False|True")
param.value = path.basename(origOutput.value)
param.value = os.path.basename(origOutput.value)
alg.addParameter(param)
alg.removeOutputFromName('output')
@ -137,12 +142,12 @@ def moveOutputTxtFile(alg):
origOutput = alg.getOutputValue('output')
userGrass7Path = rliPath()
outputDir = path.join(userGrass7Path, 'output')
output = path.join(outputDir, path.basename(origOutput))
outputDir = os.path.join(userGrass7Path, 'output')
output = os.path.join(outputDir, os.path.basename(origOutput))
# move the file
if isWindows():
command = "cp {} {}".format(output, origOutput)
command = "MOVE /Y {} {}".format(output, origOutput)
else:
command = "mv {} {}".format(output, origOutput)
command = "mv -f {} {}".format(output, origOutput)
alg.commands.append(command)