Using and Mastering Cookiecutter Transcripts
Chapter: Adding features to Cookiecutter
Lecture: Adding the feature

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Okay let's go back to the folder here basically I want to open up this directory in the editor
0:06 so I click show in finder and it'll take me where the .gitignore was which actually is right where I want to be, so I want to open this in PyCharm,
0:12 and on MacOS you can do that really nicely by just dragging and dropping the root into PyCharm.
0:19 In Linux or in Windows you have to actually go file, open directory go find this thing. So, you don't have to use PyCharm, I like it the best,
0:28 it's totally up to you on whether you want to do it or not. PyCharm has built-in source integration, so let's say add root here
0:36 here you see all the things that we're going to work with and this folder is actually where we're most interested
0:43 so let's let it do its little indexing, so we have autocomplete and then we'll be ready to roll.
0:48 First thing I'd like to be able to run this inside PyCharm, so there's a couple of options, let's go to this configuration,
0:54 say add a new Python run, and normally, you would put the script here, you could even right click and say run it
1:00 but this is a package so that makes it kind of tricky so we'll say script parameters, oops, not there, interpreter options -m
1:07 and then what are we going to do- we're going to say cookiecutter and then who knows what we're going to give it here, right now I just run the help ok
1:14 So notice the right virtual environment was found because of the naming convention PyCharm knows to load that one up,
1:22 also unnamed is probably not the best name let's just call this cookiecutter, and we'll say single instance of that
1:29 okay, now if we hit run, we should see at the bottom error, no template that's good that's actually okay right, that's what we wanted
1:37 we could pass in like -h for help or whatever but you'll see like it's running down here now. The other thing we can do if we want to fiddle with it
1:47 is we can run it in the terminal, notice the terminal automatically activates the virtual environment
1:53 so we can just say cookiecutter -V to see which one and it's the same one out of the same place
1:58 ok, so we are ready to run, this thing is set up locally it's in our editor, let's go make a change.
2:03 Now, the part that we care about, the part I want to start exploring from is from the CLI, so if you look at the CLI here, you can see it's using click
2:12 which is a thing to basically manage the command line interface for you and down here, there's a bunch of stuff happening right,
2:20 is going to come down here and run of the cookiecutter for us. And you can go see the various pieces that are being called,
2:28 and I would encourage you to sort of explore the flow from here on down so you understand how all the pieces fit together,
2:35 there is some documentation and so on, but I've already done this, I've already figured out where I want to go, so I'm going to go edit this location
2:42 okay, down here notice this is the stuff about config files remember we're working on the YAML thing, that's the default config for the user;
2:52 here is where we're working, at the very root of everything, whether we use a default config, a pass config, a user config,
3:01 we're going to be getting into this get_config() here, okay, we actually will find that the error happens right here,
3:10 so let's go ahead and set-up our system, so we can try that, let's see if we can make this thing fail basically,
3:17 so I want to say cookiecutter and I give it little output folder here,
3:21 cookiecutter cookiecutter-template -o ~/Desktop/test/ and let's just do something simple,
3:26 cookiecutter-template, alright, that runs, everything looks great. Now, let's try that again, but first let's edit and make our YAML defaults broken.
3:41 It doesn't matter if this is our default file or some other one, it's just by default this will be the one selected.
3:47 So I deleted that little bit there, let's try it again. Let's see what mistake did we make- oh there is no pattern for github_username
3:56 what is going on, and if we see where that is, that's in config line 61, which is right here, this is basically in this try accept block.
4:06 So what we need to do is add a little fix right here, so let's do this, let's add a function, right up above here,
4:15 so we're going to call this function fix_missing_endline_in_yaml() We're going to do that here, and what I actually want to do is
4:24 I want to sort of separate this out a bit here so I want to say yaml_lines this is going to return actually a set of lines of text here
4:33 we want to pass this and instead of passing in here like so, we want to create a yaml_text = "".join(yaml_lines)
4:45 put the lines back together, yaml text, okay. So our goal now is to somehow get this value passed in here,
4:53 make some changes, get it back, so say yaml lines equals this we'll say read lines which instead of returning the whole string,
5:00 the whole thing as a string, and we're going to actually return a bunch of lines it's a little easier to work with it that way, so we'll come down here
5:08 and we'll say get all the lines from the original file and then potentially modify them and then turn that back into a flat single string
5:15 which this was expecting, before. Ok, so all we got to do is write this little function here and what do we need to check for-
5:26 let's go ahead and pass in our yaml_lines, we'll return our yaml_lines, ok so we're going to say if yaml lines we don't want to crash
5:33 if for some reason that's not there, and if the yaml_lines the last entry is not equal to just a Newline, and it's Python 2 so Unicode,
5:45 if the last line in this file is not just a blank line, all we have to do is say append a Newline, that's it,
5:55 this should actually fix our code, this should fix that annoying little bug in Cookiecutter and it's six, eight lines of code, not too bad.
6:05 Let's try this now, let's just verify that we do have the missing piece here, and let's try this again; ta-da, fixed.
6:12 How do I know it's fixed, what if I skip this little part right here try it again, it crashes; put it back- fixed.
6:20 Okay, so I think this is a pretty cool little feature, this is not much work PyCharm thinks this is misspelled, so I am going to say it's not,
6:29 so everything looks ok, the one thing that we should probably do is let's go ahead and document this, add some document strings here and some there
6:36 so let's go ahead and add some docstrings here as well, just so people come along know what this thing is about,
6:41 like why is this weird thing adding a Newline, maybe in some time when poyo doesn't need it then we could take this away right.
6:48 Alright, so there is a little description I wrote beforehand so it says if the YAML configuration file does not end with a single bank line
6:56 a cookiecutter exception invalid configuration is thrown within an indecipherable error I think we can all agree that's the current state,
7:03 this method fixes that by adding a trivial Newline if it is missing. Save one more thing here, alright, it looks like it's still working, fabulous.
7:11 Okay, so let's go ahead and submit this back. Now, probably it should be running the test here, right, we should run all of our tests here,
7:20 so running our tests, with tox, give it a moment, ok so it looks like all the tests ran fine, I don't have Python 3.3 or 3.4
7:28 but when we do the check in, the continuous integration and automated build system has all of this stuff set-up, so you see like
7:37 flake 8, PyPy, Python 3.3 and 3.4 didn't quite work but they'll work in the final version, as you'll see in a little bit,
7:44 but at least we saw it worked on Python 2.7 and Python 3.5 and that gives me a lot of confidence that we didn't break anything during this process.
7:53 So, we just pip install tox, and then I just type tox and it runs all these tests, perfect. So now we're ready, we've added our feature,
8:04 created our feature branch, added feature we've run the test we're ready to check this in and create a pull request.


Talk Python's Mastodon Michael Kennedy's Mastodon