Commits

xiaq committed 1a2c530

items/content.py: Implement Content.create.

Comments (0)

Files changed (4)

MoinMoin/items/__init__.py

     )
 from MoinMoin.constants.contenttypes import charset, CONTENTTYPE_GROUPS
 
-from .content import Draw, NonExistentContent, content_registry
+from .content import Content, NonExistentContent, Draw, content_registry
 
 
 COLS = 80
     def _factory(cls, *args, **kw):
         return cls(*args, **kw)
 
-    # TODO split Content creation to Content.create
     @classmethod
     def create(cls, name=u'', itemtype=None, contenttype=None, rev_id=CURRENT, item=None):
         """
 
         # XXX Cannot pass item=item to Content.__init__ via
         # content_registry.get yet, have to patch it later.
-        content = content_registry.get(contenttype)
-        logging.debug("Content class {0!r} handles {1!r}".format(content.__class__, contenttype))
+        content = Content.create(contenttype)
 
         itemtype = rev.meta.get(ITEMTYPE) or itemtype
         logging.debug("Item {0!r}, got itemtype {1!r} from revision meta".format(name, itemtype))

MoinMoin/items/_tests/test_Content.py

 
 from MoinMoin._tests import become_trusted, update_item
 from MoinMoin.items import Item
-from MoinMoin.items.content import ApplicationXTar, Binary, Text, Image, TransformableBitmapImage, MarkupItem
+from MoinMoin.items.content import Content, ApplicationXTar, Binary, Text, Image, TransformableBitmapImage, MarkupItem
 from MoinMoin.config import CONTENTTYPE, ADDRESS, COMMENT, HOSTNAME, USERID, ACTION
 
 
-# TODO: When testing Content and derived classes, instead of creating a full
-# item with Item.create and test against item.content, implement
-# Content.create and test for returned Content instance directly. Item.create
-# will still be needed when it is required to create new items in the storage
-# to set up the test environment, but its use should be restricted to that
-# only.
 class TestContent(object):
     """ Test for arbitrary content """
 
+    def testClassFinder(self):
+        for contenttype, ExpectedClass in [
+                (u'application/x-foobar', Binary),
+                (u'text/plain', Text),
+                (u'text/plain;charset=utf-8', Text),
+                (u'image/tiff', Image),
+                (u'image/png', TransformableBitmapImage),
+            ]:
+            content = Content.create(contenttype)
+            assert isinstance(content, ExpectedClass)
+
     def test_get_templates(self):
         item_name1 = u'Template_Item1'
         item1 = Item.create(item_name1)

MoinMoin/items/_tests/test_Item.py

         assert meta == {CONTENTTYPE: u'application/x-nonexistent'}
         assert data == ''
 
-    # TODO move this to testContent after implementing Content.create
-    def testClassFinder(self):
-        for contenttype, ExpectedClass in [
-                (u'application/x-foobar', Binary),
-                (u'text/plain', Text),
-                (u'text/plain;charset=utf-8', Text),
-                (u'image/tiff', Image),
-                (u'image/png', TransformableBitmapImage),
-            ]:
-            item = Item.create(u'foo', contenttype=contenttype)
-            assert isinstance(item.content, ExpectedClass)
-
     def testCRUD(self):
         name = u'NewItem'
         contenttype = u'text/plain;charset=utf-8'

MoinMoin/items/content.py

     def _factory(cls, *args, **kw):
         return cls(*args, **kw)
 
+    @classmethod
+    def create(cls, contenttype, item=None):
+        content = content_registry.get(contenttype, item)
+        logging.debug("Content class {0!r} handles {1!r}".format(content.__class__, contenttype))
+        return content
+
     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