Generate

Introduction

The Generate Extension includes the Generate controller, and provides a mechanism for generating common content from template directories. An example use case would be the ability for application developers to easily generate new plugins for their application… similar in other applications such as Chef Software’s chef generate cookbook type utilities.
The Cement Developer Tools use this extension to generate projects, plugins, extensions, scripts, etc for developers building their applications on the framework.
Documentation References:
API References:

Requirements

Configuration

Application Configuration Settings

This extension honors the following settings under the primary namespace (ex: [myapp]) of the application configuration:
Setting
Description
template_dir
Directory path of a local template directory.

Application Meta Options

This extension honors the following App.Meta options:
Option
Description
template_handler
A template handler to use as the backend for templating
template_dirs
A list of data directories to look for templates
template_module
A python module to look for templates

Usage

Examples

Example: Using Generate Extension
cli
1
from cement import App
2
3
class MyApp(App):
4
class Meta:
5
label = 'myapp'
6
extensions = ['generate', 'jinja2']
7
template_handler = 'jinja2'
8
9
10
with MyApp() as app:
11
app.run()
Copied!
1
$ python myapp.py --help
2
usage: myapp [-h] [--debug] [--quiet] {generate} ...
3
4
optional arguments:
5
-h, --help show this help message and exit
6
--debug toggle debug output
7
--quiet suppress all output
8
9
sub-commands:
10
{generate}
11
generate generate controller
12
13
14
$ python myapp.py generate --help
15
usage: myapp generate [-h] {plugin} ...
16
17
optional arguments:
18
-h, --help show this help message and exit
19
20
sub-commands:
21
{plugin}
22
plugin generate plugin from template
Copied!

Generate Templates

The Generate Extension looks for a generate sub-directory in all defined template directory paths defined at the application level. If it finds a generate directory it treats all items within that directory as a generate template.
A Generate Template requires a single configuration YAML file called .generate.yml that looks something like:
1
---
2
ignore:
3
- "^(.*)ignore-this(.*)quot;
4
- "^(.*)ignore-that(.*)quot;
5
6
exclude:
7
- "^(.*)exclude-this(.*)quot;
8
- "^(.*)exclude-that(.*)quot;
9
10
variables:
11
- name: 'my_variable_name'
12
prompt: 'The Prompt Displayed to The User'
Copied!
Generate Template Configuration
The following configurations are supported in a generate template’s config:
ignore
A list of regular expressions to match files that you want to completely ignore
exclude
A list of regular expressions to match files that you want to copy only (not rendered as template)
variables
A list of variable definitions that support the following sub-keys:
Last modified 3yr ago