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
2 changes: 1 addition & 1 deletion grace/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.10.6-alpha"
__version__ = "0.10.8-alpha"
5 changes: 4 additions & 1 deletion grace/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ def load_logs(self) -> None:

install(
self.config.environment.get("log_level"),
fmt="[%(asctime)s] %(programname)s %(funcName)s %(module)s %(levelname)s %(message)s",
fmt=(
"[%(asctime)s] %(programname)s %(funcName)s ",
"%(module)s %(levelname)s %(message)s"
),
programname=self.config.current_environment,
)

Expand Down
19 changes: 14 additions & 5 deletions grace/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,17 @@ def validate(self, *args, **kwargs):
"""Validates the arguments passed to the command."""
return True

def generate_template(self, template_dir: str, variables: dict[str, Any] = {}):
def generate_template(
self,
template_dir: str,
variables: dict[str, Any] = {}
):
"""Generates a template using Cookiecutter.

:param template_dir: The name of the template to generate.
:type template_dir: str

:param variables: The variables to pass to the template. (default is {})
:param variables: The variables to pass to the template. (default: {})
:type variables: dict[str, Any]
"""
template = str(self.templates_path / template_dir)
Expand All @@ -142,14 +146,19 @@ def generate_file(
:param template_dir: The name of the template to generate.
:type template_dir: str

:param variables: The variables to pass to the template. (default is {})
:param variables: The variables to pass to the template.
(default is {})
:type variables: dict[str, Any]

:param output_dir: The output directory for the generated template. (default is None)
:param output_dir: The output directory for the generated template.
(default is None)
:type output_dir: str
"""
env = Environment(
loader=PackageLoader('grace', str(self.templates_path / template_dir)),
loader=PackageLoader(
'grace',
str(self.templates_path / template_dir)
),
extensions=['jinja2_strcase.StrcaseExtension']
)

Expand Down
4 changes: 3 additions & 1 deletion grace/generators/cog_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from re import match
from logging import info
from click.core import Argument
from jinja2_strcase.jinja2_strcase import to_snake


class CogGenerator(Generator):
Expand All @@ -20,6 +21,7 @@ def generate(self, name: str, description: str = ""):
self.NAME,
variables={
"cog_name": name,
"cog_module_name": to_snake(name),
"cog_description": description,
},
output_dir="bot/extensions"
Expand All @@ -39,4 +41,4 @@ def validate(self, name: str, **_kwargs) -> bool:


def generator() -> Generator:
return CogGenerator()
return CogGenerator()
9 changes: 6 additions & 3 deletions grace/generators/model_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from logging import info
from click.core import Argument
from grace.generators.migration_generator import generate_migration
from jinja2_strcase.jinja2_strcase import to_snake


class ModelGenerator(Generator):
Expand All @@ -21,8 +22,9 @@ def generate(self, name: str, params: tuple[str]):
a SQLAlchemy-style definition. You can specify column names and types
during generation using the format `column_name:Type`.

Supported types are any valid SQLAlchemy column types (e.g., `String`, `Integer`,
`Boolean`, etc.). See https://docs.sqlalchemy.org/en/20/core/types.html
Supported types are any valid SQLAlchemy column types
(e.g., `String`, `Integer`, `Boolean`, etc.).
See https://docs.sqlalchemy.org/en/20/core/types.html

Example:
```bash
Expand All @@ -38,6 +40,7 @@ def generate(self, name: str, params: tuple[str]):
self.NAME,
variables={
"model_name": name,
"model_module_name": to_snake(name),
"model_columns": model_columns,
"model_column_types": types
},
Expand Down Expand Up @@ -76,4 +79,4 @@ def extract_columns(self, params: tuple[str]) -> tuple[list, list]:


def generator() -> Generator:
return ModelGenerator()
return ModelGenerator()
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@


class {{ cog_name | to_camel }}Cog(Cog, name="{{ cog_name | camel_case_to_space }}"{{ ', description="{}"'.format(cog_description) if cog_description }}):
def __init__(self, bot: Bot):
self.bot: Bot = bot
def __init__(self, bot: Bot):
self.bot: Bot = bot


async def setup(bot: Bot):
await bot.add_cog({{ cog_name }}Cog(bot))
await bot.add_cog({{ cog_name }}Cog(bot))
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sqlalchemy import Column, Integer{{ ", {}".format(','.join(model_column_types)) }}
from sqlalchemy import Column, {{ ", {}".format(','.join(model_column_types)) }}
from bot import app
from grace.model import Model

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = ["setuptools>=64", "setuptools_scm>=8"]

[project]
name = "grace-framework"
version = "0.10.7-alpha"
version = "0.10.8-alpha"
authors = [
{ name="Simon Roy" }
]
Expand Down
24 changes: 15 additions & 9 deletions tests/generators/test_cog_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ def generator():


def test_generate_cog(mocker, generator):
"""Test if the generate method creates the correct template with a database."""
"""
Test if the generate method creates the correct template with a database.
"""
mock_generate_file = mocker.patch.object(Generator, 'generate_file')

name = "Example"
name = 'MyExample'
module_name = 'my_example'
description = "This is an example cog."

generator.generate(name, description)
Expand All @@ -21,21 +24,24 @@ def test_generate_cog(mocker, generator):
'cog',
variables={
'cog_name': name,
'cog_module_name': module_name,
'cog_description': description
},
output_dir="bot/extensions"
output_dir='bot/extensions'
)


def test_validate_valid_name(generator):
"""Test if the validate method passes for a valid project name."""
valid_name = "CogExample"
valid_name = 'CogExample'
assert generator.validate(valid_name)


def test_validate_invalid_name(generator):
"""Test if the validate method raises ValueError for name without a hyphen."""
assert not generator.validate("cog-example")
assert not generator.validate("cog_example")
assert not generator.validate("Cog-Example")
assert not generator.validate("Cog_Example")
"""
Test if the validate method raises ValueError for name without a hyphen.
"""
assert not generator.validate('cog-example')
assert not generator.validate('cog_example')
assert not generator.validate('Cog-Example')
assert not generator.validate('Cog_Example')
26 changes: 18 additions & 8 deletions tests/generators/test_project_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ def generator():


def test_generate_project_with_database(mocker, generator):
"""Test if the generate method creates the correct template with a database."""
mock_generate_template = mocker.patch.object(Generator, 'generate_template')
"""
Test if the generate method creates the correct template with a database.
"""
mock_generate_template = mocker.patch.object(
Generator,
'generate_template'
)
name = "example-project"

generator.generate(name, database=True)
Expand All @@ -23,8 +28,9 @@ def test_generate_project_with_database(mocker, generator):


def test_generate_project_without_database(mocker, generator):
"""Test if the generate method creates the correct template
without a database.
"""
Test if the generate method creates the correct template without a
database.
"""
mock_generate_template = mocker.patch.object(Generator, 'generate_template')
name = "example-project"
Expand All @@ -39,20 +45,24 @@ def test_generate_project_without_database(mocker, generator):


def test_validate_valid_name(generator):
"""Test if the validate method passes for a valid project name."""
"""
Test if the validate method passes for a valid project name.
"""
valid_name = "example-project"
assert generator.validate(valid_name)


def test_validate_invalid_name_no_hyphen(generator):
"""Test if the validate method raises ValueError for
name without a hyphen.
"""
Test if the validate method raises ValueError for name without a hyphen.
"""
invalid_name = "ExampleProject"
assert not generator.validate(invalid_name)


def test_validate_invalid_name_uppercase(generator):
"""Test if the validate method raises ValueError for uppercase name."""
"""
Test if the validate method raises ValueError for uppercase name.
"""
invalid_name = "Example-Project"
assert not generator.validate(invalid_name)