[12993] | 1 | Central Components |
---|
| 2 | ****************** |
---|
[3933] | 3 | |
---|
[5140] | 4 | .. :doctest: |
---|
[11949] | 5 | .. :layer: waeup.ikoba.testing.IkobaUnitTestLayer |
---|
[3933] | 6 | |
---|
[11949] | 7 | .. module:: waeup.ikoba.app |
---|
[3933] | 8 | |
---|
[4273] | 9 | |
---|
[11954] | 10 | Creating `Company` instances |
---|
[11971] | 11 | ============================ |
---|
[3933] | 12 | |
---|
[11954] | 13 | As :class:`Company` instances make use of the Zope Component |
---|
[4273] | 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 |
---|
[5140] | 18 | registries (this is done by the testlayer automatically). |
---|
[3933] | 19 | |
---|
[11954] | 20 | Now we can import the :class:`Company` class and create an |
---|
[4273] | 21 | instance: |
---|
[3933] | 22 | |
---|
[11954] | 23 | >>> from waeup.ikoba.app import Company |
---|
| 24 | >>> mycompany = Company() |
---|
| 25 | >>> mycompany |
---|
| 26 | <waeup.ikoba.app.Company object at 0x...> |
---|
[3933] | 27 | |
---|
[11954] | 28 | Instances of `Company` comply with the interface |
---|
| 29 | `waeup.ikoba.interfaces.ICompany`: |
---|
[3933] | 30 | |
---|
| 31 | >>> from zope.interface.verify import verifyClass |
---|
[11954] | 32 | >>> from waeup.ikoba.interfaces import ICompany |
---|
| 33 | >>> verifyClass(ICompany, Company) |
---|
[3933] | 34 | True |
---|
| 35 | |
---|
[4273] | 36 | A freshly created instance provides the attributes promised by the |
---|
| 37 | interface: |
---|
[3933] | 38 | |
---|
[11954] | 39 | >>> from waeup.ikoba.app import Company |
---|
| 40 | >>> mycompany = Company() |
---|
| 41 | >>> mycompany['configuration'].name |
---|
| 42 | u'Sample Company' |
---|
[3933] | 43 | |
---|
[11954] | 44 | >>> mycompany['users'] |
---|
[11949] | 45 | <waeup.ikoba.userscontainer.UsersContainer object at 0x...> |
---|
[6897] | 46 | |
---|
[11954] | 47 | >>> mycompany['datacenter'] |
---|
[11949] | 48 | <waeup.ikoba.datacenter.DataCenter object at 0x...> |
---|
[6897] | 49 | |
---|
[11954] | 50 | >>> mycompany['configuration'] |
---|
[11949] | 51 | <waeup.ikoba.configuration.ConfigurationContainer object at 0x...> |
---|
[6907] | 52 | |
---|
[6897] | 53 | |
---|
[11949] | 54 | Ikoba plugins |
---|
[11971] | 55 | ============= |
---|
[6897] | 56 | |
---|
[11949] | 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. |
---|
[6897] | 60 | |
---|
| 61 | The plugins are looked up by an created app, which then will call the |
---|
| 62 | ``setup()`` method of each plugin. |
---|
| 63 | |
---|
[11949] | 64 | >>> from waeup.ikoba.interfaces import IIkobaPluggable |
---|
[6897] | 65 | >>> from zope.component import getAdapters, getUtilitiesFor |
---|
[11949] | 66 | >>> sorted(list(getUtilitiesFor(IIkobaPluggable))) |
---|
[12209] | 67 | [(u'documents', <waeup.ikoba.documents.container.DocumentsPlugin ...)] |
---|
[6897] | 68 | |
---|
| 69 | |
---|
| 70 | We can provide a new plugin like this: |
---|
| 71 | |
---|
| 72 | >>> import grok |
---|
[11949] | 73 | >>> from waeup.ikoba.interfaces import IIkobaPluggable |
---|
[6897] | 74 | >>> class MyPlugin(grok.GlobalUtility): |
---|
[11949] | 75 | ... grok.implements(IIkobaPluggable) |
---|
[6897] | 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 | |
---|
[11949] | 87 | and setup a new Ikoba instance, we will get a message: |
---|
[6897] | 88 | |
---|
[11954] | 89 | >>> from waeup.ikoba.app import Company |
---|
| 90 | >>> site = Company() |
---|
[6897] | 91 | Setup was called for |
---|
[11954] | 92 | <waeup.ikoba.app.Company object at 0x...> |
---|
[6897] | 93 | |
---|
[11954] | 94 | Apparently the plugin can do with the Company object whatever it |
---|
[7811] | 95 | likes. That's what plugins are for. |
---|