Skip to content

Commit

Permalink
Fix MS-Windows implementation for r.li.* algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
Médéric RIBREUX committed Mar 3, 2016
1 parent 63eab87 commit 2275ce4
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions python/plugins/processing/algs/grass7/ext/r_li.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,29 @@
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)


Expand Down Expand Up @@ -82,39 +87,39 @@ 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
configTxt = alg.getParameterFromName('config_txt')
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')

Expand All @@ -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)

0 comments on commit 2275ce4

Please sign in to comment.