1 | Central Components |
---|
2 | ****************** |
---|
3 | |
---|
4 | .. :doctest: |
---|
5 | .. :layer: waeup.kofa.testing.KofaUnitTestLayer |
---|
6 | |
---|
7 | .. module:: waeup.kofa.app |
---|
8 | |
---|
9 | |
---|
10 | Creating `University` instances |
---|
11 | =============================== |
---|
12 | |
---|
13 | As :class:`University` instances make use of the Zope Component |
---|
14 | Architecture (ZCA), we have to setup the basic component registries, |
---|
15 | before we can create instances. We do this by simply grokking the |
---|
16 | whole :mod:`waeup` package. This way all interfaces, utilities and adapters |
---|
17 | defined in the package are looked up and registered with the global |
---|
18 | registries (this is done by the testlayer automatically). |
---|
19 | |
---|
20 | Now we can import the :class:`University` class and create an |
---|
21 | instance: |
---|
22 | |
---|
23 | >>> from waeup.kofa.app import University |
---|
24 | >>> myuniversity = University() |
---|
25 | >>> myuniversity |
---|
26 | <waeup.kofa.app.University object at 0x...> |
---|
27 | |
---|
28 | Instances 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 | |
---|
36 | A freshly created instance provides the attributes promised by the |
---|
37 | interface: |
---|
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 | |
---|
59 | Kofa plugins |
---|
60 | ============ |
---|
61 | |
---|
62 | waeup.kofa provides an API to 'plugin' components. Things that should |
---|
63 | be setup at creation time of a Kofa application can indicate |
---|
64 | that by providing a utility providing IKofaPlugin. |
---|
65 | |
---|
66 | The plugins are looked up by an created app, which then will call the |
---|
67 | ``setup()`` method of each plugin. |
---|
68 | |
---|
69 | >>> from waeup.kofa.interfaces import IKofaPluggable |
---|
70 | >>> from zope.component import getAdapters, getUtilitiesFor |
---|
71 | >>> sorted(list(getUtilitiesFor(IKofaPluggable))) |
---|
72 | [(u'academics', <waeup.kofa.university.facultiescontainer.AcademicsPlugin ...)] |
---|
73 | |
---|
74 | |
---|
75 | We can provide a new plugin like this: |
---|
76 | |
---|
77 | >>> import grok |
---|
78 | >>> from waeup.kofa.interfaces import IKofaPluggable |
---|
79 | >>> class MyPlugin(grok.GlobalUtility): |
---|
80 | ... grok.implements(IKofaPluggable) |
---|
81 | ... def setup(self, site, name, logger): |
---|
82 | ... print "Setup was called for" |
---|
83 | ... print site |
---|
84 | ... def update(self, site, name, logger): |
---|
85 | ... pass |
---|
86 | |
---|
87 | When we register the plugin |
---|
88 | |
---|
89 | >>> grok.testing.grok_component('MyPlugin', MyPlugin) |
---|
90 | True |
---|
91 | |
---|
92 | and setup a new Kofa instance, we will get a message: |
---|
93 | |
---|
94 | >>> from waeup.kofa.app import University |
---|
95 | >>> site = University() |
---|
96 | Setup was called for |
---|
97 | <waeup.kofa.app.University object at 0x...> |
---|
98 | |
---|
99 | Apparently the plugin can do with the University object whatever it |
---|
100 | likes. That's what plugins are for. |
---|