Skip to content

Commit

Permalink
Use Version directly from packaging instead of pkg_resources (#256
Browse files Browse the repository at this point in the history
)
  • Loading branch information
rmartin16 authored Jun 29, 2023
1 parent aea0c52 commit 90d4108
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 34 deletions.
9 changes: 1 addition & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,7 @@ addopts = """
--doctest-modules
-v
"""
filterwarnings = [
"error",
# this'll need to be addressed eventually...
"ignore:pkg_resources is deprecated as an API:DeprecationWarning",
"ignore:Deprecated call to `pkg_resources.declare_namespace:DeprecationWarning",
# Work around https://github.com/pytest-dev/pytest/issues/10977 for Python 3.12
'ignore:(ast\.Str|ast\.NameConstant|ast\.Num|Attribute s) is deprecated and will be removed.*:DeprecationWarning:',
]
filterwarnings = ["error"]
markers = [
"skipif_before_api_version(api_version): skips test for current api version",
"skipif_after_api_version(api_version): skips test for current api version",
Expand Down
3 changes: 1 addition & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ install_requires =
requests >= 2.16.0
six
urllib3 >= 1.24.2
# required for pkg_resources.parse_version
setuptools
packaging

[options.package_data]
qbittorrentapi =
Expand Down
4 changes: 2 additions & 2 deletions src/qbittorrentapi/_version_support.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pkg_resources import parse_version
from packaging.version import Version as _Version

try:
from functools import lru_cache
Expand Down Expand Up @@ -59,7 +59,7 @@
@lru_cache(maxsize=None)
def v(version):
"""Caching version parser."""
return parse_version(version)
return _Version(version)


class Version(object):
Expand Down
2 changes: 1 addition & 1 deletion src/qbittorrentapi/_version_support.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ from typing import Optional
from typing import Set
from typing import Text

from pkg_resources.extern.packaging.version import Version as _Version # type: ignore
from packaging.version import Version as _Version # type: ignore

MOST_RECENT_SUPPORTED_APP_VERSION: Text
MOST_RECENT_SUPPORTED_API_VERSION: Text
Expand Down
8 changes: 5 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)
from qbittorrentapi._version_support import v
from tests.utils import CHECK_SLEEP
from tests.utils import add_torrent
from tests.utils import check
from tests.utils import get_func
from tests.utils import get_torrent
Expand Down Expand Up @@ -103,7 +104,6 @@ def client():
VERIFY_WEBUI_CERTIFICATE=False,
)
client.auth_log_in()
# update preferences
client.app.preferences = dict(
# enable RSS fetching
rss_processing_enabled=True,
Expand All @@ -112,8 +112,10 @@ def client():
web_ui_ban_duration=1,
)
client.func = staticmethod(partial(get_func, client))
client.torrents_add(urls=ORIG_TORRENT_URL, upload_limit=10, download_limit=10)
check(lambda: [t.hash for t in client.torrents_info()], ORIG_TORRENT_HASH, True)
try:
add_torrent(client, ORIG_TORRENT_URL, ORIG_TORRENT_HASH)
except Exception:
pytest.exit("failed to add orig_torrent during setup")
return client


Expand Down
42 changes: 25 additions & 17 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ def mkpath(*user_path):
return ""


def retry(retries=3):
"""Decorator to retry a function if there's an exception."""

def inner(f):
def wrapper(*args, **kwargs):
for retry_count in range(retries):
try:
return f(*args, **kwargs)
except Exception:
if retry_count >= (retries - 1):
raise

return wrapper

return inner


def get_torrent(client, torrent_hash):
"""Retrieve a torrent from qBittorrent."""
try:
Expand All @@ -65,6 +82,14 @@ def get_torrent(client, torrent_hash):
pytest.exit("Failed to find torrent for %s" % torrent_hash)


@retry(200)
def add_torrent(client, torrent_url, torrent_hash):
client.torrents_add(urls=torrent_url, upload_limit=10, download_limit=10)
if torrent_hash not in [t.hash for t in client.torrents_info()]:
sleep(0.1)
raise Exception("didn't find added torrent")


def check(check_func, value, reverse=False, negate=False, any=False, check_time=None):
"""
Compare the return value of an arbitrary function to expected value with retries.
Expand Down Expand Up @@ -142,20 +167,3 @@ def _do_check(_check_func_val, _v, _negate, _reverse):

if not success:
raise Exception("Test neither succeeded nor failed...")


def retry(retries=3):
"""Decorator to retry a function if there's an exception."""

def inner(f):
def wrapper(*args, **kwargs):
for retry_count in range(retries):
try:
return f(*args, **kwargs)
except Exception:
if retry_count >= (retries - 1):
raise

return wrapper

return inner
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ setenv =
commands_pre = !ci: -docker stop qbt-tox-testing
commands =
!ci: docker run {env:DOCKER_ARGS} {env:DOCKER_QBT_IMAGE_NAME}:{env:DOCKER_QBT_IMAGE_TAG}
python -m coverage run -m pytest {posargs:-vv --color=yes}
python -Xdev -m coverage run -m pytest {posargs:-vv --color=yes}
commands_post = !ci: docker stop qbt-tox-testing

[docs]
Expand Down

0 comments on commit 90d4108

Please sign in to comment.