From ae77fc5e048685b2215149c1f8befed794263807 Mon Sep 17 00:00:00 2001 From: gofrendi Date: Fri, 22 Nov 2024 09:55:19 +0700 Subject: [PATCH] Allow None value for env and input --- amalgam/fastapp/_zrb/group.py | 4 +- amalgam/fastapp/_zrb/helper.py | 16 ++++++-- amalgam/fastapp/_zrb/main.py | 37 +++++++++++++------ amalgam/fastapp/_zrb/venv_task.py | 5 +-- amalgam/fastapp/common/app.py | 2 +- amalgam/fastapp/config.py | 4 +- .../service/user/repository/db_repository.py | 6 ++- .../auth/service/user/repository/factory.py | 8 +++- amalgam/fastapp/schema/user.py | 3 +- src/zrb/runner/cli.py | 2 +- src/zrb/task/base_task.py | 8 +++- 11 files changed, 64 insertions(+), 31 deletions(-) diff --git a/amalgam/fastapp/_zrb/group.py b/amalgam/fastapp/_zrb/group.py index 05c776d0..eb93e9bc 100644 --- a/amalgam/fastapp/_zrb/group.py +++ b/amalgam/fastapp/_zrb/group.py @@ -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") diff --git a/amalgam/fastapp/_zrb/helper.py b/amalgam/fastapp/_zrb/helper.py index 1c0e67e2..1f228f34 100644 --- a/amalgam/fastapp/_zrb/helper.py +++ b/amalgam/fastapp/_zrb/helper.py @@ -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( @@ -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, @@ -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")), diff --git a/amalgam/fastapp/_zrb/main.py b/amalgam/fastapp/_zrb/main.py index 70f5bb61..a427b3e6 100644 --- a/amalgam/fastapp/_zrb/main.py +++ b/amalgam/fastapp/_zrb/main.py @@ -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( @@ -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 ===================================================== @@ -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", ) @@ -62,7 +73,10 @@ # 🌐 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 @@ -70,7 +84,7 @@ 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", ) @@ -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] diff --git a/amalgam/fastapp/_zrb/venv_task.py b/amalgam/fastapp/_zrb/venv_task.py index dd204137..6d9fe79d 100644 --- a/amalgam/fastapp/_zrb/venv_task.py +++ b/amalgam/fastapp/_zrb/venv_task.py @@ -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", @@ -18,4 +18,3 @@ ) create_venv >> prepare_venv -app_group.add_task(create_venv, alias="prepare") diff --git a/amalgam/fastapp/common/app.py b/amalgam/fastapp/common/app.py index 34c82a56..8c206539 100644 --- a/amalgam/fastapp/common/app.py +++ b/amalgam/fastapp/common/app.py @@ -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 diff --git a/amalgam/fastapp/config.py b/amalgam/fastapp/config.py index 8727835f..8ca145ee 100644 --- a/amalgam/fastapp/config.py +++ b/amalgam/fastapp/config.py @@ -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", " at 0x7fdfca368d30>") +APP_AUTH_SUPER_USER_PASSWORD = os.getenv( + "FASTAPP_AUTH_SUPER_USER_PASSWORD", " at 0x7f23ab148ca0>" +) APP_AUTH_BASE_URL = os.getenv("FASTAPP_AUTH_BASE_URL", "http://localhost:3001") diff --git a/amalgam/fastapp/module/auth/service/user/repository/db_repository.py b/amalgam/fastapp/module/auth/service/user/repository/db_repository.py index 3f33a4a6..b8407ea6 100644 --- a/amalgam/fastapp/module/auth/service/user/repository/db_repository.py +++ b/amalgam/fastapp/module/auth/service/user/repository/db_repository.py @@ -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 diff --git a/amalgam/fastapp/module/auth/service/user/repository/factory.py b/amalgam/fastapp/module/auth/service/user/repository/factory.py index 31bd14bd..501a914d 100644 --- a/amalgam/fastapp/module/auth/service/user/repository/factory.py +++ b/amalgam/fastapp/module/auth/service/user/repository/factory.py @@ -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) diff --git a/amalgam/fastapp/schema/user.py b/amalgam/fastapp/schema/user.py index c3f366a1..62d09715 100644 --- a/amalgam/fastapp/schema/user.py +++ b/amalgam/fastapp/schema/user.py @@ -1,5 +1,6 @@ -import ulid import datetime + +import ulid from sqlmodel import Field, SQLModel diff --git a/src/zrb/runner/cli.py b/src/zrb/runner/cli.py index a9f7396a..e64aed63 100644 --- a/src/zrb/runner/cli.py +++ b/src/zrb/runner/cli.py @@ -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}") diff --git a/src/zrb/task/base_task.py b/src/zrb/task/base_task.py index 49fead58..3c733b22 100644 --- a/src/zrb/task/base_task.py +++ b/src/zrb/task/base_task.py @@ -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]: @@ -118,7 +118,7 @@ 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 @@ -126,7 +126,11 @@ def __combine_inputs( 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)