source: main/waeup.sirp/trunk/src/waeup/sirp/app.txt @ 10222

Last change on this file since 10222 was 7333, checked in by Henrik Bettermann, 13 years ago

Rename certificatecontainer, coursecontainer and facultycontainer. Now we have e.g. a container for faculties. Synonyms are 'facultiescontainer', 'faculties', 'academics' or 'Academic Section'. The faculty container is the faculty itself which contains the departments.

File size: 4.3 KB
RevLine 
[7321]1:mod:`waeup.sirp.app` -- central components for SIRP
2****************************************************
[3933]3
[5140]4.. :doctest:
[7321]5.. :layer: waeup.sirp.testing.SIRPUnitTestLayer
[3933]6
[4920]7.. module:: waeup.sirp.app
[3933]8
[4273]9.. class:: University
10
[7321]11  The main SIRP 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
[7333]23     `waeup.sirp.interfaces.IFacultiesContainer`.
[4273]24
25
[3933]26Creating `University` instances
27===============================
28
[4273]29As :class:`University` instances make use of the Zope Component
30Architecture (ZCA), we have to setup the basic component registries,
31before we can create instances. We do this by simply grokking the
32whole :mod:`waeup` package. This way all interfaces, utilities and adapters
33defined in the package are looked up and registered with the global
[5140]34registries (this is done by the testlayer automatically).
[3933]35
[4273]36Now we can import the :class:`University` class and create an
37instance:
[3933]38
[4920]39    >>> from waeup.sirp.app import University
[3933]40    >>> myuniversity = University()
41    >>> myuniversity
[4920]42    <waeup.sirp.app.University object at 0x...>
[3933]43
44Instances of `University` comply with the interface
[4920]45`waeup.sirp.interfaces.IUniversity`:
[3933]46
47    >>> from zope.interface.verify import verifyClass
[4920]48    >>> from waeup.sirp.interfaces import IUniversity
[3933]49    >>> verifyClass(IUniversity, University)
50    True
51
[4273]52A freshly created instance provides the attributes promised by the
53interface:
[3933]54
[4920]55    >>> from waeup.sirp.app import University
[3933]56    >>> myuniversity = University()
[6907]57    >>> myuniversity['configuration'].name
[3933]58    u'Sample University'
59
[4748]60    >>> myuniversity['faculties']
[7333]61    <waeup.sirp.university.facultiescontainer.FacultiesContainer object at 0x...>
[6897]62
63  >>> myuniversity['students']
64  <waeup.sirp.students.container.StudentsContainer object at 0x...>
65
66  >>> myuniversity['users']
[7172]67  <waeup.sirp.userscontainer.UsersContainer object at 0x...>
[6897]68
69  >>> myuniversity['datacenter']
70  <waeup.sirp.datacenter.DataCenter object at 0x...>
71
[6907]72  >>> myuniversity['configuration']
73  <waeup.sirp.configuration.ConfigurationContainer object at 0x...>
74
[6897]75We can export universities. For this we lookup an appropriate exporter
76first::
77
[7321]78  >>> from waeup.sirp.interfaces import ISIRPExporter
79  >>> exporter = ISIRPExporter(myuniversity)
[6897]80  >>> exporter
81  <waeup.sirp.utils.importexport.Exporter object at 0x...>
82
83Now we can trigger the export::
84
85  >>> exporter.export()
86  <cStringIO.StringO object at 0x...>
87
88We can also get an XML representation as file. If we do not provide a
89filepath, we will get an instance of `cStringIO.StringIO`, i.e. a
90memory file::
91
[7321]92  >>> from waeup.sirp.interfaces import ISIRPXMLExporter
93  >>> exporter = ISIRPXMLExporter(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
[7321]105SIRP plugins
106============
[6897]107
108waeup.sirp provides an API to 'plugin' components. Things that should
[7321]109be setup at creation time of a SIRP application can indicate
110that by providing a utility providing ISIRPPlugin.
[6897]111
112The plugins are looked up by an created app, which then will call the
113``setup()`` method of each plugin.
114
[7321]115   >>> from waeup.sirp.interfaces import ISIRPPluggable
[6897]116   >>> from zope.component import getAdapters, getUtilitiesFor
[7321]117   >>> sorted(list(getUtilitiesFor(ISIRPPluggable)))
[6897]118   [(u'accesscodes', <waeup.sirp.accesscodes...AccessCodePlugin ...)]
119
120
121We can provide a new plugin like this:
122
123   >>> import grok
[7321]124   >>> from waeup.sirp.interfaces import ISIRPPluggable
[6897]125   >>> class MyPlugin(grok.GlobalUtility):
[7321]126   ...   grok.implements(ISIRPPluggable)
[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
133When we register the plugin
134
135   >>> grok.testing.grok_component('MyPlugin', MyPlugin)
136   True
137
[7321]138and setup a new SIRP instance, we will get a message:
[6897]139
140   >>> from waeup.sirp.app import University
141   >>> site = University()
142   Setup was called for
143   <waeup.sirp.app.University object at 0x...>
144
145Apparently the plugin can do with the University object whatever it
146likes. That's what plugins are for.
Note: See TracBrowser for help on using the repository browser.