Skip to content

Commit

Permalink
Add dynamic print version and fix little bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrochar committed Feb 17, 2024
1 parent 1434446 commit 4fdcb16
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 24 deletions.
14 changes: 11 additions & 3 deletions blitz/api/blitz_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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(
(
Expand Down
56 changes: 35 additions & 21 deletions blitz/cli/commands/start.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import time
from semver import Version
import typer
Expand All @@ -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()

Expand All @@ -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
Expand All @@ -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"
)
129 changes: 129 additions & 0 deletions blitz/cli/utils.py
Original file line number Diff line number Diff line change
@@ -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]")

0 comments on commit 4fdcb16

Please sign in to comment.