Completed
Pull Request — master (#623)
by
unknown
57s
created

cookiecutter.main()   C

Complexity

Conditions 7

Size

Total Lines 87

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 87
rs 5.3501
cc 7

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
"""
5
cookiecutter.cli
6
-----------------
7
8
Main `cookiecutter` CLI.
9
"""
10
11
import os
12
import sys
13
import logging
14
import json
15
16
import click
17
18
from cookiecutter import __version__
19
from cookiecutter.config import get_user_config_path
20
from cookiecutter.main import cookiecutter
21
from cookiecutter.exceptions import (
22
    OutputDirExistsException,
23
    InvalidModeException,
24
    FailedHookException,
25
    UndefinedVariableInTemplate
26
)
27
28
logger = logging.getLogger(__name__)
29
30
31
def version_msg():
32
    python_version = sys.version[:3]
33
    location = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
34
    message = u'Cookiecutter %(version)s from {} (Python {})'
35
    return message.format(location, python_version)
36
37
38
@click.command(context_settings=dict(help_option_names=[u'-h', u'--help']))
39
@click.version_option(__version__, u'-V', u'--version', message=version_msg())
40
@click.argument(u'template')
41
@click.option(
42
    u'--no-input', is_flag=True,
43
    help=u'Do not prompt for parameters and only use cookiecutter.json '
44
         u'file content',
45
)
46
@click.option(
47
    u'-c', u'--checkout',
48
    help=u'branch, tag or commit to checkout after git clone',
49
)
50
@click.option(
51
    '-v', '--verbose',
52
    is_flag=True, help='Print debug information', default=False
53
)
54
@click.option(
55
    u'--replay', is_flag=True,
56
    help=u'Do not prompt for parameters and only use information entered '
57
         u'previously',
58
)
59
@click.option(
60
    u'-f', u'--overwrite-if-exists', is_flag=True,
61
    help=u'Overwrite the contents of the output directory if it already exists'
62
)
63
@click.option(
64
    u'-o', u'--output-dir', default='.', type=click.Path(),
65
    help=u'Where to output the generated project dir into'
66
)
67
@click.option(
68
    u'--config-file', type=click.Path(), default=None,
69
    help=u'User configuration file'
70
)
71
@click.option(
72
    u'--default-config', is_flag=True,
73
    help=u'Do not load a config file. Use the defaults instead'
74
)
75
def main(template, no_input, checkout, verbose, replay, overwrite_if_exists,
76
         output_dir, config_file, default_config):
77
    """Create a project from a Cookiecutter project template (TEMPLATE)."""
78
    if config_file is None:
79
        config_file = get_user_config_path()
80
81
    if verbose:
82
        logging.basicConfig(
83
            format=u'%(levelname)s %(filename)s: %(message)s',
84
            level=logging.DEBUG
85
        )
86
    else:
87
        # Log info and above to console
88
        logging.basicConfig(
89
            format=u'%(levelname)s: %(message)s',
90
            level=logging.INFO
91
        )
92
93
    try:
94
        # If you _need_ to support a local template in a directory
95
        # called 'help', use a qualified path to the directory.
96
        if template == u'help':
97
            click.echo(click.get_current_context().get_help())
98
            sys.exit(0)
99
100
        user_config = None if default_config else config_file
101
102
        cookiecutter(
103
            template, checkout, no_input,
104
            replay=replay,
105
            overwrite_if_exists=overwrite_if_exists,
106
            output_dir=output_dir,
107
            config_file=user_config
108
        )
109
    except (OutputDirExistsException,
110
            InvalidModeException,
111
            FailedHookException) as e:
112
        click.echo(e)
113
        sys.exit(1)
114
    except UndefinedVariableInTemplate as undefined_err:
115
        click.echo('{}'.format(undefined_err.message))
116
        click.echo('Error message: {}'.format(undefined_err.error.message))
117
118
        context_str = json.dumps(
119
            undefined_err.context,
120
            indent=4,
121
            sort_keys=True
122
        )
123
        click.echo('Context: {}'.format(context_str))
124
        sys.exit(1)
125
126
127
if __name__ == "__main__":
128
    main()
129