-
-class SearchRegistry(object):
- # Holds a registry of search types by slug.
- def __init__(self):
- self._registry = {}
-
- def register(self, search, slug=None):
- slug = slug or search.slug
- if slug in self._registry:
- registered = self._registry[slug]
- if registered.__module__ != search.__module__:
- raise RegistrationError("A different search is already registered as `%s`" % slug)
- else:
- self._registry[slug] = search
-
- def unregister(self, search, slug=None):
- if slug is not None:
- if slug in self._registry and self._registry[slug] == search:
- del self._registry[slug]
- raise RegistrationError("`%s` is not registered as `%s`" % (search, slug))
- else:
- for slug, search in self._registry.items():
- if search == search:
- del self._registry[slug]
-
- def items(self):
- return self._registry.items()
-
- def iteritems(self):
- return self._registry.iteritems()
-
- def iterchoices(self):
- for slug, search in self.iteritems():
- yield slug, search.verbose_name
-
- def __getitem__(self, key):
- return self._registry[key]
-
- def __iter__(self):
- return self._registry.__iter__()