Skip to content

Commit

Permalink
Misc fixes and optimizations (#95)
Browse files Browse the repository at this point in the history
- Reorder class hierarchy to allow independent MixIn use
- Clean up typing annotations
- Optimize Dictionary and List initializations
- Rename Alias decorator to alias for better conformity
  • Loading branch information
rmartin16 authored Jul 27, 2022
1 parent ff74d62 commit 4af3e5c
Show file tree
Hide file tree
Showing 27 changed files with 278 additions and 270 deletions.
2 changes: 1 addition & 1 deletion .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Ran code through black
0f16b9939e88d39f2731f1e010f081c931227a55
# Implemented pre-commit checks
33e08a7f5a2ab9e6bbc678f19c96e7500a80bdf9
e97d6079e4ab03df0d4d9f40f653a3e13d22aa56
22 changes: 9 additions & 13 deletions .github/workflows/tests-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ env:
QBT_LEGACY_VERS: '[ "4.2.5", "4.2.0" ]'
QBT_ALWAYS_TEST: 4.4.1, 4.4.0, 4.3.9, 4.3.8, 4.3.6, 4.3.4.1, 4.3.3, 4.3.2, 4.2.5, 4.2.0
QT_USE_DEFAULT_PAA: 4.3.9, 4.3.8, 4.3.7, 4.3.6, 4.3.5, 4.3.4.1, 4.3.3, 4.3.2, 4.3.1, 4.3.0.1
LIBTORRENT_VER: 2.0.6
LIBTORRENT_VER: 2.0.7
COMPREHENSIVE_TESTS_BRANCH: comprehensive_tests
PYTHON_QBITTORRENTAPI_HOST: localhost:8080
PYTHON_QBITTORRENTAPI_PASSWORD: adminadmin
Expand Down Expand Up @@ -160,8 +160,7 @@ jobs:
# install the python dependencies to test qbittorrent-api
run: |
python -m pip install -U pip wheel setuptools
pip install -U codecov coveralls pytest pytest-cov
pip install .
python -m pip install .[test]
- name: Test with pytest
# finally....start qBittorrent and run tests via pytest
Expand Down Expand Up @@ -306,8 +305,7 @@ jobs:
if: (contains(github.ref, env.COMPREHENSIVE_TESTS_BRANCH) || contains(env.QBT_ALWAYS_TEST, matrix.QBT_VER))
run: |
python -m pip install -U pip wheel setuptools
pip install -U codecov coveralls pytest pytest-cov
pip install .
python -m pip install .[test]
- name: Test with pytest
# finally....start qBittorrent and run tests via pytest
Expand Down Expand Up @@ -335,11 +333,11 @@ jobs:
# Run tests against old qBittorrent release and all Python releases
#######
name: "Release Legacy Test ${{ matrix.python-version }} - v${{ matrix.QBT_VER }}"
needs: [tests-extended, verify]
needs: [tests-smoke, verify]
runs-on: ubuntu-20.04
continue-on-error: true
env:
LIBTORRENT_VER: 1.2.16
LIBTORRENT_VER: 1.2.17
strategy:
matrix:
QBT_VER: ${{ fromJson(needs.verify.outputs.qbittorrent-legacy-versions) }}
Expand Down Expand Up @@ -427,8 +425,7 @@ jobs:
# install the python dependencies to test qbittorrent-api
run: |
python -m pip install -U pip wheel setuptools
pip install -U codecov coveralls pytest pytest-cov
pip install .
python -m pip install .[test]
- name: Test with pytest
# finally....start qBittorrent and run tests via pytest
Expand Down Expand Up @@ -541,8 +538,7 @@ jobs:
# install the python dependencies to test qbittorrent-api
run: |
python -m pip install -U pip wheel setuptools
pip install -U codecov coveralls pytest pytest-cov
pip install .
python -m pip install .[test]
- name: Test with pytest
run: |
Expand Down Expand Up @@ -608,7 +604,7 @@ jobs:
# Verify package can be installed on all platforms
#######
name: Verify Dev Env
needs: package
needs: tests-smoke
runs-on: ${{ matrix.os }}
strategy:
matrix:
Expand All @@ -623,7 +619,7 @@ jobs:
python-version: ${{ env.LATEST_PYTHON_VER }}

- name: Install in Dev Mode
run: python -m pip install -e .[dev]
run: python -m pip install -e .

- name: Import Package
run: python -c 'import qbittorrentapi; print(qbittorrentapi.Client())'
6 changes: 6 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Version 2022.7.33 (27 jul 2022)
- Reorder class hierarchy to allow independent MixIn use
- Clean up typing annotations
- Optimize Dictionary and List initializations
- Rename Alias decorator to alias for better conformity

Version 2022.5.32 (30 may 2022)
- Implement pre-commit checks
- Advertise support for qBittorrent 4.4.3.1
Expand Down
4 changes: 2 additions & 2 deletions qbittorrentapi/_version_support.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ MOST_RECENT_SUPPORTED_API_VERSION: Text
APP_VERSION_2_API_VERSION_MAP: Dict

class Version:
_supported_app_versions: set = None
_supported_api_versions: set = None
_supported_app_versions: Set = None
_supported_api_versions: Set = None
@classmethod
def supported_app_versions(cls) -> Set[str]: ...
@classmethod
Expand Down
32 changes: 16 additions & 16 deletions qbittorrentapi/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from json import dumps
from logging import getLogger

from qbittorrentapi.decorators import Alias
from qbittorrentapi.auth import AuthAPIMixIn
from qbittorrentapi.decorators import alias
from qbittorrentapi.decorators import aliased
from qbittorrentapi.decorators import endpoint_introduced
from qbittorrentapi.decorators import login_required
Expand All @@ -10,7 +11,6 @@
from qbittorrentapi.definitions import APINames
from qbittorrentapi.definitions import ClientCache
from qbittorrentapi.definitions import Dictionary
from qbittorrentapi.request import Request

logger = getLogger(__name__)

Expand Down Expand Up @@ -81,21 +81,21 @@ def preferences(self, value):
"""Implements :meth:`~AppAPIMixIn.app_set_preferences`"""
self.set_preferences(prefs=value)

@Alias("setPreferences")
@alias("setPreferences")
def set_preferences(self, prefs=None, **kwargs):
"""Implements :meth:`~AppAPIMixIn.app_set_preferences`"""
return self._client.app_set_preferences(prefs=prefs, **kwargs)

@property
def default_save_path(self, **kwargs):
def default_save_path(self):
"""Implements :meth:`~AppAPIMixIn.app_default_save_path`"""
return self._client.app_default_save_path(**kwargs)
return self._client.app_default_save_path()

defaultSavePath = default_save_path


@aliased
class AppAPIMixIn(Request):
class AppAPIMixIn(AuthAPIMixIn):
"""
Implementation of all Application API methods.
Expand All @@ -109,8 +109,8 @@ class AppAPIMixIn(Request):
@property
def app(self):
"""
Allows for transparent interaction with Application endpoints. (alias:
app)
Allows for transparent interaction with Application endpoints.
(alias: app)
See Application class for usage.
:return: Application object
Expand All @@ -131,7 +131,7 @@ def app_version(self, **kwargs):
"""
return self._get(_name=APINames.Application, _method="version", **kwargs)

@Alias("app_webapiVersion")
@alias("app_webapiVersion")
@response_text(str)
@login_required
def app_web_api_version(self, **kwargs):
Expand All @@ -146,7 +146,7 @@ def app_web_api_version(self, **kwargs):

@endpoint_introduced("2.3", "app/buildInfo")
@response_json(BuildInfoDictionary)
@Alias("app_buildInfo")
@alias("app_buildInfo")
@login_required
def app_build_info(self, **kwargs):
"""
Expand All @@ -171,12 +171,12 @@ def app_preferences(self, **kwargs):
""" # noqa: E501
return self._get(_name=APINames.Application, _method="preferences", **kwargs)

@Alias("app_setPreferences")
@alias("app_setPreferences")
@login_required
def app_set_preferences(self, prefs=None, **kwargs):
"""
Set one or more preferences in qBittorrent application. (alias:
app_setPreferences)
Set one or more preferences in qBittorrent application.
(alias: app_setPreferences)
:param prefs: dictionary of preferences to set
:return: None
Expand All @@ -186,13 +186,13 @@ def app_set_preferences(self, prefs=None, **kwargs):
_name=APINames.Application, _method="setPreferences", data=data, **kwargs
)

