143 lines
5.2 KiB
Python
Raw Normal View History

2016-03-02 21:27:07 +01:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
r_li.py
-------
Date : February 2016
Copyright : (C) 2016 by Médéric Ribreux
Email : medspx at medspx dot fr
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
__author__ = 'Médéric Ribreux'
__date__ = 'February 2016'
__copyright__ = '(C) 2016, Médéric Ribreux'
import shutil
from qgis.core import QgsProcessingParameterString
from processing.tools.system import (isWindows, mkdir,
getTempFilename)
import os
# for MS-Windows users who have MBCS chars in their name:
if os.name == 'nt':
import win32api
2016-03-02 21:27:07 +01:00
def rliPath():
"""Return r.li GRASS7 user dir"""
if isWindows():
homeDir = win32api.GetShortPathName(os.path.expanduser('~'))
return os.path.join(homeDir, 'AppData', 'Roaming', 'GRASS7', 'r.li')
2016-03-02 21:27:07 +01:00
else:
return os.path.join(os.path.expanduser("~"), '.grass7', 'r.li')
2016-03-02 21:27:07 +01:00
2017-10-22 10:25:09 +02:00
def removeConfigFile(alg, parameters, context):
2016-03-02 21:27:07 +01:00
""" Remove the r.li user dir config file """
2017-10-22 10:25:09 +02:00
configPath = alg.parameterAsString(parameters, 'config', context)
2016-03-02 21:27:07 +01:00
if isWindows():
command = "DEL {}".format(os.path.join(rliPath(), configPath))
2016-03-02 21:27:07 +01:00
else:
command = "rm {}".format(os.path.join(rliPath(), configPath))
2016-03-02 21:27:07 +01:00
alg.commands.append(command)
2017-10-22 10:25:09 +02:00
def checkMovingWindow(alg, parameters, context, outputTxt=False):
2016-03-02 21:27:07 +01:00
""" Verify if we have the right parameters """
2017-10-22 10:25:09 +02:00
configTxt = alg.parameterAsString(parameters, 'config_txt', context)
config = alg.parameterAsString(parameters, 'config', context)
2016-03-02 21:27:07 +01:00
if configTxt and config:
return False, alg.tr("You need to set either inline configuration or a configuration file!")
2016-03-02 21:27:07 +01:00
# Verify that configuration is in moving window
movingWindow = False
if configTxt:
if 'MOVINGWINDOW' in configTxt:
movingWindow = True
# Read config file:
if config:
with open(config) as f:
for line in f:
if 'MOVINGWINDOW' in line:
movingWindow = True
if not movingWindow and not outputTxt:
return False, alg.tr('Your configuration needs to be a "moving window" configuration!')
2016-03-02 21:27:07 +01:00
if movingWindow and outputTxt:
return False, alg.tr('Your configuration needs to be a non "moving window" configuration!')
2016-03-02 21:27:07 +01:00
return True, None
2016-03-02 21:27:07 +01:00
def configFile(alg, parameters, context, feedback, outputTxt=False):
"""Handle inline configuration
2017-05-22 14:07:53 +10:00
:param parameters:
"""
2016-03-02 21:27:07 +01:00
# Where is the GRASS7 user directory ?
userGrass7Path = rliPath()
if not os.path.isdir(userGrass7Path):
2016-03-02 21:27:07 +01:00
mkdir(userGrass7Path)
if not os.path.isdir(os.path.join(userGrass7Path, 'output')):
mkdir(os.path.join(userGrass7Path, 'output'))
2016-03-02 21:27:07 +01:00
# If we have a configuration file, we need to copy it into user dir
if parameters['config']:
fileName = alg.parameterAsString(parameters, 'config', context)
configFilePath = os.path.join(userGrass7Path, os.path.basename(fileName))
# Copy the file
shutil.copy(parameters['config'], configFilePath)
# Change the parameter value
parameters['config'] = os.path.basename(configFilePath)
2016-03-02 21:27:07 +01:00
# Handle inline configuration
elif parameters['config_txt']:
2016-03-02 21:27:07 +01:00
# Creates a temporary txt file in user r.li directory
tempConfig = os.path.basename(getTempFilename())
configFilePath = os.path.join(userGrass7Path, tempConfig)
2016-03-02 21:27:07 +01:00
# Inject rules into temporary txt file
with open(configFilePath, "w") as f:
f.write(alg.parameterAsString(parameters, 'config_txt', context))
f.write("\n")
2016-03-02 21:27:07 +01:00
# Use temporary file as rules file
parameters['config'] = os.path.basename(configFilePath)
alg.removeParameter('config_txt')
2016-03-02 21:27:07 +01:00
# For ascii output, we need a virtual output
if outputTxt:
param = QgsProcessingParameterString(
'output', 'virtual output',
'a' + os.path.basename(getTempFilename()),
False, False)
alg.addParameter(param)
2016-03-02 21:27:07 +01:00
alg.processCommand(parameters, context, feedback, outputTxt)
2016-03-02 21:27:07 +01:00
# Remove Config file:
removeConfigFile(alg, parameters, context)
2016-03-02 21:27:07 +01:00
2017-10-22 10:25:09 +02:00
def moveOutputTxtFile(alg, parameters, context):
2016-03-02 21:27:07 +01:00
# Find output file name:
txtPath = alg.parameterAsString(parameters, 'output_txt', context)
2016-03-02 21:27:07 +01:00
userGrass7Path = rliPath()
output = os.path.join(userGrass7Path, 'output',
alg.parameterAsString(parameters, 'output', context))
2016-03-02 21:27:07 +01:00
# move the file
if isWindows():
command = "MOVE /Y {} {}".format(output, txtPath)
2016-03-02 21:27:07 +01:00
else:
command = "mv -f {} {}".format(output, txtPath)
2016-03-02 21:27:07 +01:00
alg.commands.append(command)