[ADD] tests: image compression and resource file name checkers

task-2801043

closes odoo/documentation#4870

Signed-off-by: Antoine Vandevenne (anv) <anv@odoo.com>
Co-authored-by: Antoine Vandevenne (anv) <anv@odoo.com>
This commit is contained in:
Victor Feyens 2023-04-17 13:46:16 +00:00 committed by Antoine Vandevenne (anv)
parent 8eec0e7634
commit f76c28867d
5 changed files with 48 additions and 5 deletions

View File

@ -93,4 +93,5 @@ review:
@read -p "Enter content path: " path; read -p "Enter max line length (default: 100): " line_length; \
if [ -z "$$path" ]; then echo "Error: Path cannot be empty"; exit 1; fi; \
if [ -z "$$line_length" ]; then line_length=100; fi; \
python tests/main.py -e line-too-long -e early-line-breaks --max-line-length=$$line_length $(SOURCE_DIR)/$$path
export REVIEW=1; \
python tests/main.py --max-line-length=$$line_length $(SOURCE_DIR)/$$path

View File

@ -1,5 +1,6 @@
from pathlib import Path
from PIL import Image
import sphinxlint
@ -7,6 +8,10 @@ MAX_IMAGE_SIZES = { # in bytes
'.png': 505000,
'.gif': 2100000,
}
MODE_TO_BPP = {
'1': 1, 'L': 8, 'P': 8, 'RGB': 24, 'RGBA': 32, 'CMYK': 32, 'YCbCr': 24, 'I': 32, 'F': 32
}
def log_error(file, line, msg, checker_name):
""" Log an error in sphinx-lint's log format to ease the processing of linting errors on Runbot.
@ -28,6 +33,29 @@ def check_image_size(file):
'image-size',
)
def check_image_color_depth(file):
""" Check that PNG images are compressed to 8-bit color depth with PNGQuant. """
file_path = Path(file)
if file_path.suffix.lower() == '.png':
data = Image.open(file)
bpp = MODE_TO_BPP[data.mode]
if bpp > 8:
log_error(
file_path,
0,
f"the file has a color depth of {bpp} instead of 8; compress it with pngquant",
'image-color-depth'
)
def check_resource_file_name(file_path):
""" Check that resource file names use hyphens rather than underscores. """
if '_' in file_path.split('/')[-1]:
log_error(
file_path,
0,
"the resource file should have hyphens rather than underscores",
'resource-file-name'
)
@sphinxlint.checker('')
def check_file_extensions(file, lines, options=None):

View File

@ -128,7 +128,7 @@ def check_early_line_breaks(file, lines, options=None):
'#.': lambda x: x.split('#. ', 1)[0],
'default': lambda x: x.split(' ', 1)[0]
}
return next_line_dict.get(next_line_.lstrip()[:2], next_line_dict["default"])(
return next_line_dict.get(next_line_.lstrip()[:2], next_line_dict['default'])(
next_line_.lstrip()
)
else:
@ -141,7 +141,7 @@ def check_early_line_breaks(file, lines, options=None):
and is_valid_line(next_line, ('+', '|', '- ', '* ', '#. '))
):
current_line_remaining_space = options.max_line_length - len(line)
next_line_first_word = get_next_line_first_word(next_line)
next_line_first_word = get_next_line_first_word(next_line).rstrip()
if current_line_remaining_space > len(next_line_first_word):
yield lno + 1, f"consider moving \"{next_line_first_word}\" to line {lno + 1}"

View File

@ -1,3 +1,5 @@
import argparse
import os
import re
import sys
from itertools import chain
@ -15,11 +17,18 @@ CUSTOM_RST_DIRECTIVES = [
'tab', 'tabs', 'group-tab', 'code-tab', # sphinx_tabs
]
ADDITIONAL_CHECKERS = [
checkers.resource_files.check_image_size,
checkers.resource_files.check_resource_file_name,
]
def run_additional_checks(argv=None):
_enabled_checkers, args = sphinxlint.parse_args(argv)
for path in chain.from_iterable(sphinxlint.walk(path, args.ignore) for path in args.paths):
checkers.resource_files.check_image_size(path)
if not path.endswith('.rst'):
for checker in ADDITIONAL_CHECKERS:
checker(path)
"""
@ -75,5 +84,9 @@ if __name__ == '__main__':
'sphinxlint.three_dot_directive_re',
re.compile(rf'\.\.\. {sphinxlint.ALL_DIRECTIVES}::'),
):
run_additional_checks()
parser = argparse.ArgumentParser()
if os.getenv('REVIEW') == '1': # Enable checkers for `make review`.
setattr(sphinxlint.check_line_too_long, 'enabled', True)
setattr(checkers.rst_style.check_early_line_breaks, 'enabled', True)
ADDITIONAL_CHECKERS.extend([checkers.resource_files.check_image_color_depth])
sys.exit(sphinxlint.main())

View File

@ -1,2 +1,3 @@
Pillow==9.0.1
mock==5.0.1
sphinx-lint==0.6.7