Using and Mastering Cookiecutter Transcripts
Chapter: Programmatic Cookiecutter: Using the API
Lecture: Gathering game maker data

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Okay let's begin having this program, which if I run it does nothing so far.
0:05 Let's begin by having it have a cool little output here,
0:08 so I'll call this print_welcome() or something like that, we'll create this function,
0:12 and we'll put that there, let's move that below main, okay,
0:17 now if we run it, it says welcome to the game creator,
0:20 we'll create either hi-lo, pacman, or pong for you,
0:22 so this is slightly better experience, I understand this is not that big of a deal,
0:28 but, it's still cool that we have a little bit of a
0:31 welcome to the game creator type thing going on here,
0:34 the first thing that we're going to do is we're going to gather the inputs,
0:37 so we want to ask them questions like hey what's your name,
0:39 and what kind of game do you want to make,
0:42 basically the things that Cookiecutter would ask and potentially other ones as well,
0:46 but remember, in Cookiecutter, we have the Cookiecutter defaults
0:49 so it already knows what my name is,
0:51 it already knows what my GitHub username is, things like that.
0:55 So as much as possible, we would like to just leverage the defaults
0:57 and not even ask, right, just go hey we got the default values,
1:00 but how do we do that, well, that's the first step into the Cookiecutter API.
1:04 So when I come over here and say import cookiecutter
1:08 and there is a bunch of stuff here, we're going to get the cookiecutter.config
1:11 Now, let's go down here at the gather inputs,
1:14 and we're going to say the config = cookiecutter.config.get_user_config()
1:20 Now, it seems like that is what you might want to have here,
1:23 okay, you might want to say this, but it turns out if we just print this out,
1:27 this is actually not what we're looking for, if we look here,
1:31 this is the, it's like the full thing, and then down here,
1:34 there is this default context and in the default context we have our name,
1:37 our email and so on, so it's really the default context that we want,
1:40 so let's maybe say something like this, ctx = config.get('default_context')
1:47 this is just the dictionary, so now, if we print out our context,
1:51 I think that's what we're looking for, perfect,
1:53 so we can grab like full name and store it.
1:56 So once we have this, we can start writing code like this,
1:59 so we can say if, we'll say full_name = ctx.get('full_name') right
2:06 and this get function on a dictionary in Python will return none if it's not there,
2:10 or it will return value for this, so we'll just say if we found a full name,
2:14 let's say if we didn't find the full name, then we'll say full_name =
2:19 and we'll have to ask and let's see, what else do we need to do that for.
2:23 We have to figure out what type of game it is,
2:26 so we can ask a similar type of question, this time,
2:29 I don't really think there is a reasonable default
2:32 like somebody doesn't always want to make Pacman or something
2:34 and let me just tell PyCharm this is not misspelled, so it doesn't look wrong to you.
2:38 So we can figure out the game type that's going to be fun,
2:42 and let's figure out the package name,
2:45 so we'll say do an input('What do you call your game?')
2:49 Now, they might have spaces, a punctuation, all that kind of stuff,
2:53 so I wrote a function here that we can take and transform package name
2:58 and we'll say to package style, right,
3:00 what this is going to do if you just look down here,
3:03 it's going to get rid of all the white space and double spaces and things like that,
3:06 it's going to kind of normalize it, it's like what we've seen before
3:09 with the project slug but this is actually more robust than that.
3:12 We can also ask questions like where do you want to create it,
3:15 so let's come down here and ask this, we can even ask it in a loop,
3:17 in case it get it wrong, now, this I am not sure
3:20 how great of a user experience this is, but I am pointing this out
3:23 because this is not possible in Cookiecutter, and it is possible here,
3:26 so we can ask like this, we can come down here
3:29 and say what is the full path to where your project,
3:32 where you are going to create it, give us a directory,
3:34 and if they give us something wrong, instead of just trying to create it and fail,
3:38 we can go no, no, actually that directory you gave us, that didn't work,
3:42 let's try it again, give us another directory, right,
3:45 so because we're doing this in our own code, we have our own flexibility,
3:47 and just to be clear, this doesn't have to be Python code,
3:49 this could be any code, any code that we want to write this in
3:52 as long as we can somehow shell out to interact with Python for the API.
3:56 Okay, so let's add that to the top there, okay,
3:59 so we have our working directory, and then I think
4:02 we'll have everything we need to create the game.
4:05 Now, one final thing I'd like to do is package this up a little bit,
4:07 and I really like returning a bunch of data in the form we would have to there
4:12 so let's go and create using a named collection here,
4:15 let's go and create a game create info, and it's going to have a project name,
4:21 full name, game type, a working dir, surprise,
4:23 those are the things we needed and let's have that function here return it.
4:27 so we can come down here and just say actually, let's just return this.
4:31 So we'll need project name, we'll need full name, game type,
4:36 and working dir. package name, not project name.
4:40 So that is going to create it, and let's just make sure this first part is working,
4:45 I'll just print out the create info and then I want to come back
4:49 and we are going to actually do the building in a minute, let's run it see what we get.
4:53 So let's come down here I want to play Pong,
4:55 okay great, I am going to call it "The great ping pong",
4:59 the full path will just be this, boom, we're going to create this directory
5:06 and there is the full name, notice, it didn't ask me what my full name was,
5:10 right, because it got it from the defaults. the game type is Pong,
5:14 and the working directory is just right here.
5:17 Okay great, so it looks like we're gathering the inputs perfectly well here.
5:20 The next thing we need to do is actually use those inputs
5:23 to build the game and this is where we're going
5:26 to execute the Cookiecutter template.