Skip to content

Migrate to python bootstrap conventions #128

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

Merged
merged 32 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2d105dc
Start migration to python bootstrap conventions
benoit74 Feb 6, 2024
c0c0a97
Remove useless fixture mark - never had any effect
benoit74 Feb 6, 2024
13f69d7
Minimal stubs for libzim
benoit74 Feb 6, 2024
2a18db4
Automatic fixes by ruff and black
benoit74 Feb 6, 2024
b6f25e5
Automatic ruff fixes with unsafe rules
benoit74 Feb 6, 2024
3f50609
Add all needed noqa statements - to be removed in the future, one by one
benoit74 Feb 6, 2024
2baf95f
Add all needed pyright:ignore statements - to be removed in the futur…
benoit74 Feb 6, 2024
c9cf343
Add 3.12 support and standardize CI
benoit74 Feb 6, 2024
5a0feca
Migrate from iso-639 to iso639-lang library, former one is not mainta…
benoit74 Feb 6, 2024
f3d6e09
Run all tests and install fix_ogvjs_dist script
benoit74 Feb 8, 2024
07755c3
Fix nocover pragmas
benoit74 Feb 8, 2024
e1c87af
Fix changelog
benoit74 Feb 8, 2024
dcded70
Fix many qa issues and one typing issue
benoit74 Feb 9, 2024
842252e
--runinstalled is not necessary anymore with bootstrap convention, we…
benoit74 Feb 9, 2024
4e5c216
Add back a lost # pragma: no cover
benoit74 Feb 9, 2024
dccaa66
Rewrite fix_ogvjs_dist and its tests to remove tests redundancies + e…
benoit74 Feb 9, 2024
a2f4650
Increase test coverage of zim/metadata.py
benoit74 Feb 9, 2024
b588a79
Increase test coverage of download.py
benoit74 Feb 9, 2024
c399f6f
Increase test coverage of filesystem.py
benoit74 Feb 9, 2024
fbe613f
Increase test coverage of image/optimization.py
benoit74 Feb 9, 2024
6c62022
Increase test coverage of logging.py
benoit74 Feb 9, 2024
75c7241
Increase test coverage of zim/_libkiwix.py + remove useless if branch
benoit74 Feb 9, 2024
40cadd4
Increase test coverage of zim/archive.py
benoit74 Feb 9, 2024
f9f0b9c
Increase test coverage of zim/filesystem.py
benoit74 Feb 9, 2024
e018d5d
Remove pytest.raise match condition because message changed in Python…
benoit74 Feb 9, 2024
0568e64
Fix options typing + add handling for incorrect outtmpl type
benoit74 Feb 12, 2024
b6db708
Skip test temporarily
benoit74 Feb 12, 2024
f342cc2
Restore Python dependencies ranges + widen the libzim range
benoit74 Feb 12, 2024
04964e1
Use type instead of __class__
benoit74 Feb 12, 2024
222bd80
Update README contribution guidelines + user projects
benoit74 Feb 12, 2024
c46d33f
Use /usr/bin/env in every subprocess command
benoit74 Feb 12, 2024
ad4b241
Re-activate test
benoit74 Feb 12, 2024
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
12 changes: 6 additions & 6 deletions src/zimscraperlib/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def download(
self,
url: str,
options: Optional[Dict],
wait: Optional[bool] = True,
wait: Optional[bool] = True, # noqa: FBT002
) -> Union[bool, Future]:
"""Downloads video using initialized executor.

Expand All @@ -63,8 +63,8 @@ def download(


class YoutubeConfig(dict):
options = {}
defaults = {
options = {} # noqa: RUF012
defaults = { # noqa: RUF012
"writethumbnail": True,
"write_all_thumbnails": True,
"writesubtitles": True,
Expand Down Expand Up @@ -105,14 +105,14 @@ def get_options(


class BestWebm(YoutubeConfig):
options = {
options = { # noqa: RUF012
"preferredcodec": "webm",
"format": "best[ext=webm]/bestvideo[ext=webm]+bestaudio[ext=webm]/best",
}


class BestMp4(YoutubeConfig):
options = {
options = { # noqa: RUF012
"preferredcodec": "mp4",
"format": "best[ext=mp4]/bestvideo[ext=mp4]+bestaudio[ext=m4a]/best",
}
Expand Down Expand Up @@ -171,7 +171,7 @@ def stream_file(
byte_stream: Optional[io.BytesIO] = None,
block_size: Optional[int] = 1024,
proxies: Optional[dict] = None,
only_first_block: Optional[bool] = False,
only_first_block: Optional[bool] = False, # noqa: FBT002
max_retries: Optional[int] = 5,
headers: Optional[Dict[str, str]] = None,
session: Optional[requests.Session] = None,
Expand Down
6 changes: 3 additions & 3 deletions src/zimscraperlib/fix_ogvjs_dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def fix_source_dir(source_vendors_path: Union[pathlib.Path, str]):


def run():
if len(sys.argv) < 2:
print(f"Usage: {sys.argv[0]} <source_vendors_path>")
print(
if len(sys.argv) < 2: # noqa: PLR2004
print(f"Usage: {sys.argv[0]} <source_vendors_path>") # noqa: T201
print( # noqa: T201
"\t<source_vendors_path>\tpath to your folder containing "
"ogvjs/videojs/videojs-ogvjs."
)
Expand Down
8 changes: 5 additions & 3 deletions src/zimscraperlib/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
ISO_LEVELS = ["1", "2b", "2t", "3", "5"]


class NotFound(ValueError):
class NotFound(ValueError): # noqa: N818
pass


Expand Down Expand Up @@ -126,7 +126,9 @@ def update_with_macro(lang_data: Dict, macro_data: Dict):
return lang_data


def get_language_details(query: str, failsafe: Optional[bool] = False) -> Dict:
def get_language_details(
query: str, failsafe: Optional[bool] = False # noqa: FBT002
) -> Dict:
"""language details dict from query.

