Moved sobol's search registry into core as a generic registry utility. Added support...
authorStephen Burrows <stephen.r.burrows@gmail.com>
Thu, 2 Jun 2011 21:28:54 +0000 (17:28 -0400)
committerStephen Burrows <stephen.r.burrows@gmail.com>
Thu, 2 Jun 2011 21:28:54 +0000 (17:28 -0400)
philo/contrib/sobol/models.py
philo/contrib/sobol/search.py
philo/models/fields/__init__.py
philo/utils/registry.py [new file with mode: 0644]

index 7687c09..ee5f62e 100644 (file)
@@ -151,18 +151,6 @@ class Click(models.Model):
                get_latest_by = 'datetime'
 
 
-class RegistryChoiceField(SlugMultipleChoiceField):
-       def _get_choices(self):
-               if isinstance(self._choices, RegistryIterator):
-                       return self._choices.copy()
-               elif hasattr(self._choices, 'next'):
-                       choices, self._choices = itertools.tee(self._choices)
-                       return choices
-               else:
-                       return self._choices
-       choices = property(_get_choices)
-
-
 try:
        from south.modelsinspector import add_introspection_rules
 except ImportError:
@@ -175,8 +163,8 @@ class SearchView(MultiView):
        """Handles a view for the results of a search, anonymously tracks the selections made by end users, and provides an AJAX API for asynchronous search result loading. This can be particularly useful if some searches are slow."""
        #: :class:`ForeignKey` to a :class:`.Page` which will be used to render the search results.
        results_page = models.ForeignKey(Page, related_name='search_results_related')
-       #: A :class:`.SlugMultipleChoiceField` whose choices are the contents of the :class:`.SearchRegistry`
-       searches = RegistryChoiceField(choices=registry.iterchoices())
+       #: A :class:`.SlugMultipleChoiceField` whose choices are the contents of :obj:`.sobol.search.registry`
+       searches = SlugMultipleChoiceField(choices=registry.iterchoices())
        #: A :class:`BooleanField` which controls whether or not the AJAX API is enabled.
        #:
        #: .. note:: If the AJAX API is enabled, a ``ajax_api_url`` attribute will be added to each search instance containing the url and get parameters for an AJAX request to retrieve results for that search.
@@ -207,7 +195,7 @@ class SearchView(MultiView):
                return urlpatterns
        
        def get_search_instance(self, slug, search_string):
-               """Returns an instance of the :class:`.BaseSearch` subclass corresponding to ``slug`` in the :class:`.SearchRegistry` and instantiated with ``search_string``."""
+               """Gets the :class:`.BaseSearch` subclass registered with :obj:`.sobol.search.registry` as ``slug`` and instantiates it with ``search_string``."""
                return registry[slug](search_string.lower())
        
        def results_view(self, request, extra_context=None):
index 2dbd4a7..b0dca84 100644 (file)
@@ -11,7 +11,8 @@ from django.utils.safestring import mark_safe
 from django.utils.text import capfirst
 from django.template import loader, Context, Template
 
-from philo.contrib.sobol.utils import make_tracking_querydict, RegistryIterator
+from philo.contrib.sobol.utils import make_tracking_querydict
+from philo.utils.registry import Registry
 
 
 if getattr(settings, 'SOBOL_USE_EVENTLET', False):
@@ -24,7 +25,7 @@ else:
 
 
 __all__ = (
-       'Result', 'BaseSearch', 'DatabaseSearch', 'URLSearch', 'JSONSearch', 'GoogleSearch', 'SearchRegistry', 'registry'
+       'Result', 'BaseSearch', 'DatabaseSearch', 'URLSearch', 'JSONSearch', 'GoogleSearch', 'registry'
 )
 
 
@@ -36,74 +37,8 @@ DEFAULT_RESULT_TEMPLATE = Template(DEFAULT_RESULT_TEMPLATE_STRING)
 MAX_CACHE_TIMEOUT = 60*24*7
 
 
