1 from django.core.validators import slug_re
2 from django.template.defaultfilters import slugify
3 from django.utils.encoding import smart_str
6 class RegistryIterator(object):
8 Wraps the iterator returned by calling ``getattr(registry, iterattr)`` to provide late instantiation of the wrapped iterator and to allow copying of the iterator for even later instantiation.
10 :param registry: The object which provides the iterator at ``iterattr``.
11 :param iterattr: The name of the method on ``registry`` that provides the iterator.
12 :param transform: A function which will be called on each result from the wrapped iterator before it is returned.
15 def __init__(self, registry, iterattr='__iter__', transform=lambda x:x):
16 if not hasattr(registry, iterattr):
17 raise AttributeError("Registry has no attribute %s" % iterattr)
18 self.registry = registry
19 self.iterattr = iterattr
20 self.transform = transform
26 if not hasattr(self, '_iter'):
27 self._iter = getattr(self.registry, self.iterattr)()
29 return self.transform(self._iter.next())
32 """Returns a fresh copy of this iterator."""
33 return self.__class__(self.registry, self.iterattr, self.transform)
36 class RegistrationError(Exception):
37 """Raised if there is a problem registering a object with a :class:`Registry`"""
41 class Registry(object):
42 """Holds a registry of arbitrary objects by slug."""
47 def register(self, obj, slug=None, verbose_name=None):
49 Register an object with the registry.
51 :param obj: The object to register.
52 :param slug: The slug which will be used to register the object. If ``slug`` is ``None``, it will be generated from ``verbose_name`` or looked for at ``obj.slug``.
53 :param verbose_name: The verbose name for the object. If ``verbose_name`` is ``None``, it will be looked for at ``obj.verbose_name``.
54 :raises: :class:`RegistrationError` if a different object is already registered with ``slug``, or if ``slug`` is not a valid slug.
57 verbose_name = verbose_name if verbose_name is not None else obj.verbose_name
60 slug = getattr(obj, 'slug', slugify(verbose_name))
61 slug = smart_str(slug)
63 if not slug_re.search(slug):
64 raise RegistrationError(u"%s is not a valid slug." % slug)
67 if slug in self._registry:
68 reg = self._registry[slug]
70 raise RegistrationError(u"A different object is already registered as `%s`" % slug)
72 self._registry[slug] = {
74 'verbose_name': verbose_name
77 def unregister(self, obj, slug=None):
79 Unregister an object from the registry.
81 :param obj: The object to unregister.
82 :param slug: If provided, the object will only be removed if it was registered with ``slug``. If not provided, the object will be unregistered no matter what slug it was registered with.
83 :raises: :class:`RegistrationError` if ``slug`` is provided and an object other than ``obj`` is registered as ``slug``.
87 if slug in self._registry:
88 if self._registry[slug]['obj'] == obj:
89 del self._registry[slug]
91 raise RegistrationError(u"`%s` is not registered as `%s`" % (obj, slug))
93 for slug, reg in self.items():
95 del self._registry[slug]
98 """Returns a list of (slug, obj) items in the registry."""
99 return [(slug, self[slug]) for slug in self._registry]
102 """Returns a list of objects in the registry."""
103 return [self[slug] for slug in self._registry]
106 """Returns a :class:`RegistryIterator` over the (slug, obj) pairs in the registry."""
107 return RegistryIterator(self._registry, 'iteritems', lambda x: (x[0], x[1]['obj']))
109 def itervalues(self):
110 """Returns a :class:`RegistryIterator` over the objects in the registry."""
111 return RegistryIterator(self._registry, 'itervalues', lambda x: x['obj'])
113 def iterchoices(self):
114 """Returns a :class:`RegistryIterator` over (slug, verbose_name) pairs for the registry."""
115 return RegistryIterator(self._registry, 'iteritems', lambda x: (x[0], x[1]['verbose_name']))
116 choices = property(iterchoices)
118 def get(self, key, default=None):
119 """Returns the object registered with ``key`` or ``default`` if no object was registered."""
125 def get_slug(self, obj, default=None):
126 """Returns the slug used to register ``obj`` or ``default`` if ``obj`` was not registered."""
127 for slug, reg in self.iteritems():
132 def __getitem__(self, key):
133 """Returns the obj registered with ``key``."""
134 return self._registry[key]['obj']
137 """Returns an iterator over the keys in the registry."""
138 return self._registry.__iter__()
140 def __contains__(self, item):
141 return self._registry.__contains__(item)