@Alias("app_defaultSavePath")
@alias("app_defaultSavePath")
@response_text(str)
@login_required
def app_default_save_path(self, **kwargs):
"""
Retrieves the default path for where torrents are saved. (alias:
app_defaultSavePath)
Retrieves the default path for where torrents are saved.
(alias: app_defaultSavePath)
:return: string
"""
Expand Down
4 changes: 2 additions & 2 deletions qbittorrentapi/app.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ class Application(ClientCache):
def set_preferences(self, prefs: MutableMapping = None, **kwargs) -> None: ...
setPreferences = set_preferences
@property
def default_save_path(self, **kwargs) -> Text: ...
def default_save_path(self) -> Text: ...
defaultSavePath = default_save_path

class AppAPIMixIn(Request):
@property
def app(self) -> Application: ...
application: Application = app
application = app
def app_version(self, **kwargs) -> Text: ...
def app_web_api_version(self, **kwargs) -> Text: ...
app_webapiVersion = app_web_api_version
Expand Down
2 changes: 1 addition & 1 deletion qbittorrentapi/auth.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Authorization(ClientCache):
class AuthAPIMixIn(Request):
@property
def auth(self) -> Authorization: ...
authorization: Authorization = auth
authorization = auth
@property
def is_logged_in(self) -> bool: ...
def auth_log_in(
Expand Down
4 changes: 0 additions & 4 deletions qbittorrentapi/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from qbittorrentapi.app import AppAPIMixIn
from qbittorrentapi.auth import AuthAPIMixIn
from qbittorrentapi.log import LogAPIMixIn
from qbittorrentapi.rss import RSSAPIMixIn
from qbittorrentapi.search import SearchAPIMixIn
Expand Down Expand Up @@ -43,8 +41,6 @@


class Client(
AppAPIMixIn,
AuthAPIMixIn,
LogAPIMixIn,
SyncAPIMixIn,
TransferAPIMixIn,
Expand Down
2 changes: 2 additions & 0 deletions qbittorrentapi/client.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ from typing import Text
from qbittorrentapi.app import AppAPIMixIn
from qbittorrentapi.auth import AuthAPIMixIn
from qbittorrentapi.log import LogAPIMixIn
from qbittorrentapi.request import Request
from qbittorrentapi.rss import RSSAPIMixIn
from qbittorrentapi.search import SearchAPIMixIn
from qbittorrentapi.sync import SyncAPIMixIn
Expand All @@ -16,6 +17,7 @@ class Client(
SyncAPIMixIn,
TransferAPIMixIn,
TorrentsAPIMixIn,
Request,
RSSAPIMixIn,
SearchAPIMixIn,
):
Expand Down
9 changes: 4 additions & 5 deletions qbittorrentapi/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
logger = getLogger(__name__)


class Alias(object):

class alias(object):
"""
Alias class that can be used as a decorator for making methods callable
through other names (or "aliases").
Expand Down Expand Up @@ -183,7 +182,7 @@ def wrapper(client, *args, **kwargs):
return _inner


def _check_for_raise(client, error_message):
def check_for_raise(client, error_message):
"""For any nonexistent endpoint, log the error and conditionally raise an
exception."""
logger.debug(error_message)
Expand Down Expand Up @@ -211,7 +210,7 @@ def wrapper(client, *args, **kwargs):
"This endpoint is available starting in Web API v%s."
% (endpoint, version_introduced)
)
_check_for_raise(client=client, error_message=error_message)
check_for_raise(client=client, error_message=error_message)
return None

# send request to endpoint
Expand Down Expand Up @@ -242,7 +241,7 @@ def wrapper(client, *args, **kwargs):
"This endpoint was removed in Web API v%s."
% (endpoint, version_obsoleted)
)
_check_for_raise(client=client, error_message=error_message)
check_for_raise(client=client, error_message=error_message)
return None

# send request to endpoint
Expand Down
7 changes: 2 additions & 5 deletions qbittorrentapi/decorators.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ from typing import Set
from typing import Text
from typing import Type

from qbittorrentapi.client import Client
from qbittorrentapi.definitions import Dictionary
from qbittorrentapi.definitions import List
from qbittorrentapi.request import Request

class Alias:
class alias:
aliases: Set[Text]
def __init__(self, *aliases: Text) -> None: ...
def __call__(self, f: Callable) -> Callable: ...
Expand All @@ -18,8 +17,6 @@ def login_required(func: Callable): ...
def handle_hashes(func: Callable): ...
def response_text(response_class: Type[int | Text]): ...
def response_json(response_class: Type[List | Dictionary]): ...
def _check_for_raise(client: Client, error_message: Text) -> None: ...
def _version_too_old(client: Client, version_to_compare: Text) -> bool: ...
def _version_too_new(client: Client, version_to_compare: Text) -> bool: ...
def check_for_raise(client: Request, error_message: Text) -> None: ...
def endpoint_introduced(version_introduced: Text, endpoint: Text): ...
def version_removed(version_obsoleted: Text, endpoint: Text): ...
Loading

0 comments on commit 4af3e5c

Please sign in to comment.