diff MoinMoin/items/content.py @ 1613:bc8a2546468b

Clean up registry code. Quite some boilerplate codes were removed. The signature of several `_factory` and `Entry.__call__` were changed a bit as well to make things a bit more uniform.
author Cheer Xiao <xiaqqaix@gmail.com>
date Wed, 01 Aug 2012 00:21:03 +0800
parents fc1f874695e1
children 5b5e7190452f
line wrap: on
line diff
--- a/MoinMoin/items/content.py	Sun Jul 29 18:26:20 2012 +0800
+++ b/MoinMoin/items/content.py	Wed Aug 01 00:21:03 2012 +0800
@@ -23,6 +23,9 @@
 import tempfile
 from StringIO import StringIO
 from array import array
+from collections import namedtuple
+from functools import partial
+from types import TypeType
 
 from werkzeug import is_resource_modified
 
@@ -72,25 +75,11 @@
 ROWS_DATA = 20
 
 
-# XXX Too much boilerplate in Entry implementation. Maybe use namedtuple
-# as a starting point?
 class RegistryContent(RegistryBase):
-    class Entry(object):
-        def __init__(self, factory, content_type, priority):
-            self.factory = factory
-            self.content_type = content_type
-            self.priority = priority
-
-        def __call__(self, name, content_type, kw):
-            if self.content_type.issupertype(content_type):
-                return self.factory(name, content_type, **kw)
-
-        def __eq__(self, other):
-            if isinstance(other, self.__class__):
-                return (self.factory == other.factory and
-                        self.content_type == other.content_type and
-                        self.priority == other.priority)
-            return NotImplemented
+    class Entry(namedtuple('Entry', 'factory content_type priority')):
+        def __call__(self, content_type, *args, **kw):
+            if self.content_type.issupertype(Type(content_type)):
+                return self.factory(content_type, *args, **kw)
 
         def __lt__(self, other):
             if isinstance(other, self.__class__):
@@ -101,17 +90,7 @@
                 return False
             return NotImplemented
 
-        def __repr__(self):
-            return '<{0}: {1}, prio {2} [{3!r}]>'.format(self.__class__.__name__,
-                    self.content_type,
-                    self.priority,
-                    self.factory)
 
-    def get(self, name, content_type, **kw):
-        for entry in self._entries:
-            item = entry(name, content_type, kw)
-            if item is not None:
-                return item
 
     def register(self, factory, content_type, priority=RegistryBase.PRIORITY_MIDDLE):
         """
@@ -140,15 +119,16 @@
     data.
     """
     @classmethod
-    def _factory(cls, name=u'', contenttype=None, **kw):
-        return cls(name, contenttype=unicode(contenttype), **kw)
+    def _factory(cls, *args, **kw):
+        return cls(*args, **kw)
 
-    def __init__(self, item, contenttype=None):
+    def __init__(self, contenttype, item=None):
+        # We need to keep the exact contenttype since contents may be handled
+        # by a Content subclass with wildcard contenttype (eg. an unknown
+        # contenttype some/type gets handled by Binary)
+        # TODO use Type instead of strings?
+        self.contenttype = contenttype
         self.item = item
-        # TODO gradually remove self.contenttype as theoretically there is
-        # one-to-one correspondance of contenttype and Content type
-        # (pun intended :)
-        self.contenttype = contenttype
 
     # XXX For backward-compatibility (so code can be moved from Item
     # untouched), remove soon