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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
|
.. _declarative_chapter:
Declarative Configuration
=========================
The mode of configuration most comprehensively detailed by examples in
narrative chapters in this book is "imperative" configuration. This is the
configuration mode in which a developer cedes the least amount of control to
the framework; it's "imperative" because you express the configuration
directly in Python code, and you have the full power of Python at your
disposal as you issue configuration statements. However, another mode of
configuration exists within :mod:`pyramid`, which often provides better
extensibility and configuration conflict detection.
A complete listing of ZCML directives is available within
:ref:`zcml_directives`. This chapter provides an overview of how you might
get started with ZCML and highlights some common tasks performed when you use
ZCML. You can get a better understanding of when it's appropriate to use
ZCML from :ref:`extending_chapter`.
.. index::
single: declarative configuration
.. _declarative_configuration:
Declarative Configuration
-------------------------
A :mod:`pyramid` application can be configured "declaratively", if so
desired. Declarative configuration relies on *declarations* made external to
the code in a configuration file format named :term:`ZCML` (Zope
Configuration Markup Language), an XML dialect.
A :mod:`pyramid` application configured declaratively requires not
one, but two files: a Python file and a :term:`ZCML` file.
In a file named ``helloworld.py``:
.. code-block:: python
:linenos:
from paste.httpserver import serve
from pyramid.response import Response
from pyramid.configuration import Configurator
def hello_world(request):
return Response('Hello world!')
if __name__ == '__main__':
config = Configurator()
config.begin()
config.load_zcml('configure.zcml')
config.end()
app = config.make_wsgi_app()
serve(app, host='0.0.0.0')
In a file named ``configure.zcml`` in the same directory as the
previously created ``helloworld.py``:
.. code-block:: xml
:linenos:
<configure xmlns="http://pylonshq.com/pyramid">
<include package="pyramid.includes" />
<view
view="helloworld.hello_world"
/>
</configure>
This pair of files forms an application functionally equivalent to the
application we created earlier in :ref:`imperative_configuration`.
Let's examine the differences between that code listing and the code
above.
In :ref:`imperative_configuration`, we had the following lines within
the ``if __name__ == '__main__'`` section of ``helloworld.py``:
.. code-block:: python
:linenos:
if __name__ == '__main__':
config = Configurator()
config.begin()
config.add_view(hello_world)
config.end()
app = config.make_wsgi_app()
serve(app, host='0.0.0.0')
In our "declarative" code, we've removed the call to ``add_view`` and
replaced it with a call to the
:meth:`pyramid.configuration.Configurator.load_zcml` method so that
it now reads as:
.. code-block:: python
:linenos:
if __name__ == '__main__':
config = Configurator()
config.begin()
config.load_zcml('configure.zcml')
config.end()
app = config.make_wsgi_app()
serve(app, host='0.0.0.0')
Everything else is much the same.
The ``config.load_zcml('configure.zcml')`` line tells the configurator
to load configuration declarations from the file named
``configure.zcml`` which sits next to ``helloworld.py`` on the
filesystem. Let's take a look at that ``configure.zcml`` file again:
.. code-block:: xml
:linenos:
<configure xmlns="http://pylonshq.com/pyramid">
<include package="pyramid.includes" />
<view
view="helloworld.hello_world"
/>
</configure>
Note that this file contains some XML, and that the XML contains a
``<view>`` :term:`configuration declaration` tag that references a
:term:`dotted Python name`. This dotted name refers to the
``hello_world`` function that lives in our ``helloworld`` Python
module.
This ``<view>`` declaration tag performs the same function as the
``add_view`` method that was employed within
:ref:`imperative_configuration`. In fact, the ``<view>`` tag is
effectively a "macro" which calls the
:meth:`pyramid.configuration.Configurator.add_view` method on your
behalf.
The ``<view>`` tag is an example of a :mod:`pyramid` declaration
tag. Other such tags include ``<route>`` and ``<scan>``. Each of
these tags is effectively a "macro" which calls methods of a
:class:`pyramid.configuration.Configurator` object on your behalf.
Essentially, using a :term:`ZCML` file and loading it from the
filesystem allows us to put our configuration statements within this
XML file rather as declarations, rather than representing them as
method calls to a :term:`Configurator` object. Otherwise, declarative
and imperative configuration are functionally equivalent.
Using declarative configuration has a number of benefits, the primary
benefit being that applications configured declaratively can be
*overridden* and *extended* by third parties without requiring the
third party to change application code. If you want to build a
framework or an extensible application, using declarative
configuration is a good idea.
Declarative configuration has an obvious downside: you can't use
plain-old-Python syntax you probably already know and understand to
configure your application; instead you need to use :term:`ZCML`.
.. index::
single: ZCML conflict detection
ZCML Conflict Detection
~~~~~~~~~~~~~~~~~~~~~~~
A minor additional feature of ZCML is *conflict detection*. If you
define two declaration tags within the same ZCML file which logically
"collide", an exception will be raised, and the application will not
start. For example, the following ZCML file has two conflicting
``<view>`` tags:
.. code-block:: xml
:linenos:
<configure xmlns="http://pylonshq.com/pyramid">
<include package="pyramid.includes" />
<view
view="helloworld.hello_world"
/>
<view
view="helloworld.hello_world"
/>
</configure>
If you try to use this ZCML file as the source of ZCML for an
application, an error will be raised when you attempt to start the
application. This error will contain information about which tags
might have conflicted.
.. index::
single: helloworld (declarative)
.. _helloworld_declarative:
Hello World, Goodbye World (Declarative)
----------------------------------------
Another almost entirely equivalent mode of application configuration
exists named *declarative* configuration. :mod:`pyramid` can be
configured for the same "hello world" application "declaratively", if
so desired.
To do so, first, create a file named ``helloworld.py``:
.. code-block:: python
:linenos:
from pyramid.configuration import Configurator
from pyramid.response import Response
from paste.httpserver import serve
def hello_world(request):
return Response('Hello world!')
def goodbye_world(request):
return Response('Goodbye world!')
if __name__ == '__main__':
config = Configurator()
config.begin()
config.load_zcml('configure.zcml')
config.end()
app = config.make_wsgi_app()
serve(app, host='0.0.0.0')
Then create a file named ``configure.zcml`` in the same directory as
the previously created ``helloworld.py``:
.. code-block:: xml
:linenos:
<configure xmlns="http://pylonshq.com/pyramid">
<include package="pyramid.includes" />
<view
view="helloworld.hello_world"
/>
<view
name="goodbye"
view="helloworld.goodbye_world"
/>
</configure>
This pair of files forms an application functionally equivalent to the
application we created earlier in :ref:`helloworld_imperative`. We can run
it the same way.
.. code-block:: bash
$ python helloworld.py
serving on 0.0.0.0:8080 view at http://127.0.0.1:8080
Let's examine the differences between the code in that section and the code
above. In :ref:`helloworld_imperative_appconfig`, we had the following lines
within the ``if __name__ == '__main__'`` section of ``helloworld.py``:
.. code-block:: python
:linenos:
if __name__ == '__main__':
config = Configurator()
config.begin()
config.add_view(hello_world)
config.add_view(goodbye_world, name='goodbye')
config.end()
app = config.make_wsgi_app()
serve(app, host='0.0.0.0')
In our "declarative" code, we've added a call to the
:meth:`pyramid.configuration.Configurator.load_zcml` method with
the value ``configure.zcml``, and we've removed the lines which read
``config.add_view(hello_world)`` and ``config.add_view(goodbye_world,
name='goodbye')``, so that it now reads as:
.. code-block:: python
:linenos:
if __name__ == '__main__':
config = Configurator()
config.begin()
config.load_zcml('configure.zcml')
config.end()
app = config.make_wsgi_app()
serve(app, host='0.0.0.0')
Everything else is much the same.
The ``config.load_zcml('configure.zcml')`` line tells the configurator
to load configuration declarations from the ``configure.zcml`` file
which sits next to ``helloworld.py``. Let's take a look at the
``configure.zcml`` file now:
.. code-block:: xml
:linenos:
<configure xmlns="http://pylonshq.com/pyramid">
<include package="pyramid.includes" />
<view
view="helloworld.hello_world"
/>
<view
name="goodbye"
view="helloworld.goodbye_world"
/>
</configure>
We already understand what the view code does, because the application
is functionally equivalent to the application described in
:ref:`helloworld_imperative`, but use of :term:`ZCML` is new. Let's
break that down tag-by-tag.
The ``<configure>`` Tag
~~~~~~~~~~~~~~~~~~~~~~~
The ``configure.zcml`` ZCML file contains this bit of XML:
.. code-block:: xml
:linenos:
<configure xmlns="http://pylonshq.com/pyramid">
<!-- other directives -->
</configure>
Because :term:`ZCML` is XML, and because XML requires a single root
tag for each document, every ZCML file used by :mod:`pyramid` must
contain a ``configure`` container directive, which acts as the root
XML tag. It is a "container" directive because its only job is to
contain other directives.
See also :ref:`configure_directive` and :ref:`word_on_xml_namespaces`.
The ``<include>`` Tag
~~~~~~~~~~~~~~~~~~~~~
The ``configure.zcml`` ZCML file contains this bit of XML within the
``<configure>`` root tag:
.. code-block:: xml
<include package="pyramid.includes" />
This self-closing tag instructs :mod:`pyramid` to load a ZCML file
from the Python package with the :term:`dotted Python name`
``pyramid.includes``, as specified by its ``package`` attribute.
This particular ``<include>`` declaration is required because it
actually allows subsequent declaration tags (such as ``<view>``, which
we'll see shortly) to be recognized. The ``<include>`` tag
effectively just includes another ZCML file, causing its declarations
to be executed. In this case, we want to load the declarations from
the file named ``configure.zcml`` within the
:mod:`pyramid.includes` Python package. We know we want to load
the ``configure.zcml`` from this package because ``configure.zcml`` is
the default value for another attribute of the ``<include>`` tag named
``file``. We could have spelled the include tag more verbosely, but
equivalently as:
.. code-block:: xml
:linenos:
<include package="pyramid.includes"
file="configure.zcml"/>
The ``<include>`` tag that includes the ZCML statements implied by the
``configure.zcml`` file from the Python package named
:mod:`pyramid.includes` is basically required to come before any
other named declaration in an application's ``configure.zcml``. If it
is not included, subsequent declaration tags will fail to be
recognized, and the configuration system will generate an error at
startup. However, the ``<include package="pyramid.includes"/>``
tag needs to exist only in a "top-level" ZCML file, it needn't also
exist in ZCML files *included by* a top-level ZCML file.
See also :ref:`include_directive`.
The ``<view>`` Tag
~~~~~~~~~~~~~~~~~~
The ``configure.zcml`` ZCML file contains these bits of XML *after* the
``<include>`` tag, but *within* the ``<configure>`` root tag:
.. code-block:: xml
:linenos:
<view
view="helloworld.hello_world"
/>
<view
name="goodbye"
view="helloworld.goodbye_world"
/>
These ``<view>`` declaration tags direct :mod:`pyramid` to create
two :term:`view configuration` registrations. The first ``<view>``
tag has an attribute (the attribute is also named ``view``), which
points at a :term:`dotted Python name`, referencing the
``hello_world`` function defined within the ``helloworld`` package.
The second ``<view>`` tag has a ``view`` attribute which points at a
:term:`dotted Python name`, referencing the ``goodbye_world`` function
defined within the ``helloworld`` package. The second ``<view>`` tag
also has an attribute called ``name`` with a value of ``goodbye``.
These effect of the ``<view>`` tag declarations we've put into our
``configure.zcml`` is functionally equivalent to the effect of lines
we've already seen in an imperatively-configured application. We're
just spelling things differently, using XML instead of Python.
In our previously defined application, in which we added view
configurations imperatively, we saw this code:
.. ignore-next-block
.. code-block:: python
:linenos:
config.add_view(hello_world)
config.add_view(goodbye_world, name='goodbye')
Each ``<view>`` declaration tag encountered in a ZCML file effectively
invokes the :meth:`pyramid.configuration.Configurator.add_view`
method on the behalf of the developer. Various attributes can be
specified on the ``<view>`` tag which influence the :term:`view
configuration` it creates.
Since the relative ordering of calls to
:meth:`pyramid.configuration.Configurator.add_view` doesn't matter
(see the sidebar entitled *View Dispatch and Ordering* within
:ref:`adding_configuration`), the relative order of ``<view>`` tags in
ZCML doesn't matter either. The following ZCML orderings are
completely equivalent:
.. topic:: Hello Before Goodbye
.. code-block:: xml
:linenos:
<view
view="helloworld.hello_world"
/>
<view
name="goodbye"
view="helloworld.goodbye_world"
/>
.. topic:: Goodbye Before Hello
.. code-block:: xml
:linenos:
<view
name="goodbye"
view="helloworld.goodbye_world"
/>
<view
view="helloworld.hello_world"
/>
We've now configured a :mod:`pyramid` helloworld application
declaratively. More information about this mode of configuration is
available in :ref:`declarative_configuration` and within
:ref:`zcml_reference`.
.. _zcml_scanning:
Scanning via ZCML
-----------------
:term:`ZCML` can invoke a :term:`scan` via its ``<scan>`` directive. If a
ZCML file is processed that contains a scan directive, the package the ZCML
file points to is scanned.
.. code-block:: python
:linenos:
# helloworld.py
from paste.httpserver import serve
from pyramid.response import Response
from pyramid.view import view_config
@view_config()
def hello(request):
return Response('Hello')
if __name__ == '__main__':
from pyramid.configuration import Configurator
config = Configurator()
config.begin()
config.load_zcml('configure.zcml')
config.end()
app = config.make_wsgi_app()
serve(app, host='0.0.0.0')
.. code-block:: xml
:linenos:
<configure xmlns="http://namespaces.repoze.org">
<!-- configure.zcml -->
<include package="pyramid.includes"/>
<scan package="."/>
</configure>
See also :ref:`scan_directive`.
Which Mode Should I Use?
------------------------
A combination of imperative configuration, declarative configuration
via ZCML and scanning can be used to configure any application. They
are not mutually exclusive.
The :mod:`pyramid` authors often recommend using mostly declarative
configuration, because it's the more traditional form of configuration
used in :mod:`pyramid` applications, it can be overridden and
extended by third party deployers, and there are more examples for it
"in the wild".
However, imperative mode configuration can be simpler to understand,
and the framework is not "opinionated" about the choice. This book
presents examples in both styles, mostly interchangeably. You can
choose the mode that best fits your brain as necessary.
.. index::
single: ZCML view configuration
.. _mapping_views_using_zcml_section:
View Configuration Via ZCML
~~~~~~~~~~~~~~~~~~~~~~~~~~~
You may associate a view with a URL by adding :ref:`view_directive`
declarations via :term:`ZCML` in a ``configure.zcml`` file. An
example of a view declaration in ZCML is as follows:
.. code-block:: xml
:linenos:
<view
context=".models.Hello"
view=".views.hello_world"
name="hello.html"
/>
The above maps the ``.views.hello_world`` view callable function to
the following set of :term:`context finding` results:
- A :term:`context` object which is an instance (or subclass) of the
Python class represented by ``.models.Hello``
- A :term:`view name` equalling ``hello.html``.
.. note:: Values prefixed with a period (``.``) for the ``context``
and ``view`` attributes of a ``view`` declaration (such as those
above) mean "relative to the Python package directory in which this
:term:`ZCML` file is stored". So if the above ``view`` declaration
was made inside a ``configure.zcml`` file that lived in the
``hello`` package, you could replace the relative ``.models.Hello``
with the absolute ``hello.models.Hello``; likewise you could
replace the relative ``.views.hello_world`` with the absolute
``hello.views.hello_world``. Either the relative or absolute form
is functionally equivalent. It's often useful to use the relative
form, in case your package's name changes. It's also shorter to
type.
You can also declare a *default view callable* for a :term:`model`
type:
.. code-block:: xml
:linenos:
<view
context=".models.Hello"
view=".views.hello_world"
/>
A *default view callable* simply has no ``name`` attribute. For the
above registration, when a :term:`context` is found that is of the
type ``.models.Hello`` and there is no :term:`view name` associated
with the result of :term:`context finding`, the *default view
callable* will be used. In this case, it's the view at
``.views.hello_world``.
A default view callable can alternately be defined by using the empty
string as its ``name`` attribute:
.. code-block:: xml
:linenos:
<view
context=".models.Hello"
view=".views.hello_world"
name=""
/>
You may also declare that a view callable is good for any context type
by using the special ``*`` character as the value of the ``context``
attribute:
.. code-block:: xml
:linenos:
<view
context="*"
view=".views.hello_world"
name="hello.html"
/>
This indicates that when :mod:`pyramid` identifies that the
:term:`view name` is ``hello.html`` and the context is of any type,
the ``.views.hello_world`` view callable will be invoked.
A ZCML ``view`` declaration's ``view`` attribute can also name a
class. In this case, the rules described in :ref:`class_as_view`
apply for the class which is named.
See :ref:`view_directive` for complete ZCML directive documentation.
.. index::
single: ZCML directive; route
.. _zcml_route_configuration:
Configuring a Route via ZCML
----------------------------
Instead of using the imperative
:meth:`pyramid.configuration.Configurator.add_route` method to add a new
route, you can alternately use :term:`ZCML`. :ref:`route_directive`
statements in a :term:`ZCML` file used by your application is a sign that
you're using :term:`URL dispatch`. For example, the following :term:`ZCML
declaration` causes a route to be added to the application.
.. code-block:: xml
:linenos:
<route
name="myroute"
pattern="/prefix/:one/:two"
view=".views.myview"
/>
.. note::
Values prefixed with a period (``.``) within the values of ZCML
attributes such as the ``view`` attribute of a ``route`` mean
"relative to the Python package directory in which this
:term:`ZCML` file is stored". So if the above ``route``
declaration was made inside a ``configure.zcml`` file that lived in
the ``hello`` package, you could replace the relative
``.views.myview`` with the absolute ``hello.views.myview`` Either
the relative or absolute form is functionally equivalent. It's
often useful to use the relative form, in case your package's name
changes. It's also shorter to type.
The order that routes are evaluated when declarative configuration is used
is the order that they appear relative to each other in the ZCML file.
See :ref:`route_directive` for full ``route`` ZCML directive
documentation.
.. index::
triple: view; zcml; static resource
.. _zcml_static_resources_section:
Serving Static Resources Using ZCML
------------------------------------
Use of the ``static`` ZCML directive makes static files available at a name
relative to the application root URL, e.g. ``/static``.
Note that the ``path`` provided to the ``static`` ZCML directive may be a
fully qualified :term:`resource specification`, a package-relative path, or
an *absolute path*. The ``path`` with the value ``a/b/c/static`` of a
``static`` directive in a ZCML file that resides in the "mypackage" package
will resolve to a package-qualified resource such as
``some_package:a/b/c/static``.
Here's an example of a ``static`` ZCML directive that will serve files
up under the ``/static`` URL from the ``/var/www/static`` directory of
the computer which runs the :mod:`pyramid` application using an
absolute path.
.. code-block:: xml
:linenos:
<static
name="static"
path="/var/www/static"
/>
Here's an example of a ``static`` directive that will serve files up
under the ``/static`` URL from the ``a/b/c/static`` directory of the
Python package named ``some_package`` using a fully qualified
:term:`resource specification`.
.. code-block:: xml
:linenos:
<static
name="static"
path="some_package:a/b/c/static"
/>
Here's an example of a ``static`` directive that will serve files up
under the ``/static`` URL from the ``static`` directory of the Python
package in which the ``configure.zcml`` file lives using a
package-relative path.
.. code-block:: xml
:linenos:
<static
name="static"
path="static"
/>
Whether you use for ``path`` a fully qualified resource specification,
an absolute path, or a package-relative path, When you place your
static files on the filesystem in the directory represented as the
``path`` of the directive, you will then be able to view the static
files in this directory via a browser at URLs prefixed with the
directive's ``name``. For instance if the ``static`` directive's
``name`` is ``static`` and the static directive's ``path`` is
``/path/to/static``, ``http://localhost:6543/static/foo.js`` will
return the file ``/path/to/static/dir/foo.js``. The static directory
may contain subdirectories recursively, and any subdirectories may
hold files; these will be resolved by the static view as you would
expect.
While the ``path`` argument can be a number of different things, the
``name`` argument of the ``static`` ZCML directive can also be one of
a number of things: a *view name* or a *URL*. The above examples have
shown usage of the ``name`` argument as a view name. When ``name`` is
a *URL* (or any string with a slash (``/``) in it), static resources
can be served from an external webserver. In this mode, the ``name``
is used as the URL prefix when generating a URL using
:func:`pyramid.url.static_url`.
For example, the ``static`` ZCML directive may be fed a ``name``
argument which is ``http://example.com/images``:
.. code-block:: xml
:linenos:
<static
name="http://example.com/images"
path="mypackage:images"
/>
Because the ``static`` ZCML directive is provided with a ``name`` argument
that is the URL prefix ``http://example.com/images``, subsequent calls to
:func:`pyramid.url.static_url` with paths that start with the ``path``
argument passed to :meth:`pyramid.url.static_url` will generate a URL
something like ``http://example.com/logo.png``. The external webserver
listening on ``example.com`` must be itself configured to respond properly to
such a request. The :func:`pyramid.url.static_url` API is discussed in more
detail later in this chapter.
The :meth:`pyramid.configuration.Configurator.add_static_view` method offers
an imperative equivalent to the ``static`` ZCML directive. Use of the
``add_static_view`` imperative configuration method is completely equivalent
to using ZCML for the same purpose. See :ref:`static_resources_section` for
more information.
.. _zcml_authorization_policy:
Enabling an Authorization Policy Via ZCML
-----------------------------------------
If you'd rather use :term:`ZCML` to specify an authorization policy
than imperative configuration, modify the ZCML file loaded by your
application (usually named ``configure.zcml``) to enable an
authorization policy.
For example, to enable a policy which compares the value of an "auth
ticket" cookie passed in the request's environment which contains a
reference to a single :term:`principal` against the principals present
in any :term:`ACL` found in model data when attempting to call some
:term:`view`, modify your ``configure.zcml`` to look something like
this:
.. code-block:: xml
:linenos:
<configure xmlns="http://pylonshq.com/pyramid">
<!-- views and other directives before this... -->
<authtktauthenticationpolicy
secret="iamsosecret"/>
<aclauthorizationpolicy/>
</configure>
"Under the hood", these statements cause an instance of the class
:class:`pyramid.authentication.AuthTktAuthenticationPolicy` to be
injected as the :term:`authentication policy` used by this application
and an instance of the class
:class:`pyramid.authorization.ACLAuthorizationPolicy` to be
injected as the :term:`authorization policy` used by this application.
:mod:`pyramid` ships with a number of authorization and
authentication policy ZCML directives that should prove useful. See
:ref:`authentication_policies_directives_section` and
:ref:`authorization_policies_directives_section` for more information.
.. index::
pair: ZCML directive; authentication policy
.. _authentication_policies_directives_section:
Built-In Authentication Policy ZCML Directives
----------------------------------------------
Instead of configuring an authentication policy and authorization
policy imperatively, :mod:`pyramid` ships with a few "pre-chewed"
authentication policy ZCML directives that you can make use of within
your application.
``authtktauthenticationpolicy``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When this directive is used, authentication information is obtained
from an "auth ticket" cookie value, assumed to be set by a custom
login form.
An example of its usage, with all attributes fully expanded:
.. code-block:: xml
:linenos:
<authtktauthenticationpolicy
secret="goshiamsosecret"
callback=".somemodule.somefunc"
cookie_name="mycookiename"
secure="false"
include_ip="false"
timeout="86400"
reissue_time="600"
max_age="31536000"
path="/"
http_only="false"
/>
See :ref:`authtktauthenticationpolicy_directive` for details about
this directive.
``remoteuserauthenticationpolicy``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When this directive is used, authentication information is obtained
from a ``REMOTE_USER`` key in the WSGI environment, assumed to
be set by a WSGI server or an upstream middleware component.
An example of its usage, with all attributes fully expanded:
.. code-block:: xml
:linenos:
<remoteuserauthenticationpolicy
environ_key="REMOTE_USER"
callback=".somemodule.somefunc"
/>
See :ref:`remoteuserauthenticationpolicy_directive` for detailed
information.
``repozewho1authenticationpolicy``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When this directive is used, authentication information is obtained
from a ``repoze.who.identity`` key in the WSGI environment, assumed to
be set by :term:`repoze.who` middleware.
An example of its usage, with all attributes fully expanded:
.. code-block:: xml
:linenos:
<repozewho1authenticationpolicy
identifier_name="auth_tkt"
callback=".somemodule.somefunc"
/>
See :ref:`repozewho1authenticationpolicy_directive` for detailed
information.
.. index::
pair: ZCML directive; authorization policy
.. _authorization_policies_directives_section:
Built-In Authorization Policy ZCML Directives
---------------------------------------------
``aclauthorizationpolicy``
When this directive is used, authorization information is obtained
from :term:`ACL` objects attached to model instances.
An example of its usage, with all attributes fully expanded:
.. code-block:: xml
:linenos:
<aclauthorizationpolicy/>
In other words, it has no configuration attributes; its existence in a
``configure.zcml`` file enables it.
See :ref:`aclauthorizationpolicy_directive` for detailed information.
.. _zcml_adding_and_overriding_renderers:
Adding and Overriding Renderers via ZCML
----------------------------------------
New templating systems and serializers can be associated with :mod:`pyramid`
renderer names. To this end, configuration declarations can be made which
override an existing :term:`renderer factory` and which add a new renderer
factory.
Adding or overriding a renderer via ZCML is accomplished via the
:ref:`renderer_directive` ZCML directive.
For example, to add a renderer which renders views which have a
``renderer`` attribute that is a path that ends in ``.jinja2``:
.. code-block:: xml
:linenos:
<renderer
name=".jinja2"
factory="my.package.MyJinja2Renderer"/>
The ``factory`` attribute is a :term:`dotted Python name` that must
point to an implementation of a :term:`renderer factory`.
The ``name`` attribute is the renderer name.
Registering a Renderer Factory
++++++++++++++++++++++++++++++
See :ref:`adding_a_renderer` for more information for the definition of a
:term:`renderer factory`. Here's an example of the registration of a simple
:term:`renderer factory` via ZCML:
.. code-block:: xml
:linenos:
<renderer
name="amf"
factory="my.package.MyAMFRenderer"/>
Adding the above ZCML to your application will allow you to use the
``my.package.MyAMFRenderer`` renderer factory implementation in view
configurations by subseqently referring to it as ``amf`` in the ``renderer``
attribute of a :term:`view configuration`:
.. code-block:: xml
:linenos:
<view
view="mypackage.views.my_view"
renderer="amf"/>
Here's an example of the registration of a more complicated renderer
factory, which expects to be passed a filesystem path:
.. code-block:: xml
:linenos:
<renderer
name=".jinja2"
factory="my.package.MyJinja2Renderer"/>
Adding the above ZCML to your application will allow you to use the
``my.package.MyJinja2Renderer`` renderer factory implementation in
view configurations by referring to any ``renderer`` which *ends in*
``.jinja`` in the ``renderer`` attribute of a :term:`view
configuration`:
.. code-block:: xml
:linenos:
<view
view="mypackage.views.my_view"
renderer="templates/mytemplate.jinja2"/>
When a :term:`view configuration` which has a ``name`` attribute that does
contain a dot, such as ``templates/mytemplate.jinja2`` above is encountered at
startup time, the value of the name attribute is split on its final dot. The
second element of the split is typically the filename extension. This
extension is used to look up a renderer factory for the configured view. Then
the value of ``renderer`` is passed to the factory to create a renderer for the
view. In this case, the view configuration will create an instance of a
``Jinja2Renderer`` for each view configuration which includes anything ending
with ``.jinja2`` as its ``renderer`` value. The ``name`` passed to the
``Jinja2Renderer`` constructor will be whatever the user passed as
``renderer=`` to the view configuration.
See also :ref:`renderer_directive` and
:meth:`pyramid.configuration.Configurator.add_renderer`.
Overriding an Existing Renderer
+++++++++++++++++++++++++++++++
You can associate more than one filename extension with the same
existing renderer implementation as necessary if you need to use a
different file extension for the same kinds of templates. For
example, to associate the ``.zpt`` extension with the Chameleon ZPT
renderer factory, use:
.. code-block:: xml
:linenos:
<renderer
name=".zpt"
factory="pyramid.chameleon_zpt.renderer_factory"/>
After you do this, :mod:`pyramid` will treat templates ending in
both the ``.pt`` and ``.zpt`` filename extensions as Chameleon ZPT
templates.
To override the default mapping in which files with a ``.pt``
extension are rendered via a Chameleon ZPT page template renderer, use
a variation on the following in your application's ZCML:
.. code-block:: xml
:linenos:
<renderer
name=".pt"
factory="my.package.pt_renderer"/>
After you do this, the :term:`renderer factory` in
``my.package.pt_renderer`` will be used to render templates which end
in ``.pt``, replacing the default Chameleon ZPT renderer.
To override the default mapping in which files with a ``.txt``
extension are rendered via a Chameleon text template renderer, use a
variation on the following in your application's ZCML:
.. code-block:: xml
:linenos:
<renderer
name=".txt"
factory="my.package.text_renderer"/>
After you do this, the :term:`renderer factory` in
``my.package.text_renderer`` will be used to render templates which
end in ``.txt``, replacing the default Chameleon text renderer.
To associate a *default* renderer with *all* view configurations (even
ones which do not possess a ``renderer`` attribute), use a variation
on the following (ie. omit the ``name`` attribute to the renderer
tag):
.. code-block:: xml
:linenos:
<renderer
factory="pyramid.renderers.json_renderer_factory"/>
See also :ref:`renderer_directive` and
:meth:`pyramid.configuration.Configurator.add_renderer`.
.. Todo
.. ----
.. - ``narr/project.rst`` chapter describes execution of a paster template that
.. is based on XML.
.. - i18n chapter still has topics for ZCML
.. - events chapter still has topics for ZCML
.. - hooks chapter still has topics for ZCML
.. - resources chapter still has topics for ZCML
|