Package osgeo |
[hide private]
[frames] | no frames]

# Source Code for Package osgeo

      1  # __init__ for osgeo package.
      2
      3  # making the osgeo package version the same as the gdal version:
      4  from sys import platform, version_info
      5  if version_info >= (3, 8, 0) and platform == 'win32':
      6      import os
      7      if 'USE_PATH_FOR_GDAL_PYTHON' in os.environ and 'PATH' in os.environ:
      8          for p in os.environ['PATH'].split(';'):
      9              if p:
     10                  try:
     11                      os.add_dll_directory(p)
     12                  except (FileNotFoundError, OSError):
     13                      continue
     14      elif 'PATH' in os.environ:
     15          import glob
     16          for p in os.environ['PATH'].split(';'):
     17              if glob.glob(os.path.join(p, 'gdal*.dll')) or glob.glob(os.path.join(p, 'libgdal*.dll')):
     18                  try:
     19                      os.add_dll_directory(p)
     20                  except (FileNotFoundError, OSError):
     21                      continue
     22
     23
     24 -def swig_import_helper():
     25      import importlib
     26      from os.path import dirname, basename
     27      mname = basename(dirname(__file__)) + '._gdal'
     28      try:
     29          return importlib.import_module(mname)
     30      except ImportError:
     31          if version_info >= (3, 8, 0) and platform == 'win32':
     32              import os
     33              if not 'USE_PATH_FOR_GDAL_PYTHON' in os.environ:
     34                  msg = 'On Windows, with Python >= 3.8, DLLs are no longer imported from the PATH.\n'
     35                  msg += 'If gdalXXX.dll is in the PATH, then set the USE_PATH_FOR_GDAL_PYTHON=YES environment variable\n'
     36                  msg += 'to feed the PATH into os.add_dll_directory().'
     37
     38                  import sys
     39                  import traceback
     40                  traceback_string = ''.join(traceback.format_exception(*sys.exc_info()))
     41                  raise ImportError(traceback_string + '\n' + msg)
     42          return importlib.import_module('_gdal')
     43
     44
     45  _gdal = swig_import_helper()
     46  del swig_import_helper
     47
     48  __version__ = _gdal.__version__ = _gdal.VersionInfo("RELEASE_NAME")
     49
     50  gdal_version = tuple(int(s) for s in str(__version__).split('.') if s.isdigit())[:3]
     51  python_version = tuple(version_info)[:3]
     52
     53  # Setting this flag to True will cause importing osgeo to fail on an unsupported Python version.
     54  # Otherwise a deprecation warning will be issued instead.
     55  # Importing osgeo fom an unsupported Python version might still partially work
     56  # because the core of GDAL Python bindings might still support an older Python version.
     57  # Hence the default option to just issue a warning.
     58  # To get complete functionality upgrading to the minimum supported version is needed.
     59  fail_on_unsupported_version = False
     60
     61  # The following is a Sequence of tuples in the form of (gdal_version, python_version).
     62  # Each line represents the minimum supported Python version of a given GDAL version.
     63  # Introducing a new line for the next GDAL version will trigger a deprecation warning
     64  # when importing osgeo from a Python version which will not be
     65  # supported in the next version of GDAL.
     66  gdal_version_and_min_supported_python_version = (
     67      ((0, 0), (0, 0)),
     68      ((1, 0), (2, 0)),
     69      ((2, 0), (2, 7)),
     70      ((3, 3), (3, 6)),
     71      # ((3, 4), (3, 7)),
     72      # ((3, 5), (3, 8)),
     73  )
     74
     75
     76 -def ver_str(ver):
     77      return '.'.join(str(v) for v in ver) if ver is not None else None
     78
     79
     80  minimum_supported_python_version_for_this_gdal_version = None
     81  this_python_version_will_be_deprecated_in_gdal_version = None
     82  last_gdal_version_to_supported_your_python_version = None
     83  next_version_of_gdal_will_use_python_version = None
     84  for gdal_ver, py_ver in gdal_version_and_min_supported_python_version:
     85      if gdal_version >= gdal_ver:
     86          minimum_supported_python_version_for_this_gdal_version = py_ver
     87      if python_version >= py_ver:
     88          last_gdal_version_to_supported_your_python_version = gdal_ver
     89      if not this_python_version_will_be_deprecated_in_gdal_version:
     90          if python_version < py_ver:
     91              this_python_version_will_be_deprecated_in_gdal_version = gdal_ver
     92              next_version_of_gdal_will_use_python_version = py_ver
     93
     94
     95  if python_version < minimum_supported_python_version_for_this_gdal_version:
     96      msg = 'Your Python version is {}, which is no longer supported by GDAL {}. ' \
     97            'Please upgrade your Python version to Python >= {}, ' \
     98            'or use GDAL <= {}, which supports your Python version.'.\
     99          format(ver_str(python_version), ver_str(gdal_version),
    100                 ver_str(minimum_supported_python_version_for_this_gdal_version),
    101                 ver_str(last_gdal_version_to_supported_your_python_version))
    102
    103      if fail_on_unsupported_version:
    104          raise Exception(msg)
    105      else:
    106          from warnings import warn, simplefilter
    107          simplefilter('always', DeprecationWarning)
    108          warn(msg, DeprecationWarning)
    109  elif this_python_version_will_be_deprecated_in_gdal_version:
    110      msg = 'You are using Python {} with GDAL {}. ' \
    111            'This Python version will be deprecated in GDAL {}. ' \
    112            'Please consider upgrading your Python version to Python >= {}, ' \
    113            'Which will be the minimum supported Python version of GDAL {}.'.\
    114          format(ver_str(python_version), ver_str(gdal_version),
    115                 ver_str(this_python_version_will_be_deprecated_in_gdal_version),
    116                 ver_str(next_version_of_gdal_will_use_python_version),
    117                 ver_str(this_python_version_will_be_deprecated_in_gdal_version))
    118
    119      from warnings import warn, simplefilter
    120      simplefilter('always', DeprecationWarning)
    121      warn(msg, DeprecationWarning)
    122