Created
January 30, 2018 19:39
-
-
Save sivel/2c353cc16f64c6595fbf8a411a754a8e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright: 2018, Matt Martz <[email protected]> | |
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause ) | |
from __future__ import (absolute_import, division, print_function) | |
__metaclass__ = type | |
__all__ = ['Version', 'parse'] | |
try: | |
from packaging.version import LegacyVersion, Version as Pep440Version, parse | |
HAS_PACKAGING = True | |
except ImportError: | |
try: | |
from pip._vendor.packaging.version import LegacyVersion, Version as Pep440Version, parse | |
HAS_PACKAGING = True | |
except ImportError: | |
from distutils.version import LooseVersion, StrictVersion | |
HAS_PACKAGING = False | |
if not HAS_PACKAGING: | |
def parse(version): | |
try: | |
return StrictVersion(version) | |
except ValueError: | |
return LooseVersion(version) | |
class Version: | |
def __new__(cls, version, strict=False): | |
if HAS_PACKAGING: | |
if strict: | |
return Pep440Version(version) | |
else: | |
return LegacyVersion(version) | |
else: | |
if strict: | |
return StrictVersion(version) | |
else: | |
return LooseVersion(version) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment