File: __init__.py

package info (click to toggle)
qgis 3.40.10%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,183,672 kB
  • sloc: cpp: 1,595,771; python: 372,544; xml: 23,474; sh: 3,761; perl: 3,664; ansic: 2,257; sql: 2,137; yacc: 1,068; lex: 577; javascript: 540; lisp: 411; makefile: 161
file content (95 lines) | stat: -rw-r--r-- 3,230 bytes parent folder | download | duplicates (6)
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
90
91
92
93
94
95
"""
***************************************************************************
    __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"

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 then 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(f"{appname}-bin.env")
    envfile = os.path.join(*envfile)

    if not os.path.exists(envfile):
        return

    with open(envfile) as f:
        for line in f:
            line = line.rstrip("\n")
            if line.startswith("#") or not line:
                continue
            try:
                env_key, env_value = line.split("=", maxsplit=1)
                os.environ[env_key] = env_value
            except ValueError:
                pass


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()

    if sys.version_info[0] > 3 or (
        sys.version_info[0] == 3 and sys.version_info[1] >= 9
    ):
        for p in os.getenv("PATH").split(";"):
            if os.path.exists(p):
                os.add_dll_directory(p)

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 custom widgets (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