1 | :mod:`waeup.ikoba.app` -- central components for Ikoba |
---|
2 | **************************************************** |
---|
3 | |
---|
4 | .. :doctest: |
---|
5 | .. :layer: waeup.ikoba.testing.IkobaUnitTestLayer |
---|
6 | |
---|
7 | .. module:: waeup.ikoba.app |
---|
8 | |
---|
9 | .. class:: University |
---|
10 | |
---|
11 | The main Ikoba application object is given with |
---|
12 | :class:`University`. It provides the main containers as faculties, |
---|
13 | hostels, etc. |
---|
14 | |
---|
15 | .. attribute:: name |
---|
16 | |
---|
17 | A string. The name of a university. |
---|
18 | |
---|
19 | .. attribute:: faculties |
---|
20 | |
---|
21 | An arbitrary object containing "faculties". In the case of |
---|
22 | `University` it is a container of type |
---|
23 | `waeup.ikoba.interfaces.IFacultiesContainer`. |
---|
24 | |
---|
25 | |
---|
26 | Creating `University` instances |
---|
27 | =============================== |
---|
28 | |
---|
29 | As :class:`University` instances make use of the Zope Component |
---|
30 | Architecture (ZCA), we have to setup the basic component registries, |
---|
31 | before we can create instances. We do this by simply grokking the |
---|
32 | whole :mod:`waeup` package. This way all interfaces, utilities and adapters |
---|
33 | defined in the package are looked up and registered with the global |
---|
34 | registries (this is done by the testlayer automatically). |
---|
35 | |
---|
36 | Now we can import the :class:`University` class and create an |
---|
37 | instance: |
---|
38 | |
---|
39 | >>> from waeup.ikoba.app import University |
---|
40 | >>> myuniversity = University() |
---|
41 | >>> myuniversity |
---|
42 | <waeup.ikoba.app.University object at 0x...> |
---|
43 | |
---|
44 | Instances of `University` comply with the interface |
---|
45 | `waeup.ikoba.interfaces.IUniversity`: |
---|
46 | |
---|
47 | >>> from zope.interface.verify import verifyClass |
---|
48 | >>> from waeup.ikoba.interfaces import IUniversity |
---|
49 | >>> verifyClass(IUniversity, University) |
---|
50 | True |
---|
51 | |
---|
52 | A freshly created instance provides the attributes promised by the |
---|
53 | interface: |
---|
54 | |
---|
55 | >>> from waeup.ikoba.app import University |
---|
56 | >>> myuniversity = University() |
---|
57 | >>> myuniversity['configuration'].name |
---|
58 | u'Sample University' |
---|
59 | |
---|
60 | >>> myuniversity['users'] |
---|
61 | <waeup.ikoba.userscontainer.UsersContainer object at 0x...> |
---|
62 | |
---|
63 | >>> myuniversity['datacenter'] |
---|
64 | <waeup.ikoba.datacenter.DataCenter object at 0x...> |
---|
65 | |
---|
66 | >>> myuniversity['configuration'] |
---|
67 | <waeup.ikoba.configuration.ConfigurationContainer object at 0x...> |
---|
68 | |
---|
69 | |
---|
70 | Ikoba plugins |
---|
71 | ============ |
---|
72 | |
---|
73 | waeup.ikoba provides an API to 'plugin' components. Things that should |
---|
74 | be setup at creation time of a Ikoba application can indicate |
---|
75 | that by providing a utility providing IIkobaPlugin. |
---|
76 | |
---|
77 | The plugins are looked up by an created app, which then will call the |
---|
78 | ``setup()`` method of each plugin. |
---|
79 | |
---|
80 | >>> from waeup.ikoba.interfaces import IIkobaPluggable |
---|
81 | >>> from zope.component import getAdapters, getUtilitiesFor |
---|
82 | >>> sorted(list(getUtilitiesFor(IIkobaPluggable))) |
---|
83 | [(u'mandates', <waeup.ikoba.mandates.container.MandatesPlugin ...)] |
---|
84 | |
---|
85 | |
---|
86 | We can provide a new plugin like this: |
---|
87 | |
---|
88 | >>> import grok |
---|
89 | >>> from waeup.ikoba.interfaces import IIkobaPluggable |
---|
90 | >>> class MyPlugin(grok.GlobalUtility): |
---|
91 | ... grok.implements(IIkobaPluggable) |
---|
92 | ... def setup(self, site, name, logger): |
---|
93 | ... print "Setup was called for" |
---|
94 | ... print site |
---|
95 | ... def update(self, site, name, logger): |
---|
96 | ... pass |
---|
97 | |
---|
98 | When we register the plugin |
---|
99 | |
---|
100 | >>> grok.testing.grok_component('MyPlugin', MyPlugin) |
---|
101 | True |
---|
102 | |
---|
103 | and setup a new Ikoba instance, we will get a message: |
---|
104 | |
---|
105 | >>> from waeup.ikoba.app import University |
---|
106 | >>> site = University() |
---|
107 | Setup was called for |
---|
108 | <waeup.ikoba.app.University object at 0x...> |
---|
109 | |
---|
110 | Apparently the plugin can do with the University object whatever it |
---|
111 | likes. That's what plugins are for. |
---|