Source code for dmp_cli.cli
"""
Usage: [OPTIONS] COMMAND [ARGS]...
The django-mako-plus cli.
Options:
--help Show this message and exit.
Commands:
new Create a new project or app.
"""
from pathlib import Path
import subprocess as sp
import re
import os
import pkg_resources
import crayons
import click
@click.group()
def main():
"""The django-mako-plus cli."""
pass
@main.group()
def new():
"""Create a new project or app."""
pass
@new.command()
@click.argument('name')
def project(name):
"""Create a new django-mako-plus project."""
click.echo('Creating new project ' + crayons.magenta(name, bold=True))
template_path = pkg_resources.resource_filename('django_mako_plus', 'project_template')
sp.check_call(
'python3 -m django startproject --template={path} {name}'.format(path=template_path, name=name),
shell=True
)
click.echo(crayons.green('Successfully created ') + crayons.magenta(name, bold=True))
@new.command()
@click.argument('name')
def app(name):
"""Create a new django-mako-plus app."""
click.echo('Creating new app ' + crayons.magenta(name, bold=True))
template_path = pkg_resources.resource_filename('django_mako_plus', 'app_template')
sp.check_call(
'python3 manage.py startapp --template={path} --extension=py,htm,html {name}'.format(path=template_path,
name=name),
shell=True
)
click.echo(crayons.green('Successfully created ' + crayons.magenta(name, bold=True)))
[docs]def transform_module_text(matchobj):
"""Return the text from the module with the docstring generated by click prepended to it."""
with click.Context(main) as ctx:
docstring = os.linesep.join(['"""', ctx.get_help(), '"""'])
return docstring + os.linesep + ''.join(matchobj.groups()[1:])
# replace this module's docstring with the one generated from top-level cli entrypoint
transformed_module_text = re.sub(
re.compile(r'(.*?)(from|import)(.*)', re.DOTALL | re.MULTILINE),
transform_module_text,
Path(__file__).read_text()
)
with Path(__file__).open('w') as this_module:
this_module.write(transformed_module_text)
if __name__ == "__main__":
main()