forked from qgis/QGIS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
89 lines (73 loc) · 2.93 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: utf-8 -*-
"""
***************************************************************************
__init__.py
---------------------
Date : January 2007
Copyright : (C) 2007 by Martin Dobias
Email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
__author__ = 'Martin Dobias'
__date__ = 'January 2007'
__copyright__ = '(C) 2007, Martin Dobias'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from builtins import zip
import os
import sys
def setupenv():
"""
Set the environment for Windows based on the .vars files from the
OSGeo4W package format.
"""
# If the prefix path is already set the we don't do any more path setup.
if os.getenv('QGIS_PREFIX_PATH'):
return
# Setup the paths based on the .vars file.
from pathlib import PurePath
path_split = PurePath(os.path.dirname(os.path.realpath(__file__))).parts
try:
appname = os.environ['QGIS_ENVNAME']
except KeyError:
appname = path_split[-3]
envfile = list(path_split[:-4])
envfile.append("bin")
envfile.append("{0}-bin.env".format(appname))
envfile = os.path.join(*envfile)
if not os.path.exists(envfile):
return
with open(envfile) as f:
for line in f:
linedata = line.split("=")
name = linedata[0]
data = linedata[1]
os.environ[name] = data
if os.name == 'nt':
# On windows we need to setup the paths before we can import
# any of the QGIS modules or else it will error.
setupenv()
from qgis.PyQt import QtCore
# monkey patching custom widgets in case we are running on a local install
# this should fix import errors such as "ModuleNotFoundError: No module named qgsfilewidget"
# ("from qgsfilewidget import QgsFileWidget")
# In a complete install, this is normally avoided and rather imports "qgis.gui"
# (thanks to uic/widget-plugins/qgis_customwidgets.py)
try:
import qgis.gui
widget_list = dir(qgis.gui)
# remove widgets that are not allowed as customwidgets (they need to be manually promoted)
skip_list = ['QgsScrollArea']
for widget in widget_list:
if widget.startswith('Qgs') and widget not in skip_list:
sys.modules[widget.lower()] = qgis.gui
except ImportError:
# gui might not be built
pass