:mod:`waeup.sirp.app` -- central components for SIRP **************************************************** .. :doctest: .. :layer: waeup.sirp.testing.SIRPUnitTestLayer .. module:: waeup.sirp.app .. class:: University The main SIRP application object is given with :class:`University`. It provides the main containers as faculties, hostels, etc. .. attribute:: name A string. The name of a university. .. attribute:: faculties An arbitrary object containing "faculties". In the case of `University` it is a container of type `waeup.sirp.interfaces.IFacultyContainer`. Creating `University` instances =============================== As :class:`University` instances make use of the Zope Component Architecture (ZCA), we have to setup the basic component registries, before we can create instances. We do this by simply grokking the whole :mod:`waeup` package. This way all interfaces, utilities and adapters defined in the package are looked up and registered with the global registries (this is done by the testlayer automatically). Now we can import the :class:`University` class and create an instance: >>> from waeup.sirp.app import University >>> myuniversity = University() >>> myuniversity Instances of `University` comply with the interface `waeup.sirp.interfaces.IUniversity`: >>> from zope.interface.verify import verifyClass >>> from waeup.sirp.interfaces import IUniversity >>> verifyClass(IUniversity, University) True A freshly created instance provides the attributes promised by the interface: >>> from waeup.sirp.app import University >>> myuniversity = University() >>> myuniversity['configuration'].name u'Sample University' >>> myuniversity['faculties'] >>> myuniversity['students'] >>> myuniversity['users'] >>> myuniversity['datacenter'] >>> myuniversity['configuration'] We can export universities. For this we lookup an appropriate exporter first:: >>> from waeup.sirp.interfaces import ISIRPExporter >>> exporter = ISIRPExporter(myuniversity) >>> exporter Now we can trigger the export:: >>> exporter.export() We can also get an XML representation as file. If we do not provide a filepath, we will get an instance of `cStringIO.StringIO`, i.e. a memory file:: >>> from waeup.sirp.interfaces import ISIRPXMLExporter >>> exporter = ISIRPXMLExporter(myuniversity) >>> f = exporter.export() >>> f >>> print f.read() ... SIRP plugins ============ waeup.sirp provides an API to 'plugin' components. Things that should be setup at creation time of a SIRP application can indicate that by providing a utility providing ISIRPPlugin. The plugins are looked up by an created app, which then will call the ``setup()`` method of each plugin. >>> from waeup.sirp.interfaces import ISIRPPluggable >>> from zope.component import getAdapters, getUtilitiesFor >>> sorted(list(getUtilitiesFor(ISIRPPluggable))) [(u'accesscodes', >> import grok >>> from waeup.sirp.interfaces import ISIRPPluggable >>> class MyPlugin(grok.GlobalUtility): ... grok.implements(ISIRPPluggable) ... def setup(self, site, name, logger): ... print "Setup was called for" ... print site ... def update(self, site, name, logger): ... pass When we register the plugin >>> grok.testing.grok_component('MyPlugin', MyPlugin) True and setup a new SIRP instance, we will get a message: >>> from waeup.sirp.app import University >>> site = University() Setup was called for Apparently the plugin can do with the University object whatever it likes. That's what plugins are for.