Skip to content

Commit

Permalink
Merge pull request #28 from UW-Macrostrat/app-frame-fixes
Browse files Browse the repository at this point in the history
App frame fixes
  • Loading branch information
davenquinn authored Jan 18, 2024
2 parents f03dbbe + 4402acb commit bbc2c7f
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 24 deletions.
2 changes: 1 addition & 1 deletion app-frame/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ acquire features for managing Kubernetes-based applications.
app = Application(
"Mapboard",
restart_commands={"gateway": "caddy reload --config /etc/caddy/Caddyfile"},
app_module="mapboard.server",
log_modules=["mapboard.server"],
compose_files=[MAPBOARD_ROOT / "system" / "docker-compose.yaml"],
)
cli = app.control_command()
Expand Down
16 changes: 13 additions & 3 deletions app-frame/macrostrat/app_frame/control_command.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Typer command-line application

import sys
from os import environ
from time import sleep

import click
import typer
from click import Group
from typer import Context, Typer
from typer import Context, Option, Typer
from typer.core import TyperGroup
from typer.models import TyperInfo

Expand Down Expand Up @@ -44,8 +45,17 @@ def __init__(
self.app = app
self.name = app.name

def callback(ctx: Context, verbose: bool = False):
verbose_envvar = self.app.envvar_prefix + "VERBOSE"

def callback(
ctx: Context,
verbose: bool = Option(False, "--verbose", envvar=verbose_envvar),
):
ctx.obj = self.app
# Setting the environment variable allows nested commands to pick up
# the verbosity setting, if needed.
if verbose:
environ[verbose_envvar] = "1"
self.app.setup_logs(verbose=verbose)

callback.__doc__ = f"""{self.app.name} command-line interface"""
Expand Down Expand Up @@ -195,4 +205,4 @@ def restart(ctx: Context, container: str = typer.Argument(None)):
@click.argument("args", nargs=-1, type=click.UNPROCESSED)
def _compose(args):
"""Run docker compose commands in the appropriate context"""
compose(*args)
compose(*args, collect_args=False)
17 changes: 12 additions & 5 deletions app-frame/macrostrat/app_frame/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ApplicationBase:
class Application(ApplicationBase):
console: Console
_dotenv_cfg: bool | Path | list[Path]
_log_modules: list[str] = []

def __init__(
self,
Expand All @@ -33,7 +34,7 @@ def __init__(
command_name: Optional[str] = None,
project_prefix: Optional[str] = None,
restart_commands: dict[str, str] = {},
app_module: Optional[str] = None,
log_modules: Optional[str | list[str]] = None,
root_dir: Path | Callable[[Path], Path] = Path.cwd(),
compose_files: ComposeFilesDependency = [],
env: EnvironmentDependency = {},
Expand All @@ -42,9 +43,15 @@ def __init__(
self.name = name
self.command_name = command_name or name.lower()
self.project_prefix = project_prefix or name.lower().replace(" ", "_")
self.envvar_prefix = self.project_prefix.upper() + "_"
self.console = Console()
self.restart_commands = restart_commands
self.app_module = app_module

if isinstance(log_modules, str):
log_modules = [log_modules]
if log_modules is not None:
self._app_modules = log_modules

self._dotenv_cfg = load_dotenv

# Root dir and compose files can be specified using dependency injection.
Expand Down Expand Up @@ -92,11 +99,11 @@ def setup_environment(self, env: EnvironmentDependency):
environ[k] = v

def setup_logs(self, verbose: bool = False):
if self.app_module is None:
log.warning("No app module specified, not setting up logs")
if len(self._log_modules) == 0:
log.warning("No modules specified, not setting up logs")
return
if verbose:
setup_stderr_logs(self.app_module)
setup_stderr_logs(*self._log_modules)
else:
# Disable all logging
# TODO: This is a hack, we shouldn't have to explicitly disable
Expand Down
5 changes: 4 additions & 1 deletion app-frame/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ packages = [
{include = "macrostrat"},
{include = "test_app"},
]
version = "1.2.0"
version = "1.2.1"

[tool.poetry.dependencies]
"macrostrat.utils" = "^1.1.0"
Expand All @@ -21,3 +21,6 @@ typer = "^0.9.0"
[build-system]
build-backend = "poetry.core.masonry.api"
requires = ["poetry-core>=1.0.0"]

[tool.poetry.scripts]
test-app = "test_app:main"
2 changes: 1 addition & 1 deletion app-frame/test_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
app = Application(
"Test App",
restart_commands={"gateway": "caddy reload --config /etc/caddy/Caddyfile"},
app_module="test_app",
log_modules=["test_app"],
compose_files=[APP_ROOT / "docker-compose.yaml"],
)
main = app.control_command()
2 changes: 1 addition & 1 deletion database/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dinosaur/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package-tools/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ isort = "^5.13.2"
[tool.pytest.ini_options]
addopts = "--confcutdir=."

[tool.poetry.scripts]
test-app = "test_app:main"

[tool.isort]
profile = "black"
known_first_party = "macrostrat"
5 changes: 1 addition & 4 deletions utils/macrostrat/utils/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@ def get_logger(name=None, level=logging.DEBUG, handler=None):


def setup_stderr_logs(*args, level=logging.DEBUG):
# Customize Sparrow's root logger so we don't get overridden by uvicorn
# Customize the root logger so we don't get overridden by uvicorn
# We may want to customize this further eventually
# https://github.com/encode/uvicorn/issues/410
if len(args) == 0:
args = ["sparrow"]

handler = StreamHandler(stderr)
handler.setFormatter(SparrowLogFormatter())
handler.setLevel(level)
Expand Down
2 changes: 1 addition & 1 deletion utils/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ authors = ["Daven Quinn <[email protected]>"]
description = "Core utilities for Macrostrat and Sparrow"
name = "macrostrat.utils"
packages = [{ include = "macrostrat" }]
version = "1.2.0"
version = "1.2.1"

[tool.poetry.dependencies]
colorlog = "^6.5.0"
Expand Down

0 comments on commit bbc2c7f

Please sign in to comment.