diff --git a/blitz/api/blitz_api.py b/blitz/api/blitz_api.py index 798f313..8338572 100644 --- a/blitz/api/blitz_api.py +++ b/blitz/api/blitz_api.py @@ -91,7 +91,10 @@ def _create_crud_router(self, resource: BlitzResource) -> CRUDGenerator: # Rebuild the model to include forward ref types that was not available at the time of the model creation # We need to use the model AFTER the rebuild because if not, all the relationship and cie will not be set # correctly. - types_namespace = {resource.model.__name__: resource.model for resource in self.blitz_app.resources} + types_namespace = { + resource.model.__name__: resource.model + for resource in self.blitz_app.resources + } read_model.model_rebuild(_types_namespace=types_namespace) create_model.model_rebuild(_types_namespace=types_namespace) update_model.model_rebuild(_types_namespace=types_namespace) @@ -128,6 +131,7 @@ def create_blitz_api( ) -> BlitzAPI: from blitz.ui.main import init_ui + blitz_app_version = None if blitz_app is None: blitz = BlitzCore() blitz_app_name = os.getenv("BLITZ_APP") @@ -185,8 +189,12 @@ def create_blitz_api( warnings.simplefilter("ignore", category=SAWarning) BlitzAdmin(blitz_app).mount_to(blitz_api) - print("\n[bold yellow]This is still an alpha. Please do not use in production.[/bold yellow]") - print("[bold yellow]Please report any issues on https://github.com/Paperz-org/blitz[/bold yellow]") + print( + "\n[bold yellow]This is still an alpha. Please do not use in production.[/bold yellow]" + ) + print( + "[bold yellow]Please report any issues on https://github.com/Paperz-org/blitz[/bold yellow]" + ) print( "\n".join( ( diff --git a/blitz/cli/commands/start.py b/blitz/cli/commands/start.py index 31eb66a..0226afc 100644 --- a/blitz/cli/commands/start.py +++ b/blitz/cli/commands/start.py @@ -1,3 +1,4 @@ +import contextlib import time from semver import Version import typer @@ -9,20 +10,34 @@ from uvicorn.supervisors import ChangeReload from blitz.api import create_blitz_api +from blitz.cli.utils import print_version from blitz.core import BlitzCore from blitz.settings import get_settings -from rich import print -from blitz.cli.errors import BlitzAppNotFoundError, BlitzAppVersionNotFoundError, MissingBlitzAppNameError +from blitz.cli.errors import ( + BlitzAppNotFoundError, + BlitzAppVersionNotFoundError, + MissingBlitzAppNameError, +) + +from blitz import __version__ def start_blitz( - blitz_app_name: Annotated[Optional[str], typer.Argument(help="Blitz app name")] = None, + blitz_app_name: Annotated[ + Optional[str], typer.Argument(help="Blitz app name") + ] = None, admin: Annotated[bool, typer.Option(help="Don't create admin.")] = True, - port: Annotated[int, typer.Option(help="Define the port of the server")] = get_settings().BLITZ_PORT, - config_route: Annotated[bool, typer.Option(help="Enable the blitz config route.")] = True, + port: Annotated[ + int, typer.Option(help="Define the port of the server") + ] = get_settings().BLITZ_PORT, + config_route: Annotated[ + bool, typer.Option(help="Enable the blitz config route.") + ] = True, hot_reload: Annotated[bool, typer.Option(help="Enable the hot reload.")] = True, - version: Annotated[Optional[str], typer.Option(help="Define the version of the app.")] = None, + version: Annotated[ + Optional[str], typer.Option(help="Define the version of the app.") + ] = None, ) -> None: blitz = BlitzCore() @@ -43,18 +58,10 @@ def start_blitz( except Exception: raise BlitzAppVersionNotFoundError(blitz_app, version) - # https://patorjk.com/software/taag/#p=display&f=ANSI%20Shadow&t=BLITZ%200.1.0 - print( - """[bold medium_purple1] -██████╗ ██╗ ██╗████████╗███████╗ ██████╗ ██╗ ██████╗ -██╔══██╗██║ ██║╚══██╔══╝╚══███╔╝ ██╔═████╗ ███║ ██╔═████╗ -██████╔╝██║ ██║ ██║ ███╔╝ ██║██╔██║ ╚██║ ██║██╔██║ -██╔══██╗██║ ██║ ██║ ███╔╝ ████╔╝██║ ██║ ████╔╝██║ -██████╔╝███████╗██║ ██║ ███████╗ ╚██████╔╝██╗██║██╗╚██████╔╝ -╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚══════╝ ╚═════╝ ╚═╝╚═╝╚═╝ ╚═════╝ - [/bold medium_purple1]""" - ) - time.sleep(0.3) + # In case of error we don't want to stop the startup for ascii art. + with contextlib.suppress(Exception): + print_version(__version__) + time.sleep(0.3) if hot_reload: # Need to be refacto @@ -80,7 +87,14 @@ def start_blitz( log_level="info", ) server = uvicorn.Server(server_config) - ChangeReload(server_config, target=server.run, sockets=[server_config.bind_socket()]).run() + ChangeReload( + server_config, target=server.run, sockets=[server_config.bind_socket()] + ).run() else: - blitz_api = create_blitz_api(blitz_app, enable_config_route=config_route, admin=admin) - uvicorn.run(blitz_api, host="localhost", port=port, log_config=None, log_level="warning") + + blitz_api = create_blitz_api( + blitz_app, enable_config_route=config_route, admin=admin + ) + uvicorn.run( + blitz_api, host="localhost", port=port, log_config=None, log_level="warning" + ) diff --git a/blitz/cli/utils.py b/blitz/cli/utils.py new file mode 100644 index 0000000..85c56e6 --- /dev/null +++ b/blitz/cli/utils.py @@ -0,0 +1,129 @@ +from rich import print + +ZERO = [ + " ██████╗ ", + "██╔═████╗", + "██║██╔██║", + "████╔╝██║", + "╚██████╔╝", + " ╚═════╝ ", + " ", +] +ONE = [" ██╗", "███║", "╚██║", " ██║", " ██║", " ╚═╝", " "] +TWO = [ + "██████╗ ", + "╚════██╗", + " █████╔╝", + "██╔═══╝ ", + "███████╗", + "╚══════╝", + " ", +] +THREE = [ + "██████╗ ", + "╚════██╗", + " █████╔╝", + " ╚═══██╗", + "██████╔╝", + "╚═════╝ ", + " ", +] +FOUR = [ + "██╗ ██╗", + "██║ ██║", + "███████║", + "╚════██║", + " ██║", + " ╚═╝", + " ", +] + +FIVE = [ + "███████╗", + "██╔════╝", + "███████╗", + "╚════██║", + "███████║", + "╚══════╝", + " ", +] + +SIX = [ + " ██████╗ ", + "██╔════╝ ", + "███████╗ ", + "██╔═══██╗", + "╚██████╔╝", + " ╚═════╝ ", + " ", +] + +SEVEN = [ + "███████╗", + "╚════██║", + " ██╔╝", + " ██╔╝ ", + " ██║ ", + " ╚═╝ ", + " ", +] + +HEIGHT = [ + " █████╗ ", + "██╔══██╗", + "╚█████╔╝", + "██╔══██╗", + "╚█████╔╝", + " ╚════╝ ", + " ", +] + +NINE = [ + " █████╗ ", + "██╔══██╗", + "╚██████║", + " ╚═══██║", + " █████╔╝", + " ╚════╝ ", + " ", +] + +BLITZ = [ + "██████╗ ██╗ ██╗████████╗███████╗", + "██╔══██╗██║ ██║╚══██╔══╝╚══███╔╝", + "██████╔╝██║ ██║ ██║ ███╔╝ ", + "██╔══██╗██║ ██║ ██║ ███╔╝ ", + "██████╔╝███████╗██║ ██║ ███████╗", + "╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚══════╝", + " ", +] + + +POINT = [" ", " ", " ", " ", "██╗", "╚═╝", " "] +SPACE = [" ", " ", " ", " ", " ", " ", " "] + + +def print_version(version: str) -> None: + # https://patorjk.com/software/taag/#p=display&f=ANSI%20Shadow&t=BLITZ%200.1.0 + major, minor, patch = version.split(".") + ASCII_INT_MAPPING = { + "0": ZERO, + "1": ONE, + "2": TWO, + "3": THREE, + "4": FOUR, + "5": FIVE, + "6": SIX, + "7": SEVEN, + "8": HEIGHT, + "9": NINE, + } + major_ascii = [ASCII_INT_MAPPING[i] for i in major] + minor_ascii = [ASCII_INT_MAPPING[i] for i in minor] + patch_ascii = [ASCII_INT_MAPPING[i] for i in patch] + major_list = ["".join(col) for col in zip(*major_ascii)] + minor_list = ["".join(col) for col in zip(*minor_ascii)] + patch_list = ["".join(col) for col in zip(*patch_ascii)] + + for line in zip(BLITZ, SPACE, major_list, POINT, minor_list, POINT, patch_list): + print(f"[bold medium_purple1]{''.join(line)}[/bold medium_purple1]")