Raises NotFound or return `und` language details if failsafe
Expand All @@ -142,7 +144,7 @@ def get_language_details(query: str, failsafe: Optional[bool] = False) -> Dict:

"""

if query.isalpha() and (2 <= len(query) <= 3):
if query.isalpha() and (2 <= len(query) <= 3): # noqa: PLR2004
# possibility of iso-639 code
adjusted_query = query
native_query = query
Expand Down
2 changes: 1 addition & 1 deletion src/zimscraperlib/image/convertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def convert_image(
fmt = format_for(dst)
with PIL.Image.open(src) as image:
if image.mode == "RGBA" and fmt in ALPHA_NOT_SUPPORTED or colorspace:
image = image.convert(colorspace or "RGB")
image = image.convert(colorspace or "RGB") # noqa: PLW2901
save_image(image, dst, fmt, **params)


Expand Down
30 changes: 15 additions & 15 deletions src/zimscraperlib/image/optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ def ensure_matches(
def optimize_png(
src: Union[pathlib.Path, io.BytesIO],
dst: Optional[pathlib.Path] = None,
reduce_colors: Optional[bool] = False,
reduce_colors: Optional[bool] = False, # noqa: FBT002
max_colors: Optional[int] = 256,
fast_mode: Optional[bool] = True,
remove_transparency: Optional[bool] = False,
fast_mode: Optional[bool] = True, # noqa: FBT002
remove_transparency: Optional[bool] = False, # noqa: FBT002
background_color: Optional[Tuple[int, int, int]] = (255, 255, 255),
**options,
**options, # noqa: ARG001
) -> Union[pathlib.Path, io.BytesIO]:
"""method to optimize PNG files using a pure python external optimizer

