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 5cf8187..3f5403f 100644 --- a/grace/cli.py +++ b/grace/cli.py @@ -1,10 +1,11 @@ import discord -from os import getpid, getcwd from sys import path -from logging import info -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 +from textwrap import dedent APP_INFO = """ @@ -18,15 +19,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 @@ -37,27 +32,82 @@ 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 + """)) -@cli.command() -@option("--environment", default='development') -@option("--sync/--no-sync", default=True) -def run(environment=None, sync=None): - path.insert(0, getcwd()) - try: - from bot import app, run +@group() +@option("--environment", default='development', help="The environment to load.") +@pass_context +def app_cli(ctx, environment): + app = ctx.obj["app"] - _loading_application(app, environment, sync) - _load_database(app) - _show_application_info(app) + register_generators(generate) + app.load(environment) - run() - except ImportError: - print("Unable to find a Grace project in this directory.") +@app_cli.group() +def generate(): + pass + + +@app_cli.group() +def db(): + pass + + +@app_cli.command() +@option("--sync/--no-sync", default=True, help="Sync the application command.") +@pass_context +def run(ctx, sync): + app = ctx.obj["app"] + bot = ctx.obj["bot"] + app.command_sync = sync + + _load_database(app) + _show_application_info(app) -def _loading_application(app, environment, command_sync): - app.load(environment, command_sync=command_sync) + bot.run() + + +@db.command() +@pass_context +def create(ctx): + app = ctx.obj["app"] + + if app.database_exists: + return warning("Database already exists") + + app.create_database() + app.create_tables() + + +@db.command() +@pass_context +def drop(ctx): + app = ctx.obj["app"] + + if not app.database_exists: + return warning("Database does not exist") + + app.drop_tables() + app.drop_database() + + +@db.command() +@pass_context +def seed(ctx): + app = ctx.obj["app"] + + if not app.database_exists: + return warning("Database does not exist") + + from db import seed + seed.seed_database() def _load_database(app): @@ -65,10 +115,11 @@ def _load_database(app): app.create_database() app.create_tables() + 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"], @@ -77,4 +128,10 @@ def _show_application_info(app): def main(): - cli() + 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 }}/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 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. """