ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 02.01.2026
Просмотров: 2729
Скачиваний: 0
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
20.COMMAND-LINE PYRAMID
>>>request.route_url(’home’) ’https://www.example.com/’
20.2.2 IPython or bpython
If you have IPython or bpython or both installed in the interpreter you use to invoke the pshell command, pshell will autodiscover them and use the first respectively found in this order : IPython, bpython, standard Python interpreter. However you could specifically invoke one of your choice with the -p choice or --python-shell choice option.
[chrism@vitaminf shellenv]$ ../bin/pshell -p ipython | bpython | python \ development.ini#MyProject
20.3 Displaying All Application Routes
You can use the proutes command in a terminal window to print a summary of routes related to your application. Much like the pshell command (see The Interactive Shell), the proutes command accepts one argument with the format config_file#section_name. The 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. By default, the section_name is main and can be omitted.
For example:
1 |
[chrism@thinko MyProject]$ ../bin/proutes development.ini |
||
2 |
Name |
Pattern |
View |
3 |
---- |
------- |
---- |
4 |
home |
/ |
<function my_view> |
5 |
home2 |
/ |
<function my_view> |
6 |
another |
/another |
None |
7 |
static/ |
static/*subpath |
<static_view object> |
8 |
catchall |
/*subpath |
<function static_view> |
proutes generates a table. The table has three columns: a Name column, a Pattern column, and a View column. The items listed in the Name column are route names, the items listed in the Pattern column are route patterns, and the items listed in the View column are representations of the view callable that will be invoked when a request matches the associated route pattern. The view column may show None if no associated view callable could be found. If no routes are configured within your application, nothing will be printed to the console when proutes is executed.
216
20.4. DISPLAYING “TWEENS”
20.4 Displaying “Tweens”
A tween is a bit of code that sits between the main Pyramid application request handler and the WSGI application which calls it. A user can get a representation of both the implicit tween ordering (the ordering specified by calls to pyramid.config.Configurator.add_tween()) and the explicit tween ordering (specified by the pyramid.tweens configuration setting) orderings using the ptweens command. Tween factories will show up represented by their standard Python dotted name in the ptweens output.
For example, here’s the pwteens command run against a system configured without any explicit tweens:
1 |
[chrism@thinko pyramid]$ myenv/bin/ptweens development.ini |
|
|
2 |
"pyramid.tweens" config value NOT set (implicitly ordered tweens used) |
||
3 |
|
|
|
4 |
Implicit Tween Chain |
|
|
5 |
|
|
|
6 |
Position |
Name |
Alias |
7 |
-------- |
---- |
----- |
8 |
- |
- |
INGRESS |
9 |
0 |
pyramid_debugtoolbar.toolbar.toolbar_tween_factory |
pdbt |
10 |
1 |
pyramid.tweens.excview_tween_factory |
excview |
11 |
- |
- |
MAIN |
|
|
|
|
Here’s the pwteens command run against a system configured with explicit tweens defined in its development.ini file:
1 |
[chrism@thinko pyramid]$ ptweens development.ini |
|
|
2 |
"pyramid.tweens" config value set (explicitly ordered tweens used) |
||
3 |
|
|
|
4 |
Explicit Tween Chain (used) |
|
|
5 |
|
|
|
6 |
Position |
Name |
|
7 |
-------- |
---- |
|
8 |
- |
INGRESS |
|
9 |
0 |
starter.tween_factory2 |
|
10 |
1 |
starter.tween_factory1 |
|
11 |
2 |
pyramid.tweens.excview_tween_factory |
|
12 |
- |
MAIN |
|
13 |
|
|
|
14 |
Implicit Tween Chain (not used) |
|
|
15 |
|
|
|
16 |
Position |
Name |
Alias |
17 |
-------- |
---- |
----- |
18 |
- |
- |
INGRESS |
217
20. COMMAND-LINE PYRAMID
19
20
21
0 |
pyramid_debugtoolbar.toolbar.toolbar_tween_factory |
pdbt |
1 |
pyramid.tweens.excview_tween_factory |
excview |
- |
- |
MAIN |
|
|
|
Here’s the application configuration section of the development.ini used by the above ptweens command which reports that the explicit tween chain is used:
1 |
[app:main] |
|
2 |
use = egg:starter |
|
3 |
reload_templates |
= true |
4 |
debug_authorization = false |
|
5 |
debug_notfound = |
false |
6 |
debug_routematch |
= false |
7 |
debug_templates = true |
|
8 |
default_locale_name = en |
|
9 |
pyramid.include = pyramid_debugtoolbar |
|
10 |
pyramid.tweens = |
starter.tween_factory2 |
11 |
|
starter.tween_factory1 |
12 |
|
pyramid.tweens.excview_tween_factory |
|
|
|
See Registering “Tweens” for more information about tweens.
20.5 Invoking a Request
You can use the prequest command-line utility to send a request to your application and see the response body without starting a server.
There are two required arguments to prequest:
•The config file/section: 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. The section_name is optional, it defaults to main. For example: development.ini.
•The path: this should be the non-url-quoted path element of the URL to the resource you’d like to be rendered on the server. For example, /.
For example:
218