Skip to content

Commit 2275ce4

Browse files
author
Médéric RIBREUX
committedMar 3, 2016
Fix MS-Windows implementation for r.li.* algorithms
1 parent 63eab87 commit 2275ce4

File tree

1 file changed

+22
-17
lines changed
  • python/plugins/processing/algs/grass7/ext

1 file changed

+22
-17
lines changed
 

‎python/plugins/processing/algs/grass7/ext/r_li.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,29 @@
2828
import shutil
2929
from processing.tools.system import isWindows, isMac, userFolder, mkdir
3030
from processing.core.parameters import getParameterFromString
31-
from os import path
31+
import os
32+
33+
# for MS-Windows users who have MBCS chars in their name:
34+
if os.name == 'nt':
35+
import win32api
3236

3337

3438
def rliPath():
3539
"""Return r.li GRASS7 user dir"""
3640
if isWindows():
37-
return path.join(path.expanduser("~").decode('mcbs'), 'GRASS7', 'r.li')
41+
homeDir = win32api.GetShortPathName(os.path.expanduser('~'))
42+
return os.path.join(homeDir, 'AppData', 'Roaming', 'GRASS7', 'r.li')
3843
else:
39-
return path.join(path.expanduser("~"), '.grass7', 'r.li')
44+
return os.path.join(os.path.expanduser("~"), '.grass7', 'r.li')
4045

4146

4247
def removeConfigFile(alg):
4348
""" Remove the r.li user dir config file """
4449
configPath = alg.getParameterValue('config')
4550
if isWindows():
46-
command = "DEL {}".format(path.join(rliPath(), configPath))
51+
command = "DEL {}".format(os.path.join(rliPath(), configPath))
4752
else:
48-
command = "rm {}".format(path.join(rliPath(), configPath))
53+
command = "rm {}".format(os.path.join(rliPath(), configPath))
4954
alg.commands.append(command)
5055

5156

@@ -82,39 +87,39 @@ def configFile(alg, outputTxt=False):
8287
""" Handle inline configuration """
8388
# Where is the GRASS7 user directory ?
8489
userGrass7Path = rliPath()
85-
if not path.isdir(userGrass7Path):
90+
if not os.path.isdir(userGrass7Path):
8691
mkdir(userGrass7Path)
87-
if not path.isdir(path.join(userGrass7Path, 'output')):
88-
mkdir(path.join(userGrass7Path, 'output'))
92+
if not os.path.isdir(os.path.join(userGrass7Path, 'output')):
93+
mkdir(os.path.join(userGrass7Path, 'output'))
8994
origConfigFile = alg.getParameterValue('config')
9095

9196
# Handle inline configuration
9297
configTxt = alg.getParameterFromName('config_txt')
9398
if configTxt.value:
9499
# Creates a temporary txt file in user r.li directory
95100
tempConfig = alg.getTempFilename()
96-
configFilePath = path.join(userGrass7Path, tempConfig)
101+
configFilePath = os.path.join(userGrass7Path, tempConfig)
97102
# Inject rules into temporary txt file
98103
with open(configFilePath, "w") as f:
99104
f.write(configTxt.value)
100105

101106
# Use temporary file as rules file
102-
alg.setParameterValue('config', path.basename(configFilePath))
107+
alg.setParameterValue('config', os.path.basename(configFilePath))
103108
alg.parameters.remove(configTxt)
104109

105110
# If we have a configuration file, we need to copy it into user dir
106111
if origConfigFile:
107-
configFilePath = path.join(userGrass7Path, path.basename(origConfigFile))
112+
configFilePath = os.path.join(userGrass7Path, os.path.basename(origConfigFile))
108113
# Copy the file
109114
shutil.copy(origConfigFile, configFilePath)
110115

111116
# Change the parameter value
112-
alg.setParameterValue('config', path.basename(configFilePath))
117+
alg.setParameterValue('config', os.path.basename(configFilePath))
113118

114119
origOutput = alg.getOutputFromName('output')
115120
if outputTxt:
116121
param = getParameterFromString("ParameterString|output|txt output|None|False|True")
117-
param.value = path.basename(origOutput.value)
122+
param.value = os.path.basename(origOutput.value)
118123
alg.addParameter(param)
119124
alg.removeOutputFromName('output')
120125

@@ -137,12 +142,12 @@ def moveOutputTxtFile(alg):
137142
origOutput = alg.getOutputValue('output')
138143
userGrass7Path = rliPath()
139144

140-
outputDir = path.join(userGrass7Path, 'output')
141-
output = path.join(outputDir, path.basename(origOutput))
145+
outputDir = os.path.join(userGrass7Path, 'output')
146+
output = os.path.join(outputDir, os.path.basename(origOutput))
142147

143148
# move the file
144149
if isWindows():
145-
command = "cp {} {}".format(output, origOutput)
150+
command = "MOVE /Y {} {}".format(output, origOutput)
146151
else:
147-
command = "mv {} {}".format(output, origOutput)
152+
command = "mv -f {} {}".format(output, origOutput)
148153
alg.commands.append(command)

0 commit comments

Comments
 (0)
Please sign in to comment.