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