Coverage for enpt_enmapboxapp/enpt_algorithm.py: 92%
40 statements
« prev ^ index » next coverage.py v7.4.1, created at 2024-03-05 19:40 +0000
« prev ^ index » next coverage.py v7.4.1, created at 2024-03-05 19:40 +0000
1# -*- coding: utf-8 -*-
3# enpt_enmapboxapp, A QGIS EnMAPBox plugin providing a GUI for the EnMAP processing tools (EnPT)
4#
5# Copyright (C) 2018-2024 Daniel Scheffler (GFZ Potsdam, daniel.scheffler@gfz-potsdam.de)
6#
7# This software was developed within the context of the EnMAP project supported
8# by the DLR Space Administration with funds of the German Federal Ministry of
9# Economic Affairs and Energy (on the basis of a decision by the German Bundestag:
10# 50 EE 1529) and contributions from DLR, GFZ and OHB System AG.
11#
12# This program is free software: you can redistribute it and/or modify it under
13# the terms of the GNU Lesser General Public License as published by the Free
14# Software Foundation, either version 3 of the License, or (at your option) any
15# later version.
16#
17# This program is distributed in the hope that it will be useful, but WITHOUT
18# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
20# details.
21#
22# You should have received a copy of the GNU Lesser General Public License along
23# with this program. If not, see <https://www.gnu.org/licenses/>.
25"""This module provides the EnPTAlgorithm which is used in case EnPT is installed into QGIS Python environment."""
27import os
28from importlib.util import find_spec
29from importlib.metadata import version as get_version
31from qgis.core import \
32 (QgsProcessingContext,
33 QgsProcessingFeedback,
34 NULL
35 )
37from ._enpt_alg_base import _EnPTBaseAlgorithm
38from .version import check_minimal_enpt_version
41class EnPTAlgorithm(_EnPTBaseAlgorithm):
42 @staticmethod
43 def _prepare_enpt_environment() -> dict:
44 os.environ['PYTHONUNBUFFERED'] = '1'
45 os.environ['IS_ENPT_GUI_CALL'] = '1'
47 enpt_env = os.environ.copy()
48 enpt_env["PATH"] = ';'.join([i for i in enpt_env["PATH"].split(';') if 'OSGEO' not in i]) # actually not needed
49 if "PYTHONHOME" in enpt_env.keys():
50 del enpt_env["PYTHONHOME"]
51 if "PYTHONPATH" in enpt_env.keys():
52 del enpt_env["PYTHONPATH"]
54 # FIXME is this needed?
55 enpt_env['IPYTHONENABLE'] = 'True'
56 enpt_env['PROMPT'] = '$P$G'
57 enpt_env['PYTHONDONTWRITEBYTECODE'] = '1'
58 enpt_env['PYTHONIOENCODING'] = 'UTF-8'
59 enpt_env['TEAMCITY_VERSION'] = 'LOCAL'
60 enpt_env['O4W_QT_DOC'] = 'C:/OSGEO4~3/apps/Qt5/doc'
61 if 'SESSIONNAME' in enpt_env.keys():
62 del enpt_env['SESSIONNAME']
64 # import pprint
65 # s = pprint.pformat(enpt_env)
66 # with open('D:\\env.json', 'w') as fp:
67 # fp.write(s)
69 return enpt_env
71 def processAlgorithm(self, parameters: dict, context: QgsProcessingContext, feedback: QgsProcessingFeedback):
72 if not find_spec('enpt'):
73 raise ImportError("enpt", "EnPT must be installed into the QGIS Python environment "
74 "when calling 'EnPTAlgorithm'.")
76 # check if the minimal needed EnPT backend version is installed
77 # (only works if EnPT is installed in the same environment)
78 check_minimal_enpt_version(get_version('enpt'))
80 parameters = self._get_preprocessed_parameters(parameters)
82 # print parameters and console call to log
83 # for key in sorted(parameters):
84 # feedback.pushInfo('{} = {}'.format(key, repr(parameters[key])))
85 keyval_str = ' '.join(['--{} {}'.format(key, parameters[key])
86 for key in sorted(parameters)
87 if parameters[key] not in [None, NULL, 'NULL', '']])
88 print(parameters)
89 print(keyval_str + '\n\n')
90 feedback.pushInfo("\nCalling EnPT with the following command:\n"
91 "enpt %s\n\n" % keyval_str)
93 # prepare environment for subprocess
94 enpt_env = self._prepare_enpt_environment()
95 # path_enpt_runscript = self._locate_enpt_run_script()
97 # run EnPT in subprocess that activates the EnPT Conda environment
98 # feedback.pushDebugInfo('Using %s to start EnPT.' % path_enpt_runscript)
99 feedback.pushInfo("The log messages of the EnMAP processing tool are written to the *.log file "
100 "in the specified output folder.")
102 exitcode = self._run_cmd(f"enpt {keyval_str}",
103 qgis_feedback=feedback,
104 env=enpt_env)
106 return self._handle_results(parameters, feedback, exitcode)