r_li.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. """
  2. ***************************************************************************
  3. r_li.py
  4. -------
  5. Date : February 2016
  6. Copyright : (C) 2016 by Médéric Ribreux
  7. Email : medspx at medspx dot fr
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Médéric Ribreux'
  18. __date__ = 'February 2016'
  19. __copyright__ = '(C) 2016, Médéric Ribreux'
  20. import shutil
  21. from qgis.core import QgsProcessingParameterString
  22. from processing.tools.system import (isWindows, mkdir,
  23. getTempFilename)
  24. import os
  25. # for MS-Windows users who have MBCS chars in their name:
  26. if os.name == 'nt':
  27. import win32api
  28. def rliPath():
  29. """Return r.li GRASS7 user dir"""
  30. if isWindows():
  31. homeDir = win32api.GetShortPathName(os.path.expanduser('~'))
  32. return os.path.join(homeDir, 'AppData', 'Roaming', 'GRASS7', 'r.li')
  33. else:
  34. return os.path.join(os.path.expanduser("~"), '.grass7', 'r.li')
  35. def removeConfigFile(alg, parameters, context):
  36. """ Remove the r.li user dir config file """
  37. configPath = alg.parameterAsString(parameters, 'config', context)
  38. if isWindows():
  39. command = "DEL {}".format(os.path.join(rliPath(), configPath))
  40. else:
  41. command = "rm {}".format(os.path.join(rliPath(), configPath))
  42. alg.commands.append(command)
  43. def checkMovingWindow(alg, parameters, context, outputTxt=False):
  44. """ Verify if we have the right parameters """
  45. configTxt = alg.parameterAsString(parameters, 'config_txt', context)
  46. config = alg.parameterAsString(parameters, 'config', context)
  47. if configTxt and config:
  48. return False, alg.tr("You need to set either inline configuration or a configuration file!")
  49. # Verify that configuration is in moving window
  50. movingWindow = False
  51. if configTxt:
  52. if 'MOVINGWINDOW' in configTxt:
  53. movingWindow = True
  54. # Read config file:
  55. if config:
  56. with open(config) as f:
  57. for line in f:
  58. if 'MOVINGWINDOW' in line:
  59. movingWindow = True
  60. if not movingWindow and not outputTxt:
  61. return False, alg.tr('Your configuration needs to be a "moving window" configuration!')
  62. if movingWindow and outputTxt:
  63. return False, alg.tr('Your configuration needs to be a non "moving window" configuration!')
  64. return True, None
  65. def configFile(alg, parameters, context, feedback, outputTxt=False):
  66. """Handle inline configuration
  67. :param parameters:
  68. """
  69. # Where is the GRASS7 user directory ?
  70. userGrass7Path = rliPath()
  71. if not os.path.isdir(userGrass7Path):
  72. mkdir(userGrass7Path)
  73. if not os.path.isdir(os.path.join(userGrass7Path, 'output')):
  74. mkdir(os.path.join(userGrass7Path, 'output'))
  75. # If we have a configuration file, we need to copy it into user dir
  76. if parameters['config']:
  77. fileName = alg.parameterAsString(parameters, 'config', context)
  78. configFilePath = os.path.join(userGrass7Path, os.path.basename(fileName))
  79. # Copy the file
  80. shutil.copy(parameters['config'], configFilePath)
  81. # Change the parameter value
  82. parameters['config'] = os.path.basename(configFilePath)
  83. # Handle inline configuration
  84. elif parameters['config_txt']:
  85. # Creates a temporary txt file in user r.li directory
  86. tempConfig = os.path.basename(getTempFilename(context=context))
  87. configFilePath = os.path.join(userGrass7Path, tempConfig)
  88. # Inject rules into temporary txt file
  89. with open(configFilePath, "w") as f:
  90. f.write(alg.parameterAsString(parameters, 'config_txt', context))
  91. f.write("\n")
  92. # Use temporary file as rules file
  93. parameters['config'] = os.path.basename(configFilePath)
  94. alg.removeParameter('config_txt')
  95. # For ascii output, we need a virtual output
  96. if outputTxt:
  97. param = QgsProcessingParameterString(
  98. 'output', 'virtual output',
  99. 'a' + os.path.basename(getTempFilename(context=context)),
  100. False, False)
  101. alg.addParameter(param)
  102. alg.processCommand(parameters, context, feedback, outputTxt)
  103. # Remove Config file:
  104. removeConfigFile(alg, parameters, context)
  105. def moveOutputTxtFile(alg, parameters, context):
  106. # Find output file name:
  107. txtPath = alg.parameterAsString(parameters, 'output_txt', context)
  108. userGrass7Path = rliPath()
  109. output = os.path.join(userGrass7Path, 'output',
  110. alg.parameterAsString(parameters, 'output', context))
  111. # move the file
  112. if isWindows():
  113. command = "MOVE /Y {} {}".format(output, txtPath)
  114. else:
  115. command = "mv -f {} {}".format(output, txtPath)
  116. alg.commands.append(command)