1
0
mirror of https://github.com/konpa/devicon.git synced 2025-08-16 19:44:05 +02:00

Create a monthly script that checks all svgs

This commit is contained in:
Thomas Bui
2021-01-08 12:07:47 -08:00
parent 5fea57350f
commit b7ae338d2f
8 changed files with 156 additions and 75 deletions

View File

@@ -40,15 +40,22 @@ def get_selenium_runner_args(peek_mode=False):
return parser.parse_args()
def get_check_svgs_args():
def get_check_svgs_on_pr_args():
"""
Get the commandline arguments for the chec_svgs.py.
Get the commandline arguments for the check_svgs_on_pr.py.
"""
parser = ArgumentParser(description="Check the SVGs to ensure their attributes are correct.")
parser.add_argument("icomoon_json_path",
help="The path to the icomoon.json aka the selection.json created by Icomoon",
parser = ArgumentParser(description="Check the SVGs to ensure their attributes are correct. Run whenever a PR is opened")
parser.add_argument("files_changed_json_path",
help="The path to the files.json created by the gh-action-get-changed-files@2.1.4",
action=PathResolverAction)
return parser.parse_args()
def get_check_svgs_monthly_args():
"""
Get the commandline arguments for the check_svgs_monthly.py.
"""
parser = ArgumentParser(description="Check the SVGs to ensure their attributes are correct. Run monthly.")
parser.add_argument("devicon_json_path",
help="The path to the devicon.json",
action=PathResolverAction)

View File

@@ -6,7 +6,7 @@ import os
import re
def find_new_icons(devicon_json_path: str, icomoon_json_path: str) -> List[dict]:
def find_new_icons(devicon_json_path: str, icomoon_json_path: str):
"""
Find the newly added icons by finding the difference between
the devicon.json and the icomoon.json.
@@ -14,11 +14,8 @@ def find_new_icons(devicon_json_path: str, icomoon_json_path: str) -> List[dict]
:param icomoon_json_path: a path to the iconmoon.json.
:return: a list of the new icons as JSON objects.
"""
with open(devicon_json_path) as json_file:
devicon_json = json.load(json_file)
with open(icomoon_json_path) as json_file:
icomoon_json = json.load(json_file)
devicon_json = get_json_file_content(devicon_json_path)
icomoon_json = get_json_file_content(icomoon_json_path)
new_icons = []
for icon in devicon_json:
@@ -28,7 +25,17 @@ def find_new_icons(devicon_json_path: str, icomoon_json_path: str) -> List[dict]
return new_icons
def is_not_in_icomoon_json(icon, icomoon_json) -> bool:
def get_json_file_content(json_path: str):
"""
Get the json content of the json_path.
:param: json_path, the path to the json file.
:return: a dict representing the file content.
"""
with open(json_path) as json_file:
return json.load(json_file)
def is_not_in_icomoon_json(icon, icomoon_json):
"""
Checks whether the icon's name is not in the icomoon_json.
:param icon: the icon object we are searching for.
@@ -45,7 +52,7 @@ def is_not_in_icomoon_json(icon, icomoon_json) -> bool:
def get_svgs_paths(new_icons: List[dict], icons_folder_path: str,
icon_versions_only: bool=False, as_str: bool=True) -> List[str] or List[Path]:
icon_versions_only: bool=False, as_str: bool=True):
"""
Get all the suitable svgs file path listed in the devicon.json.
:param new_icons, a list containing the info on the new icons.
@@ -199,3 +206,23 @@ def create_screenshot_folder(dir, screenshot_name: str="screenshots/"):
print(f"{screenshot_folder} already exist. Script will do nothing.")
finally:
return str(screenshot_folder)
def get_added_modified_svgs(files_changed_json_path: str):
"""
Get the svgs added and modified from the files_changed_json_path.
:param: the path to the files.json created by the gh-action-get-changed-files@2.1.4
:return: a list of the svg file paths that were added/modified in this pr.
"""
files_dict = get_json_file_content(files_changed_json_path)
svgs = []
for file in files_dict["added"]:
path = Path(file)
if path.suffix.lower() == ".svg":
svgs.append(file)
for file in files_dict["modified"]:
path = Path(file)
if path.suffix.lower() == ".svg":
svgs.append(file)
return svgs

View File

@@ -0,0 +1,53 @@
from typing import List
import xml.etree.ElementTree as et
from pathlib import Path
def check_svgs(svg_file_paths: List[Path]):
"""
Check the width, height, viewBox and style of each svgs passed in.
The viewBox must be '0 0 128 128'.
If the svg has a width and height attr, ensure it's '128px'.
The style must not contain any 'fill' declarations.
If any error is found, they will be thrown.
:param: svg_file_paths, the file paths to the svg to check for.
"""
# batch err messages together so user can fix everything at once
err_msgs = []
for svg_path in svg_file_paths:
tree = et.parse(svg_path)
root = tree.getroot()
namespace = "{http://www.w3.org/2000/svg}"
err_msg = [f"{svg_path.name}:"]
if root.tag != f"{namespace}svg":
err_msg.append(f"-root is '{root.tag}'. Root must be an 'svg' element")
if root.get("viewBox") != "0 0 128 128":
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")
acceptable_size = [None, "128px", "128"]
if root.get("height") not in acceptable_size:
err_msg.append("-'height' is present in svg element but is not '128' or '128px' -> Remove it or set it to '128' or '128px'")
if root.get("width") not in acceptable_size:
err_msg.append("-'width' is present in svg element but is not '128' or '128px' -> Remove it or set it to '128' or '128px'")
if root.get("style") is not None and "enable-background" in root.get("style"):
err_msg.append("-deprecated 'enable-background' in style attribute -> Remove it")
if root.get("x") is not None:
err_msg.append("-unneccessary 'x' attribute in svg element -> Remove it")
if root.get("y") is not None:
err_msg.append("-unneccessary 'y' attribute in svg element -> Remove it")
style = root.findtext(f".//{namespace}style")
if style != None and "fill" in style:
err_msg.append("-contains style declaration using 'fill' -> Replace classes with the 'fill' attribute instead")
if len(err_msg) > 1:
err_msgs.append("\n".join(err_msg))
if len(err_msgs) > 0:
raise Exception("Errors found in these files:\n" + "\n\n".join(err_msgs))