Back to index

plone3  3.1.7
registry.py
Go to the documentation of this file.
00001 # Marshall: A framework for pluggable marshalling policies
00002 # Copyright (C) 2004-2006 Enfold Systems, LLC
00003 #
00004 # This program is free software; you can redistribute it and/or modify
00005 # it under the terms of the GNU General Public License as published by
00006 # the Free Software Foundation; either version 2 of the License, or
00007 # (at your option) any later version.
00008 #
00009 # This program is distributed in the hope that it will be useful,
00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 # GNU General Public License for more details.
00013 #
00014 # You should have received a copy of the GNU General Public License
00015 # along with this program; if not, write to the Free Software
00016 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 #
00018 """
00019 $Id: registry.py 5626 2006-01-24 02:26:47Z dreamcatcher $
00020 """
00021 
00022 from OFS.OrderedFolder import OrderedFolder
00023 from Globals import PersistentMapping, InitializeClass
00024 from AccessControl import ClassSecurityInfo
00025 from AccessControl.Permissions import view, manage_properties
00026 from Products.Marshall.interfaces import IPredicate
00027 from Products.Marshall.config import TOOL_ID
00028 from Products.Marshall.export import Export
00029 
00030 class RegistryItem:
00031 
00032     def __init__(self, name, title, factory):
00033         self.name = name
00034         if not title:
00035             title = name
00036         self.title = title
00037         self.factory = factory
00038 
00039     def info(self):
00040         return {'name':self.name,
00041                 'title':self.title}
00042 
00043     def create(self, id, title, expression, component_name):
00044         return self.factory(id, title or self.title, expression, component_name)
00045 
00046 comp_registry = {}
00047 def registerComponent(name, title, component):
00048     comp_registry[name] = RegistryItem(name, title, component)
00049 
00050 def getRegisteredComponents():
00051     return [item.info() for item in comp_registry.values()]
00052 
00053 def getComponent(name):
00054     return comp_registry[name].factory()
00055 
00056 registry = {}
00057 def registerPredicate(name, title, component):
00058     component.predicate_type = title
00059     registry[name] = RegistryItem(name, title, component)
00060 
00061 def getRegisteredPredicates():
00062     return [item.info() for item in registry.values()]
00063 
00064 def createPredicate(name, id, title, expression, component_name):
00065     return registry[name].create(id, title, expression, component_name)
00066 
00067 class Registry(OrderedFolder, Export):
00068     """ A registry that holds predicates and applies them to
00069     objects in the hope of selecting the right one that matches
00070     a given set of constraints.
00071     """
00072 
00073     meta_type = 'Marshaller Registry'
00074     id = TOOL_ID
00075     security = ClassSecurityInfo()
00076 
00077     def __init__(self, id='', title=''):
00078         OrderedFolder.__init__(self, self.id)
00079         self.title = title or self.meta_type
00080 
00081     def all_meta_types(self):
00082         return OrderedFolder.all_meta_types(self, interfaces=(IPredicate,))
00083 
00084     security.declareProtected(view, 'getMarshallersFor')
00085     def getMarshallersFor(self, obj, **kw):
00086         set = []
00087         for predicate in self.objectValues():
00088             # An OrderedSet, in the lack of a real one.
00089             # The order in which predicates are evaluated
00090             # is managed by the underlying OrderedFolder impl.
00091             [set.append(s) for s in predicate.apply(obj, **kw)
00092              if s not in set]
00093         return tuple(set)
00094 
00095 InitializeClass(Registry)
00096 
00097 def manage_addRegistry(self, REQUEST=None, **ignored):
00098     """ Factory method that creates a Registry"""
00099     obj = Registry()
00100     self._setObject(obj.id, obj)
00101 
00102     if REQUEST is not None:
00103         REQUEST['RESPONSE'].redirect(self.absolute_url()+'/manage_main')
00104     else:
00105         return self._getOb(obj.id)