You can use the repoze.bfg sample application generator to get started. This generator uses Paste templates to aid in the creation of a new project.
To start a repoze.bfg project, use the paster create facility using the interpreter from the virtualenv (bfgenv) directory you created in Installing repoze.bfg.
1 | $ bin/paster create -t bfg_starter
|
paster create will ask you a single question: the name of the project. You should use a string without spaces and with only letters in it. Here’s sample output from a run of paster create for a project we name MyProject:
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 31 32 33 34 35 36 37 38 39 | $ bin/paster create -t bfg_starter
Selected and implied templates:
repoze.bfg#bfg repoze.bfg starter project
Enter project name: MyProject
Variables:
egg: MyProject
package: myproject
project: MyProject
Creating template bfg
Creating directory ./MyProject
Recursing into +package+
Creating ./MyProject/myproject/
Copying __init__.py to ./MyProject/myproject/__init__.py
Copying configure.zcml to ./MyProject/myproject/configure.zcml
Copying models.py to ./MyProject/myproject/models.py
Copying run.py_tmpl to ./MyProject/myproject/run.py
Recursing into templates
Creating ./MyProject/myproject/templates/
Copying mytemplate.pt to ./MyProject/myproject/templates/mytemplate.pt
Recursing into static
Creating ./MyProject/myproject/templates/static/
Copying default.css to ./MyProject/myproject/templates/static/default.css
Recursing into images
Creating ./MyProject/myproject/templates/static/images/
Copying img01.gif to ./MyProject/myproject/templates/static/images/img01.gif
Copying img02.gif to ./MyProject/myproject/templates/static/images/img02.gif
Copying img03.gif to ./MyProject/myproject/templates/static/images/img03.gif
Copying img04.gif to ./MyProject/myproject/templates/static/images/img04.gif
Copying spacer.gif to ./MyProject/myproject/templates/static/images/spacer.gif
Copying templatelicense.txt to ./MyProject/myproject/templates/static/templatelicense.txt
Copying tests.py_tmpl to ./MyProject/myproject/tests.py
Copying views.py_tmpl to ./MyProject/myproject/views.py
Copying +project+.ini_tmpl to ./MyProject/MyProject.ini
Copying CHANGES.txt_tmpl to ./MyProject/CHANGES.txt
Copying README.txt_tmpl to ./MyProject/README.txt
Copying ez_setup.py to ./MyProject/ez_setup.py
Copying setup.py_tmpl to ./MyProject/setup.py
Running /Users/chrism/projects/repoze/bfg/bin/python setup.py egg_info
|
As a result of invoking paster create, a project is created in a directory named MyProject. That directory is a setuptools project directory from which a Python setuptools distribution can be created. The setup.py file in that directory can be used to distribute your application, or install your application for deployment or development. A sample PasteDeploy .ini file named MyProject.ini will also be created in the project directory. You will use the paster serve command against this .ini file to run your application.
The MyProject project directory contains an additional subdirectory named myproject (note the case difference) representing a Python package which holds very simple repoze.bfg sample code. This is where you’ll edit your application’s Python code and templates.
Note
You can skip the interrogative question about a project name during paster create by adding the project name to the command line, e.g. paster create -t bfg_starter MyProject.
Note
Convenience Paste templates for projects which will depend on ZODB or SQLAlchemy also exist. Use paster create -t bfg_zodb to create a project that depends on ZODB. Use paster create -t bfg_routesalchemy to create a project that depends on SQLAlchemy and URL dispatch (no traversal). Use paster create -t bfg_alchemy to create a project that depends on SQLAlchemy but not URL dispatch (uses only traversal).
Using the interpreter from the virtualenv you create during Installing repoze.bfg, invoke the following command when inside the project directory against the generated setup.py:
1 | $ ../bin/python setup.py develop
|
Elided output from a run of this command is shown below:
1 2 3 | $ ../bin/python setup.py develop
...
Finished processing dependencies for MyProject==0.1
|
This will install your application’s package into the interpreter so it can be found and run as a WSGI application inside a WSGI server.
To run unit tests for your application, you should invoke them like so:
1 | $ ../bin/python setup.py test -q
|
Here’s sample output from a test run:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | $ python setup.py test -q
running test
running egg_info
writing requirements to MyProject.egg-info/requires.txt
writing MyProject.egg-info/PKG-INFO
writing top-level names to MyProject.egg-info/top_level.txt
writing dependency_links to MyProject.egg-info/dependency_links.txt
writing entry points to MyProject.egg-info/entry_points.txt
reading manifest file 'MyProject.egg-info/SOURCES.txt'
writing manifest file 'MyProject.egg-info/SOURCES.txt'
running build_ext
..
----------------------------------------------------------------------
Ran 2 tests in 0.647s
OK
|
The tests are found in the tests.py module in your paster create -generated project. Two sample tests exist.
Once you’ve installed your program for development using setup.py develop, you can use an interactive shell to examine your repoze.bfg application model objects from a Python prompt. To do so, use the paster shell command with the bfgshell argument:
The first argument to bfgshell is the path to your application’s .ini file. The second is the section name inside the .ini file which points to your application as opposed to any other section within the .ini file. For example, if your application .ini file might have a [app:main] section that looks like so:
If so, you can use the following command to invoke a debug shell using the name main as a section name:
1 2 3 4 5 6 7 | [chrism@vitaminf bfgshellenv]$ ../bin/paster bfgshell MyProject.ini main
Python 2.4.5 (#1, Aug 29 2008, 12:27:37)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help" for more information. "root" is the BFG app root object.
>>> root
<foo.models.MyModel object at 0x445270>
|
If that command fails because paster claims it knows nothing about the “bfgshell” command (this happens under certain conditions that are not yet well-understood) try passing the flag --plugin=repoze.bfg before the filename:
1 | [chrism@vitaminf bfgshellenv]$ ../bin/paster --plugin=repoze.bfg bfgshell MyProject.ini main
|
Press “Ctrl-D” to exit the interactive shell.
You should always use a section name argument that refers to the actual app section within the Paste configuration file that points at your BFG application without any middleware wrapping. In particular, a section name is inappropriate as the second argument to “bfgshell” if the configuration section it names is a pipeline rather than an app.
Once the project is installed for development, you can run the application it represents using the paster serve command against the generated MyProject.ini configuration file:
1 | $ ../bin/paster serve MyProject.ini
|
Here’s sample output from a run:
1 2 3 | $ paster serve MyProject.ini
Starting server in PID 16601.
serving on 0.0.0.0:6543 view at http://127.0.0.1:6543
|
By default, generated repoze.bfg applications will listen on port 6543.
Note
During development, it’s often useful to run paster serve using its --reload option. When any Python module your project uses, changes, it will restart the server, which makes development easier, as changes to Python code under repoze.bfg is not put into effect until the server restarts.
Visit http://localhost:6543/ in your browser. You will see something in your browser like what is displayed below:
That’s the page shown by default when you visit an unmodified paster create-generated application.
Our generated repoze.bfg application is a setuptools project (named MyProject), which contains a Python package (which is also named myproject, but lowercased; the paster template generates a project which contains a package that shares its name except for case).
The MyProject project has the following directory structure:
MyProject/
|-- CHANGES.txt
|-- README.txt
|-- ez_setup.py
|-- myproject
| |-- __init__.py
| |-- configure.zcml
| |-- models.py
| |-- run.py
| |-- templates
| | |-- mytemplate.pt
| | `-- static/
| |-- tests.py
| `-- views.py
|-- MyProject.ini
`-- setup.py
The MyProject project is the distribution and deployment wrapper for your application. It contains both the myproject package representing your application as well as files used to describe, run, and test your application.
We won’t describe the CHANGES.txt or README.txt files. ez_setup.py is a file only used by setup.py in case a user who wants to install your package does not have Setuptools already installed. It is only imported by and used by setup.py, so we won’t describe it here either.
The MyProject.ini file is a PasteDeploy configuration file. Its purpose is to specify an application to run when you invoke paster serve when you start an application, as well as the options provided to that application.
The generated MyProject.ini file looks like so:
[DEFAULT]
debug = true
[app:main]
use = egg:MyProject#app
reload_templates = true
debug_authorization = false
debug_notfound = false
[server:main]
use = egg:Paste#http
host = 0.0.0.0
port = 6543
This file contains several “sections” including [DEFAULT], [app:main], and [server:main].
The [DEFAULT] section consists of global parameters that are shared by all the applications, servers and middleware defined within the configuration file. By default it contains one key debug, which is set to true. This key is used by various components to decide whether to act in a “debugging” mode. repoze.bfg itself does not do anything with this parameter as of this writing, and neither does the generated sample application.
The [app:main] section represents configuration for your application. This section name represents the main application (and it’s an app -lication, thus app:main), sigifiying that this is the default application run by paster serve when it is invoked against this configuration file. The name main is a convention signifying that it the default application.
The use setting is required in the [app:main] section. The use setting points at a setuptools entry point named MyProject#app (the egg: prefix in egg:MyProject#app indicates that this is an entry point URI specifier, where the “scheme” is “egg”; there are no other schemes currently, so the egg: prefix is arguably not very useful).
Note
This part of configuration can be confusing so let’s try to clear things up a bit. Take a look at the generated setup.py file for this project. Note that the entry_point line in setup.py points at a string which looks a lot like an .ini file. This string representation of an .ini file has a section named [paste.app_factory]. Within this section, there is a key named app (the entry point name) which has a value myproject.run:app. The key app is what our egg:MyProject#app value of the use section in our config file is pointing at. The value represents a Python “dotted-name” path, which refers to a callable in our myproject package’s run.py module.
In English, this entry point can thus be referred to as a “Paste application factory in the MyProject project which has the entry point named app where the entry point refers to a app function in the mypackage.run module”. If indeed if you open up the run.py module generated within the myproject package, you’ll see a app function. This is the function called PasteDeploy when the paster serve command is invoked against our application. It accepts a global configuration object and returns an instance of our application.
The use setting is the only setting required in the [app:main] section unless you’ve changed the callable referred to by the MyProject#app entry point to accept more arguments: other settings you add to this section are passed as keywords arguments to the callable represented by this entry point (app in our run.py module). You can provide startup-time configuration parameters to your application by requiring more settings in this section.
The reload_templates setting in the [app:main] section is a repoze.bfg-specific setting which is passed into the framework. If it exists, and is true, Chameleon template changes will not require an application restart to be detected. See Automatically Reloading Templates for more information.
Warning
The reload_templates option should be turned off for production applications, as template rendering is slowed when it is turned on.
Various other settings may exist in this section having to do with debugging or influencing runtime behavior of a repoze.bfg application. See Environment and Configuration for more information about these settings.
The [server:main] section of the configuration file configures a WSGI server which listens on port 6543. It is configured to listen on all interfaces (0.0.0.0). The Paste#http server will create a new thread for each request.
Note
In general, repoze.bfg applications should be threading-aware. It is not required that a repoze.bfg application be nonblocking as all application code will run in its own thread, provided by the server you’re using.
See the PasteDeploy documentation for more information about other types of things you can put into this .ini file, such as other applications, middleware and alternate servers.
The setup.py file is a setuptools setup file. It is meant to be run directly from the command line to perform a variety of functions, such as testing your application, packaging, and distributing your application.
Note
setup.py is the defacto standard which Python developers use to distribute their reusable code. You can read more about setup.py files and their usage in the Setuptools documentation.
Our generated setup.py looks like this:
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 31 32 33 34 35 36 37 38 39 40 41 42 43 | import os
from ez_setup import use_setuptools
use_setuptools()
from setuptools import setup, find_packages
here = os.path.abspath(os.path.dirname(__file__))
README = open(os.path.join(here, 'README.txt')).read()
CHANGES = open(os.path.join(here, 'CHANGES.txt')).read()
setup(name='MyProject',
version='0.1',
description='MyProject',
long_description=README + '\n\n' + CHANGES,
classifiers=[
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"Programming Language :: Python",
"Topic :: Internet :: WWW/HTTP",
"Topic :: Internet :: WWW/HTTP :: Dynamic Content",
"Topic :: Internet :: WWW/HTTP :: WSGI",
"Topic :: Internet :: WWW/HTTP :: WSGI :: Application",
],
author='',
author_email='',
url='',
keywords='web wsgi bfg zope',
packages=find_packages(),
include_package_data=True,
zip_safe=False,
install_requires=[
'repoze.bfg',
],
tests_require=[
'repoze.bfg',
],
test_suite="myproject",
entry_points = """\
[paste.app_factory]
app = myproject.run:app
"""
)
|
The top of the file imports and uses ez_setup, which causes Setuptools to be installed on an invoking user’s computer if it isn’t already. The setup.py file calls the setuptools setup function, which does various things depending on the arguments passed to setup.py on the command line.
Within the arguments to this function call, information about your application is kept. While it’s beyond the scope of this documentation to explan everything about setuptools setup files, we’ll provide a whirlwind tour of what exists in this file here.
Your application’s name (this can be any string) is specified in the name field. The version number is specified in the version value. A short description is provided in the description field. The long_description is conventionally the content of the README and CHANGES file appended together. The classifiers field is a list of Trove classifiers describing your application. author and author_email are text fields which probably don’t need any description. url is a field that should point at your application project’s URL (if any). packages=find_packages() causes all packages within the project to be found when packaging the application. include_package_data will include non-Python files when the application is packaged if those files are checked into version control. zip_safe indicates that this package is not safe to ship as a zipped egg (it will unpack as a directory, which is more convenient). install_requires and tests_require indicate that this package depends on the repoze.bfg package. test_suite points at the package for our application, which means all tests found in the package will be installed. We examined entry_points in our discussion of the MyProject.ini file; this file defines the app entry point that represent’s our project’s application.
Usually you only need to think about the contents of the setup.py file when distributing your application to other people, or when versioning your application for your own use. For fun, you can try this command now:
python setup.py sdist
This will create a tarball of your application in a dist subdirectory named MyProject-0.1.tar.gz. You can send this tarball to other people who want to use your application.
Warning
By default, setup.py sdist does not place non-Python-source files in generated tarballs. This means, in this case, that the templates/mytemplate.pt file and the files in the templates/static directory are not packaged in the tarball. To allow this to happen, check all the files that you’d like to be distributed along with your application’s Python files into a version control system such as Subversion. After you do this, when you rerun setup.py sdist, all files checked into the version control system will be included in the tarball.
The myproject package lives inside the MyProject project. It contains:
These are purely conventions established by the paster template: repoze.bfg doesn’t insist that you name things in any particular way.
The configure.zcml represents the application registry. It looks like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <configure xmlns="http://namespaces.repoze.org/bfg">
<!-- this must be included for the view declarations to work -->
<include package="repoze.bfg.includes" />
<view
for=".models.MyModel"
view=".views.my_view"
/>
<view
for=".models.MyModel"
view=".views.static_view"
name="static"
/>
</configure>
|
Much of the heavy lifting in a repoze.bfg application comes in the form of views. A view is the bridge between the content in the model, and the response given back to a browser.
1 2 3 4 5 6 7 8 9 | from repoze.bfg.chameleon_zpt import render_template_to_response
from repoze.bfg.view import static
static_view = static('templates/static')
def my_view(context, request):
return render_template_to_response('templates/mytemplate.pt',
request = request,
project = 'MyProject')
|
Lines 1-2 import required functions.
Line 4 sets up a static_view which will be consulted when visitors visit /static/<something>. This view will serve up CSS and images in our default application. This view is registered in configure.zcml as the static view name for the class MyModel (the root).
Lines 6-9 provide the my_view that was registered as the view. configure.zcml said that the default URL for instances that are of the class MyModel should run this my_view function.
The function is handed two pieces of information: the context and the request. The context is the term model found via traversal (or via URL dispatch). The request is an instance of the WebOb Request class representing the browser’s request to our server.
The view renders a Chameleon template and returns the result as the response. Note that because our MyProject.ini has a reload_templates = true directive indicating that templates should be reloaded when they change, you won’t need to restart the application server to see changes you make to templates. During development, this is handy. If this directive had been false (or if the directive did not exist), you would need to restart the application server for each template change. For production applications, you should set your project’s reload_templates to false to increase the speed at which templates may be rendered.
Note
This example uses render_template_to_response which is a shortcut function. If you want more control over the response, use the render_template function, also present in repoze.bfg.chameleon_zpt. You may then create your own WebOb Response object, using the result of render_template as the response’s body. There is also a get_template API in the same module, which you can use to retrieve the template object without rendering it at all, for additional control.
Note
For more information about the static view helper function see Serving Static Resources Using a View.
The models.py module provides the model data for our application. We write a class named MyModel that provides the behavior.
1 2 3 4 5 6 7 | class MyModel(object):
pass
root = MyModel()
def get_root(environ):
return root
|
In a “real” application, the root object would not be such a simple object. Instead, it would be an object that could access some persistent data store, such as a database. repoze.bfg doesn’t make any assumption about which sort of datastore you’ll want to use, so the sample application uses an instance of MyModel to represent the root.
We need a small Python module that configures our application and advertises itself to our PasteDeploy .ini file. For convenience, we also make it possible to run this module directory without the PasteDeploy configuration file:
1 2 3 4 5 6 7 8 9 10 | from repoze.bfg.router import make_app
def app(global_config, **kw):
""" This function returns a repoze.bfg.router.Router object. It
is usually called by the PasteDeploy framework during ``paster
serve``"""
# paster app config callback
from myproject.models import get_root
import myproject
return make_app(get_root, myproject, options=kw)
|
The single Chameleon template in the project looks like so:
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:tal="http://xml.zope.org/namespaces/tal">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>${project} Application</title>
<meta name="keywords" content="python web application" />
<meta name="description" content="repoze.bfg web application" />
<link href="${request.application_url}/static/default.css" rel="stylesheet" type="text/css" />
</head>
<body>
<!-- start header -->
<div id="logo">
<h2><code>${project}</code>, a <code>repoze.bfg</code> application</h2>
</div>
<div id="header">
<div id="menu">
</div>
</div>
<!-- end header -->
<div id="wrapper">
<!-- start page -->
<div id="page">
<!-- start content -->
<div id="content">
<div class="post">
<h1 class="title">Welcome to <code>${project}</code>, an
application generated by the <a
href="http://static.repoze.org/bfgdocs">repoze.bfg</a> web
application framework.</h1>
</div>
</div>
<!-- end content -->
<!-- start sidebar -->
<div id="sidebar">
<ul>
<li id="search">
<h2>Search<br/> <code>repoze.bfg</code> Documentation</h2>
<form method="get"
action="http://static.repoze.org/bfgdocs/search.html">
<fieldset>
<input type="text" id="q" name="q" value="" />
<input type="submit" id="x" value="Search" />
</fieldset>
</form>
</li>
<li>
<h2><code>repoze.bfg</code> links</h2>
<ul>
<li><a
href="http://static.repoze.org/bfgdocs/#narrative-documentation">Narrative
Documentation</a>
</li>
<li>
<a
href="http://static.repoze.org/bfgdocs/#api-documentation">API
Documentation</a>
</li>
<li>
<a
href="http://static.repoze.org/bfgdocs/#tutorials">Tutorials</a>
</li>
<li>
<a
href="http://static.repoze.org/bfgdocs/#change-history">Change
History</a>
</li>
<li>
<a
href="http://static.repoze.org/bfgdocs/#sample-applications">Sample
Applications</a>
</li>
<li>
<a
href="http://static.repoze.org/bfgdocs/#support-and-development">Support
and Development</a>
</li>
<li>
<a
href="irc://irc.freenode.net#repoze">IRC Channel</a>
</li>
</ul>
</li>
</ul>
</div>
<!-- end sidebar -->
<div style="clear: both;"> </div>
</div>
</div>
<!-- end page -->
<!-- start footer -->
<div id="footer">
<p id="legal">( c ) 2008. All Rights Reserved. Template design
by <a href="http://www.freecsstemplates.org/">Free CSS
Templates</a>.</p>
</div>
<!-- end footer -->
</body>
</html>
|
It displays a default page when rendered. It is referenced by the my_view function in the views.py module. Templates are accessed and used by view functions.
This directory contains static resources which support the mytemplate.pt template. It includes CSS and images.
The tests.py module includes unit tests for your application.
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | import unittest
from repoze.bfg import testing
class ViewTests(unittest.TestCase):
""" These tests are unit tests for the view. They test the
functionality of *only* the view. They register and use dummy
implementations of repoze.bfg functionality to allow you to avoid
testing 'too much'"""
def setUp(self):
""" cleanUp() is required to clear out the application registry
between tests (done in setUp for good measure too)
"""
testing.cleanUp()
def tearDown(self):
""" cleanUp() is required to clear out the application registry
between tests
"""
testing.cleanUp()
def test_my_view(self):
from myproject.views import my_view
context = testing.DummyModel()
request = testing.DummyRequest()
renderer = testing.registerDummyRenderer('templates/mytemplate.pt')
response = my_view(context, request)
renderer.assert_(project='MyProject')
class ViewIntegrationTests(unittest.TestCase):
""" These tests are integration tests for the view. These test
the functionality the view *and* its integration with the rest of
the repoze.bfg framework. They cause the entire environment to be
set up and torn down as if your application was running 'for
real'. This is a heavy-hammer way of making sure that your tests
have enough context to run properly, and it tests your view's
integration with the rest of BFG. You should not use this style
of test to perform 'true' unit testing as tests will run faster
and will be easier to write if you use the testing facilities
provided by bfg and only the registrations you need, as in the
above ViewTests.
"""
def setUp(self):
""" This sets up the application registry with the
registrations your application declares in its configure.zcml
(including dependent registrations for repoze.bfg itself).
"""
testing.cleanUp()
import myproject
import zope.configuration.xmlconfig
zope.configuration.xmlconfig.file('configure.zcml',
package=myproject)
def tearDown(self):
""" Clear out the application registry """
testing.cleanUp()
def test_my_view(self):
from myproject.views import my_view
context = testing.DummyModel()
request = testing.DummyRequest()
result = my_view(context, request)
self.assertEqual(result.status, '200 OK')
body = result.app_iter[0]
self.failUnless('Welcome to' in body)
self.assertEqual(len(result.headerlist), 2)
self.assertEqual(result.headerlist[0],
('content-type', 'text/html; charset=UTF-8'))
self.assertEqual(result.headerlist[1], ('Content-Length',
str(len(body))))
|
This sample tests.py file has a single unit test and a single integration test defined within it. These two tests are executed when you run python setup.py test -q. You may add more tests here as you build your application. You are not required to write tests to use repoze.bfg, this file is simply provided as convenience and example.