pktools  2.6.7
Processing Kernel for geospatial data
pktoolsUtils.py
1 # -*- coding: utf-8 -*-
2 
3 """
4 ***************************************************************************
5  pktoolsUtils.py
6  ---------------------
7  Date : April 2015
8  Copyright : (C) 2015 by Pieter Kempeneers
9  Email : kempenep 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__ = 'Pieter Kempeneers'
21 __date__ = 'April 2015'
22 __copyright__ = '(C) 2015, Pieter Kempeneers'
23 # This will get replaced with a git SHA1 when you do a git archive
24 __revision__ = '$Format:%H$'
25 
26 from PyQt4.QtCore import *
27 from PyQt4.QtGui import *
28 import os
29 import subprocess
30 from qgis.core import QgsApplication
31 from processing.core.ProcessingLog import ProcessingLog
32 from processing.core.ProcessingConfig import ProcessingConfig
33 from processing.tools.system import isWindows, isMac, userFolder
34 
35 class pktoolsUtils():
36 
37  PKTOOLS_FOLDER = "PKTOOLS_FOLDER"
38 
39  @staticmethod
40  def pktoolsPath():
41  folder = ProcessingConfig.getSetting(pktoolsUtils.PKTOOLS_FOLDER)
42 
43  if folder is None or folder == '':
44  if isWindows():
45  testfolder = os.path.join(os.path.dirname(QgsApplication.prefixPath()), 'pktools')
46  testfolder = os.path.join(testfolder, 'bin')
47  if os.path.exists(os.path.join(testfolder, 'pkinfo')):
48  folder = testfolder
49  folder = testfolder
50  else:
51  testfolder = "/usr/bin"
52  if os.path.exists(os.path.join(testfolder, "pkinfo")):
53  folder = testfolder
54  else:
55  testfolder = "/usr/local/bin"
56  if os.path.exists(os.path.join(testfolder, "pkinfo")):
57  folder = testfolder
58  folder = testfolder
59  return folder
60 
61  @staticmethod
62  def runpktools(commands, progress):
63  settings = QSettings()#from gdal
64  loglines = []
65  loglines.append("pktools execution console output")
66  loglines.append(commands)
67  progress.setInfo('pktools command:')
68  commandline = " ".join(commands)
69  progress.setCommand(commandline)
70  proc = subprocess.Popen(
71  commandline,
72  shell=True,
73  stdout=subprocess.PIPE,
74  stdin=open(os.devnull),
75  stderr=subprocess.STDOUT,
76  universal_newlines=True,
77  ).stdout
78  progress.setInfo('pktools command output:')
79 
80  for line in iter(proc.readline, ""):
81  progress.setConsoleInfo(line)
82  loglines.append(line)
83  ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
84 
85  ProcessingLog.addToLog(ProcessingLog.LOG_INFO, commandline)
86  pktoolsUtils.consoleOutput = loglines
87 
88 # @staticmethod
89 # def getConsoleOutput():
90 # return pktoolsUtils.consoleOutput