-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from HansBug/dev/system
dev(hansbug): add python and package version support
- Loading branch information
Showing
15 changed files
with
257 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,5 @@ hbutils.system | |
|
||
filesystem | ||
os | ||
python | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
hbutils.system.python | ||
======================================== | ||
|
||
.. currentmodule:: hbutils.system.python | ||
|
||
.. automodule:: hbutils.system.python | ||
|
||
|
||
python_version | ||
----------------------------------- | ||
|
||
.. autofunction:: python_version | ||
|
||
|
||
|
||
is_cpython | ||
----------------------------------- | ||
|
||
.. autofunction:: is_cpython | ||
|
||
|
||
|
||
is_ironpython | ||
----------------------------------- | ||
|
||
.. autofunction:: is_ironpython | ||
|
||
|
||
|
||
is_jython | ||
----------------------------------- | ||
|
||
.. autofunction:: is_jython | ||
|
||
|
||
|
||
is_pypy | ||
----------------------------------- | ||
|
||
.. autofunction:: is_pypy | ||
|
||
|
||
|
||
package_version | ||
----------------------------------- | ||
|
||
.. autofunction:: package_version | ||
|
||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .filesystem import * | ||
from .os import * | ||
from .python import * |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .implementation import * | ||
from .package import * | ||
from .version import * |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import platform | ||
from enum import IntEnum, unique | ||
from functools import lru_cache | ||
|
||
from ...model import int_enum_loads | ||
|
||
__all__ = [ | ||
'is_cpython', | ||
'is_ironpython', | ||
'is_jython', | ||
'is_pypy', | ||
] | ||
|
||
|
||
@int_enum_loads(name_preprocess=str.upper) | ||
@unique | ||
class PythonImplementation(IntEnum): | ||
CPYTHON = 1 | ||
IRONPYTHON = 2 | ||
JYTHON = 3 | ||
PYPY = 4 | ||
|
||
|
||
@lru_cache() | ||
def _get_python_implementation() -> PythonImplementation: | ||
return PythonImplementation.loads(platform.python_implementation()) | ||
|
||
|
||
def is_cpython() -> bool: | ||
""" | ||
Overview: | ||
Return ``True`` is current python is CPython, otherwise return ``False``. | ||
:return: Current python is CPython or not. | ||
""" | ||
return _get_python_implementation() == PythonImplementation.CPYTHON | ||
|
||
|
||
def is_ironpython() -> bool: | ||
""" | ||
Overview: | ||
Return ``True`` is current python is IronPython, otherwise return ``False``. | ||
:return: Current python is IronPython or not. | ||
""" | ||
return _get_python_implementation() == PythonImplementation.IRONPYTHON | ||
|
||
|
||
def is_jython() -> bool: | ||
""" | ||
Overview: | ||
Return ``True`` is current python is Jython, otherwise return ``False``. | ||
:return: Current python is Jython or not. | ||
""" | ||
return _get_python_implementation() == PythonImplementation.JYTHON | ||
|
||
|
||
def is_pypy() -> bool: | ||
""" | ||
Overview: | ||
Return ``True`` is current python is PyPy, otherwise return ``False``. | ||
:return: Current python is PyPy or not. | ||
""" | ||
return _get_python_implementation() == PythonImplementation.PYPY |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from typing import Dict, Optional | ||
|
||
from packaging.version import Version | ||
from pkg_resources import working_set, parse_version | ||
|
||
__all__ = [ | ||
'package_version', | ||
] | ||
|
||
|
||
def _get_packages() -> Dict[str, Version]: | ||
return { | ||
i.key.lower(): i.version | ||
for i in working_set | ||
} | ||
|
||
|
||
def package_version(name: str) -> Optional[Version]: | ||
""" | ||
Overview: | ||
Get version of package with given ``name``. | ||
:param name: Name of the package, case is not sensitive. | ||
:return: A :class:`packing.version.Version` object. If the package is not installed, return ``None``. | ||
Examples:: | ||
>>> from hbutils.system import package_version | ||
>>> | ||
>>> package_version('pip') | ||
<Version('21.3.1')> | ||
>>> package_version('setuptools') | ||
<Version('59.6.0')> | ||
>>> package_version('not_a_package') | ||
None | ||
""" | ||
_packages = _get_packages() | ||
_name = name.lower() | ||
if _name in _packages: | ||
return parse_version(_packages[name]) | ||
else: | ||
return None |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import platform | ||
|
||
from packaging.version import Version | ||
from pkg_resources import parse_version | ||
|
||
__all__ = [ | ||
'python_version', | ||
] | ||
|
||
|
||
def python_version() -> Version: | ||
""" | ||
Overview: | ||
Get version of python. | ||
:return: Version of python. | ||
Examples:: | ||
>>> from hbutils.system import python_version | ||
>>> | ||
>>> python_version() | ||
Version('3.8.1') # for example | ||
""" | ||
return parse_version(platform.python_version()) |
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from hbutils.system import is_cpython, is_pypy, is_jython, is_ironpython | ||
from ...testings import cpython_mark, pypy_mark | ||
|
||
|
||
class TestSystemPythonImplementation: | ||
@cpython_mark | ||
def test_is_cpython(self): | ||
assert is_cpython() | ||
assert not is_pypy() | ||
assert not is_jython() | ||
assert not is_ironpython() | ||
|
||
@pypy_mark | ||
def test_is_pypy(self): | ||
assert not is_cpython() | ||
assert is_pypy() | ||
assert not is_jython() | ||
assert not is_ironpython() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import pytest | ||
from pkg_resources import parse_version | ||
|
||
from hbutils.system import package_version | ||
from .test_version import _Version | ||
|
||
|
||
@pytest.mark.unittest | ||
class TestSystemPythonPackage: | ||
def test_package_version(self): | ||
assert package_version("pip") >= parse_version("19") | ||
assert isinstance(package_version("chardet"), _Version) | ||
assert package_version("chardet") >= parse_version("3.0.4") | ||
assert package_version("chardet") < parse_version("5") | ||
assert package_version("This_is_an_fxxking_name") is None |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import sys | ||
|
||
import pytest | ||
from pkg_resources import parse_version | ||
|
||
from hbutils.system import python_version | ||
|
||
_Version = type(parse_version("0.0.1")) | ||
|
||
|
||
@pytest.mark.unittest | ||
class TestSystemPythonVersion: | ||
def test_python_version(self): | ||
_actual_version = sys.version_info | ||
_get_version = python_version() | ||
assert isinstance(_get_version, _Version) | ||
assert _get_version.major == _actual_version.major | ||
assert _get_version.minor == _actual_version.minor | ||
assert _get_version.micro == _actual_version.micro |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from .platform import linux_mark, macos_mark, windows_mark | ||
from .platform import linux_mark, macos_mark, windows_mark, pypy_mark, cpython_mark |
This file contains 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