Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
pyramid.pdf
Скачиваний:
10
Добавлен:
24.03.2015
Размер:
3.82 Mб
Скачать

CHAPTER

NINETEEN

PASTEDEPLOY CONFIGURATION FILES

Packages generated via a scaffold 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 in 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 scaffolds 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.

19.1 PasteDeploy

PasteDeploy is the system that Pyramid uses to allow deployment settings to be spelled 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.

207

19. PASTEDEPLOY CONFIGURATION FILES

19.1.1 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

[app:main]

 

2

use = egg:MyProject

 

3

 

 

4

pyramid.reload_templates

= true

5

pyramid.debug_authorization = false

6

pyramid.debug_notfound =

false

7

pyramid.debug_routematch

= false

8

pyramid.default_locale_name = en

9

pyramid.includes =

 

10

pyramid_debugtoolbar

 

11

 

 

12[server:main]

13use = egg:waitress#main

14host = 0.0.0.0

15port = 6543

16

17 # Begin logging configuration

18

19[loggers]

20keys = root, myproject

21

22[handlers]

23keys = console

24

25[formatters]

26keys = generic

27

28[logger_root]

29level = INFO

30handlers = console

31

32[logger_myproject]

33level = DEBUG

34handlers =

35qualname = myproject

36

37[handler_console]

38class = StreamHandler

39args = (sys.stderr,)

208

19.1. PASTEDEPLOY

40level = NOTSET

41formatter = generic

42

43[formatter_generic]

44format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s

45

46 # End logging configuration

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

import os

2

 

3

from setuptools import setup, find_packages

4

 

5

here = os.path.abspath(os.path.dirname(__file__))

6

README = open(os.path.join(here, ’README.txt’)).read()

7

CHANGES = open(os.path.join(here, ’CHANGES.txt’)).read()

8

 

9

requires = [

10’pyramid’,

11’pyramid_debugtoolbar’,

12’waitress’,

13]

14

15setup(name=’MyProject’,

16version=’0.0’,

17description=’MyProject’,

18long_description=README + \n\n+ CHANGES,

19classifiers=[

20"Programming Language :: Python",

21"Framework :: Pylons",

22"Topic :: Internet :: WWW/HTTP",

23"Topic :: Internet :: WWW/HTTP :: WSGI :: Application",

24],

25author=’’,

26author_email=’’,

27url=’’,

28keywords=’web pyramid pylons’,

29packages=find_packages(),

30include_package_data=True,

209

19. PASTEDEPLOY CONFIGURATION FILES

31

32

33

34

35

36

37

38

39

zip_safe=False, install_requires=requires, tests_require=requires, test_suite="myproject", entry_points = """\ [paste.app_factory]

main = myproject:main

""",

)

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 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 scaffold-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.

19.1.2 [DEFAULTS] Section of a PasteDeploy .ini File

You can add a [DEFAULT] section to your PasteDeploy .ini file. Such a section should consists 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).

210

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]