Generator
The base class for every grace generate subcommand, plus the discovery function that registers them. See Writing Generators for a full guide.
from grace.generator import Generator
class MyGenerator(Generator):
NAME = "my_generator"
def generate(self, *args, **kwargs):
...
def generator() -> Generator:
return MyGenerator()
grace.generator.Generator
Generator()
Bases: Command
Base class for implementing a generator command.
This class provides a foundation for defining generator commands that use Cookiecutter to generate project templates. It includes methods for template generation, validation, and argument handling.
Attributes:
- NAME: The name of the generator command (must be defined by subclasses).
- OPTIONS: A dictionary of additional Click options for the command.
Ensures that the NAME attribute is defined by the subclass.
Registers the command with Click using the specified name and options.
| RAISES | DESCRIPTION |
|---|---|
GeneratorError
|
If the |
Source code in grace/generator.py
83 84 85 86 87 88 89 90 91 92 93 94 | |
NAME
class-attribute
instance-attribute
NAME = None
OPTIONS
class-attribute
instance-attribute
OPTIONS = {}
app
instance-attribute
app = None
templates_path
property
templates_path
invoke
invoke(ctx)
Source code in grace/generator.py
100 101 102 | |
generate
generate(*args, **kwargs)
Generates template.
| PARAMETER | DESCRIPTION |
|---|---|
args
|
The positional arguments passed to the command.
DEFAULT:
|
kwargs
|
The keyword arguments passed to the command
DEFAULT:
|
| RAISES | DESCRIPTION |
|---|---|
NotImplementedError
|
If the method is not implemented by the subclass. |
Source code in grace/generator.py
104 105 106 107 108 109 110 111 112 | |
validate
validate(*args, **kwargs)
Validates the arguments passed to the command.
Source code in grace/generator.py
119 120 121 | |
generate_template
generate_template(template_dir, variables={}, output_dir='')
Generate a template using Cookiecutter.
Renders template_dir (a subdirectory of templates_path) with the given
variables, writing the result into output_dir (defaults to the current
working directory). Returns the path to the generated project directory.
Example
self.generate_template(
"project",
variables={"project_name": "my-bot"},
output_dir="my-bot",
)
Source code in grace/generator.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
generate_file
generate_file(template_dir, variables={}, output_dir='')
Generate a module using jinja2 template.
| PARAMETER | DESCRIPTION |
|---|---|
template_dir
|
The name of the template to generate.
TYPE:
|
variables
|
The variables to pass to the template. (default is {})
TYPE:
|
output_dir
|
The output directory for the generated template. (default is None)
TYPE:
|
Source code in grace/generator.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
grace.generator.register_generators
register_generators(command_group)
Registers generator commands to the given Click command group.
This function dynamically imports all modules in the grace.generators package
and registers each module's generator command to the provided command_group.
| PARAMETER | DESCRIPTION |
|---|---|
command_group
|
The Click command group to register the generators to.
TYPE:
|
Source code in grace/generator.py
39 40 41 42 43 44 45 46 47 48 49 50 51 | |