PasteDeploy Configuration Files¶
Packages generated via a cookiecutter make use of a system created by Ian
Bicking named PasteDeploy. PasteDeploy defines a way to declare
WSGI application configuration in an .ini file.
Pyramid uses this configuration file format as input to its WSGI server
runner pserve, as well as other commands such as pviews, pshell,
proutes, and ptweens.
PasteDeploy is not a particularly integral part of Pyramid. It's possible to create a Pyramid application which does not use PasteDeploy at all. We show a Pyramid application that doesn't use PasteDeploy in Creating Your First Pyramid Application. However, all Pyramid cookiecutters render PasteDeploy configuration files, to provide new developers with a standardized way of setting deployment values, and to provide new users with a standardized way of starting, stopping, and debugging an application.
This chapter is not a replacement for documentation about PasteDeploy; it only contextualizes the use of PasteDeploy within Pyramid. For detailed documentation, see http://pythonpaste.org/deploy/.
PasteDeploy¶
PasteDeploy is the system that Pyramid uses to allow deployment
settings to be specified using an .ini configuration file format. It also
allows the pserve command to work. Its configuration format provides a
convenient place to define application deployment settings and WSGI
server settings, and its server runner allows you to stop and start a Pyramid
application easily.
Entry Points and PasteDeploy .ini Files¶
In the Creating a Pyramid Project chapter, we breezed over the meaning of a
configuration line in the deployment.ini file. This was the use =
egg:myproject line in the [app:main] section. We breezed over it because
it's pretty confusing and "too much information" for an introduction to the
system. We'll try to give it a bit of attention here. Let's see the config
file again:
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 | ###
# app configuration
# http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/environment.html
###
[app:main]
use = egg:myproject
pyramid.reload_templates = true
pyramid.debug_authorization = false
pyramid.debug_notfound = false
pyramid.debug_routematch = false
pyramid.default_locale_name = en
pyramid.includes =
pyramid_debugtoolbar
# By default, the toolbar only appears for clients from IP addresses
# '127.0.0.1' and '::1'.
# debugtoolbar.hosts = 127.0.0.1 ::1
###
# wsgi server configuration
###
[server:main]
use = egg:waitress#main
listen = 127.0.0.1:6543 [::1]:6543
###
# logging configuration
# http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/logging.html
###
[loggers]
keys = root, myproject
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = INFO
handlers = console
[logger_myproject]
level = DEBUG
handlers =
qualname = myproject
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s:%(lineno)s][%(threadName)s] %(message)s
|
The line in [app:main] above that says use = egg:myproject is actually
shorthand for a longer spelling: use = egg:myproject#main. The #main
part is omitted for brevity, as #main is a default defined by PasteDeploy.
egg:myproject#main is a string which has meaning to PasteDeploy. It points
at a setuptools entry point named main defined in the
myproject project.
Take a look at the generated setup.py file for this project.
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 | import os
from setuptools import setup, find_packages
here = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(here, 'README.txt')) as f:
README = f.read()
with open(os.path.join(here, 'CHANGES.txt')) as f:
CHANGES = f.read()
requires = [
'pyramid',
'pyramid_jinja2',
'pyramid_debugtoolbar',
'waitress',
]
tests_require = [
'WebTest >= 1.3.1', # py3 compat
'pytest',
'pytest-cov',
]
setup(
name='myproject',
version='0.0',
description='MyProject',
long_description=README + '\n\n' + CHANGES,
classifiers=[
'Programming Language :: Python',
'Framework :: Pyramid',
'Topic :: Internet :: WWW/HTTP',
'Topic :: Internet :: WWW/HTTP :: WSGI :: Application',
],
author='',
author_email='',
url='',
keywords='web pyramid pylons',
packages=find_packages(),
include_package_data=True,
zip_safe=False,
extras_require={
'testing': tests_require,
},
install_requires=requires,
entry_points={
'paste.app_factory': [
'main = myproject:main',
],
},
)
|
Note that entry_points is assigned 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
main (the entry point name) which has a value myproject:main. The
key main is what our egg:myproject#main value of the use section
in our config file is pointing at, although it is actually shortened to
egg:myproject there. The value represents a dotted Python name
path, which refers to a callable in our myproject package's __init__.py
module.
The egg: prefix in egg:myproject indicates that this is an entry point
URI specifier, where the "scheme" is "egg". An "egg" is created when you run
setup.py install or setup.py develop within your project.
In English, this entry point can thus be referred to as a "PasteDeploy
application factory in the myproject project which has the entry point
named main where the entry point refers to a main function in the
mypackage module". Indeed, if you open up the __init__.py module
generated within any cookiecutter-generated package, you'll see a main
function. This is the function called by PasteDeploy when the
pserve command is invoked against our application. It accepts a global
configuration object and returns an instance of our application.
[DEFAULT] Section of a PasteDeploy .ini File¶
You can add a [DEFAULT] section to your PasteDeploy .ini file. Such a
section should consist of global parameters that are shared by all the
applications, servers, and middleware defined within the configuration
file. The values in a [DEFAULT] section will be passed to your
application's main function as global_config (see the reference to the
main function in __init__.py).