Juicy lil' bytes

Naked Make | How to Stub a Python Command Line Application Project in Under Five Minutes

Naked is a brand new Python application framework that provides a wide range of features for command line application developers. This tutorial will demonstrate how to go from an empty directory to a complete directory structure with file stubs for development and distribution of a command suite application in under five minutes.

Install Naked

The Naked framework is distributed as a command line executable and a library of Python source files. You can install it with pip using the command:

1
pip install Naked

or download the source, unpack it, and navigate to the top level directory. Then run:

1
python setup.py install

The Naked Executable

The naked executable is run with the naked command. There are a number of useful secondary commands that you can view with naked --help. We will focus on

1
$ naked make

Naked Make

The make command builds the directory tree and project files for a new Python project and can be configured with a simple YAML settings file.

The file and directory structure for command line parsing logic, command development, testing, profiling/benchmarking, licensing, application documentation, and deployment are included in the new project. Help, version, and usage command handling is automatically implemented for you. This eliminates many of the setup tasks for a new project and should allow you to start coding pronto.

How to Create a Python Project from a YAML File

The syntax of the Naked YAML settings file is:

1
2
3
application: <application name>
developer: <developer or organization name>
license: <license name>

Here’s an example of my naked.yaml file from status :

1
2
3
application: status
developer: Christopher Simpkins
license: MIT license

Save this file as naked.yaml in the directory where you would like to create your project directories. Then navigate into the directory to make it your working directory and run:

1
naked make

Naked will prompt you to confirm your project settings. Here is the prompt screen that I received when I created status:

1
2
3
4
5
6
7
8
9
10
11
12
██ naked make
Detected a Naked project YAML setup file (naked.yaml).

Please confirm the information below:

----------------------------------------------------------
 status
 Copyright 2014 Christopher Simpkins
 MIT license
----------------------------------------------------------

Is this correct? (y/n)

If your settings are correct, enter ‘y’ and your project is generated in the same directory with the following structure:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
PROJECT---|
          |
          docs---|
          |      LICENSE
          |      |
          |      README.rst
          |
          tests---(__init__.py)
          |
          |
          lib----|
          |      PROJECT-------|
          |      |             commands---(__init__.py)
          |      |             |
          |      profiler.py   app.py
          |                    |
          |                    settings.py
          |
          MANIFEST.in
          |
          README.md
          |
          setup.cfg
          |
          setup.py

The LICENSE file was populated with the complete text of my MIT license, including the copyright statement and the current year. The setup.py file is set to automatically import my version number from the settings.py file and to populate the long description of the project from the README.rst reStructuredText file in the docs directory (which gets pushed to PyPI as your project description). The app.py file is the main application script and it is fully tricked out to handle help, version, and usage command parsing. I simply added the text that will be displayed to the user for each of these requests in the settings.py file as definitions of the respective string variables.

The project setup.py file is generated in such a way that, when installed on a user’s system (or on your own with a develop install — see below), my project name can be used as the executable:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
$ status --help
---------------------------------------
 status
 Copyright 2014 Christopher Simpkins
 MIT license
---------------------------------------

Status reports the HTTP response status code(s) for GET and POST requests.

USAGE
  status [-p --post] <url>

OPTIONS
  -p  --post    POST request

The default is a GET request.  You can modify this to use a POST request with the -p or --post option.

The URL argument can be entered with or without the protocol. If you do not enter a protocol (http:// or https://), then http:// is assumed.  If you intend to test with the secure HTTP protocol, then make this explicit in your URL.

EXAMPLES
  status http://google.com
  status https://www.google.com
  status google.com
  status -p httpbin.org/post

SOURCE REPOSITORY
  https://github.com/chrissimpkins/status

ISSUE TRACKING
  https://github.com/chrissimpkins/status/issues

In order to begin testing your new project, enter the following in the top level directory that contains your setup.py file:

1
python setup.py develop

And in under 5 minutes, you can begin coding and testing your application without the need to manually address these common setup tasks.

Learn More

Naked is a large project and both the executable as well as the Naked toolshed library contain many more helpful features for command line application developers.

The documentation is available at http://docs.naked-py.com if you’d like to learn more.