mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-07-24 15:49:42 +02:00
lang: options updates + gen utils cleanup (#1520)
* generate new langs * add to nuxt * cleanup generator code * additional cleanups
This commit is contained in:
parent
5fca94dd45
commit
6649ccf224
8 changed files with 119 additions and 94 deletions
|
@ -1,3 +1,4 @@
|
|||
import logging
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
@ -5,9 +6,15 @@ from pathlib import Path
|
|||
import black
|
||||
import isort
|
||||
from jinja2 import Template
|
||||
from rich.logging import RichHandler
|
||||
|
||||
FORMAT = "%(message)s"
|
||||
logging.basicConfig(level=logging.INFO, format=FORMAT, datefmt="[%X]", handlers=[RichHandler()])
|
||||
|
||||
log = logging.getLogger("rich")
|
||||
|
||||
|
||||
def render_python_template(template_file: Path | str, dest: Path, data: dict) -> str:
|
||||
def render_python_template(template_file: Path | str, dest: Path, data: dict):
|
||||
"""Render and Format a Jinja2 Template for Python Code"""
|
||||
if isinstance(template_file, Path):
|
||||
tplt = Template(template_file.read_text())
|
||||
|
@ -37,7 +44,6 @@ class CodeSlicer:
|
|||
|
||||
def push_line(self, string: str) -> None:
|
||||
self._next_line = self._next_line or self.start + 1
|
||||
print(self.indentation)
|
||||
self.text.insert(self._next_line, self.indentation + string + "\n")
|
||||
self._next_line += 1
|
||||
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
from pathlib import Path
|
||||
|
||||
from _gen_utils import log
|
||||
from jinja2 import Template
|
||||
from pydantic2ts import generate_typescript_defs
|
||||
from rich import print
|
||||
|
||||
# ============================================================
|
||||
# Global Compoenents Generator
|
||||
|
@ -99,26 +99,24 @@ def generate_typescript_types() -> None:
|
|||
generate_typescript_defs(path_as_module, str(out_path), exclude=("MealieModel")) # type: ignore
|
||||
except Exception as e:
|
||||
failed_modules.append(module)
|
||||
print("\nModule Errors:", module, "-----------------") # noqa
|
||||
print(e) # noqa
|
||||
print("Finished Module Errors:", module, "-----------------\n") # noqa
|
||||
log.error(f"Module Error: {e}") # noqa
|
||||
|
||||
print("\n📁 Skipped Directories:") # noqa
|
||||
log.info("\n📁 Skipped Directories:") # noqa
|
||||
for skipped_dir in skipped_dirs:
|
||||
print(" 📁", skipped_dir.name) # noqa
|
||||
log.info(" 📁", skipped_dir.name) # noqa
|
||||
|
||||
print("📄 Skipped Files:") # noqa
|
||||
log.info("📄 Skipped Files:") # noqa
|
||||
for f in skipped_files:
|
||||
print(" 📄", f.name) # noqa
|
||||
log.info(" 📄", f.name) # noqa
|
||||
|
||||
print("❌ Failed Modules:") # noqa
|
||||
log.error("❌ Failed Modules:") # noqa
|
||||
for f in failed_modules:
|
||||
print(" ❌", f.name) # noqa
|
||||
log.error(" ❌", f.name) # noqa
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("\n-- Starting Global Components Generator --") # noqa
|
||||
log.info("\n-- Starting Global Components Generator --") # noqa
|
||||
generate_global_components_types()
|
||||
|
||||
print("\n-- Starting Pydantic To Typescript Generator --") # noqa
|
||||
log.info("\n-- Starting Pydantic To Typescript Generator --") # noqa
|
||||
generate_typescript_types()
|
||||
|
|
|
@ -3,9 +3,9 @@ import pathlib
|
|||
import _static
|
||||
import dotenv
|
||||
import requests
|
||||
from _gen_utils import log
|
||||
from jinja2 import Template
|
||||
from requests import Response
|
||||
from rich import print
|
||||
|
||||
from mealie.schema._mealie import MealieModel
|
||||
|
||||
|
@ -13,6 +13,10 @@ BASE = pathlib.Path(__file__).parent.parent.parent
|
|||
|
||||
API_KEY = dotenv.get_key(BASE / ".env", "CROWDIN_API_KEY")
|
||||
|
||||
if API_KEY is None or API_KEY == "":
|
||||
log.info("CROWDIN_API_KEY is not set")
|
||||
exit(1)
|
||||
|
||||
NAMES = {
|
||||
"en-US": "American English",
|
||||
"en-GB": "British English",
|
||||
|
@ -55,6 +59,7 @@ export const LOCALES = [{% for locale in locales %}
|
|||
progress: {{ locale.progress }},
|
||||
},{% endfor %}
|
||||
]
|
||||
|
||||
"""
|
||||
|
||||
|
||||
|
@ -122,22 +127,57 @@ class CrowdinApi:
|
|||
return response.json()
|
||||
|
||||
|
||||
def main():
|
||||
print("Starting...") # noqa
|
||||
from pathlib import Path
|
||||
|
||||
if API_KEY is None:
|
||||
print("CROWDIN_API_KEY is not set") # noqa
|
||||
return
|
||||
from _gen_utils import inject_inline
|
||||
from _static import CodeKeys
|
||||
|
||||
PROJECT_DIR = Path(__file__).parent.parent.parent
|
||||
|
||||
|
||||
datetime_dir = PROJECT_DIR / "frontend" / "lang" / "dateTimeFormats"
|
||||
locales_dir = PROJECT_DIR / "frontend" / "lang" / "messages"
|
||||
nuxt_config = PROJECT_DIR / "frontend" / "nuxt.config.js"
|
||||
|
||||
"""
|
||||
This snippet walks the message and dat locales directories and generates the import information
|
||||
for the nuxt.config.js file and automatically injects it into the nuxt.config.js file. Note that
|
||||
the code generation ID is hardcoded into the script and required in the nuxt config.
|
||||
"""
|
||||
|
||||
|
||||
def inject_nuxt_values():
|
||||
all_date_locales = [
|
||||
f'"{match.stem}": require("./lang/dateTimeFormats/{match.name}"),' for match in datetime_dir.glob("*.json")
|
||||
]
|
||||
|
||||
all_langs = []
|
||||
for match in locales_dir.glob("*.json"):
|
||||
lang_string = f'{{ code: "{match.stem}", file: "{match.name}" }},'
|
||||
all_langs.append(lang_string)
|
||||
|
||||
log.info(f"injecting locales into nuxt config -> {nuxt_config}")
|
||||
inject_inline(nuxt_config, CodeKeys.nuxt_local_messages, all_langs)
|
||||
inject_inline(nuxt_config, CodeKeys.nuxt_local_dates, all_date_locales)
|
||||
|
||||
|
||||
def generate_locales_ts_file():
|
||||
api = CrowdinApi("")
|
||||
models = api.get_languages()
|
||||
tmpl = Template(LOCALE_TEMPLATE)
|
||||
rendered = tmpl.render(locales=models)
|
||||
|
||||
log.info(f"generating locales ts file -> {_static.CodeDest.use_locales}")
|
||||
with open(_static.CodeDest.use_locales, "w") as f:
|
||||
f.write(rendered) # type:ignore
|
||||
|
||||
print("Finished...") # noqa
|
||||
|
||||
def main():
|
||||
generate_locales_ts_file()
|
||||
|
||||
inject_nuxt_values()
|
||||
|
||||
log.info("finished code generation")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -1,39 +0,0 @@
|
|||
from pathlib import Path
|
||||
|
||||
from _gen_utils import inject_inline
|
||||
from _static import CodeKeys
|
||||
|
||||
PROJECT_DIR = Path(__file__).parent.parent.parent
|
||||
|
||||
|
||||
datetime_dir = PROJECT_DIR / "frontend" / "lang" / "dateTimeFormats"
|
||||
locales_dir = PROJECT_DIR / "frontend" / "lang" / "messages"
|
||||
nuxt_config = PROJECT_DIR / "frontend" / "nuxt.config.js"
|
||||
|
||||
"""
|
||||
This snippet walks the message and dat locales directories and generates the import information
|
||||
for the nuxt.config.js file and automatically injects it into the nuxt.config.js file. Note that
|
||||
the code generation ID is hardcoded into the script and required in the nuxt config.
|
||||
"""
|
||||
|
||||
|
||||
def main(): # sourcery skip: list-comprehension
|
||||
print("Starting...")
|
||||
|
||||
all_date_locales = []
|
||||
for match in datetime_dir.glob("*.json"):
|
||||
all_date_locales.append(f'"{match.stem}": require("./lang/dateTimeFormats/{match.name}"),')
|
||||
|
||||
all_langs = []
|
||||
for match in locales_dir.glob("*.json"):
|
||||
lang_string = f'{{ code: "{match.stem}", file: "{match.name}" }},'
|
||||
all_langs.append(lang_string)
|
||||
|
||||
inject_inline(nuxt_config, CodeKeys.nuxt_local_messages, all_langs)
|
||||
inject_inline(nuxt_config, CodeKeys.nuxt_local_dates, all_date_locales)
|
||||
|
||||
print("Finished...")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
|
@ -1,4 +1,4 @@
|
|||
from _gen_utils import render_python_template
|
||||
from _gen_utils import log, render_python_template
|
||||
from _static import PROJECT_DIR
|
||||
|
||||
template = """# GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
@ -10,13 +10,12 @@ SCHEMA_PATH = PROJECT_DIR / "mealie" / "schema"
|
|||
|
||||
|
||||
def generate_init_files() -> None:
|
||||
|
||||
for schema in SCHEMA_PATH.iterdir():
|
||||
if not schema.is_dir():
|
||||
print(f"Skipping {schema}")
|
||||
log.info(f"Skipping {schema}")
|
||||
continue
|
||||
|
||||
print(f"Generating {schema}")
|
||||
log.info(f"Generating {schema}")
|
||||
init_file = schema.joinpath("__init__.py")
|
||||
|
||||
module_files = [
|
||||
|
@ -26,9 +25,9 @@ def generate_init_files() -> None:
|
|||
|
||||
|
||||
def main():
|
||||
print("Starting...")
|
||||
log.info("Starting...")
|
||||
generate_init_files()
|
||||
print("Finished...")
|
||||
log.info("Finished...")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -1,15 +1,13 @@
|
|||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
from _gen_utils import render_python_template
|
||||
from _gen_utils import log, render_python_template
|
||||
from slugify import slugify
|
||||
|
||||
CWD = Path(__file__).parent
|
||||
|
||||
TEMPLATE = CWD / "templates" / "test_data.py.j2"
|
||||
|
||||
TEST_DATA = CWD.parent.parent / "tests" / "data"
|
||||
|
||||
GENERATED = CWD / "generated"
|
||||
|
||||
|
||||
|
@ -99,7 +97,7 @@ def rename_non_compliant_paths():
|
|||
|
||||
|
||||
def main():
|
||||
print("Starting Template Generation")
|
||||
log.info("Starting Template Generation")
|
||||
|
||||
rename_non_compliant_paths()
|
||||
|
||||
|
@ -117,7 +115,7 @@ def main():
|
|||
{"children": all_children},
|
||||
)
|
||||
|
||||
print("Finished Template Generation")
|
||||
log.info("Finished Template Generation")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue