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