mealie/dev/code-generation/gen_py_schema_exports.py
Hayden 9ecef4c25f
chore: file generation cleanup (#1736)
This PR does too many things :( 

1. Major refactoring of the dev/scripts and dev/code-generation folders. 

Primarily this was removing duplicate code and cleaning up some poorly written code snippets as well as making them more idempotent so then can be re-run over and over again but still maintain the same results. This is working on my machine, but I've been having problems in CI and comparing diffs so running generators in CI will have to wait. 

2. Re-Implement using the generated api routes for testing

This was a _huge_ refactor that touched damn near every test file but now we have auto-generated typed routes with inline hints and it's used for nearly every test excluding a few that use classes for better parameterization. This should greatly reduce errors when writing new tests. 

3. Minor Perf improvements for the All Recipes endpoint

  A. Removed redundant loops
  B. Uses orjson to do the encoding directly and returns a byte response instead of relying on the default 
       jsonable_encoder.

4. Fix some TS type errors that cropped up for seemingly no reason half way through the PR.

See this issue https://github.com/phillipdupuis/pydantic-to-typescript/issues/28

Basically, the generated TS type is not-correct since Pydantic will automatically fill in null fields. The resulting TS type is generated with a ? to indicate it can be null even though we _know_ that i can't be.
2022-10-18 14:49:41 -08:00

102 lines
2.5 KiB
Python

import pathlib
import re
from dataclasses import dataclass, field
from utils import PROJECT_DIR, log, render_python_template
template = """# This file is auto-generated by gen_schema_exports.py
{% for file in data.module.files %}{{ file.import_str() }}
{% endfor %}
__all__ = [
{% for file in data.module.files %}
{%- for class in file.classes -%}
"{{ class }}",
{%- endfor -%}
{%- endfor %}
]
"""
SCHEMA_PATH = PROJECT_DIR / "mealie" / "schema"
SKIP = {"static", "__pycache__"}
class PyFile:
import_path: str
"""The import path of the file"""
classes: list[str]
"""A list of classes in the file"""
def __init__(self, path: pathlib.Path):
self.import_path = path.stem
self.classes = []
self.classes = PyFile.extract_classes(path)
self.classes.sort()
def import_str(self) -> str:
"""Returns a string that can be used to import the file"""
return f"from .{self.import_path} import {', '.join(self.classes)}"
@staticmethod
def extract_classes(file_path: pathlib.Path) -> list[str]:
name = file_path.stem
if name == "__init__" or name.startswith("_"):
return []
classes = re.findall(r"(?m)^class\s(\w+)", file_path.read_text())
return classes
@dataclass
class Modules:
directory: pathlib.Path
"""The directory to search for modules"""
files: list[PyFile] = field(default_factory=list)
"""A list of files in the directory"""
def __post_init__(self):
for file in self.directory.glob("*.py"):
if file.name.startswith("_"):
continue
pfile = PyFile(file)
if len(pfile.classes) > 0:
self.files.append(pfile)
else:
log.debug(f"Skipping {file.name} as it has no classes")
def find_modules(root: pathlib.Path) -> list[Modules]:
"""Finds all the top level modules in the provided folder"""
modules: list[Modules] = []
for file in root.iterdir():
if file.is_dir() and file.name not in SKIP:
modules.append(Modules(directory=file))
return modules
def main():
modules = find_modules(SCHEMA_PATH)
for module in modules:
log.debug(f"Module: {module.directory.name}")
for file in module.files:
log.debug(f" File: {file.import_path}")
log.debug(f" Classes: [{', '.join(file.classes)}]")
render_python_template(template, module.directory / "__init__.py", {"module": module})
if __name__ == "__main__":
main()