forked from circleops/circle-ecosystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logo_validator.py
94 lines (74 loc) · 2.7 KB
/
logo_validator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Copyright (c) 2022 Circle Internet Financial Trading Company Limited.
# All rights reserved.
#
# Circle Internet Financial Trading Company Limited CONFIDENTIAL
# This file includes unpublished proprietary source code of Circle Internet
# Financial Trading Company Limited, Inc. The copyright notice above does not
# evidence any actual or intended publication of such source code. Disclosure
# of this source code or any related proprietary information is strictly
# prohibited without the express written permission of Circle Internet
# Financial Trading Company Limited.
import logging
import os
import sys
from glob import glob
from PIL import Image
from svgpathtools import svg2paths2
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(levelname)s %(message)s',
)
LOGGER = logging.getLogger(__name__)
ACCEPTABLE_FILE_EXTENSIONS = ['.jpg', '.png', '.svg']
IMAGE_SIZE = 5 * 1024 * 1024
class ValidationError(Exception):
"""Exception raised for invalid logos."""
pass
def validate_logo(logo_path: str) -> None:
"""
Validate a logo to ensure it follows the following requirements:
1. Acceptable File Types: .jpg, .png, .svg
2. Max File Size: 5 MB
2. Image Size: 200 x 200 pixels
3. Aspect Ratio: 1:1 (square)
:param logo_path: path of the logo
:return: boolean whether the logo passes requirements
"""
# 1. Check the file type
file_ext_index = logo_path.rfind('.')
file_ext = logo_path[file_ext_index:]
if file_ext not in ACCEPTABLE_FILE_EXTENSIONS:
raise ValidationError(f'Invalid file type {file_ext} for {logo_path}.')
# 2. Check the file size
size = os.path.getsize(logo_path)
if size > IMAGE_SIZE:
raise ValidationError(f'Invalid file size {(size / (1024 * 1024)):.2f} MB for {logo_path}.')
# 3. Check the image size and ratio
if file_ext == '.svg':
_, __, svg_attributes = svg2paths2(logo_path)
width, height = int(svg_attributes['width']), int(svg_attributes['height'])
else:
im = Image.open(logo_path)
width, height = im.size
if width != 200 or height != 200:
raise ValidationError(f'Invalid image size {width}x{height} for {logo_path}.')
def main():
logos = glob('catalog/**/logos/*', recursive=True)
failed = False
pass_count = 0
for logo_path in logos:
try:
validate_logo(logo_path)
pass_count += 1
except ValidationError as e:
LOGGER.error(e)
failed = True
LOGGER.info(f'PASS: {pass_count}/{len(logos)}')
if failed:
LOGGER.info('FAIL')
sys.exit(1)
else:
LOGGER.info('SUCCESS')
sys.exit()
if __name__ == '__main__':
main()