Skip to content

Commit

Permalink
migrate to generic syntax in all std collections
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanA101a authored and benoit74 committed Feb 21, 2024
1 parent ca59669 commit d203625
Show file tree
Hide file tree
Showing 22 changed files with 121 additions and 81 deletions.
5 changes: 3 additions & 2 deletions contrib/encode_video.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import sys
from pathlib import Path
from typing import List

from zimscraperlib import logger
from zimscraperlib.video import presets, reencode
Expand All @@ -25,7 +26,7 @@ def encode_video(src_path: Path, dst_path: Path, preset: str):
logger.error(f"conversion failed:\n{process.stdout}")


def run(args: List[str] = sys.argv):
def run(args: list[str] = sys.argv):
if len(args) < 4: # noqa: PLR2004
print(f"Usage: {args[0]} <src_path> <dst_path> <preset>") # noqa: T201
print( # noqa: T201
Expand Down
16 changes: 8 additions & 8 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, Dict, Optional, Union
from typing import ClassVar, Optional, Union

import requests
import yt_dlp as youtube_dl
Expand All @@ -34,14 +34,14 @@ def shutdown(self) -> None:
"""shuts down the executor, awaiting completion"""
self.executor.shutdown(wait=True)

def _run_youtube_dl(self, url: str, options: Dict) -> None:
def _run_youtube_dl(self, url: str, options: dict) -> None:
with youtube_dl.YoutubeDL(options) as ydl:
ydl.download([url])

def download(
self,
url: str,
options: Optional[Dict],
options: Optional[dict],
wait: Optional[bool] = True, # noqa: FBT002
) -> Union[bool, Future]:
"""Downloads video using initialized executor.
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, Optional[Union[str, bool, int]]]] = {}
defaults: ClassVar[dict[str, Optional[Union[str, bool, int]]]] = {
"writethumbnail": True,
"write_all_thumbnails": True,
"writesubtitles": True,
Expand Down Expand Up @@ -109,14 +109,14 @@ def get_options(


class BestWebm(YoutubeConfig):
options: ClassVar[Dict[str, Optional[Union[str, bool, int]]]] = {
options: ClassVar[dict[str, Optional[Union[str, bool, int]]]] = {
"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, Optional[Union[str, bool, int]]]] = {
"preferredcodec": "mp4",
"format": "best[ext=mp4]/bestvideo[ext=mp4]+bestaudio[ext=m4a]/best",
}
Expand Down Expand Up @@ -179,7 +179,7 @@ def stream_file(
proxies: Optional[dict] = None,
only_first_block: Optional[bool] = False, # noqa: FBT002
max_retries: Optional[int] = 5,
headers: Optional[Dict[str, str]] = None,
headers: Optional[dict[str, str]] = None,
session: Optional[requests.Session] = None,
) -> tuple[int, requests.structures.CaseInsensitiveDict]: # pyright: ignore
"""Stream data from a URL to either a BytesIO object or a file
Expand Down
3 changes: 2 additions & 1 deletion src/zimscraperlib/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

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

import magic

Expand Down
6 changes: 4 additions & 2 deletions src/zimscraperlib/fix_ogvjs_dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

""" quick script to fix videojs-ogvjs so that it triggers on webm mimetype """

from __future__ import annotations

import logging
import pathlib
import sys
from typing import List, Union
from typing import Union

logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.DEBUG)
logger = logging.getLogger(__name__)
Expand All @@ -33,7 +35,7 @@ def fix_source_dir(source_vendors_path: Union[pathlib.Path, str]):
logger.info("all done.")