Expand Down Expand Up @@ -101,9 +101,9 @@ def optimize_jpeg(
src: Union[pathlib.Path, io.BytesIO],
dst: Optional[pathlib.Path] = None,
quality: Optional[int] = 85,
fast_mode: Optional[bool] = True,
keep_exif: Optional[bool] = True,
**options,
fast_mode: Optional[bool] = True, # noqa: FBT002
keep_exif: Optional[bool] = True, # noqa: FBT002
**options, # noqa: ARG001
) -> Union[pathlib.Path, io.BytesIO]:
"""method to optimize JPEG files using a pure python external optimizer
quality: JPEG quality (integer between 1 and 100)
Expand All @@ -130,7 +130,7 @@ def optimize_jpeg(
had_exif = True

# only use progressive if file size is bigger
use_progressive_jpg = orig_size > 10240 # 10KiB
use_progressive_jpg = orig_size > 10240 # 10KiB # noqa: PLR2004

if fast_mode:
quality_setting = quality
Expand Down Expand Up @@ -168,10 +168,10 @@ def optimize_jpeg(
def optimize_webp(
src: Union[pathlib.Path, io.BytesIO],
dst: Optional[pathlib.Path] = None,
lossless: Optional[bool] = False,
lossless: Optional[bool] = False, # noqa: FBT002
quality: Optional[int] = 60,
method: Optional[int] = 6,
**options,
**options, # noqa: ARG001
) -> Union[pathlib.Path, io.BytesIO]:
"""method to optimize WebP using Pillow options
lossless: Whether to use lossless compression (boolean)
Expand Down Expand Up @@ -213,10 +213,10 @@ def optimize_gif(
dst: pathlib.Path,
optimize_level: Optional[int] = 1,
lossiness: Optional[int] = None,
interlace: Optional[bool] = True,
no_extensions: Optional[bool] = True,
interlace: Optional[bool] = True, # noqa: FBT002
no_extensions: Optional[bool] = True, # noqa: FBT002
max_colors: Optional[int] = None,
**options,
**options, # noqa: ARG001
) -> pathlib.Path:
"""method to optimize GIFs using gifsicle >= 1.92
optimize_level: Optimization level;
Expand Down Expand Up @@ -264,8 +264,8 @@ def optimize_gif(
def optimize_image(
src: pathlib.Path,
dst: pathlib.Path,
delete_src: Optional[bool] = False,
convert: Optional[Union[bool, str]] = False,
delete_src: Optional[bool] = False, # noqa: FBT002
convert: Optional[Union[bool, str]] = False, # noqa: FBT002
**options,
) -> bool:
"""Optimize image, automatically selecting correct optimizer
Expand Down
24 changes: 12 additions & 12 deletions src/zimscraperlib/image/presets.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class WebpLow:
ext = "webp"
mimetype = f"{preset_type}/webp"

