| Conditions | 8 |
| Total Lines | 91 |
| Lines | 0 |
| Ratio | 0 % |
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:
If many parameters/temporary variables are present:
| 1 | #!/usr/bin/env python |
||
| 49 | @click.command(context_settings=dict(help_option_names=[u'-h', u'--help'])) |
||
| 50 | @click.version_option(__version__, u'-V', u'--version', message=version_msg()) |
||
| 51 | @click.argument(u'template') |
||
| 52 | @click.argument('extra_context', nargs=-1, callback=validate_extra_context) |
||
| 53 | @click.option( |
||
| 54 | u'--no-input', is_flag=True, |
||
| 55 | help=u'Do not prompt for parameters and only use cookiecutter.json ' |
||
| 56 | u'file content', |
||
| 57 | ) |
||
| 58 | @click.option( |
||
| 59 | u'-c', u'--checkout', |
||
| 60 | help=u'branch, tag or commit to checkout after git clone', |
||
| 61 | ) |
||
| 62 | @click.option( |
||
| 63 | '-v', '--verbose', |
||
| 64 | is_flag=True, help='Print debug information', default=False |
||
| 65 | ) |
||
| 66 | @click.option( |
||
| 67 | u'--replay', is_flag=True, |
||
| 68 | help=u'Do not prompt for parameters and only use information entered ' |
||
| 69 | u'previously', |
||
| 70 | ) |
||
| 71 | @click.option( |
||
| 72 | u'-f', u'--overwrite-if-exists', is_flag=True, |
||
| 73 | help=u'Overwrite the contents of the output directory if it already exists' |
||
| 74 | ) |
||
| 75 | @click.option( |
||
| 76 | u'-o', u'--output-dir', default='.', type=click.Path(), |
||
| 77 | help=u'Where to output the generated project dir into' |
||
| 78 | ) |
||
| 79 | @click.option( |
||
| 80 | u'--config-file', type=click.Path(), default=USER_CONFIG_PATH, |
||
| 81 | help=u'User configuration file' |
||
| 82 | ) |
||
| 83 | @click.option( |
||
| 84 | u'--default-config', is_flag=True, |
||
| 85 | help=u'Do not load a config file. Use the defaults instead' |
||
| 86 | ) |
||
| 87 | def main(template, extra_context, no_input, checkout, verbose, replay, overwrite_if_exists, |
||
| 88 | output_dir, config_file, default_config): |
||
| 89 | """Create a project from a Cookiecutter project template (TEMPLATE).""" |
||
| 90 | if verbose: |
||
| 91 | logging.basicConfig( |
||
| 92 | format=u'%(levelname)s %(filename)s: %(message)s', |
||
| 93 | level=logging.DEBUG |
||
| 94 | ) |
||
| 95 | else: |
||
| 96 | # Log info and above to console |
||
| 97 | logging.basicConfig( |
||
| 98 | format=u'%(levelname)s: %(message)s', |
||
| 99 | level=logging.INFO |
||
| 100 | ) |
||
| 101 | |||
| 102 | if extra_context: |
||
| 103 | # Convert tuple -- e.g.: (u'program_name=foobar', u'startsecs=66') |
||
| 104 | # to dict -- e.g.: {'program_name': 'foobar', 'startsecs': '66'} |
||
| 105 | extra_context = dict(s.split('=', 1) for s in extra_context) |
||
| 106 | |||
| 107 | try: |
||
| 108 | # If you _need_ to support a local template in a directory |
||
| 109 | # called 'help', use a qualified path to the directory. |
||
| 110 | if template == u'help': |
||
| 111 | click.echo(click.get_current_context().get_help()) |
||
| 112 | sys.exit(0) |
||
| 113 | |||
| 114 | user_config = None if default_config else config_file |
||
| 115 | |||
| 116 | cookiecutter( |
||
| 117 | template, checkout, no_input, |
||
| 118 | extra_context=extra_context, |
||
| 119 | replay=replay, |
||
| 120 | overwrite_if_exists=overwrite_if_exists, |
||
| 121 | output_dir=output_dir, |
||
| 122 | config_file=user_config |
||
| 123 | ) |
||
| 124 | except (OutputDirExistsException, |
||
| 125 | InvalidModeException, |
||
| 126 | FailedHookException) as e: |
||
| 127 | click.echo(e) |
||
| 128 | sys.exit(1) |
||
| 129 | except UndefinedVariableInTemplate as undefined_err: |
||
| 130 | click.echo('{}'.format(undefined_err.message)) |
||
| 131 | click.echo('Error message: {}'.format(undefined_err.error.message)) |
||
| 132 | |||
| 133 | context_str = json.dumps( |
||
| 134 | undefined_err.context, |
||
| 135 | indent=4, |
||
| 136 | sort_keys=True |
||
| 137 | ) |
||
| 138 | click.echo('Context: {}'.format(context_str)) |
||
| 139 | sys.exit(1) |
||
| 140 | |||
| 144 |