Skip to content

Commit

Permalink
fix: Only run imports for actually used command
Browse files Browse the repository at this point in the history
Noticeably speeds up app startup (e.g. when just printing the version),
because e.g. `server` module code isn't run unnecessarily.
  • Loading branch information
alexpovel committed Sep 20, 2022
1 parent ab7ca67 commit 432311a
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions ancv/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,10 @@
locally.
"""

import logging
import os
from pathlib import Path
from typing import Optional

import structlog
import typer
from pydantic import ValidationError
from rich import print as rprint
from rich.tree import Tree
from structlog.processors import JSONRenderer, TimeStamper, add_log_level

from ancv.exceptions import ResumeConfigError
from ancv.reflection import METADATA
from ancv.visualization.templates import Template
from ancv.visualization.themes import THEMES
from ancv.visualization.translations import TRANSLATIONS
from ancv.web.server import APIHandler, FileHandler, ServerContext

app = typer.Typer(no_args_is_help=True, help=__doc__)
server_app = typer.Typer(no_args_is_help=True, help="Interacts with the web server.")
Expand All @@ -39,6 +25,11 @@ def api(
) -> None:
"""Starts the web server and serves the API."""

import os

from ancv.reflection import METADATA
from ancv.web.server import APIHandler, ServerContext

context = ServerContext(host=host, port=port, path=path)
api = APIHandler(
# https://docs.github.com/en/rest/overview/resources-in-the-rest-api#user-agent-required :
Expand Down Expand Up @@ -69,6 +60,8 @@ def file(
) -> None:
"""Starts the web server and serves a single, rendered resume file."""

from ancv.web.server import FileHandler, ServerContext

context = ServerContext(host=host, port=port, path=path)
FileHandler(file).run(context)

Expand All @@ -82,6 +75,8 @@ def render(
) -> None:
"""Locally renders the JSON resume at the given file path."""

from ancv.visualization.templates import Template

template = Template.from_file(path)
output = template.render()
print(output)
Expand All @@ -97,6 +92,11 @@ def validate(
) -> None:
"""Checks the validity of the given JSON resume without rendering."""

from pydantic import ValidationError

from ancv.exceptions import ResumeConfigError
from ancv.visualization.templates import Template

try:
Template.from_file(path)
except (ValidationError, ResumeConfigError) as e:
Expand All @@ -110,6 +110,8 @@ def validate(
def version() -> None:
"""Prints the application version."""

from ancv.reflection import METADATA

print(f"ancv {METADATA.version}")


Expand All @@ -118,6 +120,12 @@ def list() -> None:
"""Lists all available components (templates, themes and translations)."""

# This is pretty raw, but it works. Could make it prettier using more of `rich`.
from rich import print
from rich.tree import Tree

from ancv.visualization.templates import Template
from ancv.visualization.themes import THEMES
from ancv.visualization.translations import TRANSLATIONS

tree = Tree("Components")

Expand All @@ -136,7 +144,7 @@ def list() -> None:
translation_tree.add(translation)
tree.add(translation_tree)

rprint(tree)
print(tree)


@app.callback()
Expand All @@ -150,6 +158,12 @@ def main(
https://typer.tiangolo.com/tutorial/commands/callback/
"""

import logging
import os

import structlog
from structlog.processors import JSONRenderer, TimeStamper, add_log_level

structlog.configure( # This is global state
processors=[ # https://www.structlog.org/en/stable/api.html#procs
TimeStamper(fmt="iso", utc=True, key="ts"),
Expand Down

0 comments on commit 432311a

Please sign in to comment.