Skip to content

Commit

Permalink
Merge pull request #51 from enthec/IconValidator
Browse files Browse the repository at this point in the history
icon path validator
  • Loading branch information
enthec-opensource authored May 30, 2024
2 parents a6ff2cf + d89df76 commit 81ed852
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 16 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/scripts/icon_path_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import json
import pathlib
import string
from typing import Final


class InvalidStructureException(Exception):
def __init__(self, msg: str):
super().__init__(msg)


class IconValidator:
def __init__(self):
self._SOURCE_DIR: Final[str] = "src"
self._TECH_DIR: Final[str] = "technologies"
self._FULL_TECH_DIR: Final[pathlib.Path] = pathlib.Path(self._SOURCE_DIR).joinpath(self._TECH_DIR)
self._IMAGES_DIR: Final[str] = "images"
self._ICONS_DIR: Final[str] = "icons"
self._FULL_IMAGES_DIR: Final[pathlib.Path] = pathlib.Path(self._SOURCE_DIR).joinpath(self._IMAGES_DIR).joinpath(self._ICONS_DIR)

def validate(self) -> None:
json_icons: set[str] = self.get_json_icons()
for file in self._FULL_IMAGES_DIR.iterdir():
if file.name not in json_icons:
raise InvalidStructureException(f"{file.name} must be used, {file} isn't used!")

def get_json_icons(self) -> set[str]:
letters: list[str] = list(string.ascii_lowercase)
letters.insert(0, '_')
json_icons: set[str] = set()

for letter in letters:
icon_path: pathlib.Path = self._FULL_TECH_DIR.joinpath(f"{letter}.json")
with icon_path.open("r", encoding="utf8") as json_file:
technologies: dict = json.load(json_file)
for tech, data in technologies.items():
if value := data.get("icon"):
json_icons.add(value)
return json_icons


if __name__ == '__main__':
IconValidator().validate()
6 changes: 3 additions & 3 deletions .github/workflows/scripts/technology_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,18 @@ def _validate(self, tech_name: str, data: Any) -> bool:
return True

def _validate_regex(self, tech_name: str, data: Any) -> bool:
if type(data) == str:
if type(data) is str:
try:
re.compile(data)
except re.error as e:
self._set_custom_error(InvalidRegexException(f"Unable to compile regex '{data}' for tech '{tech_name}', got error: {e.msg}"))
return False
elif type(data) == dict:
elif type(data) is dict:
for _, val in data.items():
valid: bool = self._validate_regex(tech_name, val)
if not valid:
return False
elif type(data) == list:
elif type(data) is list:
for item in data:
valid: bool = self._validate_regex(tech_name, item)
if not valid:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tech_matrix_prep.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-22.04
steps:
- name: checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: get current technology matrix
id: getTechMatrix
Expand Down
42 changes: 30 additions & 12 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ jobs:
runs-on: ubuntu-22.04
strategy:
matrix:
python-version: [ "3.11" ]
python-version: [ "3.12" ]
steps:
- name: checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

Expand All @@ -23,13 +23,13 @@ jobs:
needs: validate_structure
strategy:
matrix:
python-version: [ "3.11" ]
python-version: [ "3.12" ]
steps:
- name: checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

Expand All @@ -41,13 +41,13 @@ jobs:
needs: validate_structure
strategy:
matrix:
python-version: [ "3.11" ]
python-version: [ "3.12" ]
steps:
- name: checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

Expand All @@ -65,17 +65,35 @@ jobs:
max-parallel: 20
matrix:
file_name: ${{ fromJson(needs.tech_matrix_prep.outputs.technologies) }}
python-version: [ "3.11" ]
python-version: [ "3.12" ]
steps:
- name: checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: run tech validator
run: python3 .github/workflows/scripts/technology_validator.py
env:
TECH_FILE_NAME: ${{ matrix.file_name }}

validate_icon_path:
runs-on: ubuntu-22.04
needs: validate_techs
strategy:
matrix:
python-version: [ "3.12" ]
steps:
- name: checkout repository
uses: actions/checkout@v4

- name: set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: run category validator
run: python3 .github/workflows/scripts/icon_path_validator.py

0 comments on commit 81ed852

Please sign in to comment.