Creating Skeleton Apps with Coily in Spring Python

Exclusive offer: get 50% off this eBook here
Spring Python 1.1

Spring Python 1.1 — Save 50%

Create powerful and versatile Spring Python applications using pragmatic libraries and useful abstractions

$26.99    $13.50
by Greg L. Turnquist | December 2010 | Open Source

Spring Python has many useful building blocks. These illustrate the bottom line task for software developers: delivering runnable applications.

To speed up the process for building apps, Spring Python provides the Python script coily. This script is built to support extensible plugins. The first plugin provided by the Spring Python team is gen-cherrypy-app, which is based on creating a skeleton CherryPy application using Spring Python IoC and security.

In this article, by Greg Lee Turnquist, author of Spring Python 1.1, we will learn:

  • The plugin driven approach of coily, which allows us to utilize plugins written by other developers or to write our own
  • The easy-to-code requirements of creating a plugin
  • Building a CherryPy application from scratch, with fully configured security, using the template-based gen-cherrypy-app plugin

 

Spring Python 1.1

Spring Python 1.1

Create powerful and versatile Spring Python applications using pragmatic libraries and useful abstractions

 

  • Maximize the use of Spring features in Python and develop impressive Spring Python applications
  • Explore the versatility of Spring Python by integrating it with frameworks, libraries, and tools
  • Discover the non-intrusive Spring way of wiring together Python components
  • Packed with hands-on-examples, case studies, and clear explanations for better understanding 
        Read more about this book      

(For more resources on this subject, see here.)

Plugin approach of Coily

coily is a Python script designed from the beginning to provide a plugin based platform for building Spring Python apps. Another important feature is version control of the plugins. Developers should not have to worry about installing an out-of-date plugin that was designed for an older version of Spring Python. coily allows different users on a system to have different sets of plugins installed. It also requires no administrative privileges to install a plugin.

Key functions of coily

coily is included in the standard installation of Spring Python. To see the available commands, just ask for help.

The following table elaborates these commands.

Required parts of a plugin

A coily plugin closely resembles a Python package with some slight tweaks. This doesn't mean that a plugin is meant to be installed as a Python package. It is only a description of the folder structure.

Let's look at the layout of the gen-cherrypy-app plugin as an example.

Some parts of this layout are required, and other parts are not. The top folder is the name of the plugin.

  • A plugin requires a __init__.py file inside the top directory.
  • __init__.py must include a __description__ variable. This description is shown when we run the coily --help command.
  • __init__.py must include a command function, which is either a create or apply function. create is used when the plugin needs one argument from the user. apply is used when no argument is needed from the user.

Let's look at how gen-cherrypy-app meets each of these requirements.

  1. We can already see from the diagram that the top level folder has the same name as our plugin.
  2. Inside __init__.py, we can see the following help message defined.

    __description__ = "plugin to create skeleton CherryPy applications"

  3. gen-cherrypy-app is used to create a skeleton application. It needs the user to supply the name of the application it will create. Again, looking inside __init__.py, the following method signature can be found.

    def create(plugin_path, name)

  4. plugin_path is an argument provided to gen-cherrypy-app by coily, which points at the base directory of gen-cherrypy-app. This argument is also provided for plug-ins that use the apply command function.
  5. name is the name of the application provided by the user.
Spring Python 1.1 Create powerful and versatile Spring Python applications using pragmatic libraries and useful abstractions
Published: May 2010
eBook Price: $26.99
Book Price: $44.99
See more
Select your format and quantity:
        Read more about this book      

(For more resources on this subject, see here.)

Creating a skeleton CherryPy app

The rest of the files listed on the diagram above form a template of an application.

  1. Before we create our CherryPy application, we are missing something. In the earlier screenshot, this plugin wasn't listed. We need to find it and install it. First, let's see how we can look up existing plugins.
  2. Seeing gen-cherrypy-app listed, let's install it using coily without touching a browser.
  3. Let's use the plugin to create a CherryPy application called sample_app. Since we just installed it, the plugin now shows up on coily's help menu. With that in place, we can then run the command to create sample_app.
    • It replaces all instances of ${name} in each file with sample_app
    • It replaces all instances of ${properName} in each file with Sample_app
    • It renames cherrypy-app.py as sample_app.py
  4. gen-cherrypy-app creates a directory named sample_app and copies the files listed above into it. It also does some transformations of the files, based on the argument sample_app.

  5. To run the app, switch to the sample_app directory, and run the main script.

    ./sample_app.py

  6. Now we can visit our running application at http://127.0.0.1:8080.
  7. Let's inspect app_context.py and see some of the key features wired by gen-cherrypy-app.
Spring Python 1.1 Create powerful and versatile Spring Python applications using pragmatic libraries and useful abstractions
Published: May 2010
eBook Price: $26.99
Book Price: $44.99
See more
Select your format and quantity:
        Read more about this book      

(For more resources on this subject, see here.)

This is the root object being wired. The rest of the configuration (not shown here) is mostly security configuration steps.

The security configuration with Spring Python requires many steps. gen-cherrypy-app helps out by pre-wiring most of the security parts, allowing the user to modify as needed rather than build from scratch.

From here, we can log in with the hard-wired credentials to view our expandable application.

Currently, there are no controller objects. However, it would take little effort to add such a layer.

This plugin is simple enough that most of the work spent in improving this plugin can be focused on the template files. This tactic is very useful to build other templates for other types of application.

Summary

coily was built to download, install, and utilize plugins. gen-cherrypy-app nicely creates a Spring-ified CherryPy application using a set of templates. This pattern is easy to replicate for other types of applications.

In this article, we have learned:

  • The basic coily commands used to install and uninstall plugins
  • The structure of a plug-in, including defining the description shown on coily's help screen and the functions needed to process a command
  • That using template files and pattern substitution makes it easy for gen-cherrypy-app to generate CherryPy applications

Further resources on this subject:


About the Author :


Greg L. Turnquist

Greg has worked since 1997 as a software engineer at Harris Corporation, always seeking the right tool for the job. Since 2002, Greg has been part of the senior software team working on Harris' $3.5 billion FAA telco program, architecting mission-critical enterprise apps while managing a software team. He provides after hours support and 2nd-level engineering to support the nation-wide telco network and is no stranger to midnight failures and software triage. In 2010, Greg joined the SpringSource division of VMware.

Being a test-bitten script junky, Greg has used JUnit, TestNG, JMock, FEST, PyUnit, and pMock testing frameworks, along with other agile practices to produce top-quality code.

He has worked with Java, Spring, Spring Security, AspectJ, and Jython technologies and also developed sophisticated scripts for *nix and Windows platforms. Being a wiki evangelist, he also deployed a LAMP-based wiki website to provide finger-tip knowledge to users.

In 2006, Greg created the Spring Python project. The Spring Framework provided many useful features, and he wanted those same features available when working with Python.

Greg completed a master's degree in Computer Engineering at Auburn University, and lives in the United States with his family.

Books From Packt


Python Text Processing with NLTK 2.0 Cookbook
Python Text Processing with NLTK 2.0 Cookbook

MySQL for Python
MySQL for Python

Python 2.6 Text Processing Beginners Guide
Python 2.6 Text Processing Beginners Guide

Python Multimedia
Python Multimedia

Python Geo-Spatial Development
Python Geo-Spatial Development

wxPython 2.8 Application Development Cookbook
wxPython 2.8 Application Development Cookbook

Python Testing: Beginner's Guide
Python Testing: Beginner's Guide

Python 3 Object Oriented Programming
Python 3 Object Oriented Programming


Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software