1# -*- coding: utf-8 -*- 2 3""" 4*************************************************************************** 5 py 6 --------------------- 7 Date : August 2012 8 Copyright : (C) 2012 by Victor Olaya 9 Email : volayaf at gmail dot com 10*************************************************************************** 11* * 12* This program is free software; you can redistribute it and/or modify * 13* it under the terms of the GNU General Public License as published by * 14* the Free Software Foundation; either version 2 of the License, or * 15* (at your option) any later version. * 16* * 17*************************************************************************** 18""" 19 20__author__ = 'Victor Olaya' 21__date__ = 'August 2012' 22__copyright__ = '(C) 2012, Victor Olaya' 23 24import os 25import time 26import sys 27import uuid 28import math 29 30from qgis.PyQt.QtCore import QDir 31from qgis.core import (QgsApplication, 32 QgsProcessingUtils) 33 34numExported = 1 35 36 37def userFolder(): 38 userDir = os.path.join(QgsApplication.qgisSettingsDirPath(), 'processing') 39 if not QDir(userDir).exists(): 40 QDir().mkpath(userDir) 41 42 return str(QDir.toNativeSeparators(userDir)) 43 44 45def defaultOutputFolder(): 46 folder = os.path.join(userFolder(), 'outputs') 47 if not QDir(folder).exists(): 48 QDir().mkpath(folder) 49 50 return str(QDir.toNativeSeparators(folder)) 51 52 53def isWindows(): 54 return os.name == 'nt' 55 56 57def isMac(): 58 return sys.platform == 'darwin' 59 60 61def getTempFilename(ext=None): 62 tmpPath = QgsProcessingUtils.tempFolder() 63 t = time.time() 64 m = math.floor(t) 65 uid = '{:8x}{:05x}'.format(m, int((t - m) * 1000000)) 66 if ext is None: 67 filename = os.path.join(tmpPath, '{}{}'.format(uid, getNumExportedLayers())) 68 else: 69 filename = os.path.join(tmpPath, '{}{}.{}'.format(uid, getNumExportedLayers(), ext)) 70 return filename 71 72 73def getTempDirInTempFolder(): 74 """Returns a temporary directory, putting it into a temp folder. 75 """ 76 77 path = QgsProcessingUtils.tempFolder() 78 path = os.path.join(path, uuid.uuid4().hex) 79 mkdir(path) 80 return path 81 82 83def removeInvalidChars(string): 84 validChars = \ 85 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:.' 86 string = ''.join(c for c in string if c in validChars) 87 return string 88 89 90def getNumExportedLayers(): 91 global numExported 92 numExported += 1 93 return numExported 94 95 96def mkdir(newdir): 97 newdir = newdir.strip('\n\r ') 98 if os.path.isdir(newdir): 99 pass 100 else: 101 (head, tail) = os.path.split(newdir) 102 if head and not os.path.isdir(head): 103 mkdir(head) 104 if tail: 105 os.mkdir(newdir) 106 107 108def tempHelpFolder(): 109 tmp = os.path.join(str(QDir.tempPath()), 'processing_help') 110 if not QDir(tmp).exists(): 111 QDir().mkpath(tmp) 112 113 return str(os.path.abspath(tmp)) 114 115 116def escapeAndJoin(strList): 117 """ 118 .. deprecated:: 3.0 119 Do not use, will be removed in QGIS 4.0 120 """ 121 122 from warnings import warn 123 warn("processing.escapeAndJoin is deprecated and will be removed in QGIS 4.0", DeprecationWarning) 124 125 joined = '' 126 for s in strList: 127 if s[0] != '-' and ' ' in s: 128 escaped = '"' + s.replace('\\', '\\\\').replace('"', '\\"') \ 129 + '"' 130 else: 131 escaped = s 132 joined += escaped + ' ' 133 return joined.strip() 134