def run(args: List[str] = sys.argv):
def run(args: list[str] = sys.argv):
if len(args) < 2: # noqa: PLR2004
print(f"Usage: {args[0]} <source_vendors_path>") # noqa: T201
print( # noqa: T201
Expand Down
14 changes: 8 additions & 6 deletions src/zimscraperlib/i18n.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#!/usr/bin/env python3
# vim: ai ts=4 sts=4 et sw=4 nu

from __future__ import annotations

import gettext
import locale
import pathlib
import re
from typing import Dict, Optional, Tuple, Union
from typing import Optional, Union

import babel
import iso639
Expand Down Expand Up @@ -58,7 +60,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, Union[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 @@ -112,8 +114,8 @@ def replace_types(new_type: str) -> str:


def find_language_names(
query: str, lang_data: Optional[Dict] = None
) -> Tuple[str, str]:
query: str, lang_data: Optional[dict] = 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 All @@ -140,7 +142,7 @@ def find_language_names(
return default, default


def update_with_macro(lang_data: Dict, macro_data: Dict):
def update_with_macro(lang_data: dict, macro_data: dict):
"""update empty keys from lang_data with ones of macro_data"""
if macro_data:
for key, value in macro_data.items():
Expand All @@ -151,7 +153,7 @@ def update_with_macro(lang_data: Dict, macro_data: Dict):

def get_language_details(
query: str, failsafe: Optional[bool] = False # noqa: FBT002
) -> Dict:
) -> dict:
"""language details dict from query.
Raises NotFound or return `und` language details if failsafe
Expand Down
5 changes: 3 additions & 2 deletions src/zimscraperlib/image/optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
can still run on default settings which give
a bit less size than the original images but maintain a high quality. """

from __future__ import annotations

import io
import os
import pathlib
import subprocess
from typing import Optional, Tuple, Union
from typing import Optional, Union

import piexif
from optimize_images.img_aux_processing import do_reduce_colors, rebuild_palette
Expand Down Expand Up @@ -57,7 +58,7 @@ def optimize_png(
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),
background_color: Optional[tuple[int, int, int]] = (255, 255, 255),
**options, # noqa: ARG001
) -> Union[pathlib.Path, io.BytesIO]:
"""method to optimize PNG files using a pure python external optimizer
Expand Down
28 changes: 15 additions & 13 deletions src/zimscraperlib/image/presets.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env python3
# vim: ai ts=4 sts=4 et sw=4 nu

from typing import ClassVar, Dict, Optional, Union
from __future__ import annotations

from typing import ClassVar, Optional, Union

""" presets for ImageOptimizer in zimscraperlib.image.optimization module """

Expand All @@ -20,7 +22,7 @@ class WebpLow:
ext = "webp"
mimetype = f"{preset_type}/webp"

options: ClassVar[Dict[str, Optional[Union[str, bool, int]]]] = {
options: ClassVar[dict[str, Optional[Union[str, bool, int]]]] = {
"lossless": False,
"quality": 40,
"method": 6,
Expand All @@ -39,7 +41,7 @@ class WebpMedium:
ext = "webp"
mimetype = f"{preset_type}/webp"

options: ClassVar[Dict[str, Optional[Union[str, bool, int]]]] = {
options: ClassVar[dict[str, Optional[Union[str, bool, int]]]] = {
"lossless": False,
"quality": 50,
"method": 6,
Expand All @@ -58,7 +60,7 @@ class WebpHigh:
ext = "webp"
mimetype = f"{preset_type}/webp"

options: ClassVar[Dict[str, Optional[Union[str, bool, int]]]] = {
options: ClassVar[dict[str, Optional[Union[str, bool, int]]]] = {
"lossless": False,
"quality": 90,
"method": 6,
Expand All @@ -79,7 +81,7 @@ class GifLow:
ext = "gif"
mimetype = f"{preset_type}/gif"

options: ClassVar[Dict[str, Optional[Union[str, bool, int]]]] = {
options: ClassVar[dict[str, Optional[Union[str, bool, int]]]] = {
"optimize_level": 3,
"max_colors": 256,
"lossiness": 80,
Expand All @@ -102,7 +104,7 @@ class GifMedium:
ext = "gif"
mimetype = f"{preset_type}/gif"

options: ClassVar[Dict[str, Optional[Union[str, bool, int]]]] = {
options: ClassVar[dict[str, Optional[Union[str, bool, int]]]] = {
"optimize_level": 3,
"lossiness": 20,
"no_extensions": True,
Expand All @@ -124,7 +126,7 @@ class GifHigh:
ext = "gif"
mimetype = f"{preset_type}/gif"

options: ClassVar[Dict[str, Optional[Union[str, bool, int]]]] = {
options: ClassVar[dict[str, Optional[Union[str, bool, int]]]] = {
"optimize_level": 2,
"lossiness": None,
"no_extensions": True,
Expand All @@ -143,7 +145,7 @@ class PngLow:
ext = "png"
mimetype = f"{preset_type}/png"

options: ClassVar[Dict[str, Optional[Union[str, bool, int]]]] = {
options: ClassVar[dict[str, Optional[Union[str, bool, int]]]] = {
"reduce_colors": True,
"remove_transparency": False,
"max_colors": 256,
Expand All @@ -162,7 +164,7 @@ class PngMedium:
ext = "png"
mimetype = f"{preset_type}/png"

options: ClassVar[Dict[str, Optional[Union[str, bool, int]]]] = {
options: ClassVar[dict[str, Optional[Union[str, bool, int]]]] = {
"reduce_colors": False,
"remove_transparency": False,
"fast_mode": False,
Expand All @@ -180,7 +182,7 @@ class PngHigh:
ext = "png"
mimetype = f"{preset_type}/png"

options: ClassVar[Dict[str, Optional[Union[str, bool, int]]]] = {
options: ClassVar[dict[str, Optional[Union[str, bool, int]]]] = {
"reduce_colors": False,
"remove_transparency": False,
"fast_mode": True,
Expand All @@ -199,7 +201,7 @@ class JpegLow:
ext = "png"
mimetype = f"{preset_type}/png"

options: ClassVar[Dict[str, Optional[Union[str, bool, int]]]] = {
options: ClassVar[dict[str, Optional[Union[str, bool, int]]]] = {
"quality": 45,
"keep_exif": False,
"fast_mode": True,
Expand All @@ -218,7 +220,7 @@ class JpegMedium:
ext = "jpg"
mimetype = f"{preset_type}/jpeg"

options: ClassVar[Dict[str, Optional[Union[str, bool, int]]]] = {
options: ClassVar[dict[str, Optional[Union[str, bool, int]]]] = {
"quality": 65,
"keep_exif": False,
"fast_mode": True,
Expand All @@ -237,7 +239,7 @@ class JpegHigh:
ext = "jpg"
mimetype = f"{preset_type}/jpeg"

options: ClassVar[Dict[str, Optional[Union[str, bool, int]]]] = {
options: ClassVar[dict[str, Optional[Union[str, bool, int]]]] = {
"quality": 80,
"keep_exif": True,
"fast_mode": True,
Expand Down
10 changes: 6 additions & 4 deletions src/zimscraperlib/image/probing.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
#!/usr/bin/env python3
# vim: ai ts=4 sts=4 et sw=4 nu

from __future__ import annotations

import colorsys
import io
import pathlib
import re
from typing import Optional, Tuple, Union
from typing import Optional, Union

import colorthief
import PIL.Image


def get_colors(
src: pathlib.Path, use_palette: Optional[bool] = True # noqa: FBT002
) -> Tuple[str, str]:
) -> tuple[str, str]:
"""(main, secondary) HTML color codes from an image path"""

def rgb_to_hex(r: int, g: int, b: int) -> str:
"""hexadecimal HTML-friendly color code for RGB tuple"""
return "#{}{}{}".format(*[str(hex(x)[2:]).zfill(2) for x in (r, g, b)]).upper()

def solarize(r: int, g: int, b: int) -> Tuple[int, int, int]:
def solarize(r: int, g: int, b: int) -> tuple[int, int, int]:
# calculate solarized color for main
h, l, s = colorsys.rgb_to_hls( # noqa: E741
float(r) / 256, float(g) / 256, float(b) / 256
Expand Down Expand Up @@ -71,7 +73,7 @@ def format_for(
def is_valid_image(
image: Union[pathlib.Path, io.IOBase, bytes],
imformat: str,
size: Optional[Tuple[int, int]] = None,
size: Optional[tuple[int, int]] = None,
) -> bool:
"""whether image is a valid imformat (PNG) image, optionnaly of requested size"""
if isinstance(image, bytes):
Expand Down
6 changes: 4 additions & 2 deletions src/zimscraperlib/inputs.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#!/usr/bin/env python3
# vim: ai ts=4 sts=4 et sw=4 nu

from __future__ import annotations

import pathlib
import shutil
import tempfile
from typing import Optional, Tuple, Union
from typing import Optional, Union

from zimscraperlib import logger
from zimscraperlib.constants import (
Expand Down Expand Up @@ -60,7 +62,7 @@ def compute_descriptions(
default_description: str,
user_description: Optional[str],
user_long_description: Optional[str],
) -> Tuple[str, Optional[str]]:
) -> tuple[str, Optional[str]]:
"""Computes short and long descriptions compliant with ZIM standard.
Based on provided parameters, the function computes a short and a long description
Expand Down
3 changes: 2 additions & 1 deletion src/zimscraperlib/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import logging
import pathlib
import sys
from collections.abc import Iterable
from logging.handlers import RotatingFileHandler
from typing import Iterable, Optional
from typing import Optional

from zimscraperlib.constants import NAME

Expand Down
Loading

0 comments on commit d203625

Please sign in to comment.