Skip to content

Commit

Permalink
Implement version parse caching (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmartin16 authored Oct 15, 2022
1 parent d50c48c commit e72ae25
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 28 deletions.
32 changes: 19 additions & 13 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
repos:
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
language_version: python3
- repo: https://github.com/PyCQA/flake8
rev: 4.0.1
hooks:
- id: flake8
additional_dependencies: [flake8-eradicate==1.2.0]

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.2.0
rev: v4.3.0
hooks:
- id: check-toml
- id: check-yaml
- id: check-case-conflict
- id: check-docstring-first
- id: end-of-file-fixer
- id: trailing-whitespace

- repo: https://github.com/PyCQA/isort
rev: 5.10.1
hooks:
- id: isort
additional_dependencies: [toml]

- repo: https://github.com/asottile/pyupgrade
rev: v2.32.1
rev: v2.38.4 # DO NOT UPGRADE THIS - v3 drops python 2 support
hooks:
- id: pyupgrade

- repo: https://github.com/myint/docformatter
rev: v1.4
rev: v1.5.0
hooks:
- id: docformatter
args: [--in-place, --pre-summary-newline]

- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
language_version: python3

- repo: https://github.com/PyCQA/flake8
rev: 5.0.4
hooks:
- id: flake8
additional_dependencies: [flake8-eradicate~=1.4]
14 changes: 14 additions & 0 deletions qbittorrentapi/_version_support.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
from pkg_resources import parse_version

try:
from functools import lru_cache
except ImportError:
from backports.functools_lru_cache import lru_cache


APP_VERSION_2_API_VERSION_MAP = {
"v4.1.0": "2.0",
"v4.1.1": "2.0.1",
Expand Down Expand Up @@ -40,6 +48,12 @@
MOST_RECENT_SUPPORTED_API_VERSION = "2.8.5"


@lru_cache(maxsize=None)
def v(version):
"""Caching version parser."""
return parse_version(version)


class Version(object):
"""
Allows introspection for whether this Client supports different versions of
Expand Down
4 changes: 4 additions & 0 deletions qbittorrentapi/_version_support.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ from typing import Dict
from typing import Set
from typing import Text

from pkg_resources.extern.packaging.version import Version as _Version

MOST_RECENT_SUPPORTED_APP_VERSION: Text
MOST_RECENT_SUPPORTED_API_VERSION: Text
APP_VERSION_2_API_VERSION_MAP: Dict

def v(version: Text) -> _Version: ...

class Version:
_supported_app_versions: Set = None
_supported_api_versions: Set = None
Expand Down
3 changes: 1 addition & 2 deletions qbittorrentapi/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
from json import loads
from logging import getLogger

from pkg_resources import parse_version as v

from qbittorrentapi._version_support import v
from qbittorrentapi.exceptions import APIError
from qbittorrentapi.exceptions import HTTP403Error

Expand Down
3 changes: 1 addition & 2 deletions qbittorrentapi/rss.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from json import dumps

from pkg_resources import parse_version as v

from qbittorrentapi._version_support import v
from qbittorrentapi.app import AppAPIMixIn
from qbittorrentapi.decorators import alias
from qbittorrentapi.decorators import aliased
Expand Down
3 changes: 1 addition & 2 deletions qbittorrentapi/torrents.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from os import path
from os import strerror as os_strerror

from pkg_resources import parse_version as v

try:
from collections.abc import Iterable
from collections.abc import Mapping
Expand All @@ -14,6 +12,7 @@

from six import text_type as six_text_type

from qbittorrentapi._version_support import v
from qbittorrentapi.app import AppAPIMixIn
from qbittorrentapi.decorators import alias
from qbittorrentapi.decorators import aliased
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

import pytest
import six
from pkg_resources import parse_version as v

from qbittorrentapi import APIConnectionError
from qbittorrentapi import Client
from qbittorrentapi._version_support import (
APP_VERSION_2_API_VERSION_MAP as api_version_map,
)
from qbittorrentapi._version_support import v

environ.setdefault("QBITTORRENTAPI_HOST", "localhost:8080")
environ.setdefault("QBITTORRENTAPI_USERNAME", "admin")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from pkg_resources import parse_version as v

from qbittorrentapi._attrdict import AttrDict
from qbittorrentapi._version_support import v
from tests.conftest import IS_QBT_DEV


Expand Down
2 changes: 1 addition & 1 deletion tests/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
from mock import MagicMock

import pytest
from pkg_resources import parse_version as v
from requests import Response

from qbittorrentapi import APINames
from qbittorrentapi import Client
from qbittorrentapi import exceptions
from qbittorrentapi._version_support import v
from qbittorrentapi.request import Request
from qbittorrentapi.torrents import TorrentDictionary
from qbittorrentapi.torrents import TorrentInfoList
Expand Down
2 changes: 1 addition & 1 deletion tests/test_rss.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from time import sleep

import pytest
from pkg_resources import parse_version as v

from qbittorrentapi._version_support import v
from qbittorrentapi.rss import RSSitemsDictionary
from tests.conftest import check
from tests.conftest import get_func
Expand Down
2 changes: 1 addition & 1 deletion tests/test_search.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from time import sleep

import pytest
from pkg_resources import parse_version as v

from qbittorrentapi import NotFound404Error
from qbittorrentapi._version_support import v
from qbittorrentapi.search import SearchJobDictionary
from qbittorrentapi.search import SearchResultsDictionary
from qbittorrentapi.search import SearchStatusesList
Expand Down
2 changes: 1 addition & 1 deletion tests/test_torrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from time import sleep

import pytest
from pkg_resources import parse_version as v

from qbittorrentapi import Conflict409Error
from qbittorrentapi import TorrentDictionary
Expand All @@ -12,6 +11,7 @@
from qbittorrentapi import TorrentStates
from qbittorrentapi import TrackersList
from qbittorrentapi import WebSeedsList
from qbittorrentapi._version_support import v
from tests.conftest import get_func
from tests.test_torrents import check
from tests.test_torrents import disable_queueing
Expand Down
3 changes: 1 addition & 2 deletions tests/test_torrents.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from sys import version_info
from time import sleep

from pkg_resources import parse_version as v

try:
from collections.abc import Iterable
except ImportError:
Expand All @@ -14,6 +12,7 @@
import pytest
import requests

from qbittorrentapi._version_support import v
from qbittorrentapi.exceptions import Conflict409Error
from qbittorrentapi.exceptions import Forbidden403Error
from qbittorrentapi.exceptions import InvalidRequest400Error
Expand Down
2 changes: 1 addition & 1 deletion tests/test_transfer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
from pkg_resources import parse_version as v

from qbittorrentapi._version_support import v
from qbittorrentapi.transfer import TransferInfoDictionary


Expand Down

0 comments on commit e72ae25

Please sign in to comment.