Skip to content

Add pre_generate and post_generate hooks #29

@pydanny

Description

@pydanny

It would be nice to be able to do optional pre- and post-processing on a template. Amongst other things this would allow for generation of random hashes for secret keys and other things. Perhaps something like this:

├── cookiecutter.json
├── {{cookiecutter.repo_name}}
├── pre_generate.py (optional)
├── post_generate.py (optional)

Perhaps the pre-generate module would expect a main() callable that returns a dictionary. This dictionary could include things that inject into the cookiecutter context.

For example, this would allow code for generating customized secret keys:

import random
import hashlib

try:
    random = random.SystemRandom()
except NotImplementedError:
    import warnings
    warnings.warn('A secure pseudo-random number generator is not available '
                  'on your system. Falling back to Mersenne Twister.')    

def main():
    chars = list('abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)')
    random.shuffle(chars)
    context = {"secret_key" = "".join(chars)}
    return context

if __name__ == "__main__":
    main()

I could imagine cookiecutter's documentation including a recipe page for pre- and post-generate hooks. 🐹

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementThis issue/PR relates to a feature request.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions