Using and Mastering Cookiecutter Transcripts
Chapter: Creating Cookiecutter templates
Lecture: Concept: pre-generation hooks

Login or purchase this course to watch this video and the rest of the course contents.
0:01 You've seen that the cookiecutter.json is more powerful than it initially appears, it's not just static JSON.
0:08 That said, there is plenty of advanced processing and validation and other types of things that we might want to put in here
0:16 that just doesn't really fit; and so pre generation hooks and as we'll come to later post generation hooks can together solve this problem.
0:24 So here we wanted to have some required input like if we are going to put nothing you must put your full name.
0:30 If we're going to put just a blank name you have to put your name, like maybe it's Michael and Michael Kennedy
0:34 and if we don't have a small bounded set of items using the choice variable which does make you to choose one of the things out of that choice,
0:41 that is not a good option, right, maybe we want any possible color and having that list would be kind of insane,
0:47 so we could put something like this required, so we could do that for full name as well, or we could just have it blank,
0:53 so these are however you want to present it to the user, now if you have defaults, as a user set in your Cookiecutter defaults,
0:59 it will fill out full name with your real name, and it will fill out name and I guess if you've had a favorite color
1:04 you could get it to put something there as well, but let's talk about how we use pre generation hooks to validate this.
1:10 So, we're going to create a folder, in our Cookiecutter template right at the top,
1:14 we're going to put a hooks folder and we're going to put a pre_gen_project.py and possibly a post as well when we get to that section.
1:21 And then, when we run this, we're going to come along and if we leave this empty, it's going to say no, no, you must specify a favorite color,
1:29 you must specify a name, whatever we put in our pre generation hook, so the blue message comes from our code, the red messages come from cookiecutter.
1:39 So think about how those two messages fit together because I don't think you can really suppress the Cookiecutter error
1:45 and you can decide whether or not you put out yours, but you don't want to just exit 2, status code 2, failed,
1:50 who knows what that means, you want to give them a message, so something like your favorite_color is required.
1:56 Here is an example of the code we wrote for that validating required input and what you just saw in this screen.
2:03 So, we say favorite_color = and name doesn't have to match, it can just be fav color, whatever, right, it's just a variable, equals quotes as a string,
2:09 the Cookiecutter definition, so {{cookiecutter.favorite_color}} and we saw that this gets actually replaced by Cookiecutter before it ever executes,
2:19 so this is a flat static string, this is not some kind of lookup, there is no dependencies or import statements you need to do for this.
2:26 And we can just say hey did they not put a favorite color, or is the lower empty version of it required either way,
2:32 you are going to have to enter something, and so we just call this function at the bottom, and notice the return values, we're returning either 1 or 2
2:41 on an error case and we're returning 0 from this function at the very bottom we're saying sys.exit() whatever validate said.
2:47 Okay, if it exits 0, everything is okay, 1 or 2, not so much.


Talk Python's Mastodon Michael Kennedy's Mastodon