options = {
options = { # noqa: RUF012
"lossless": False,
"quality": 40,
"method": 6,
Expand All @@ -37,7 +37,7 @@ class WebpMedium:
ext = "webp"
mimetype = f"{preset_type}/webp"

options = {
options = { # noqa: RUF012
"lossless": False,
"quality": 50,
"method": 6,
Expand All @@ -56,7 +56,7 @@ class WebpHigh:
ext = "webp"
mimetype = f"{preset_type}/webp"

options = {
options = { # noqa: RUF012
"lossless": False,
"quality": 90,
"method": 6,
Expand All @@ -77,7 +77,7 @@ class GifLow:
ext = "gif"
mimetype = f"{preset_type}/gif"

options = {
options = { # noqa: RUF012
"optimize_level": 3,
"max_colors": 256,
"lossiness": 80,
Expand All @@ -100,7 +100,7 @@ class GifMedium:
ext = "gif"
mimetype = f"{preset_type}/gif"

options = {
options = { # noqa: RUF012
"optimize_level": 3,
"lossiness": 20,
"no_extensions": True,
Expand All @@ -122,7 +122,7 @@ class GifHigh:
ext = "gif"
mimetype = f"{preset_type}/gif"

options = {
options = { # noqa: RUF012
"optimize_level": 2,
"lossiness": None,
"no_extensions": True,
Expand All @@ -141,7 +141,7 @@ class PngLow:
ext = "png"
mimetype = f"{preset_type}/png"

options = {
options = { # noqa: RUF012
"reduce_colors": True,
"remove_transparency": False,
"max_colors": 256,
Expand All @@ -160,7 +160,7 @@ class PngMedium:
ext = "png"
mimetype = f"{preset_type}/png"

options = {
options = { # noqa: RUF012
"reduce_colors": False,
"remove_transparency": False,
"fast_mode": False,
Expand All @@ -178,7 +178,7 @@ class PngHigh:
ext = "png"
mimetype = f"{preset_type}/png"

options = {
options = { # noqa: RUF012
"reduce_colors": False,
"remove_transparency": False,
"fast_mode": True,
Expand All @@ -197,7 +197,7 @@ class JpegLow:
ext = "png"
mimetype = f"{preset_type}/png"

options = {
options = { # noqa: RUF012
"quality": 45,
"keep_exif": False,
"fast_mode": True,
Expand All @@ -216,7 +216,7 @@ class JpegMedium:
ext = "jpg"
mimetype = f"{preset_type}/jpeg"

options = {
options = { # noqa: RUF012
"quality": 65,
"keep_exif": False,
"fast_mode": True,
Expand All @@ -235,7 +235,7 @@ class JpegHigh:
ext = "jpg"
mimetype = f"{preset_type}/jpeg"

options = {
options = { # noqa: RUF012
"quality": 80,
"keep_exif": True,
"fast_mode": True,
Expand Down
13 changes: 9 additions & 4 deletions src/zimscraperlib/image/probing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


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

Expand All @@ -22,7 +22,9 @@ def rgb_to_hex(r: int, g: int, b: int) -> str:

def solarize(r: int, g: int, b: int) -> Tuple[int, int, int]:
# calculate solarized color for main
h, l, s = colorsys.rgb_to_hls(float(r) / 256, float(g) / 256, float(b) / 256)
h, l, s = colorsys.rgb_to_hls( # noqa: E741
float(r) / 256, float(g) / 256, float(b) / 256
)
r2, g2, b2 = (int(x * 256) for x in colorsys.hls_to_rgb(h, 0.95, s))
return r2, g2, b2

Expand All @@ -48,13 +50,16 @@ def is_hex_color(text: str) -> bool:
return re.search(r"^#(?:[0-9a-fA-F]{3}){1,2}$", text)


def format_for(src: Union[pathlib.Path, io.BytesIO], from_suffix: bool = True) -> str:
def format_for(
src: Union[pathlib.Path, io.BytesIO],
from_suffix: bool = True, # noqa: FBT001, FBT002
) -> str:
"""Pillow format of a given filename, either Pillow-detected or from suffix"""
if not from_suffix:
with PIL.Image.open(src) as img:
return img.format

from PIL.Image import EXTENSION as ext_fmt_map
from PIL.Image import EXTENSION as ext_fmt_map # noqa: N811
from PIL.Image import init as init_pil

init_pil()
Expand Down
10 changes: 7 additions & 3 deletions src/zimscraperlib/image/transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def resize_image(
height: Optional[int] = None,
dst: Optional[Union[pathlib.Path, io.BytesIO]] = None,
method: Optional[str] = "width",
allow_upscaling: Optional[bool] = True,
allow_upscaling: Optional[bool] = True, # noqa: FBT002
**params: Optional[dict],
) -> None:
"""resize an image to requested dimensions
Expand All @@ -34,9 +34,13 @@ def resize_image(
if allow_upscaling:
height_width_ratio = float(image.size[1]) / float(image.size[0])
if image.size[0] < width:
image = image.resize((width, int(width * height_width_ratio)))
image = image.resize( # noqa: PLW2901
(width, int(width * height_width_ratio))
)
if height and image.size[1] < height:
image = image.resize((int(height / height_width_ratio), height))
image = image.resize( # noqa: PLW2901
(int(height / height_width_ratio), height)
)

# resize using the requested method
if method == "width":
Expand Down
2 changes: 1 addition & 1 deletion src/zimscraperlib/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def handle_user_provided_file(
source: Optional[Union[pathlib.Path, str]] = None,
dest: Optional[pathlib.Path] = None,
in_dir: Optional[pathlib.Path] = None,
nocopy: bool = False,
nocopy: bool = False, # noqa: FBT001, FBT002
) -> Union[pathlib.Path, None]:
"""path to downloaded or copied a user provided file (URL or path)

Expand Down
6 changes: 3 additions & 3 deletions src/zimscraperlib/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
VERBOSE_DEPENDENCIES = ["urllib3", "PIL", "boto3", "botocore", "s3transfer"]


def getLogger(
def getLogger( # noqa: N802
name: str,
level: Optional[int] = logging.INFO,
console: Optional[io.TextIOBase] = sys.stdout,
log_format: Optional[str] = DEFAULT_FORMAT,
file: Optional[pathlib.Path] = False,
file: Optional[pathlib.Path] = False, # noqa: FBT002
file_level: Optional[int] = None,
file_format: Optional[str] = None,
file_max: Optional[int] = 2**20,
file_nb_backup: Optional[int] = 1,
deps_level: Optional[int] = logging.WARNING,
deps_level: Optional[int] = logging.WARNING, # noqa: ARG001
additional_deps: Optional[Iterable] = None,
):
"""configured logger for most usages
Expand Down
Loading