Back to index

plone3  3.1.7
utils.py
Go to the documentation of this file.
00001 from zope.interface import alsoProvides
00002 
00003 from zope.component import queryUtility
00004 from zope.component import getSiteManager
00005 from zope.component import getAllUtilitiesRegisteredFor
00006 
00007 from plone.browserlayer.interfaces import ILocalBrowserLayerType
00008 
00009 def register_layer(layer, name, site_manager=None):
00010     """Register the given layer (an interface) with the given name. If it is
00011     already registered, nothing will be done. If site_manager is not given,
00012     the current site manager will be used.
00013     """
00014     
00015     existing = queryUtility(ILocalBrowserLayerType, name=name)
00016     if existing is not None:
00017         return
00018     
00019     if site_manager is None:
00020         site_manager = getSiteManager()
00021     
00022     if not ILocalBrowserLayerType.providedBy(layer):
00023         alsoProvides(layer, ILocalBrowserLayerType)
00024     
00025     site_manager.registerUtility(component=layer, 
00026                                  provided=ILocalBrowserLayerType, 
00027                                  name=name)
00028     
00029 def unregister_layer(name, site_manager=None):
00030     """Unregister the layer with the given name. If it cannot be found, a
00031     KeyError is raised.
00032     """
00033     
00034     existing = queryUtility(ILocalBrowserLayerType, name=name)
00035     if existing is None:
00036         raise KeyError("No browser layer with name %s is registered." % name)
00037     
00038     if site_manager is None:
00039         site_manager = getSiteManager()
00040         
00041     site_manager.unregisterUtility(component=existing,
00042                                    provided=ILocalBrowserLayerType,
00043                                    name=name)
00044     
00045 def registered_layers():
00046     """Return all currently registered layers
00047     """
00048     return getAllUtilitiesRegisteredFor(ILocalBrowserLayerType)