|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Utilities for handling dependencies and version changes. |
| 4 | +""" |
| 5 | +from numbers import Real |
| 6 | + |
| 7 | +# Matplotlib version |
| 8 | +import matplotlib |
| 9 | + |
| 10 | +from . import warnings |
| 11 | + |
| 12 | + |
| 13 | +class _version(list): |
| 14 | + """ |
| 15 | + Casual parser for ``major.minor`` style version strings. We do not want to |
| 16 | + add a 'packaging' dependency and only care about major and minor tags. |
| 17 | + """ |
| 18 | + def __str__(self): |
| 19 | + return self._version |
| 20 | + |
| 21 | + def __repr__(self): |
| 22 | + return f'version({self._version})' |
| 23 | + |
| 24 | + def __init__(self, version): |
| 25 | + try: |
| 26 | + if isinstance(version, Real): |
| 27 | + version = str(version) |
| 28 | + if not isinstance(version, str): |
| 29 | + version = '.'.join(version) |
| 30 | + major, minor, *_ = version.split('.') |
| 31 | + major, minor = int(major or 0), int(minor or 0) |
| 32 | + except Exception: |
| 33 | + warnings._warn_proplot( |
| 34 | + f"Unexpected version {version!r}. Interpreting as '0.0'." |
| 35 | + ) |
| 36 | + major = minor = 0 |
| 37 | + self._version = version |
| 38 | + super().__init__([major, minor]) # then use builtin python list sorting |
| 39 | + |
| 40 | + def __eq__(self, other): |
| 41 | + return super().__eq__(_version(other)) |
| 42 | + |
| 43 | + def __ne__(self, other): |
| 44 | + return super().__ne__(_version(other)) |
| 45 | + |
| 46 | + def __gt__(self, other): |
| 47 | + return super().__gt__(_version(other)) |
| 48 | + |
| 49 | + def __lt__(self, other): |
| 50 | + return super().__lt__(_version(other)) |
| 51 | + |
| 52 | + def __ge__(self, other): |
| 53 | + return super().__ge__(_version(other)) |
| 54 | + |
| 55 | + def __le__(self, other): |
| 56 | + return super().__le__(_version(other)) |
| 57 | + |
| 58 | + |
| 59 | +_version_mpl = _version(matplotlib.__version__) |
| 60 | + |
| 61 | +# Cartopy version |
| 62 | +try: |
| 63 | + import cartopy |
| 64 | +except ImportError: |
| 65 | + _version_cartopy = _version('0.0') |
| 66 | +else: |
| 67 | + _version_cartopy = _version(cartopy.__version__) |
0 commit comments