Skip to content

Commit

Permalink
Allow None value for env and input
Browse files Browse the repository at this point in the history
  • Loading branch information
goFrendiAsgard committed Nov 22, 2024
1 parent 18d619f commit ae77fc5
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 31 deletions.
4 changes: 1 addition & 3 deletions amalgam/fastapp/_zrb/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
Group(name="fastapp", description="🚀 Managing Fastapp")
)

app_run_group = app_group.add_group(
Group(name="run", description="🟢 Run Fastapp")
)
app_run_group = app_group.add_group(Group(name="run", description="🟢 Run Fastapp"))

app_migrate_group = app_group.add_group(
Group(name="migrate", description="📦 Run Fastapp DB migration")
Expand Down
16 changes: 12 additions & 4 deletions amalgam/fastapp/_zrb/helper.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import os

from zrb import Cmd, CmdTask, EnvFile, EnvMap, Task, StrInput
from fastapp._zrb.config import (
APP_DIR, MICROSERVICES_ENV_VARS, MONOLITH_ENV_VARS, ACTIVATE_VENV_SCRIPT
ACTIVATE_VENV_SCRIPT,
APP_DIR,
MICROSERVICES_ENV_VARS,
MONOLITH_ENV_VARS,
)

from zrb import Cmd, CmdTask, EnvFile, EnvMap, StrInput, Task


def create_migration(name: str, module: str) -> Task:
return CmdTask(
Expand Down Expand Up @@ -32,7 +36,7 @@ def create_migration(name: str, module: str) -> Task:
"alembic upgrade head",
Cmd(
"alembic revision --autogenerate -m {double_quote(ctx.input.message)}",
auto_render=True
auto_render=True,
),
],
auto_render_cmd=False,
Expand All @@ -43,7 +47,11 @@ def create_migration(name: str, module: str) -> Task:
def migrate_module(name: str, module: str, as_microservices: bool) -> Task:
env_vars = MICROSERVICES_ENV_VARS if as_microservices else MONOLITH_ENV_VARS
return CmdTask(
name=f"migrate-fastapp-{name}" if as_microservices else f"migrate-{name}-on-monolith",
name=(
f"migrate-fastapp-{name}"
if as_microservices
else f"migrate-{name}-on-monolith"
),
description=f"🧩 Run Fastapp {name.capitalize()} DB migration",
env=[
EnvFile(path=os.path.join(APP_DIR, "template.env")),
Expand Down
37 changes: 26 additions & 11 deletions amalgam/fastapp/_zrb/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import os

from zrb import CmdTask, Env, EnvFile, Task
from fastapp._zrb.config import APP_DIR, ACTIVATE_VENV_SCRIPT
from fastapp._zrb.group import app_run_group, app_migrate_group, app_create_group
from fastapp._zrb.helper import migrate_module, run_microservice, create_migration
from fastapp._zrb.config import ACTIVATE_VENV_SCRIPT, APP_DIR
from fastapp._zrb.group import (
app_create_group,
app_migrate_group,
app_run_group,
)
from fastapp._zrb.helper import (
create_migration,
migrate_module,
run_microservice,
)
from fastapp._zrb.venv_task import prepare_venv

from zrb import CmdTask, Env, EnvFile, Task

# 🚀 Run/Migrate All ===========================================================

run_all = app_run_group.add_task(
Expand All @@ -18,14 +27,16 @@
migrate_all = app_migrate_group.add_task(
Task(
name="migrate-fastapp",
description="📦 Run Fastapp DB migration for monolith and microservices"
description="📦 Run Fastapp DB migration for monolith and microservices",
),
alias="all",
)

create_all_migration = app_create_group.add_task(
Task(name="create-fastapp-migration", description="📦 Create Fastapp DB migration"),
alias="migration"
Task(
name="create-fastapp-migration", description="📦 Create Fastapp DB migration"
),
alias="migration",
)

# 🗿 Run/Migrate Monolith =====================================================
Expand Down Expand Up @@ -53,7 +64,7 @@
migrate_monolith = app_migrate_group.add_task(
Task(
name="migrate-monolith-fastapp",
description="🗿 Run Fastapp DB migration for monolith"
description="🗿 Run Fastapp DB migration for monolith",
),
alias="monolith",
)
Expand All @@ -62,15 +73,18 @@
# 🌐 Run/Migrate Microsevices ==================================================

run_microservices = app_run_group.add_task(
Task(name="run-microservices-fastapp", description="🌐 Run Fastapp as microservices"),
Task(
name="run-microservices-fastapp",
description="🌐 Run Fastapp as microservices",
),
alias="microservices",
)
run_microservices >> run_all

migrate_microservices = app_migrate_group.add_task(
Task(
name="migrate-microservices-fastapp",
description="🌐 Run Fastapp DB migration for microservices"
description="🌐 Run Fastapp DB migration for microservices",
),
alias="microservices",
)
Expand All @@ -92,7 +106,8 @@
prepare_venv >> migrate_monolith_gateway >> [migrate_monolith, run_monolith]

migrate_microservices_gateway = app_migrate_group.add_task(
migrate_module("gateway", "gateway", as_microservices=True), alias="microservices-gateway"
migrate_module("gateway", "gateway", as_microservices=True),
alias="microservices-gateway",
)
prepare_venv >> migrate_microservices_gateway >> [migrate_microservices, run_gateway]

Expand Down
5 changes: 2 additions & 3 deletions amalgam/fastapp/_zrb/venv_task.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os

from fastapp._zrb.config import ACTIVATE_VENV_SCRIPT, APP_DIR

from zrb import CmdTask
from fastapp._zrb.config import APP_DIR, ACTIVATE_VENV_SCRIPT
from fastapp._zrb.group import app_group

create_venv = CmdTask(
name="create-fastapp-venv",
Expand All @@ -18,4 +18,3 @@
)
create_venv >> prepare_venv

app_group.add_task(create_venv, alias="prepare")
2 changes: 1 addition & 1 deletion amalgam/fastapp/common/app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from contextlib import asynccontextmanager

from fastapi import FastAPI
from fastapp.common.db_engine import engine
from fastapp.config import APP_MODE, APP_MODULES
from fastapi import FastAPI
from sqlmodel import SQLModel


Expand Down
4 changes: 3 additions & 1 deletion amalgam/fastapp/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
),
)
APP_AUTH_SUPER_USER = os.getenv("FASTAPP_AUTH_SUPER_USER", "admin")
APP_AUTH_SUPER_USER_PASSWORD = os.getenv("FASTAPP_AUTH_SUPER_USER_PASSWORD", "<function <lambda> at 0x7fdfca368d30>")
APP_AUTH_SUPER_USER_PASSWORD = os.getenv(
"FASTAPP_AUTH_SUPER_USER_PASSWORD", "<function <lambda> at 0x7f23ab148ca0>"
)

APP_AUTH_BASE_URL = os.getenv("FASTAPP_AUTH_BASE_URL", "http://localhost:3001")
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from fastapp.common.db_repository import BaseDBRepository
from fastapp.common.error import NotFoundError
from fastapp.module.auth.service.user.repository.repository import UserRepository
from passlib.context import CryptContext
from fastapp.module.auth.service.user.repository.repository import (
UserRepository,
)
from fastapp.schema.user import User, UserCreate, UserResponse, UserUpdate
from passlib.context import CryptContext
from sqlalchemy.ext.asyncio import AsyncSession
from sqlmodel import Session, select

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from fastapp.common.db_engine import engine
from fastapp.config import APP_REPOSITORY_TYPE
from fastapp.module.auth.service.user.repository.db_repository import UserDBRepository
from fastapp.module.auth.service.user.repository.repository import UserRepository
from fastapp.module.auth.service.user.repository.db_repository import (
UserDBRepository,
)
from fastapp.module.auth.service.user.repository.repository import (
UserRepository,
)

if APP_REPOSITORY_TYPE == "db":
user_repository: UserRepository = UserDBRepository(engine)
Expand Down
3 changes: 2 additions & 1 deletion amalgam/fastapp/schema/user.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ulid
import datetime

import ulid
from sqlmodel import Field, SQLModel


Expand Down
2 changes: 1 addition & 1 deletion src/zrb/runner/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def _show_task_info(self, task: AnyTask):
print()
if len(inputs) > 0:
print(stylize_section_header("INPUTS"))
max_input_name_length = max(len(s) for s in inputs)
max_input_name_length = max(len(task_input.name) for task_input in inputs)
for task_input in inputs:
task_input_name = task_input.name.ljust(max_input_name_length + 1)
print(f" --{task_input_name}: {task_input.description}")
Expand Down
8 changes: 6 additions & 2 deletions src/zrb/task/base_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def envs(self) -> list[AnyEnv]:
envs.append(self._envs)
elif self._envs is not None:
envs += self._envs
return envs
return [env for env in envs if env is not None]

@property
def inputs(self) -> list[AnyInput]:
Expand All @@ -118,15 +118,19 @@ def inputs(self) -> list[AnyInput]:
self.__combine_inputs(inputs, upstream.inputs)
if self._inputs is not None:
self.__combine_inputs(inputs, self._inputs)
return inputs
return [task_input for task_input in inputs if task_input is not None]

def __combine_inputs(
self, inputs: list[AnyInput], other_inputs: list[AnyInput] | AnyInput
):
input_names = [task_input.name for task_input in inputs]
if isinstance(other_inputs, AnyInput):
other_inputs = [other_inputs]
elif other_inputs is None:
other_inputs = []
for task_input in other_inputs:
if task_input is None:
continue
if task_input.name not in input_names:
inputs.append(task_input)

Expand Down

0 comments on commit ae77fc5

Please sign in to comment.