-class RegistrationError(Exception):
-       """Raised if there is a problem registering a search with a :class:`SearchRegistry`"""
-       pass
-
-
-class SearchRegistry(object):
-       """Holds a registry of search types by slug."""
-       
-       def __init__(self):
-               self._registry = {}
-       
-       def register(self, search, slug=None):
-               """
-               Register a search with the registry.
-               
-               :param search: The search class to register - generally a subclass of :class:`BaseSearch`
-               :param slug: The slug which will be used to register the search class. If ``slug`` is ``None``, the search's default slug will be used.
-               :raises: :class:`RegistrationError` if a different search is already registered with ``slug``.
-               
-               """
-               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):
-               """
-               Unregister a search from the registry.
-               
-               :param search: The search class to unregister - generally a subclass of :class:`BaseSearch`
-               :param slug: If provided, the search will only be removed if it was registered with ``slug``. If not provided, the search class will be unregistered no matter what slug it was registered with.
-               :raises: :class:`RegistrationError` if a slug is provided but the search registered with that slug is not ``search``.
-               
-               """
-               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):
-               """Returns a list of (slug, search) items in the registry."""
-               return self._registry.items()
-       
-       def iteritems(self):
-               """Returns an iterator over the (slug, search) pairs in the registry."""
-               return RegistryIterator(self._registry, 'iteritems')
-       
-       def iterchoices(self):
-               """Returns an iterator over (slug, search.verbose_name) pairs for the registry."""
-               return RegistryIterator(self._registry, 'iteritems', lambda x: (x[0], x[1].verbose_name))
-       
-       def __getitem__(self, key):
-               """Returns the search registered with ``key``."""
-               return self._registry[key]
-       
-       def __iter__(self):
-               """Returns an iterator over the keys in the registry."""
-               return self._registry.__iter__()
-
-
-registry = SearchRegistry()
+#: A registry for :class:`BaseSearch` subclasses that should be available in the admin.
+registry = Registry()
 
 
 class Result(object):
index efd315f..7ab4326 100644 (file)
@@ -7,6 +7,7 @@ from django.utils.text import capfirst
 from django.utils.translation import ugettext_lazy as _
 
 from philo.forms.fields import JSONFormField
+from philo.utils.registry import RegistryIterator
 from philo.validators import TemplateValidator, json_validator
 #from philo.models.fields.entities import *
 
@@ -71,7 +72,7 @@ class JSONField(models.TextField):
 
 
 class SlugMultipleChoiceField(models.Field):
-       """Stores a selection of multiple items with unique slugs in the form of a comma-separated list."""
+       """Stores a selection of multiple items with unique slugs in the form of a comma-separated list. Also knows how to correctly handle :class:`RegistryIterator`\ s passed in as choices."""
        __metaclass__ = models.SubfieldBase
        description = _("Comma-separated slug field")
        
@@ -127,6 +128,16 @@ class SlugMultipleChoiceField(models.Field):
                if invalid_values:
                        # should really make a custom message.
                        raise ValidationError(self.error_messages['invalid_choice'] % invalid_values)
+       
+       def _get_choices(self):
+               if isinstance(self._choices, RegistryIterator):
+                       return self._choices.copy()
+               elif hasattr(self._choices, 'next'):
+                       choices, self._choices = itertools.tee(self._choices)
+                       return choices
+               else:
+                       return self._choices
+       choices = property(_get_choices)
 
 
 try:
