system.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. """
  2. ***************************************************************************
  3. py
  4. ---------------------
  5. Date : August 2012
  6. Copyright : (C) 2012 by Victor Olaya
  7. Email : volayaf at gmail dot com
  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__ = 'Victor Olaya'
  18. __date__ = 'August 2012'
  19. __copyright__ = '(C) 2012, Victor Olaya'
  20. from typing import Optional
  21. import os
  22. import time
  23. import sys
  24. import uuid
  25. import math
  26. from qgis.PyQt.QtCore import QDir
  27. from qgis.core import (QgsApplication,
  28. QgsProcessingUtils,
  29. QgsProcessingContext)
  30. numExported = 1
  31. def userFolder():
  32. userDir = os.path.join(QgsApplication.qgisSettingsDirPath(), 'processing')
  33. if not QDir(userDir).exists():
  34. QDir().mkpath(userDir)
  35. return str(QDir.toNativeSeparators(userDir))
  36. def defaultOutputFolder():
  37. folder = os.path.join(userFolder(), 'outputs')
  38. if not QDir(folder).exists():
  39. QDir().mkpath(folder)
  40. return str(QDir.toNativeSeparators(folder))
  41. def isWindows():
  42. return os.name == 'nt'
  43. def isMac():
  44. return sys.platform == 'darwin'
  45. def getTempFilename(ext=None, context: Optional[QgsProcessingContext] = None):
  46. tmpPath = QgsProcessingUtils.tempFolder(context)
  47. t = time.time()
  48. m = math.floor(t)
  49. uid = f'{m:8x}{int((t - m) * 1000000):05x}'
  50. if ext is None:
  51. filename = os.path.join(tmpPath, f'{uid}{getNumExportedLayers()}')
  52. else:
  53. filename = os.path.join(tmpPath, f'{uid}{getNumExportedLayers()}.{ext}')
  54. return filename
  55. def getNumExportedLayers():
  56. global numExported
  57. numExported += 1
  58. return numExported
  59. def mkdir(newdir):
  60. os.makedirs(newdir.strip('\n\r '), exist_ok=True)
  61. def tempHelpFolder():
  62. tmp = os.path.join(str(QDir.tempPath()), 'processing_help')
  63. if not QDir(tmp).exists():
  64. QDir().mkpath(tmp)
  65. return str(os.path.abspath(tmp))
  66. def escapeAndJoin(strList):
  67. """
  68. .. deprecated:: 3.0
  69. Do not use, will be removed in QGIS 4.0
  70. """
  71. from warnings import warn
  72. warn("processing.escapeAndJoin is deprecated and will be removed in QGIS 4.0", DeprecationWarning)
  73. joined = ''
  74. for s in strList:
  75. if s[0] != '-' and ' ' in s:
  76. escaped = '"' + s.replace('\\', '\\\\').replace('"', '\\"') \
  77. + '"'
  78. else:
  79. escaped = s
  80. joined += escaped + ' '
  81. return joined.strip()