Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions grace/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
109 changes: 83 additions & 26 deletions grace/cli.py
Original file line number Diff line number Diff line change
@@ -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 = """
Expand All @@ -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
Expand All @@ -37,38 +32,94 @@ 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):
if not app.database_exists:
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"],
Expand All @@ -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()
28 changes: 14 additions & 14 deletions grace/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"]
Expand All @@ -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}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@
from bot.{{ cookiecutter.__project_slug }} import {{ cookiecutter.__project_class }}

app = Application()


def run():
{{ cookiecutter.__project_class }}(app).run()
bot = {{ cookiecutter.__project_class }}(app)
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""


Expand Down