🚸 Add App.dict(); represent as JSON
This commit is contained in:
parent
3ebebe7fbd
commit
9becfc857d
1 changed files with 26 additions and 14 deletions
|
|
@ -2,10 +2,11 @@
|
||||||
import copy
|
import copy
|
||||||
import importlib
|
import importlib
|
||||||
import importlib.util
|
import importlib.util
|
||||||
|
import json
|
||||||
import pkgutil
|
import pkgutil
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Dict, List, Optional
|
from typing import Any, Dict, Generator, List, Optional
|
||||||
|
|
||||||
import click
|
import click
|
||||||
from aerich import Command as AerichCommand
|
from aerich import Command as AerichCommand
|
||||||
|
|
@ -218,28 +219,39 @@ class App:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
out = ""
|
return json.dumps(self.dict(), indent=2)
|
||||||
out += f"App: {self.name}\n"
|
|
||||||
out += f"Models:\n"
|
|
||||||
for model in self.models:
|
|
||||||
out += f" - {model.__name__}\n"
|
|
||||||
out += "Routes:\n"
|
|
||||||
for route in (self.routes or []):
|
|
||||||
out += f" - {route}\n"
|
|
||||||
return out
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.__repr__()
|
return self.__repr__()
|
||||||
|
|
||||||
|
def _serialize_route(self, route):
|
||||||
|
"""Convert APIRoute to JSON-serializable dict."""
|
||||||
|
return {
|
||||||
|
"path": route.path,
|
||||||
|
"name": route.name,
|
||||||
|
"methods": list(route.methods),
|
||||||
|
"endpoint": route.endpoint.__name__, # just the function name
|
||||||
|
"response_model": getattr(route, "response_model", None).__name__
|
||||||
|
if getattr(route, "response_model", None) else None,
|
||||||
|
"tags": getattr(route, "tags", None),
|
||||||
|
}
|
||||||
|
|
||||||
|
def _serialize_router(self):
|
||||||
|
return [self._serialize_route(route) for route in self.routes]
|
||||||
|
|
||||||
|
def dict(self) -> Dict[str, Any]:
|
||||||
|
return {
|
||||||
|
'models': [m.__name__ for m in self.models],
|
||||||
|
'routes': self._serialize_router(),
|
||||||
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def models(self) -> List[Model]:
|
def models(self) -> Generator[Model, None, None]:
|
||||||
models: List[Model] = []
|
|
||||||
for mod in self.model_modules:
|
for mod in self.model_modules:
|
||||||
models_mod = importlib.import_module(mod)
|
models_mod = importlib.import_module(mod)
|
||||||
for obj in models_mod.__dict__.values():
|
for obj in models_mod.__dict__.values():
|
||||||
if isinstance(obj, type) and getattr(obj, "_meta", None) is not None and obj.__name__ != 'Model':
|
if isinstance(obj, type) and getattr(obj, "_meta", None) is not None and obj.__name__ != 'Model':
|
||||||
models.append(obj)
|
yield obj
|
||||||
return models
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def routes(self):
|
def routes(self):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue