Back to index

plone3  3.1.7
Registry.py
Go to the documentation of this file.
00001 import types
00002 
00003 from Products.Archetypes.utils import className
00004 from Products.Archetypes.utils import setSecurity
00005 from Products.Archetypes.ArchetypeTool import listTypes
00006 from Products.Archetypes.interfaces.base import IBaseObject
00007 
00008 def getDoc(klass):
00009     doc = klass.__doc__ or ''
00010     return doc
00011 
00012 class Registry:
00013 
00014     def __init__(self, allowed_class):
00015         self.__registry = {}
00016         self.__allowed_class = allowed_class
00017 
00018     def register(self, name, item):
00019         if not isinstance(item, self.__allowed_class):
00020             raise TypeError, "Invalid value for item: %r (should be %r)" % \
00021                   (item, self.__allowed_class)
00022         self.__registry[name] = item
00023 
00024     def unregister(self, name):
00025         if self.__registry.has_key(name):
00026             del self.__registry[name]
00027 
00028     def keys(self):
00029         return [k for k, v in self.items()]
00030 
00031     def values(self):
00032         return [v for k, v in self.items()]
00033 
00034     def items(self):
00035         return self.__registry.items()
00036 
00037     def __getitem__(self, name):
00038         return self.__registry[name]
00039 
00040     def get(self, name, default=None):
00041         return self.__registry.get(name, default)
00042 
00043 class FieldDescription:
00044 
00045     __allow_access_to_unprotected_subobjects__ = 1
00046 
00047     def __init__(self, klass, default_widget=None,
00048                  title='', description=''):
00049         self.id = className(klass)
00050         self.klass = klass
00051         default_widget = default_widget or klass._properties.get('widget', None)
00052         if default_widget is None:
00053             raise ValueError, '%r Must have a default_widget' % klass
00054         if type(default_widget) not in [types.StringType, types.UnicodeType]:
00055             default_widget = className(default_widget)
00056         self.default_widget = default_widget
00057         self.title = title or klass.__name__
00058         self.description = description or getDoc(klass)
00059 
00060     def allowed_widgets(self):
00061         from Products.Archetypes.Registry import availableWidgets
00062         widgets = []
00063         for k, v in availableWidgets():
00064             if v.used_for is None or \
00065                self.id in v.used_for:
00066                 widgets.append(k)
00067         return widgets
00068 
00069     def properties(self):
00070         from Products.Archetypes.Registry import getPropertyType
00071         props = []
00072         for k, v in self.klass._properties.items():
00073             prop = {}
00074             prop['name'] = k
00075             prop['type'] = getPropertyType(k, self.klass)
00076             prop['default'] = v
00077             props.append(prop)
00078 
00079         return props
00080 
00081 class WidgetDescription:
00082 
00083     __allow_access_to_unprotected_subobjects__ = 1
00084 
00085     def __init__(self, klass, title='', description='', used_for=()):
00086         self.id = className(klass)
00087         self.klass = klass
00088         self.title = title or klass.__name__
00089         self.description = description or getDoc(klass)
00090         self.used_for = used_for
00091 
00092     def properties(self):
00093         from Products.Archetypes.Registry import getPropertyType
00094         props = []
00095         for k, v in self.klass._properties.items():
00096             prop = {}
00097             prop['name'] = k
00098             prop['type'] = getPropertyType(k, self.klass)
00099             prop['default'] = str(v)
00100             props.append(prop)
00101 
00102         return props
00103 
00104 class ValidatorDescription:
00105 
00106     __allow_access_to_unprotected_subobjects__ = 1
00107 
00108     def __init__(self, klass, title='', description=''):
00109         self.id = className(klass)
00110         self.klass = klass
00111         self.title = title or klass.__name__
00112         self.description = description or getDoc(klass)
00113 
00114 class StorageDescription:
00115 
00116     __allow_access_to_unprotected_subobjects__ = 1
00117 
00118     def __init__(self, klass, title='', description=''):
00119         self.id = className(klass)
00120         self.klass = klass
00121         self.title = title or klass.__name__
00122         self.description = description or getDoc(klass)
00123 
00124 def findBaseTypes(klass):
00125     bases = []
00126     if hasattr(klass, '__bases__'):
00127         for b in klass.__bases__:
00128             if IBaseObject.isImplementedByInstancesOf(b):
00129                 bases.append(className(b))
00130     return bases
00131 
00132 class TypeDescription:
00133 
00134     __allow_access_to_unprotected_subobjects__ = 1
00135 
00136     def __init__(self, klass, title='', description='',
00137                  package='', module=''):
00138         self.id = className(klass)
00139         self.klass = klass
00140         self.title = title or klass.__name__
00141         self.description = description or getDoc(klass)
00142         self.package = package
00143         self.module = module
00144 
00145     def schemata(self):
00146         from Products.Archetypes.Schema import getSchemata
00147         # Build a temp instance.
00148         return getSchemata(self.klass('test'))
00149 
00150     def signature(self):
00151         # Build a temp instance.
00152         return self.klass('test').Schema().signature()
00153 
00154     def portal_type(self):
00155         return self.klass.portal_type
00156 
00157     def read_only(self):
00158         return 1
00159 
00160     def basetypes(self):
00161         return findBaseTypes(self.klass)
00162 
00163 fieldDescriptionRegistry = Registry(FieldDescription)
00164 availableFields = fieldDescriptionRegistry.items
00165 def registerField(klass, **kw):
00166     setSecurity(klass, defaultAccess='allow', objectPermission=None)
00167     field = FieldDescription(klass, **kw)
00168     fieldDescriptionRegistry.register(field.id, field)
00169 
00170 widgetDescriptionRegistry = Registry(WidgetDescription)
00171 availableWidgets = widgetDescriptionRegistry.items
00172 def registerWidget(klass, **kw):
00173     setSecurity(klass, defaultAccess='allow', objectPermission=None)
00174     widget = WidgetDescription(klass, **kw)
00175     widgetDescriptionRegistry.register(widget.id, widget)
00176 
00177 storageDescriptionRegistry = Registry(StorageDescription)
00178 availableStorages = storageDescriptionRegistry.items
00179 def registerStorage(klass, **kw):
00180     setSecurity(klass, defaultAccess=None, objectPermission=None)
00181     storage = StorageDescription(klass, **kw)
00182     storageDescriptionRegistry.register(storage.id, storage)
00183 
00184 class TypeRegistry:
00185 
00186     def __init__(self):
00187         pass
00188 
00189     def items(self):
00190         return [(className(t['klass']),
00191                  TypeDescription(t['klass'],
00192                                  title=t['name'],
00193                                  package=t['package'],
00194                                  module=t['module'],
00195                                  )
00196                  )
00197                  for t in listTypes()]
00198 
00199     def keys(self):
00200         return [k for k, v in self.items()]
00201 
00202     def values(self):
00203         return [v for k, v in self.items()]
00204 
00205     def __getitem__(self, name):
00206         items = self.items()
00207         for k, v in items:
00208             if k == name:
00209                 return v
00210         raise KeyError, name
00211 
00212     def get(self, name, default=None):
00213         items = self.items()
00214         for k, v in items:
00215             if k == name:
00216                 return v
00217         return default
00218 
00219 class ValidatorRegistry:
00220 
00221     def __init__(self):
00222         from Products.validation import validation
00223         self.validation = validation
00224 
00225     def register(self,  name, item):
00226         self.validation.register(item)
00227 
00228     def unregister(self, name):
00229         self.validation.unregister(name)
00230 
00231     def items(self):
00232         return [(k, ValidatorDescription(v,
00233                                          title=v.title,
00234                                          description=v.description))
00235                 for k, v in self.validation.items()]
00236 
00237     def keys(self):
00238         return [k for k, v in self.items()]
00239 
00240     def values(self):
00241         return [v for k, v in self.items()]
00242 
00243 validatorDescriptionRegistry = ValidatorRegistry()
00244 availableValidators = validatorDescriptionRegistry.items
00245 def registerValidator(item, name=''):
00246     name = name or item.name
00247     validatorDescriptionRegistry.register(name, item)
00248 
00249 typeDescriptionRegistry = TypeRegistry()
00250 availableTypes = typeDescriptionRegistry.items
00251 
00252 class PropertyMapping:
00253 
00254     def __init__(self):
00255         self._default = {}
00256         self._mapping = {}
00257 
00258     def register(self, property, type, klass=None):
00259         if not klass:
00260             map = self._default
00261         else:
00262             if not self._mapping.has_key(klass):
00263                 self._mapping[klass] = {}
00264             map = self._mapping[klass]
00265         map[property] = type
00266 
00267     def getType(self, property, klass):
00268         value = None
00269         if self._mapping.has_key(klass):
00270             value = self._mapping[klass].get(property, None)
00271         return value or self._default.get(property, 'not-registered')
00272 
00273 propertyMapping = PropertyMapping()
00274 registerPropertyType = propertyMapping.register
00275 getPropertyType = propertyMapping.getType