source: main/waeup.kofa/trunk/src/waeup/kofa/doctests/app.txt @ 12957

Last change on this file since 12957 was 12951, checked in by Henrik Bettermann, 10 years ago

Simplify headlines in doctests. Remove some API documentation.

File size: 3.8 KB
Line 
1Central Components
2******************
3
4.. :doctest:
5.. :layer: waeup.kofa.testing.KofaUnitTestLayer
6
7.. module:: waeup.kofa.app
8
9
10Creating `University` instances
11===============================
12
13As :class:`University` instances make use of the Zope Component
14Architecture (ZCA), we have to setup the basic component registries,
15before we can create instances. We do this by simply grokking the
16whole :mod:`waeup` package. This way all interfaces, utilities and adapters
17defined in the package are looked up and registered with the global
18registries (this is done by the testlayer automatically).
19
20Now we can import the :class:`University` class and create an
21instance:
22
23  >>> from waeup.kofa.app import University
24  >>> myuniversity = University()
25  >>> myuniversity
26  <waeup.kofa.app.University object at 0x...>
27
28Instances of `University` comply with the interface
29`waeup.kofa.interfaces.IUniversity`:
30
31  >>> from zope.interface.verify import verifyClass
32  >>> from waeup.kofa.interfaces import IUniversity
33  >>> verifyClass(IUniversity, University)
34  True
35
36A freshly created instance provides the attributes promised by the
37interface:
38
39  >>> from waeup.kofa.app import University
40  >>> myuniversity = University()
41  >>> myuniversity['configuration'].name
42  u'Sample University'
43
44  >>> myuniversity['faculties']
45  <waeup.kofa.university.facultiescontainer.FacultiesContainer object at 0x...>
46
47  >>> myuniversity['students']
48  <waeup.kofa.students.container.StudentsContainer object at 0x...>
49
50  >>> myuniversity['users']
51  <waeup.kofa.userscontainer.UsersContainer object at 0x...>
52
53  >>> myuniversity['datacenter']
54  <waeup.kofa.datacenter.DataCenter object at 0x...>
55
56  >>> myuniversity['configuration']
57  <waeup.kofa.configuration.ConfigurationContainer object at 0x...>
58
59We can export universities. For this we lookup an appropriate exporter
60first::
61
62  >>> from waeup.kofa.interfaces import IKofaExporter
63  >>> exporter = IKofaExporter(myuniversity)
64  >>> exporter
65  <waeup.kofa.utils.importexport.Exporter object at 0x...>
66
67Now we can trigger the export::
68
69  >>> exporter.export()
70  <cStringIO.StringO object at 0x...>
71
72We can also get an XML representation as file. If we do not provide a
73filepath, we will get an instance of `cStringIO.StringIO`, i.e. a
74memory file::
75
76  >>> from waeup.kofa.interfaces import IKofaXMLExporter
77  >>> exporter = IKofaXMLExporter(myuniversity)
78  >>> f = exporter.export()
79  >>> f
80  <cStringIO.StringO object at 0x...>
81
82  >>> print f.read()
83  <?xml version="1.0" encoding="utf-8" ?>
84  <pickle>
85    <initialized_object id="...">
86  ...
87  </pickle>
88
89Kofa plugins
90============
91
92waeup.kofa provides an API to 'plugin' components. Things that should
93be setup at creation time of a Kofa application can indicate
94that by providing a utility providing IKofaPlugin.
95
96The plugins are looked up by an created app, which then will call the
97``setup()`` method of each plugin.
98
99   >>> from waeup.kofa.interfaces import IKofaPluggable
100   >>> from zope.component import getAdapters, getUtilitiesFor
101   >>> sorted(list(getUtilitiesFor(IKofaPluggable)))
102   [(u'accesscodes', <waeup.kofa.accesscodes...AccessCodePlugin ...)]
103
104
105We can provide a new plugin like this:
106
107   >>> import grok
108   >>> from waeup.kofa.interfaces import IKofaPluggable
109   >>> class MyPlugin(grok.GlobalUtility):
110   ...   grok.implements(IKofaPluggable)
111   ...   def setup(self, site, name, logger):
112   ...     print "Setup was called for"
113   ...     print site
114   ...   def update(self, site, name, logger):
115   ...     pass
116
117When we register the plugin
118
119   >>> grok.testing.grok_component('MyPlugin', MyPlugin)
120   True
121
122and setup a new Kofa instance, we will get a message:
123
124   >>> from waeup.kofa.app import University
125   >>> site = University()
126   Setup was called for
127   <waeup.kofa.app.University object at 0x...>
128
129Apparently the plugin can do with the University object whatever it
130likes. That's what plugins are for.
Note: See TracBrowser for help on using the repository browser.