Coverage for C:\Program Files\QGIS 3.10\apps\qgis-ltr\python\qgis\__init__.py: 90%
40 statements
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-10 14:40 +0900
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-10 14:40 +0900
1# -*- coding: utf-8 -*-
3"""
4***************************************************************************
5 __init__.py
6 ---------------------
7 Date : January 2007
8 Copyright : (C) 2007 by Martin Dobias
9 Email : wonder dot sk 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"""
20__author__ = 'Martin Dobias'
21__date__ = 'January 2007'
22__copyright__ = '(C) 2007, Martin Dobias'
24from builtins import zip
25import os
26import sys
29def setupenv():
30 """
31 Set the environment for Windows based on the .vars files from the
32 OSGeo4W package format.
33 """
34 # If the prefix path is already set the we don't do any more path setup.
35 if os.getenv('QGIS_PREFIX_PATH'):
36 return
38 # Setup the paths based on the .vars file.
39 from pathlib import PurePath
41 path_split = PurePath(os.path.dirname(os.path.realpath(__file__))).parts
43 try:
44 appname = os.environ['QGIS_ENVNAME']
45 except KeyError:
46 appname = path_split[-3]
48 envfile = list(path_split[:-4])
49 envfile.append("bin")
50 envfile.append("{0}-bin.env".format(appname))
51 envfile = os.path.join(*envfile)
53 if not os.path.exists(envfile):
54 return
56 with open(envfile) as f:
57 for line in f:
58 linedata = line.split("=")
59 name = linedata[0]
60 data = linedata[1]
61 os.environ[name] = data
64if os.name == 'nt':
65 # On windows we need to setup the paths before we can import
66 # any of the QGIS modules or else it will error.
67 setupenv()
70from qgis.PyQt import QtCore
72# monkey patching custom widgets in case we are running on a local install
73# this should fix import errors such as "ModuleNotFoundError: No module named qgsfilewidget"
74# ("from qgsfilewidget import QgsFileWidget")
75# In a complete install, this is normally avoided and rather imports "qgis.gui"
76# (thanks to uic/widget-plugins/qgis_customwidgets.py)
77try:
78 import qgis.gui
79 widget_list = dir(qgis.gui)
80 # remove widgets that are not allowed as customwidgets (they need to be manually promoted)
81 skip_list = ['QgsScrollArea']
82 for widget in widget_list:
83 if widget.startswith('Qgs') and widget not in skip_list:
84 sys.modules[widget.lower()] = qgis.gui
85except ImportError:
86 # gui might not be built
87 pass