Skip to content

Commit

Permalink
Merge pull request #150 from elfkuzco/simplify-types
Browse files Browse the repository at this point in the history
simplify Optional and Union types
  • Loading branch information
benoit74 authored Mar 28, 2024
2 parents 7948d6e + cfbabf7 commit 7d49831
Show file tree
Hide file tree
Showing 31 changed files with 342 additions and 350 deletions.
247 changes: 124 additions & 123 deletions CHANGELOG.md

Large diffs are not rendered by default.

42 changes: 21 additions & 21 deletions src/zimscraperlib/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pathlib
import subprocess
from concurrent.futures import Future, ThreadPoolExecutor
from typing import ClassVar, Optional, Union
from typing import ClassVar

import requests
import yt_dlp as youtube_dl
Expand All @@ -21,7 +21,7 @@ class YoutubeDownloader:
Shutdown method must be run explicitly to
free any occupied resources"""

def __init__(self, threads: Optional[int] = 1) -> None:
def __init__(self, threads: int | None = 1) -> None:
self.executor = ThreadPoolExecutor(max_workers=threads)

def __enter__(self):
Expand All @@ -41,9 +41,9 @@ def _run_youtube_dl(self, url: str, options: dict) -> None:
def download(
self,
url: str,
options: Optional[dict],
wait: Optional[bool] = True, # noqa: FBT002
) -> Union[bool, Future]:
options: dict | None,
wait: bool | None = True, # noqa: FBT002
) -> bool | Future:
"""Downloads video using initialized executor.
url: URL or Video ID
Expand All @@ -65,8 +65,8 @@ def download(


class YoutubeConfig(dict):
options: ClassVar[dict[str, Optional[Union[str, bool, int]]]] = {}
defaults: ClassVar[dict[str, Optional[Union[str, bool, int]]]] = {
options: ClassVar[dict[str, str | bool | int | None]] = {}
defaults: ClassVar[dict[str, str | bool | int | None]] = {
"writethumbnail": True,
"write_all_thumbnails": True,
"writesubtitles": True,
Expand All @@ -88,8 +88,8 @@ def __init__(self, **kwargs):
@classmethod
def get_options(
cls,
target_dir: Optional[pathlib.Path] = None,
filepath: Optional[pathlib.Path] = None,
target_dir: pathlib.Path | None = None,
filepath: pathlib.Path | None = None,
**options,
):
if "outtmpl" not in options:
Expand All @@ -109,14 +109,14 @@ def get_options(


class BestWebm(YoutubeConfig):
options: ClassVar[dict[str, Optional[Union[str, bool, int]]]] = {
options: ClassVar[dict[str, str | bool | int | None]] = {
"preferredcodec": "webm",
"format": "best[ext=webm]/bestvideo[ext=webm]+bestaudio[ext=webm]/best",
}


class BestMp4(YoutubeConfig):
options: ClassVar[dict[str, Optional[Union[str, bool, int]]]] = {
options: ClassVar[dict[str, str | bool | int | None]] = {
"preferredcodec": "mp4",
"format": "best[ext=mp4]/bestvideo[ext=mp4]+bestaudio[ext=m4a]/best",
}
Expand All @@ -142,7 +142,7 @@ def save_large_file(url: str, fpath: pathlib.Path) -> None:


def _get_retry_adapter(
max_retries: Optional[int] = 5,
max_retries: int | None = 5,
) -> requests.adapters.BaseAdapter: # pyright: ignore
retries = requests.packages.urllib3.util.retry.Retry( # pyright: ignore
total=max_retries, # total number of retries
Expand All @@ -164,7 +164,7 @@ def _get_retry_adapter(
return requests.adapters.HTTPAdapter(max_retries=retries) # pyright: ignore


def get_session(max_retries: Optional[int] = 5) -> requests.Session:
def get_session(max_retries: int | None = 5) -> requests.Session:
"""Session to hold cookies and connection pool together"""
session = requests.Session()
session.mount("http", _get_retry_adapter(max_retries)) # tied to http and https
Expand All @@ -173,14 +173,14 @@ def get_session(max_retries: Optional[int] = 5) -> requests.Session:

def stream_file(
url: str,
fpath: Optional[pathlib.Path] = None,
byte_stream: Optional[io.BytesIO] = None,
block_size: Optional[int] = 1024,
proxies: Optional[dict] = None,
only_first_block: Optional[bool] = False, # noqa: FBT002
max_retries: Optional[int] = 5,
headers: Optional[dict[str, str]] = None,
session: Optional[requests.Session] = None,
fpath: pathlib.Path | None = None,
byte_stream: io.BytesIO | None = None,
block_size: int | None = 1024,
proxies: dict | None = None,
only_first_block: bool | None = False, # noqa: FBT002
max_retries: int | None = 5,
headers: dict[str, str] | None = None,
session: requests.Session | None = None,
) -> tuple[int, requests.structures.CaseInsensitiveDict]: # pyright: ignore
"""Stream data from a URL to either a BytesIO object or a file
Arguments -
Expand Down
8 changes: 5 additions & 3 deletions src/zimscraperlib/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
Shortcuts to retrieve mime type using magic"""

from __future__ import annotations

import os
import pathlib
from collections.abc import Callable
from typing import Any, Optional, Union
from typing import Any

import magic

Expand Down Expand Up @@ -43,8 +45,8 @@ def get_content_mimetype(content: bytes) -> str:


def delete_callback(
fpath: Union[str, pathlib.Path],
callback: Optional[Callable] = None,
fpath: str | pathlib.Path,
callback: Callable | None = None,
*callback_args: Any,
):
"""helper deleting passed filepath, optionnaly calling an additional callback"""
Expand Down
3 changes: 1 addition & 2 deletions src/zimscraperlib/fix_ogvjs_dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
import logging
import pathlib
import sys
from typing import Union

logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.DEBUG)
logger = logging.getLogger(__name__)


def fix_source_dir(source_vendors_path: Union[pathlib.Path, str]):
def fix_source_dir(source_vendors_path: pathlib.Path | str):
"""update ogvjs plugin to trigger on webm mimetype"""
root = pathlib.Path(source_vendors_path)
logger.info("fixing videosjs-ogvjs.js")
Expand Down
7 changes: 4 additions & 3 deletions src/zimscraperlib/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
# vim: ai ts=4 sts=4 et sw=4 nu

""" Tools to work with HTML contents """
from __future__ import annotations

import pathlib
from typing import BinaryIO, TextIO, Union
from typing import BinaryIO, TextIO

from bs4 import BeautifulSoup

from zimscraperlib.types import ARTICLE_MIME


def find_title_in(content: Union[str, BinaryIO, TextIO], mime_type: str) -> str:
def find_title_in(content: str | BinaryIO | TextIO, mime_type: str) -> str:
"""Extracted title from HTML content
blank on failure to extract and non-HTML files"""
Expand All @@ -32,7 +33,7 @@ def find_title_in_file(fpath: pathlib.Path, mime_type: str) -> str:
return ""


def find_language_in(content: Union[str, BinaryIO, TextIO], mime_type: str) -> str:
def find_language_in(content: str | BinaryIO | TextIO, mime_type: str) -> str:
"""Extracted language from HTML content
blank on failure to extract and non-HTML files"""
Expand Down
9 changes: 3 additions & 6 deletions src/zimscraperlib/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import locale
import pathlib
import re
from typing import Optional, Union

import babel
import iso639
Expand Down Expand Up @@ -60,7 +59,7 @@ def setlocale(root_dir: pathlib.Path, locale_name: str):
return Locale.setup(root_dir / "locale", locale_name)


def get_iso_lang_data(lang: str) -> tuple[dict, Union[dict, None]]:
def get_iso_lang_data(lang: str) -> tuple[dict, dict | None]:
"""ISO-639-x languages details for lang. Raises NotFound
Included keys: iso-639-1, iso-639-2b, iso-639-2t, iso-639-3, iso-639-5
Expand Down Expand Up @@ -113,9 +112,7 @@ def replace_types(new_type: str) -> str:
return lang_data, None


def find_language_names(
query: str, lang_data: Optional[dict] = None
) -> tuple[str, str]:
def find_language_names(query: str, lang_data: dict | None = None) -> tuple[str, str]:
"""(native, english) language names for lang with help from language_details dict
Falls back to English name if available or query if not"""
Expand Down Expand Up @@ -152,7 +149,7 @@ def update_with_macro(lang_data: dict, macro_data: dict):


def get_language_details(
query: str, failsafe: Optional[bool] = False # noqa: FBT002
query: str, failsafe: bool | None = False # noqa: FBT002
) -> dict:
"""language details dict from query.
Expand Down
7 changes: 4 additions & 3 deletions src/zimscraperlib/image/convertion.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env python3
# vim: ai ts=4 sts=4 et sw=4 nu

from __future__ import annotations

import io
import pathlib
from typing import Union

import PIL

Expand All @@ -14,8 +15,8 @@


def convert_image(
src: Union[pathlib.Path, io.BytesIO],
dst: Union[pathlib.Path, io.BytesIO],
src: pathlib.Path | io.BytesIO,
dst: pathlib.Path | io.BytesIO,
**params: str,
) -> None:
"""convert an image file from one format to another
Expand Down
55 changes: 27 additions & 28 deletions src/zimscraperlib/image/optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import os
import pathlib
import subprocess
from typing import Optional, Union

import piexif
from optimize_images.img_aux_processing import do_reduce_colors, rebuild_palette
Expand All @@ -52,15 +51,15 @@ def ensure_matches(


def optimize_png(
src: Union[pathlib.Path, io.BytesIO],
dst: Optional[pathlib.Path] = None,
reduce_colors: Optional[bool] = False, # noqa: FBT002
max_colors: Optional[int] = 256,
fast_mode: Optional[bool] = True, # noqa: FBT002
remove_transparency: Optional[bool] = False, # noqa: FBT002
background_color: Optional[tuple[int, int, int]] = (255, 255, 255),
src: pathlib.Path | io.BytesIO,
dst: pathlib.Path | None = None,
reduce_colors: bool | None = False, # noqa: FBT002
max_colors: int | None = 256,
fast_mode: bool | None = True, # noqa: FBT002
remove_transparency: bool | None = False, # noqa: FBT002
background_color: tuple[int, int, int] | None = (255, 255, 255),
**options, # noqa: ARG001
) -> Union[pathlib.Path, io.BytesIO]:
) -> pathlib.Path | io.BytesIO:
"""method to optimize PNG files using a pure python external optimizer
Arguments:
Expand Down Expand Up @@ -99,13 +98,13 @@ def optimize_png(


def optimize_jpeg(
src: Union[pathlib.Path, io.BytesIO],
dst: Optional[pathlib.Path] = None,
quality: Optional[int] = 85,
fast_mode: Optional[bool] = True, # noqa: FBT002
keep_exif: Optional[bool] = True, # noqa: FBT002
src: pathlib.Path | io.BytesIO,
dst: pathlib.Path | None = None,
quality: int | None = 85,
fast_mode: bool | None = True, # noqa: FBT002
keep_exif: bool | None = True, # noqa: FBT002
**options, # noqa: ARG001
) -> Union[pathlib.Path, io.BytesIO]:
) -> pathlib.Path | io.BytesIO:
"""method to optimize JPEG files using a pure python external optimizer
quality: JPEG quality (integer between 1 and 100)
values: 50 | 55 | 35 | 100 | XX
Expand Down Expand Up @@ -169,13 +168,13 @@ def optimize_jpeg(


def optimize_webp(
src: Union[pathlib.Path, io.BytesIO],
dst: Optional[pathlib.Path] = None,
lossless: Optional[bool] = False, # noqa: FBT002
quality: Optional[int] = 60,
method: Optional[int] = 6,
src: pathlib.Path | io.BytesIO,
dst: pathlib.Path | None = None,
lossless: bool | None = False, # noqa: FBT002
quality: int | None = 60,
method: int | None = 6,
**options, # noqa: ARG001
) -> Union[pathlib.Path, io.BytesIO]:
) -> pathlib.Path | io.BytesIO:
"""method to optimize WebP using Pillow options
lossless: Whether to use lossless compression (boolean)
values: True | False
Expand Down Expand Up @@ -214,11 +213,11 @@ def optimize_webp(
def optimize_gif(
src: pathlib.Path,
dst: pathlib.Path,
optimize_level: Optional[int] = 1,
lossiness: Optional[int] = None,
interlace: Optional[bool] = True, # noqa: FBT002
no_extensions: Optional[bool] = True, # noqa: FBT002
max_colors: Optional[int] = None,
optimize_level: int | None = 1,
lossiness: int | None = None,
interlace: bool | None = True, # noqa: FBT002
no_extensions: bool | None = True, # noqa: FBT002
max_colors: int | None = None,
**options, # noqa: ARG001
) -> pathlib.Path:
"""method to optimize GIFs using gifsicle >= 1.92
Expand Down Expand Up @@ -267,8 +266,8 @@ def optimize_gif(
def optimize_image(
src: pathlib.Path,
dst: pathlib.Path,
delete_src: Optional[bool] = False, # noqa: FBT002
convert: Optional[Union[bool, str]] = False, # noqa: FBT002
delete_src: bool | None = False, # noqa: FBT002
convert: bool | str | None = False, # noqa: FBT002
**options,
) -> bool: # pyright: ignore
"""Optimize image, automatically selecting correct optimizer
Expand Down
Loading

0 comments on commit 7d49831

Please sign in to comment.