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