1 | Central Components |
---|
2 | ****************** |
---|
3 | |
---|
4 | .. :doctest: |
---|
5 | .. :layer: waeup.ikoba.testing.IkobaUnitTestLayer |
---|
6 | |
---|
7 | .. module:: waeup.ikoba.app |
---|
8 | |
---|
9 | |
---|
10 | Creating `Company` instances |
---|
11 | ============================ |
---|
12 | |
---|
13 | As :class:`Company` 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:`Company` class and create an |
---|
21 | instance: |
---|
22 | |
---|
23 | >>> from waeup.ikoba.app import Company |
---|
24 | >>> mycompany = Company() |
---|
25 | >>> mycompany |
---|
26 | <waeup.ikoba.app.Company object at 0x...> |
---|
27 | |
---|
28 | Instances of `Company` comply with the interface |
---|
29 | `waeup.ikoba.interfaces.ICompany`: |
---|
30 | |
---|
31 | >>> from zope.interface.verify import verifyClass |
---|
32 | >>> from waeup.ikoba.interfaces import ICompany |
---|
33 | >>> verifyClass(ICompany, Company) |
---|
34 | True |
---|
35 | |
---|
36 | A freshly created instance provides the attributes promised by the |
---|
37 | interface: |
---|
38 | |
---|
39 | >>> from waeup.ikoba.app import Company |
---|
40 | >>> mycompany = Company() |
---|
41 | >>> mycompany['configuration'].name |
---|
42 | u'Sample Company' |
---|
43 | |
---|
44 | >>> mycompany['users'] |
---|
45 | <waeup.ikoba.userscontainer.UsersContainer object at 0x...> |
---|
46 | |
---|
47 | >>> mycompany['datacenter'] |
---|
48 | <waeup.ikoba.datacenter.DataCenter object at 0x...> |
---|
49 | |
---|
50 | >>> mycompany['configuration'] |
---|
51 | <waeup.ikoba.configuration.ConfigurationContainer object at 0x...> |
---|
52 | |
---|
53 | |
---|
54 | Ikoba plugins |
---|
55 | ============= |
---|
56 | |
---|
57 | waeup.ikoba provides an API to 'plugin' components. Things that should |
---|
58 | be setup at creation time of a Ikoba application can indicate |
---|
59 | that by providing a utility providing IIkobaPlugin. |
---|
60 | |
---|
61 | The plugins are looked up by an created app, which then will call the |
---|
62 | ``setup()`` method of each plugin. |
---|
63 | |
---|
64 | >>> from waeup.ikoba.interfaces import IIkobaPluggable |
---|
65 | >>> from zope.component import getAdapters, getUtilitiesFor |
---|
66 | >>> sorted(list(getUtilitiesFor(IIkobaPluggable))) |
---|
67 | [(u'documents', <waeup.ikoba.documents.container.DocumentsPlugin ...)] |
---|
68 | |
---|
69 | |
---|
70 | We can provide a new plugin like this: |
---|
71 | |
---|
72 | >>> import grok |
---|
73 | >>> from waeup.ikoba.interfaces import IIkobaPluggable |
---|
74 | >>> class MyPlugin(grok.GlobalUtility): |
---|
75 | ... grok.implements(IIkobaPluggable) |
---|
76 | ... def setup(self, site, name, logger): |
---|
77 | ... print "Setup was called for" |
---|
78 | ... print site |
---|
79 | ... def update(self, site, name, logger): |
---|
80 | ... pass |
---|
81 | |
---|
82 | When we register the plugin |
---|
83 | |
---|
84 | >>> grok.testing.grok_component('MyPlugin', MyPlugin) |
---|
85 | True |
---|
86 | |
---|
87 | and setup a new Ikoba instance, we will get a message: |
---|
88 | |
---|
89 | >>> from waeup.ikoba.app import Company |
---|
90 | >>> site = Company() |
---|
91 | Setup was called for |
---|
92 | <waeup.ikoba.app.Company object at 0x...> |
---|
93 | |
---|
94 | Apparently the plugin can do with the Company object whatever it |
---|
95 | likes. That's what plugins are for. |
---|