[7819] | 1 | :mod:`waeup.kofa.app` -- central components for Kofa |
---|
[7321] | 2 | **************************************************** |
---|
[3933] | 3 | |
---|
[5140] | 4 | .. :doctest: |
---|
[7819] | 5 | .. :layer: waeup.kofa.testing.KofaUnitTestLayer |
---|
[3933] | 6 | |
---|
[7811] | 7 | .. module:: waeup.kofa.app |
---|
[3933] | 8 | |
---|
[4273] | 9 | .. class:: University |
---|
| 10 | |
---|
[7819] | 11 | The main Kofa application object is given with |
---|
[4273] | 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 |
---|
[7811] | 23 | `waeup.kofa.interfaces.IFacultiesContainer`. |
---|
[4273] | 24 | |
---|
| 25 | |
---|
[3933] | 26 | Creating `University` instances |
---|
| 27 | =============================== |
---|
| 28 | |
---|
[4273] | 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 |
---|
[5140] | 34 | registries (this is done by the testlayer automatically). |
---|
[3933] | 35 | |
---|
[4273] | 36 | Now we can import the :class:`University` class and create an |
---|
| 37 | instance: |
---|
[3933] | 38 | |
---|
[7811] | 39 | >>> from waeup.kofa.app import University |
---|
[3933] | 40 | >>> myuniversity = University() |
---|
| 41 | >>> myuniversity |
---|
[7811] | 42 | <waeup.kofa.app.University object at 0x...> |
---|
[3933] | 43 | |
---|
| 44 | Instances of `University` comply with the interface |
---|
[7811] | 45 | `waeup.kofa.interfaces.IUniversity`: |
---|
[3933] | 46 | |
---|
| 47 | >>> from zope.interface.verify import verifyClass |
---|
[7811] | 48 | >>> from waeup.kofa.interfaces import IUniversity |
---|
[3933] | 49 | >>> verifyClass(IUniversity, University) |
---|
| 50 | True |
---|
| 51 | |
---|
[4273] | 52 | A freshly created instance provides the attributes promised by the |
---|
| 53 | interface: |
---|
[3933] | 54 | |
---|
[7811] | 55 | >>> from waeup.kofa.app import University |
---|
[3933] | 56 | >>> myuniversity = University() |
---|
[6907] | 57 | >>> myuniversity['configuration'].name |
---|
[3933] | 58 | u'Sample University' |
---|
| 59 | |
---|
[4748] | 60 | >>> myuniversity['faculties'] |
---|
[7811] | 61 | <waeup.kofa.university.facultiescontainer.FacultiesContainer object at 0x...> |
---|
[6897] | 62 | |
---|
| 63 | >>> myuniversity['students'] |
---|
[7811] | 64 | <waeup.kofa.students.container.StudentsContainer object at 0x...> |
---|
[6897] | 65 | |
---|
| 66 | >>> myuniversity['users'] |
---|
[7811] | 67 | <waeup.kofa.userscontainer.UsersContainer object at 0x...> |
---|
[6897] | 68 | |
---|
| 69 | >>> myuniversity['datacenter'] |
---|
[7811] | 70 | <waeup.kofa.datacenter.DataCenter object at 0x...> |
---|
[6897] | 71 | |
---|
[6907] | 72 | >>> myuniversity['configuration'] |
---|
[7811] | 73 | <waeup.kofa.configuration.ConfigurationContainer object at 0x...> |
---|
[6907] | 74 | |
---|
[6897] | 75 | We can export universities. For this we lookup an appropriate exporter |
---|
| 76 | first:: |
---|
| 77 | |
---|
[7819] | 78 | >>> from waeup.kofa.interfaces import IKofaExporter |
---|
| 79 | >>> exporter = IKofaExporter(myuniversity) |
---|
[6897] | 80 | >>> exporter |
---|
[7811] | 81 | <waeup.kofa.utils.importexport.Exporter object at 0x...> |
---|
[6897] | 82 | |
---|
| 83 | Now we can trigger the export:: |
---|
| 84 | |
---|
| 85 | >>> exporter.export() |
---|
| 86 | <cStringIO.StringO object at 0x...> |
---|
| 87 | |
---|
| 88 | We can also get an XML representation as file. If we do not provide a |
---|
| 89 | filepath, we will get an instance of `cStringIO.StringIO`, i.e. a |
---|
| 90 | memory file:: |
---|
| 91 | |
---|
[7819] | 92 | >>> from waeup.kofa.interfaces import IKofaXMLExporter |
---|
| 93 | >>> exporter = IKofaXMLExporter(myuniversity) |
---|
[6897] | 94 | >>> f = exporter.export() |
---|
| 95 | >>> f |
---|
| 96 | <cStringIO.StringO object at 0x...> |
---|
| 97 | |
---|
| 98 | >>> print f.read() |
---|
| 99 | <?xml version="1.0" encoding="utf-8" ?> |
---|
| 100 | <pickle> |
---|
| 101 | <initialized_object id="..."> |
---|
| 102 | ... |
---|
| 103 | </pickle> |
---|
| 104 | |
---|
[7819] | 105 | Kofa plugins |
---|
[7321] | 106 | ============ |
---|
[6897] | 107 | |
---|
[7811] | 108 | waeup.kofa provides an API to 'plugin' components. Things that should |
---|
[7819] | 109 | be setup at creation time of a Kofa application can indicate |
---|
| 110 | that by providing a utility providing IKofaPlugin. |
---|
[6897] | 111 | |
---|
| 112 | The plugins are looked up by an created app, which then will call the |
---|
| 113 | ``setup()`` method of each plugin. |
---|
| 114 | |
---|
[7819] | 115 | >>> from waeup.kofa.interfaces import IKofaPluggable |
---|
[6897] | 116 | >>> from zope.component import getAdapters, getUtilitiesFor |
---|
[7819] | 117 | >>> sorted(list(getUtilitiesFor(IKofaPluggable))) |
---|
[7811] | 118 | [(u'accesscodes', <waeup.kofa.accesscodes...AccessCodePlugin ...)] |
---|
[6897] | 119 | |
---|
| 120 | |
---|
| 121 | We can provide a new plugin like this: |
---|
| 122 | |
---|
| 123 | >>> import grok |
---|
[7819] | 124 | >>> from waeup.kofa.interfaces import IKofaPluggable |
---|
[6897] | 125 | >>> class MyPlugin(grok.GlobalUtility): |
---|
[7819] | 126 | ... grok.implements(IKofaPluggable) |
---|
[6897] | 127 | ... def setup(self, site, name, logger): |
---|
| 128 | ... print "Setup was called for" |
---|
| 129 | ... print site |
---|
| 130 | ... def update(self, site, name, logger): |
---|
| 131 | ... pass |
---|
| 132 | |
---|
| 133 | When we register the plugin |
---|
| 134 | |
---|
| 135 | >>> grok.testing.grok_component('MyPlugin', MyPlugin) |
---|
| 136 | True |
---|
| 137 | |
---|
[7819] | 138 | and setup a new Kofa instance, we will get a message: |
---|
[6897] | 139 | |
---|
[7811] | 140 | >>> from waeup.kofa.app import University |
---|
[6897] | 141 | >>> site = University() |
---|
| 142 | Setup was called for |
---|
[7811] | 143 | <waeup.kofa.app.University object at 0x...> |
---|
[6897] | 144 | |
---|
| 145 | Apparently the plugin can do with the University object whatever it |
---|
[7811] | 146 | likes. That's what plugins are for. |
---|