36 lines
1.0 KiB
Python
36 lines
1.0 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Works around an openapi-generator python codegen bug where generated models
|
|
that use @field_validator don't import it from pydantic. Run after regenerating
|
|
generated/immich.
|
|
"""
|
|
import re
|
|
import pathlib
|
|
import sys
|
|
|
|
MODELS_DIR = pathlib.Path(__file__).resolve().parent.parent / "generated/immich/openapi_client/models"
|
|
|
|
|
|
def main():
|
|
fixed = 0
|
|
for f in MODELS_DIR.glob("*.py"):
|
|
text = f.read_text()
|
|
if "@field_validator" not in text:
|
|
continue
|
|
m = re.search(r"^from pydantic import (.*)$", text, re.M)
|
|
if not m:
|
|
continue
|
|
names = [n.strip() for n in m.group(1).split(",")]
|
|
if "field_validator" in names:
|
|
continue
|
|
names.append("field_validator")
|
|
new_line = "from pydantic import " + ", ".join(names)
|
|
text = text[: m.start()] + new_line + text[m.end() :]
|
|
f.write_text(text)
|
|
fixed += 1
|
|
print(f"Fixed {fixed} model file(s) missing the field_validator import")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|