Completed
Push — vcs-error-messages ( 931585...7a9569 )
by Michael
01:11
created

cookiecutter()   B

Complexity

Conditions 5

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
c 0
b 0
f 0
dl 0
loc 63
rs 8.3013

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
# -*- coding: utf-8 -*-
2
3
"""
4
cookiecutter.main
5
-----------------
6
7
Main entry point for the `cookiecutter` command.
8
9
The code in this module is also a good example of how to use Cookiecutter as a
10
library rather than a script.
11
"""
12
13
from __future__ import unicode_literals
14
import logging
15
import os
16
17
from .config import get_user_config, USER_CONFIG_PATH
18
from .generate import generate_context, generate_files
19
from .exceptions import InvalidModeException
20
from .prompt import prompt_for_config
21
from .replay import dump, load
22
from .repository import determine_repo_dir
23
24
logger = logging.getLogger(__name__)
25
26
27
def cookiecutter(
28
        template, checkout=None, no_input=False, extra_context=None,
29
        replay=False, overwrite_if_exists=False, output_dir='.',
30
        config_file=USER_CONFIG_PATH):
31
    """
32
    API equivalent to using Cookiecutter at the command line.
33
34
    :param template: A directory containing a project template directory,
35
        or a URL to a git repository.
36
    :param checkout: The branch, tag or commit ID to checkout after clone.
37
    :param no_input: Prompt the user at command line for manual configuration?
38
    :param extra_context: A dictionary of context that overrides default
39
        and user configuration.
40
    :param: overwrite_if_exists: Overwrite the contents of output directory
41
        if it exists
42
    :param output_dir: Where to output the generated project dir into.
43
    :param config_file: User configuration file path.
44
    """
45
    if replay and ((no_input is not False) or (extra_context is not None)):
46
        err_msg = (
47
            "You can not use both replay and no_input or extra_context "
48
            "at the same time."
49
        )
50
        raise InvalidModeException(err_msg)
51
52
    # Get user config from ~/.cookiecutterrc or equivalent
53
    # If no config file, sensible defaults from config.DEFAULT_CONFIG are used
54
    config_dict = get_user_config(config_file=config_file)
55
56
    repo_dir = determine_repo_dir(
57
        template=template,
58
        abbreviations=config_dict['abbreviations'],
59
        clone_to_dir=config_dict['cookiecutters_dir'],
60
        checkout=checkout,
61
        no_input=no_input,
62
    )
63
64
    template_name = os.path.basename(os.path.abspath(template))
65
66
    if replay:
67
        context = load(config_dict['replay_dir'], template_name)
68
    else:
69
        context_file = os.path.join(repo_dir, 'cookiecutter.json')
70
        logging.debug('context_file is {0}'.format(context_file))
71
72
        context = generate_context(
73
            context_file=context_file,
74
            default_context=config_dict['default_context'],
75
            extra_context=extra_context,
76
        )
77
78
        # prompt the user to manually configure at the command line.
79
        # except when 'no-input' flag is set
80
        context['cookiecutter'] = prompt_for_config(context, no_input)
81
82
        dump(config_dict['replay_dir'], template_name, context)
83
84
    # Create project from local context and project template.
85
    return generate_files(
86
        repo_dir=repo_dir,
87
        context=context,
88
        overwrite_if_exists=overwrite_if_exists,
89
        output_dir=output_dir
90
    )
91