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

20.2. THE INTERACTIVE SHELL

40 view predicates (request_param test2)

In this case, we are dealing with a URL dispatch application. This specific URL has two matching routes. The matching route information is displayed first, followed by any views that are associated with that route. As you can see from the second matching route output, a route can be associated with more than one view.

For a URL that doesn’t match any views, pviews will simply print out a Not found message.

20.2 The Interactive Shell

Once you’ve installed your program for development using setup.py develop, you can use an interactive Python shell to execute expressions in a Python environment exactly like the one that will be used when your application runs “for real”. To do so, use the pshell command line utility.

The argument to pshell follows the format config_file#section_name where config_file is the path to your application’s .ini file and section_name is the app section name inside the

.ini file which points to your application. For example, if your application .ini file might have a [app:main] section that looks like so:

1

[app:main]

2

use = egg:MyProject

3

pyramid.reload_templates = true

4

pyramid.debug_authorization = false

5

pyramid.debug_notfound = false

6

pyramid.debug_templates = true

7

pyramid.default_locale_name = en

 

 

If so, you can use the following command to invoke a debug shell using the name main as a section name:

chrism@thinko env26]$

bin/pshell starter/development.ini#main

Python 2.6.5 (r265:79063, Apr 29 2010, 00:31:32)

[GCC 4.4.3] on linux2

 

Type "help" for more information.

Environment:

 

 

app

The WSGI application.

registry

Active

Pyramid registry.

request

Active

request object.

213

20. COMMAND-LINE PYRAMID

root

Root of

the default resource

tree.

root_factory Default

root factory used to

create ‘root‘.

>>> root

<myproject.resources.MyResource object at 0x445270>

>>>registry <Registry myproject>

>>>registry.settings[’pyramid.debug_notfound’] False

>>>from myproject.views import my_view

>>>from pyramid.request import Request

>>>r = Request.blank(’/’)

>>>my_view(r)

{’project’: ’myproject’}

The WSGI application that is loaded will be available in the shell as the app global. Also, if the application that is loaded is the Pyramid app with no surrounding middleware, the root object returned by the default root factory, registry, and request will be available.

You can also simply rely on the main default section name by omitting any hash after the filename:

chrism@thinko env26]$ bin/pshell starter/development.ini

Press Ctrl-D to exit the interactive shell (or Ctrl-Z on Windows).

20.2.1 Extending the Shell

It is convenient when using the interactive shell often to have some variables significant to your application already loaded as globals when you start the pshell. To facilitate this, pshell will look for a special [pshell] section in your INI file and expose the subsequent key/value pairs to the shell. Each key is a variable name that will be global within the pshell session; each value is a dotted Python name. If specified, the special key setup should be a dotted Python name pointing to a callable that accepts the dictionary of globals that will be loaded into the shell. This allows for some custom initializing code to be executed each time the pshell is run. The setup callable can also be specified from the commandline using the --setup option which will override the key in the INI file.

For example, you want to expose your model to the shell, along with the database session so that you can mutate the model on an actual database. Here, we’ll assume your model is stored in the myapp.models package.

214

20.2. THE INTERACTIVE SHELL

1

2

3

4

5

[pshell]

setup = myapp.lib.pshell.setup m = myapp.models

session = myapp.models.DBSession t = transaction

By defining the setup callable, we will create the module myapp.lib.pshell containing a callable named setup that will receive the global environment before it is exposed to the shell. Here we mutate the environment’s request as well as add a new value containing a WebTest version of the application to which we can easily submit requests.

1

# myapp/lib/pshell.py

2

from webtest import TestApp

3

 

4

def setup(env):

5

env[’request’].host = ’www.example.com’

6env[’request’].scheme = ’https’

7env[’testapp’] = TestApp(env[’app’])

When this INI file is loaded, the extra variables m, session and t will be available for use immediately. Since a setup callable was also specified, it is executed and a new variable testapp is exposed, and the request is configured to generate urls from the host http://www.example.com. For example:

chrism@thinko env26]$ bin/pshell starter/development.ini Python 2.6.5 (r265:79063, Apr 29 2010, 00:31:32)

[GCC 4.4.3] on linux2

Type "help" for more information.

Environment:

 

app

The WSGI application.

registry

Active Pyramid registry.

request

Active request object.

root

Root of the default resource tree.

root_factory

Default root factory used to create ‘root‘.

testapp

<webtest.TestApp object at ...>

Custom Variables:

mmyapp.models

session myapp.models.DBSession

ttransaction

>>> testapp.get(’/’)

<200 OK text/html body=’<!DOCTYPE...l>\n’/3337>

215

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