1# -*- coding: utf-8 -*-

2

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"""

19

20__author__ = 'Martin Dobias'

21__date__ = 'January 2007'

22__copyright__ = '(C) 2007, Martin Dobias'

23

24 from builtins import zip

25 import os

26 import sys

27

28

29 def 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

37

38 # Setup the paths based on the .vars file.

39 from pathlib import PurePath

40

41 path_split = PurePath(os.path.dirname(os.path.realpath(__file__))).parts

42

43 try:

44 appname = os.environ['QGIS_ENVNAME']

45 except KeyError:

46 appname = path_split[-3]

47

48 envfile = list(path_split[:-4])

49 envfile.append("bin")

50 envfile.append("{0}-bin.env".format(appname))

51 envfile = os.path.join(*envfile)

52

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

54 return

55

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

62

63

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

68

69

70 from qgis.PyQt import QtCore

71

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)

77 try:

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

85 except ImportError:

86 # gui might not be built

87 pass