Skip to content

Commit

Permalink
remove packaging package dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
mjurbanski-reef committed Mar 23, 2024
1 parent ea8626b commit ab7a2f9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
39 changes: 34 additions & 5 deletions b2sdk/version_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,44 @@
from __future__ import annotations

import inspect
import re
import warnings
from abc import ABCMeta, abstractmethod
from functools import wraps

from packaging.version import parse
from functools import total_ordering, wraps

from b2sdk.version import VERSION


@total_ordering
class _Version:
"""
Rudimentary semver version parser.
It uses VERY naive parsing which is only supposed to produce a tuple, able to
compare major.minor.patch versions.
It does not support PEP 440 epoch, pre-releases, post-releases, local versions, etc.
"""

def __init__(self, version: str):
self._raw = version
self._parsed = self._parse_version(version)

def __str__(self):
return self._raw

def __eq__(self, other):
return self._parsed == other._parsed

def __lt__(self, other):
return self._parsed < other._parsed

@classmethod
def _parse_version(cls, version: str) -> tuple[int, ...]:
if "!" in version: # strip PEP 440 epoch
version = version.split("!", 1)[1]
return tuple(map(int, re.findall(r'\d+', version)))


class AbstractVersionDecorator(metaclass=ABCMeta):
WHAT = NotImplemented # 'function', 'method', 'class' etc

Expand All @@ -28,7 +57,7 @@ def __init__(self, changed_version, cutoff_version=None, reason='', current_vers
"""
if current_version is None: # this is for tests only
current_version = VERSION # TODO autodetect by going up the qualname tree and trying getattr(part, '__version__')
self.current_version = parse(current_version) #: current version
self.current_version = _Version(current_version) #: current version
self.reason = reason

self.changed_version = self._parse_if_not_none(
Expand All @@ -42,7 +71,7 @@ def __init__(self, changed_version, cutoff_version=None, reason='', current_vers
def _parse_if_not_none(cls, version):
if version is None:
return None
return parse(version)
return _Version(version)

@abstractmethod
def __call__(self, func):
Expand Down
1 change: 1 addition & 0 deletions changelog.d/+remove_packaging_dep.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove unnecessary `packaging` package dependency.
6 changes: 3 additions & 3 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ dependencies = [
"requests<3.0.0,>=2.9.1",
"tqdm<5.0.0,>=4.5.0",
"typing-extensions>=4.7.1; python_version < '3.12'",
"packaging>=21.0",
]
requires-python = ">=3.7"
readme = "README.md"
Expand Down

0 comments on commit ab7a2f9

Please sign in to comment.