1- import sys
2- import time
1+ from enum import Enum
2+ from typing import List
3+ import xml .etree .ElementTree as et
4+ from pathlib import Path
35
46
57# pycharm complains that build_assets is an unresolved ref
810from build_assets import util
911
1012
13+ class SVG_STATUS_CODE (Enum ):
14+ """
15+ The status codes to check for in post_check_svgs_comment.yml
16+ """
17+ NO_SVG = 0 # action: do nothing
18+ SVG_OK = 1 # action: let user know their svgs are fine
19+
20+
1121def main ():
1222 """
1323 Check the quality of the svgs.
@@ -23,14 +33,68 @@ def main():
2333
2434 if len (svgs ) == 0 :
2535 print ("No SVGs to check, ending script." )
26- return
36+ err_messages = SVG_STATUS_CODE .NO_SVG .value
37+ else :
38+ err_messages = check_svgs (svgs )
2739
28- err_messages = util .check_svgs (svgs )
29- filehandler .write_to_file ("./svg_err_messages.txt" , err_messages )
40+ filehandler .write_to_file ("./svg_err_messages.txt" , str (err_messages ))
3041 print ("Task completed." )
3142 except Exception as e :
3243 util .exit_with_err (e )
3344
3445
46+ def check_svgs (svg_file_paths : List [Path ]):
47+ """
48+ Check the width, height, viewBox and style of each svgs passed in.
49+ The viewBox must be '0 0 128 128'.
50+ If the svg has a width and height attr, ensure it's '128px'.
51+ The style must not contain any 'fill' declarations.
52+ If any error is found, they will be thrown.
53+ :param: svg_file_paths, the file paths to the svg to check for.
54+ :return: None if there no errors. If there is, return a JSON.stringified
55+ list with the error messages in it.
56+ """
57+ # batch err messages together so user can fix everything at once
58+ err_msgs = []
59+ for svg_path in svg_file_paths :
60+ tree = et .parse (svg_path )
61+ root = tree .getroot ()
62+ namespace = "{http://www.w3.org/2000/svg}"
63+ err_msg = [f"{ svg_path } :" ]
64+
65+ if root .tag != f"{ namespace } svg" :
66+ err_msg .append (f"-root is '{ root .tag } '. Root must be an 'svg' element" )
67+
68+ if root .get ("viewBox" ) != "0 0 128 128" :
69+ err_msg .append ("-'viewBox' is not '0 0 128 128' -> Set it or scale the file using https://www.iloveimg.com/resize-image/resize-svg" )
70+
71+ acceptable_size = [None , "128px" , "128" ]
72+ if root .get ("height" ) not in acceptable_size :
73+ err_msg .append ("-'height' is present in svg element but is not '128' or '128px' -> Remove it or set it to '128' or '128px'" )
74+
75+ if root .get ("width" ) not in acceptable_size :
76+ err_msg .append ("-'width' is present in svg element but is not '128' or '128px' -> Remove it or set it to '128' or '128px'" )
77+
78+ if root .get ("style" ) is not None and "enable-background" in root .get ("style" ):
79+ err_msg .append ("-deprecated 'enable-background' in style attribute -> Remove it" )
80+
81+ if root .get ("x" ) is not None :
82+ err_msg .append ("-unneccessary 'x' attribute in svg element -> Remove it" )
83+
84+ if root .get ("y" ) is not None :
85+ err_msg .append ("-unneccessary 'y' attribute in svg element -> Remove it" )
86+
87+ style = root .findtext (f".//{ namespace } style" )
88+ if style != None and "fill" in style :
89+ err_msg .append ("-contains style declaration using 'fill' -> Replace classes with the 'fill' attribute instead" )
90+
91+ if len (err_msg ) > 1 :
92+ err_msgs .append ("\n " .join (err_msg ))
93+
94+ if len (err_msgs ) > 0 :
95+ return "\n \n " .join (err_msgs )
96+ return SVG_STATUS_CODE .SVG_OK .value
97+
98+
3599if __name__ == "__main__" :
36100 main ()
0 commit comments