From 5566d7b478ce7e84012b9e722597c416e5abc14b Mon Sep 17 00:00:00 2001 From: penguinboi Date: Thu, 27 Feb 2025 18:57:53 -0500 Subject: [PATCH 1/4] Added db command --- grace/cli.py | 41 +++++++++++++++++++++++++++++++++++------ pyproject.toml | 2 +- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/grace/cli.py b/grace/cli.py index 5cf8187..3542cde 100644 --- a/grace/cli.py +++ b/grace/cli.py @@ -2,7 +2,7 @@ from os import getpid, getcwd from sys import path -from logging import info +from logging import info, warning from click import group, argument, option, pass_context from grace.generator import register_generators @@ -56,6 +56,38 @@ def run(environment=None, sync=None): print("Unable to find a Grace project in this directory.") +@cli.group() +def db(): + pass + + +# db group commands (create, drop, seed, reset) +@db.command() +def create(): + if app.database_exists: + return warning("Database already exists") + + app.create_database() + app.create_tables() + + +@db.command() +def drop(): + if not app.database_exists: + return warning("Database does not exist") + + app.drop_tables() + app.drop_database() + + +@db.command() +def seed(): + if not app.database_exists: + return warning("Database does not exist") + + app.seed_database() + + def _loading_application(app, environment, command_sync): app.load(environment, command_sync=command_sync) @@ -65,6 +97,7 @@ def _load_database(app): app.create_database() app.create_tables() + def _show_application_info(app): info(APP_INFO.format( discord_version=discord.__version__, @@ -73,8 +106,4 @@ def _show_application_info(app): command_sync=app.command_sync, database=app.database_infos["database"], dialect=app.database_infos["dialect"], - )) - - -def main(): - cli() + )) \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 5c5ec5b..20f537a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,7 +35,7 @@ dependencies = [ "Source" = "https://github.com/Code-Society-Lab/grace-framework" [project.scripts] -grace = "grace.cli:main" +grace = "grace.cli:cli" [tool.setuptools] packages = ["grace"] From ba19482205630ff0e37527910f93d119121dda42 Mon Sep 17 00:00:00 2001 From: penguinboi Date: Fri, 28 Feb 2025 01:58:12 -0500 Subject: [PATCH 2/4] Refactor of the CLI to better handle command requiring the application --- grace/bot.py | 1 + grace/cli.py | 85 ++++++++++++------- grace/config.py | 28 +++--- .../db/seed.py | 7 +- pyproject.toml | 2 +- 5 files changed, 71 insertions(+), 52 deletions(-) diff --git a/grace/bot.py b/grace/bot.py index d8a04e3..8f4547a 100644 --- a/grace/bot.py +++ b/grace/bot.py @@ -3,6 +3,7 @@ from discord.ext.commands import Bot as DiscordBot, when_mentioned_or from grace.application import Application, SectionProxy + class Bot(DiscordBot): """This class is the bot core diff --git a/grace/cli.py b/grace/cli.py index 3542cde..1f674d9 100644 --- a/grace/cli.py +++ b/grace/cli.py @@ -1,9 +1,9 @@ import discord -from os import getpid, getcwd from sys import path -from logging import info, warning -from click import group, argument, option, pass_context +from os import getpid, getcwd +from logging import info, warning, critical +from click import group, argument, option, pass_context, echo from grace.generator import register_generators @@ -18,15 +18,9 @@ @group() def cli(): - # There's probably a better to create the group register_generators(generate) -@cli.group() -def generate(): - pass - - @cli.command() @argument("name") # This database option is currently disabled since the application and config @@ -38,32 +32,44 @@ def new(ctx, name, database=True): ctx.forward(cmd) -@cli.command() -@option("--environment", default='development') -@option("--sync/--no-sync", default=True) -def run(environment=None, sync=None): - path.insert(0, getcwd()) +@group() +@option("--environment", default='development', help="The environment to load.") +@option("--sync/--no-sync", default=True, help="Sync the application command.") +@pass_context +def app_cli(ctx, environment, sync): + app = ctx.obj["app"] - try: - from bot import app, run + register_generators(generate) + app.load(environment, command_sync=sync) - _loading_application(app, environment, sync) - _load_database(app) - _show_application_info(app) - run() - except ImportError: - print("Unable to find a Grace project in this directory.") +@app_cli.group() +def generate(): + pass -@cli.group() +@app_cli.group() def db(): pass -# db group commands (create, drop, seed, reset) +@app_cli.command() +@pass_context +def run(ctx, environment, sync): + app = ctx.obj["app"] + bot = ctx.obj["bot"] + + _load_database(app) + _show_application_info(app) + + bot.run() + + @db.command() -def create(): +@pass_context +def create(ctx): + app = ctx.obj["app"] + if app.database_exists: return warning("Database already exists") @@ -72,7 +78,10 @@ def create(): @db.command() -def drop(): +@pass_context +def drop(ctx): + app = ctx.obj["app"] + if not app.database_exists: return warning("Database does not exist") @@ -81,15 +90,15 @@ def drop(): @db.command() -def seed(): +@pass_context +def seed(ctx): + app = ctx.obj["app"] + if not app.database_exists: return warning("Database does not exist") - - app.seed_database() - -def _loading_application(app, environment, command_sync): - app.load(environment, command_sync=command_sync) + from db import seed + seed.seed_database() def _load_database(app): @@ -106,4 +115,14 @@ def _show_application_info(app): command_sync=app.command_sync, database=app.database_infos["database"], dialect=app.database_infos["dialect"], - )) \ No newline at end of file + )) + + +def main(): + path.insert(0, getcwd()) + + try: + from bot import app, bot + app_cli(obj={"app": app, "bot": bot}) + except ImportError: + cli() \ No newline at end of file diff --git a/grace/config.py b/grace/config.py index 119262c..70e476f 100644 --- a/grace/config.py +++ b/grace/config.py @@ -79,20 +79,6 @@ def __init__(self): self.read("config/database.cfg") self.read("config/environment.cfg") - @property - def database_uri(self) -> Union[str, URL]: - if self.database.get("url"): - return self.database.get("url") - - return URL.create( - self.database["adapter"], - self.database.get("user"), - self.database.get("password"), - self.database.get("host"), - self.database.getint("port"), - self.database.get("database", self.database_name) - ) - @property def database(self) -> SectionProxy: return self.__config[f"database.{self.__environment}"] @@ -109,6 +95,20 @@ def environment(self) -> SectionProxy: def current_environment(self) -> Optional[str]: return self.__environment + @property + def database_uri(self) -> Union[str, URL]: + if self.database.get("url"): + return self.database.get("url") + + return URL.create( + self.database.get("adapter"), + self.database.get("user"), + self.database.get("password"), + self.database.get("host"), + self.database.getint("port"), + self.database.get("database", self.database_name) + ) + @property def database_name(self) -> str: return f"{self.client['name']}_{self.current_environment}" diff --git a/grace/generators/templates/project/{{ cookiecutter.__project_slug }}/db/seed.py b/grace/generators/templates/project/{{ cookiecutter.__project_slug }}/db/seed.py index f5c9481..1f66b4b 100644 --- a/grace/generators/templates/project/{{ cookiecutter.__project_slug }}/db/seed.py +++ b/grace/generators/templates/project/{{ cookiecutter.__project_slug }}/db/seed.py @@ -17,10 +17,9 @@ def seed_database(): model.save() ``` -If you have many seeds or want a more structured approach to seeding, -it is recommended to create a `db/seeds/` directory and organize your seeding -scripts within that folder. You can then import your seeding modules into -this script and call them as needed. +If you have multiple seed file or prefer a structured approach, consider +creating a `db/seeds/` directory to organize your seeding scripts. +You can then import and execute these modules within this script as needed. """ diff --git a/pyproject.toml b/pyproject.toml index 20f537a..5c5ec5b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,7 +35,7 @@ dependencies = [ "Source" = "https://github.com/Code-Society-Lab/grace-framework" [project.scripts] -grace = "grace.cli:cli" +grace = "grace.cli:main" [tool.setuptools] packages = ["grace"] From 3861957c535129b880ff0575f467952c0ae4abfd Mon Sep 17 00:00:00 2001 From: penguinboi Date: Fri, 28 Feb 2025 02:33:49 -0500 Subject: [PATCH 3/4] Removed old run function --- .../{{ cookiecutter.__project_slug }}/bot/__init__.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/grace/generators/templates/project/{{ cookiecutter.__project_slug }}/bot/__init__.py b/grace/generators/templates/project/{{ cookiecutter.__project_slug }}/bot/__init__.py index 7868206..cff1245 100644 --- a/grace/generators/templates/project/{{ cookiecutter.__project_slug }}/bot/__init__.py +++ b/grace/generators/templates/project/{{ cookiecutter.__project_slug }}/bot/__init__.py @@ -2,7 +2,4 @@ from bot.{{ cookiecutter.__project_slug }} import {{ cookiecutter.__project_class }} app = Application() - - -def run(): - {{ cookiecutter.__project_class }}(app).run() \ No newline at end of file +bot = {{ cookiecutter.__project_class }}(app) \ No newline at end of file From 3fb72037e14b483eda4b7dfd2fb01350e938965b Mon Sep 17 00:00:00 2001 From: penguinboi Date: Fri, 28 Feb 2025 02:47:19 -0500 Subject: [PATCH 4/4] Fix command missing parameters --- grace/cli.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/grace/cli.py b/grace/cli.py index 1f674d9..3f5403f 100644 --- a/grace/cli.py +++ b/grace/cli.py @@ -5,6 +5,7 @@ from logging import info, warning, critical from click import group, argument, option, pass_context, echo from grace.generator import register_generators +from textwrap import dedent APP_INFO = """ @@ -31,16 +32,22 @@ def new(ctx, name, database=True): cmd = generate.get_command(ctx, 'project') ctx.forward(cmd) + echo(dedent(f""" + Done! Please do :\n + 1. cd {name} + 2. set your token in your .env + 3. grace run + """)) + @group() @option("--environment", default='development', help="The environment to load.") -@option("--sync/--no-sync", default=True, help="Sync the application command.") @pass_context -def app_cli(ctx, environment, sync): +def app_cli(ctx, environment): app = ctx.obj["app"] register_generators(generate) - app.load(environment, command_sync=sync) + app.load(environment) @app_cli.group() @@ -54,10 +61,12 @@ def db(): @app_cli.command() +@option("--sync/--no-sync", default=True, help="Sync the application command.") @pass_context -def run(ctx, environment, sync): +def run(ctx, sync): app = ctx.obj["app"] bot = ctx.obj["bot"] + app.command_sync = sync _load_database(app) _show_application_info(app) @@ -110,7 +119,7 @@ def _load_database(app): def _show_application_info(app): info(APP_INFO.format( discord_version=discord.__version__, - env=app.config.current_environment, + env=app.environment, pid=getpid(), command_sync=app.command_sync, database=app.database_infos["database"],