mealie/dev/code-generation/gen_py_pytest_data_paths.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

116 lines
2.7 KiB
Python

from dataclasses import dataclass
from pathlib import Path
from slugify import slugify
from utils import render_python_template
CWD = Path(__file__).parent
TEMPLATE = CWD / "templates" / "test_data.py.j2"
TEST_DATA = CWD.parent.parent / "tests" / "data"
GENERATED = CWD / "generated"
@dataclass
class TestDataPath:
var: str
path: str
@classmethod
def from_path(cls, path: Path):
var = str(path)
var = var.replace(str(TEST_DATA), "")
rel_path = var.removeprefix("/")
# Remove any file extension
var = var.split(".")[0]
var = var.replace("'", "")
var = slugify(var, separator="_")
return cls(var, rel_path)
@dataclass
class DataDir:
name: str
path: Path
children: list[TestDataPath]
def get_data_paths(path: Path) -> list[DataDir]:
"""
Recursively walks the given path and returns a list of TestDataPaths
"""
def recursive_test_paths(p: Path) -> list[TestDataPath]:
test_paths = []
for child in p.iterdir():
if child.is_dir():
test_paths += recursive_test_paths(child)
else:
test_paths.append(TestDataPath.from_path(child))
return [x for x in test_paths if not None]
data_paths = []
for p in path.iterdir():
if p.is_dir():
data_paths.append(DataDir(p.name, p, recursive_test_paths(p)))
return data_paths
def rename_non_compliant_paths():
"""
Recursively itterates through a directory and renames all files/folders to be
kabab case.
"""
ignore_files = ["DS_Store", ".gitkeep"]
ignore_extensions = [".pyc", ".pyo", ".py"]
def recursive_rename(p: Path):
for child in p.iterdir():
if str(child).startswith("."):
continue
if child.suffix in ignore_extensions:
continue
if child.name in ignore_files:
continue
if child.is_dir():
recursive_rename(child)
else:
new_name = slugify(child.stem)
extension = child.suffix
if new_name != child.name:
child.rename(child.parent / f"{new_name}{extension}")
recursive_rename(TEST_DATA)
def main():
rename_non_compliant_paths()
GENERATED.mkdir(exist_ok=True)
data_dirs = get_data_paths(TEST_DATA)
all_children = [x.children for x in data_dirs]
# Flatten list of lists
all_children = [item for sublist in all_children for item in sublist]
render_python_template(
TEMPLATE,
GENERATED / "__init__.py",
{"children": all_children},
)
if __name__ == "__main__":
main()