diff --git a/philo/utils/registry.py b/philo/utils/registry.py
new file mode 100644 (file)
index 0000000..1673429
--- /dev/null
@@ -0,0 +1,141 @@
+from django.core.validators import slug_re
+from django.template.defaultfilters import slugify
+from django.utils.encoding import smart_str
+
+
+class RegistryIterator(object):
+       """
+       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.
+       
+       :param registry: The object which provides the iterator at ``iterattr``.
+       :param iterattr: The name of the method on ``registry`` that provides the iterator.
+       :param transform: A function which will be called on each result from the wrapped iterator before it is returned.
+       
+       """
+       def __init__(self, registry, iterattr='__iter__', transform=lambda x:x):
+               if not hasattr(registry, iterattr):
+                       raise AttributeError("Registry has no attribute %s" % iterattr)
+               self.registry = registry
+               self.iterattr = iterattr
+               self.transform = transform
+       
+       def __iter__(self):
+               return self
+       
+       def next(self):
+               if not hasattr(self, '_iter'):
+                       self._iter = getattr(self.registry, self.iterattr)()
+               
+               return self.transform(self._iter.next())
+       
+       def copy(self):
+               """Returns a fresh copy of this iterator."""
+               return self.__class__(self.registry, self.iterattr, self.transform)
+
+
+class RegistrationError(Exception):
+       """Raised if there is a problem registering a object with a :class:`Registry`"""
+       pass
+
+
+class Registry(object):
+       """Holds a registry of arbitrary objects by slug."""
+       
+       def __init__(self):
+               self._registry = {}
+       
+       def register(self, obj, slug=None, verbose_name=None):
+               """
+               Register an object with the registry.
+               
+               :param obj: The object to register.
+               :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``.
+               :param verbose_name: The verbose name for the object. If ``verbose_name`` is ``None``, it will be looked for at ``obj.verbose_name``.
+               :raises: :class:`RegistrationError` if a different object is already registered with ``slug``, or if ``slug`` is not a valid slug.
+               
+               """
+               verbose_name = verbose_name if verbose_name is not None else obj.verbose_name
+               
+               if slug is None:
+                       slug = getattr(obj, 'slug', slugify(verbose_name))
+               slug = smart_str(slug)
+               
+               if not slug_re.search(slug):
+                       raise RegistrationError(u"%s is not a valid slug." % slug)
+               
+               
+               if slug in self._registry:
+                       reg = self._registry[slug]
+                       if reg['obj'] != obj:
+                               raise RegistrationError(u"A different object is already registered as `%s`" % slug)
+               else:
+                       self._registry[slug] = {
+                               'obj': obj,
+                               'verbose_name': verbose_name
+                       }
+       
+       def unregister(self, obj, slug=None):
+               """
+               Unregister an object from the registry.
+               
+               :param obj: The object to unregister.
+               :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.
+               :raises: :class:`RegistrationError` if ``slug`` is provided and an object other than ``obj`` is registered as ``slug``.
+               
+               """
+               if slug is not None:
+                       if slug in self._registry:
+                               if self._registry[slug]['obj'] == obj:
+                                       del self._registry[slug]
+                               else:
+                                       raise RegistrationError(u"`%s` is not registered as `%s`" % (obj, slug))
+               else:
+                       for slug, reg in self.items():
+                               if obj == reg:
+                                       del self._registry[slug]
+       
+       def items(self):
+               """Returns a list of (slug, obj) items in the registry."""
+               return [(slug, self[slug]) for slug in self._registry]
+       
+       def values(self):
+               """Returns a list of objects in the registry."""
+               return [self[slug] for slug in self._registry]
+       
+       def iteritems(self):
+               """Returns a :class:`RegistryIterator` over the (slug, obj) pairs in the registry."""
+               return RegistryIterator(self._registry, 'iteritems', lambda x: (x[0], x[1]['obj']))
+       
+       def itervalues(self):
+               """Returns a :class:`RegistryIterator` over the objects in the registry."""
+               return RegistryIterator(self._registry, 'itervalues', lambda x: x['obj'])
+       
+       def iterchoices(self):
+               """Returns a :class:`RegistryIterator` over (slug, verbose_name) pairs for the registry."""
+               return RegistryIterator(self._registry, 'iteritems', lambda x: (x[0], x[1]['verbose_name']))
+       choices = property(iterchoices)
+       
+       def get(self, key, default=None):
+               """Returns the object registered with ``key`` or ``default`` if no object was registered."""
+               try:
+                       return self[key]
+               except KeyError:
+                       return default
+       
+       def get_slug(self, obj, default=None):
+               """Returns the slug used to register ``obj`` or ``default`` if ``obj`` was not registered."""
+               for slug, reg in self.iteritems():
+                       if obj == reg:
+                               return slug
+               return default
+       
+       def __getitem__(self, key):
+               """Returns the obj registered with ``key``."""
+               return self._registry[key]['obj']
+       
+       def __iter__(self):
+               """Returns an iterator over the keys in the registry."""
+               return self._registry.__iter__()
+       
+       def __contains__(self, item):
+               return self._registry.__contains__(item)
\ No newline at end of file