Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type hints to make objects easier to track during development. #131

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion plugin_cmds/cmd_get-annotations.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import click

from audible import AsyncClient
from audible.exceptions import NotFoundError
from audible_cli.decorators import pass_client


@click.command("get-annotations")
@click.argument("asin")
@pass_client
async def cli(client, asin):
async def cli(client: AsyncClient, asin: str):
url = f"https://cde-ta-g7g.amazon.com/FionaCDEServiceEngine/sidecar"
params = {
"type": "AUDI",
Expand Down
5 changes: 3 additions & 2 deletions plugin_cmds/cmd_goodreads-transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime, timezone

import click
from audible import AsyncClient
from audible_cli.decorators import (
bunch_size_option,
timeout_option,
Expand All @@ -29,7 +30,7 @@
@bunch_size_option
@pass_session
@pass_client
async def cli(session, client, output):
async def cli(session, client: AsyncClient, output: str):
"""YOUR COMMAND DESCRIPTION"""

logger.debug("fetching library")
Expand Down Expand Up @@ -58,7 +59,7 @@ async def cli(session, client, output):
logger.info(f"File saved to {output}")


def _prepare_library_for_export(library):
def _prepare_library_for_export(library: Library):
prepared_library = []

isbn_counter = 0
Expand Down
3 changes: 2 additions & 1 deletion plugin_cmds/cmd_image-urls.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import click
from audible import AsyncClient
from audible_cli.decorators import pass_client, timeout_option


@click.command("image-urls")
@click.argument("asin")
@timeout_option()
@pass_client()
async def cli(client, asin):
async def cli(client: AsyncClient, asin: str):
"""Print out the image urls for different resolutions for a book"""
r = await client.get(
f"catalog/products/{asin}",
Expand Down
7 changes: 4 additions & 3 deletions plugin_cmds/cmd_listening-stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import datetime

import click
from audible import AsyncClient
from audible_cli.decorators import pass_client


Expand All @@ -13,14 +14,14 @@
current_year = datetime.now().year


def ms_to_hms(milliseconds):
def ms_to_hms(milliseconds: int):
seconds = int((milliseconds / 1000) % 60)
minutes = int(((milliseconds / (1000*60)) % 60))
hours = int(((milliseconds / (1000*60*60)) % 24))
return {"hours": hours, "minutes": minutes, "seconds": seconds}


async def _get_stats_year(client, year):
async def _get_stats_year(client: AsyncClient, year: int) -> dict:
stats_year = {}
stats = await client.get(
"stats/aggregates",
Expand Down Expand Up @@ -50,7 +51,7 @@ async def _get_stats_year(client, year):
help="start year for collecting listening stats"
)
@pass_client
async def cli(client, output, signup_year):
async def cli(client: AsyncClient, output: pathlib.Path, signup_year: int):
"""get and analyse listening statistics"""
year_range = [y for y in range(signup_year, current_year+1)]

Expand Down
2 changes: 1 addition & 1 deletion src/audible_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def cli():
@click.pass_context
@version_option
@verbosity_option(cli_logger=logger)
def quickstart(ctx):
def quickstart(ctx: click.Context):
"""Entrypoint for the quickstart command"""
try:
sys.exit(ctx.forward(cmd_quickstart.cli))
Expand Down
3 changes: 2 additions & 1 deletion src/audible_cli/cmds/cmd_activation_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
fetch_activation_sign_auth
)

from ..config import Session
from ..decorators import pass_session


Expand All @@ -18,7 +19,7 @@
is_flag=True,
help="Reload activation bytes and save to auth file.")
@pass_session
def cli(session, **options):
def cli(session: Session, **options):
"""Get activation bytes."""
auth = session.auth
if auth.activation_bytes is None or options.get("reload"):
Expand Down
3 changes: 2 additions & 1 deletion src/audible_cli/cmds/cmd_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import click
from audible import Client

from ..config import Session
from ..decorators import pass_session


Expand Down Expand Up @@ -61,7 +62,7 @@
"the current profile is used."
)
@pass_session
def cli(session, **options):
def cli(session: Session, **options):
"""Send requests to an Audible API endpoint

Take a look at
Expand Down
36 changes: 27 additions & 9 deletions src/audible_cli/cmds/cmd_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
import click
import httpx
import questionary
from audible import AsyncClient
from audible.exceptions import NotFoundError
from click import echo

from ..config import Session
from ..decorators import (
bunch_size_option,
end_date_option,
Expand All @@ -28,7 +30,7 @@
NotDownloadableAsAAX,
VoucherNeedRefresh
)
from ..models import Library
from ..models import Library, LibraryItem
from ..utils import datetime_type, Downloader


Expand Down Expand Up @@ -180,7 +182,11 @@ async def download_cover(


async def download_pdf(
client, output_dir, base_filename, item, overwrite_existing
client: AsyncClient,
output_dir: str,
base_filename: str,
item: LibraryItem,
overwrite_existing: bool,
):
url = item.get_pdf_url()
if url is None:
Expand All @@ -200,8 +206,12 @@ async def download_pdf(


async def download_chapters(
output_dir, base_filename, item, quality, overwrite_existing
):
output_dir: str,
base_filename: str,
item: LibraryItem,
quality: str,
overwrite_existing: bool,
) -> bool | None:
if not output_dir.is_dir():
raise DirectoryDoesNotExists(output_dir)

Expand All @@ -228,8 +238,11 @@ async def download_chapters(


async def download_annotations(
output_dir, base_filename, item, overwrite_existing
):
output_dir: str,
base_filename: str,
item: LibraryItem,
overwrite_existing: bool,
) -> bool | None:
if not output_dir.is_dir():
raise DirectoryDoesNotExists(output_dir)

Expand All @@ -256,8 +269,13 @@ async def download_annotations(


async def download_aax(
client, output_dir, base_filename, item, quality, overwrite_existing,
aax_fallback
client: AsyncClient,
output_dir: str,
base_filename: str,
item: LibraryItem,
quality: str,
overwrite_existing: bool,
aax_fallback: bool,
):
# url, codec = await item.get_aax_url(quality)
try:
Expand Down Expand Up @@ -658,7 +676,7 @@ def display_counter():
@bunch_size_option
@pass_session
@pass_client(headers=CLIENT_HEADERS)
async def cli(session, api_client, **params):
async def cli(session: Session, api_client: AsyncClient, **params):
"""download audiobook(s) from library"""
client = api_client.session
output_dir = pathlib.Path(params.get("output_dir")).resolve()
Expand Down
16 changes: 11 additions & 5 deletions src/audible_cli/cmds/cmd_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import pathlib

import click
from audible import AsyncClient
from click import echo

from ..config import Session
from ..decorators import (
bunch_size_option,
end_date_option,
Expand All @@ -23,7 +25,11 @@ def cli():
"""interact with library"""


async def _get_library(session, client, resolve_podcasts):
async def _get_library(
session: Session,
client: AsyncClient,
resolve_podcasts: bool,
):
bunch_size = session.params.get("bunch_size")
start_date = session.params.get("start_date")
end_date = session.params.get("end_date")
Expand Down Expand Up @@ -76,11 +82,11 @@ async def _get_library(session, client, resolve_podcasts):
@end_date_option
@pass_session
@pass_client
async def export_library(session, client, **params):
async def export_library(session: Session, client: AsyncClient, **params):
"""export library"""

@wrap_async
def _prepare_item(item):
def _prepare_item(item: dict):
data_row = {}
for key in item:
v = getattr(item, key)
Expand Down Expand Up @@ -164,11 +170,11 @@ def _prepare_item(item):
@end_date_option
@pass_session
@pass_client
async def list_library(session, client, resolve_podcasts):
async def list_library(session: Session, client: AsyncClient, resolve_podcasts: bool):
"""list titles in library"""

@wrap_async
def _prepare_item(item):
def _prepare_item(item: dict):
fields = [item.asin]

authors = ", ".join(
Expand Down
42 changes: 33 additions & 9 deletions src/audible_cli/cmds/cmd_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from click import echo, secho
from tabulate import tabulate

from ..config import Session
from ..decorators import pass_session
from ..utils import build_auth_file

Expand Down Expand Up @@ -35,14 +36,14 @@ def manage_auth_files():

@manage_config.command("edit")
@pass_session
def config_editor(session):
def config_editor(session: Session):
"""Open the config file with default editor"""
click.edit(filename=session.config.filename)


@manage_profiles.command("list")
@pass_session
def list_profiles(session):
def list_profiles(session: Session):
"""List all profiles in the config file"""
head = ["P", "Profile", "auth file", "cc"]
config = session.config
Expand Down Expand Up @@ -89,7 +90,14 @@ def list_profiles(session):
)
@pass_session
@click.pass_context
def add_profile(ctx, session, profile, country_code, auth_file, is_primary):
def add_profile(
ctx: None,
session: Session,
profile: pathlib.Path,
country_code: str,
auth_file: pathlib.Path,
is_primary: bool,
):
"""Adds a profile to config file"""
if not (session.config.dirname / auth_file).exists():
logger.error("Auth file doesn't exists")
Expand All @@ -110,7 +118,7 @@ def add_profile(ctx, session, profile, country_code, auth_file, is_primary):
help="The profile name to remove from config."
)
@pass_session
def remove_profile(session, profile):
def remove_profile(session: Session, profile: dict):
"""Remove one or multiple profile(s) from config file"""
profiles = session.config.data.get("profile")
for p in profile:
Expand All @@ -126,7 +134,12 @@ def remove_profile(session, profile):


@pass_session
def check_if_auth_file_not_exists(session, ctx, param, value):
def check_if_auth_file_not_exists(
session: Session,
ctx: None,
param: None,
value: pathlib.Path,
):
value = session.config.dirname / value
if pathlib.Path(value).exists():
logger.error("The file already exists.")
Expand Down Expand Up @@ -176,8 +189,14 @@ def check_if_auth_file_not_exists(session, ctx, param, value):
)
@pass_session
def add_auth_file(
session, auth_file, password, audible_username,
audible_password, country_code, external_login, with_username
session: Session,
auth_file: pathlib.Path,
password: str | None,
audible_username: str | None,
audible_password: str | None,
country_code: str,
external_login: bool = False,
with_username: bool = False,
):
"""Register a new device and add an auth file to config dir"""
build_auth_file(
Expand All @@ -192,7 +211,12 @@ def add_auth_file(


@pass_session
def check_if_auth_file_exists(session, ctx, param, value):
def check_if_auth_file_exists(
session: Session,
ctx: None,
param: None,
value: pathlib.Path,
):
value = session.config.dirname / value
if not pathlib.Path(value).exists():
logger.error("The file doesn't exists.")
Expand All @@ -212,7 +236,7 @@ def check_if_auth_file_exists(session, ctx, param, value):
"--password", "-p",
help="The optional password for the auth file."
)
def remove_auth_file(auth_file, password):
def remove_auth_file(auth_file: pathlib.Path, password: str | None):
"""Deregister a device and remove auth file from config dir"""
auth = Authenticator.from_file(auth_file, password)
device_name = auth.device_info["device_name"]
Expand Down
4 changes: 2 additions & 2 deletions src/audible_cli/cmds/cmd_quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from tabulate import tabulate

from .. import __version__
from ..config import ConfigFile
from ..config import ConfigFile, Session
from ..constants import CONFIG_FILE, DEFAULT_AUTH_FILE_EXTENSION
from ..decorators import pass_session
from ..utils import build_auth_file
Expand Down Expand Up @@ -140,7 +140,7 @@ def ask_user(config: ConfigFile):

@click.command("quickstart")
@pass_session
def cli(session):
def cli(session: Session):
"""Quick setup audible"""
config_file: pathlib.Path = session.app_dir / CONFIG_FILE
config = ConfigFile(config_file, file_exists=False)
Expand Down
Loading