[ADD] tests: image size checker
closes odoo/documentation#4100
X-original-commit: 380a5499de
Signed-off-by: Antoine Vandevenne (anv) <anv@odoo.com>
Co-authored-by: Antoine Vandevenne (anv) <anv@odoo.com>
This commit is contained in:
parent
4314b73e2c
commit
b5140e2d22
@ -1,6 +1,34 @@
|
||||
from pathlib import Path
|
||||
|
||||
import sphinxlint
|
||||
|
||||
|
||||
MAX_IMAGE_SIZES = { # in bytes
|
||||
'.png': 505000,
|
||||
'.gif': 2100000,
|
||||
}
|
||||
|
||||
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.
|
||||
"""
|
||||
print(f"{file}:{line or 0}: {msg} ({checker_name})")
|
||||
|
||||
|
||||
def check_image_size(file):
|
||||
""" Check that images are not larger than the maximum file size allowed for their extension. """
|
||||
file_path = Path(file)
|
||||
file_size = file_path.stat().st_size
|
||||
max_size = MAX_IMAGE_SIZES.get(file_path.suffix)
|
||||
if max_size and file_size > max_size:
|
||||
log_error(
|
||||
file_path,
|
||||
0,
|
||||
f"the file has a size of {round(file_size / 10**6, 2)} MB, larger than the maximum"
|
||||
f" allowed size of {round(max_size / 10**6, 2)} MB; compress it with pngquant",
|
||||
'image-size',
|
||||
)
|
||||
|
||||
|
||||
@sphinxlint.checker('')
|
||||
def check_file_extensions(file, lines, options=None):
|
||||
""" Check that there is no file without extension. """
|
||||
|
@ -1,5 +1,6 @@
|
||||
import re
|
||||
import sys
|
||||
from itertools import chain
|
||||
from unittest.mock import patch
|
||||
|
||||
import sphinxlint
|
||||
@ -14,6 +15,13 @@ CUSTOM_RST_DIRECTIVES = [
|
||||
'tab', 'tabs', 'group-tab', 'code-tab', # sphinx_tabs
|
||||
]
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
"""
|
||||
The following checkers are selected.
|
||||
|
||||
@ -68,4 +76,5 @@ if __name__ == '__main__':
|
||||
'sphinxlint.three_dot_directive_re',
|
||||
re.compile(rf'\.\.\. {sphinxlint.ALL_DIRECTIVES}::'),
|
||||
):
|
||||
run_additional_checks()
|
||||
sys.exit(sphinxlint.main())
|
||||
|
Loading…
Reference in New Issue
Block a user