changeset 1649:5dc142ab43d2

Keep itemtype and contenttype in Item and Content subclasses. Class decorators named @register are provided as helpers in items and items.content modules.
author Cheer Xiao <xiaqqaix@gmail.com>
date Wed, 08 Aug 2012 22:51:16 +0800
parents 1a2c53092ea1
children 07b2146ba96f
files MoinMoin/items/__init__.py MoinMoin/items/content.py
diffstat 2 files changed, 95 insertions(+), 60 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/items/__init__.py	Wed Aug 08 22:48:14 2012 +0800
+++ b/MoinMoin/items/__init__.py	Wed Aug 08 22:51:16 2012 +0800
@@ -89,6 +89,10 @@
 
 item_registry = RegistryItem()
 
+def register(cls):
+    item_registry.register(cls._factory, cls.itemtype)
+    return cls
+
 
 class DummyRev(dict):
     """ if we have no stored Revision, we use this dummy """
@@ -535,18 +539,20 @@
         return C
 
 
+@register
 class Default(Contentful):
     """
     A "conventional" wiki item.
     """
+    itemtype = u'default'
+
     def _do_modify_show_templates(self):
         # call this if the item is still empty
         rev_ids = []
         item_templates = self.content.get_templates(self.contenttype)
         return render_template('modify_select_template.html',
                                item_name=self.name,
-                               # XXX u'default' should be a constant
-                               itemtype=u'default',
+                               itemtype=self.itemtype,
                                rev=self.rev,
                                contenttype=self.contenttype,
                                templates=item_templates,
@@ -562,8 +568,7 @@
             if isinstance(self.content, NonExistentContent):
                 return render_template('modify_select_contenttype.html',
                                        item_name=self.name,
-                                       # XXX see comment above
-                                       itemtype=u'default',
+                                       itemtype=self.itemtype,
                                        contenttype_groups=CONTENTTYPE_GROUPS,
                                       )
             item = self
@@ -604,27 +609,32 @@
 
     modify_template = 'modify.html'
 
-item_registry.register(Default._factory, u'default')
 
-
+@register
 class Ticket(Contentful):
     """
     Stub for ticket item class.
     """
-
-item_registry.register(Ticket._factory, u'ticket')
+    itemtype = u'ticket'
 
 
+@register
 class Userprofile(Item):
     """
     Currently userprofile is implemented as a contenttype. This is a stub of an
     itemtype implementation of userprofile.
     """
-
-item_registry.register(Userprofile._factory, u'userprofile')
+    itemtype = u'userprofile'
 
 
+@register
 class NonExistent(Item):
+    """
+    A dummy Item for nonexistent items (when modifying, a nonexistent item with
+    undetermined itemtype)
+    """
+    itemtype = u'nonexistent'
+
     def _convert(self, doc):
         abort(404)
 
@@ -645,5 +655,3 @@
                                item_name=self.name,
                                itemtypes=ITEMTYPES,
                               )
-
-item_registry.register(NonExistent._factory, u'nonexistent')
--- a/MoinMoin/items/content.py	Wed Aug 08 22:48:14 2012 +0800
+++ b/MoinMoin/items/content.py	Wed Aug 08 22:51:16 2012 +0800
@@ -101,6 +101,10 @@
 
 content_registry = RegistryContent()
 
+def register(cls):
+    content_registry.register(cls._factory, Type(cls.contenttype))
+    return cls
+
 
 def conv_serialize(doc, namespaces, method='polyglot'):
     out = array('u')
@@ -241,8 +245,11 @@
         return [rev.meta[NAME] for rev in revs]
 
 
+@register
 class NonExistentContent(Content):
     """Dummy Content to use with NonExistent."""
+    contenttype = 'application/x-nonexistent'
+
     def do_get(self, force_attachment=False, mimetype=None):
         abort(404)
 
@@ -250,11 +257,10 @@
         abort(404)
 
 
-content_registry.register(NonExistentContent._factory, Type('application/x-nonexistent'))
-
-
+@register
 class Binary(Content):
     """ An arbitrary binary item, fallback class for every item mimetype. """
+    contenttype = '*/*'
 
     # XXX reads item rev data into memory!
     def get_data(self):
@@ -342,8 +348,6 @@
                          cache_timeout=10, # wiki data can change rapidly
                          add_etags=True, etag=hash, conditional=True)
 
-content_registry.register(Binary._factory, Type('*/*'))
-
 
 class RenderableBinary(Binary):
     """ Base class for some binary stuff that renders with a object tag. """
@@ -421,13 +425,20 @@
             os.remove(temp_fname)
 
 
+@register
 class ApplicationXTar(TarMixin, Application):
     """
     Tar items
     """
+    contenttype = 'application/x-tar'
 
-content_registry.register(ApplicationXTar._factory, Type('application/x-tar'))
-content_registry.register(ApplicationXTar._factory, Type('application/x-gtar'))
+
+@register
+class ApplicationXGTar(ApplicationXTar):
+    """
+    Compressed tar items
+    """
+    contenttype = 'application/x-gtar'
 
 
 class ZipMixin(object):
@@ -457,46 +468,46 @@
         raise NotImplementedError
 
 
+@register
 class ApplicationZip(ZipMixin, Application):
     """
     Zip items
     """
-
-content_registry.register(ApplicationZip._factory, Type('application/zip'))
+    contenttype = 'application/zip'
 
 
+@register
 class PDF(Application):
     """ PDF """
-
-content_registry.register(PDF._factory, Type('application/pdf'))
+    contenttype = 'application/pdf'
 
 
+@register
 class Video(Binary):
     """ Base class for video/* """
-
-content_registry.register(Video._factory, Type('video/*'))
+    contenttype = 'video/*'
 
 
+@register
 class Audio(Binary):
     """ Base class for audio/* """
-
-content_registry.register(Audio._factory, Type('audio/*'))
+    contenttype = 'audio/*'
 
 
+@register
 class Image(Binary):
     """ Base class for image/* """
-
-content_registry.register(Image._factory, Type('image/*'))
+    contenttype = 'image/*'
 
 
 class RenderableImage(RenderableBinary):
     """ Base class for renderable Image mimetypes """
 
 
+@register
 class SvgImage(RenderableImage):
     """ SVG images use <object> tag mechanism from RenderableBinary base class """
-
-content_registry.register(SvgImage._factory, Type('image/svg+xml'))
+    contenttype = 'image/svg+xml'
 
 
 class RenderableBitmapImage(RenderableImage):
@@ -657,13 +668,29 @@
     def _render_data_diff_text(self, oldrev, newrev):
         return super(TransformableBitmapImage, self)._render_data_diff_text(oldrev, newrev)
 
-content_registry.register(TransformableBitmapImage._factory, Type('image/png'))
-content_registry.register(TransformableBitmapImage._factory, Type('image/jpeg'))
-content_registry.register(TransformableBitmapImage._factory, Type('image/gif'))
+
+@register
+class PNG(TransformableBitmapImage):
+    """ PNG image. """
+    contenttype = 'image/png'
 
 
+@register
+class JPEG(TransformableBitmapImage):
+    """ JPEG image. """
+    contenttype = 'image/jpeg'
+
+
+@register
+class GIF(TransformableBitmapImage):
+    """ GIF image. """
+    contenttype = 'image/gif'
+
+
+@register
 class Text(Binary):
     """ Base class for text/* """
+    contenttype = 'text/*'
 
     class ModifyForm(Binary.ModifyForm):
         template = 'modify_text.html'
@@ -746,8 +773,6 @@
         doc = html_conv(doc)
         return conv_serialize(doc, {html.namespace: ''})
 
-content_registry.register(Text._factory, Type('text/*'))
-
 
 class MarkupItem(Text):
     """
@@ -756,30 +781,31 @@
     """
 
 
+@register
 class MoinWiki(MarkupItem):
     """ MoinMoin wiki markup """
-
-content_registry.register(MoinWiki._factory, Type('text/x.moin.wiki'))
-
-
-class CreoleWiki(MarkupItem):
-    """ Creole wiki markup """
-
-content_registry.register(CreoleWiki._factory, Type('text/x.moin.creole'))
+    contenttype = 'text/x.moin.wiki'
 
 
-class MediaWiki(MarkupItem):
-    """ MediaWiki markup """
-
-content_registry.register(MediaWiki._factory, Type('text/x-mediawiki'))
+@register
+class CreoleWiki(MarkupItem):
+    """ Creole wiki markup """
+    contenttype = 'text/x.moin.creole'
 
 
+@register
+class MediaWiki(MarkupItem):
+    """ MediaWiki markup """
+    contenttype = 'text/x-mediawiki'
+
+
+@register
 class ReST(MarkupItem):
     """ ReStructured Text markup """
-
-content_registry.register(ReST._factory, Type('text/x-rst'))
+    contenttype = 'text/x-rst'
 
 
+@register
 class HTML(Text):
     """
     HTML markup
@@ -790,14 +816,17 @@
 
     Note: If raw revision data is accessed, unsafe stuff might be present!
     """
+    contenttype = 'text/html'
+
     class ModifyForm(Text.ModifyForm):
         template = "modify_text_html.html"
 
-content_registry.register(HTML._factory, Type('text/html'))
 
-
+@register
 class DocBook(MarkupItem):
     """ DocBook Document """
+    contenttype = 'application/docbook+xml'
+
     def _convert(self, doc):
         from emeraldtree import ElementTree as ET
         from MoinMoin.converter import default_registry as reg
@@ -841,8 +870,6 @@
                          cache_timeout=10, # wiki data can change rapidly
                          add_etags=False, etag=None, conditional=True)
 
-content_registry.register(DocBook._factory, Type('application/docbook+xml'))
-
 
 class Draw(TarMixin, Image):
     """
@@ -856,10 +883,12 @@
         raise NotImplementedError
 
 
+@register
 class TWikiDraw(Draw):
     """
     drawings by TWikiDraw applet. It creates three files which are stored as tar file.
     """
+    contenttype = 'application/x-twikidraw'
 
     class ModifyForm(Draw.ModifyForm):
         template = "modify_twikidraw.html"
@@ -916,13 +945,13 @@
         else:
             return Markup(u'<img src="{0}" alt="{1}" />'.format(png_url, title))
 
-content_registry.register(TWikiDraw._factory, Type('application/x-twikidraw'))
 
-
+@register
 class AnyWikiDraw(Draw):
     """
     drawings by AnyWikiDraw applet. It creates three files which are stored as tar file.
     """
+    contenttype = 'application/x-anywikidraw'
 
     class ModifyForm(Draw.ModifyForm):
         template = "modify_anywikidraw.html"
@@ -985,11 +1014,11 @@
         else:
             return Markup(u'<img src="{0}" alt="{1}" />'.format(png_url, title))
 
-content_registry.register(AnyWikiDraw._factory, Type('application/x-anywikidraw'))
 
-
+@register
 class SvgDraw(Draw):
     """ drawings by svg-edit. It creates two files (svg, png) which are stored as tar file. """
+    contenttype = 'application/x-svgdraw'
 
     class ModifyForm(Draw.ModifyForm):
         template = "modify_svg-edit.html"
@@ -1016,5 +1045,3 @@
         drawing_url = url_for('frontend.get_item', item_name=item_name, member='drawing.svg', rev=self.rev.revid)
         png_url = url_for('frontend.get_item', item_name=item_name, member='drawing.png', rev=self.rev.revid)
         return Markup(u'<img src="{0}" alt="{1}" />'.format(png_url, drawing_url))
-
-content_registry.register(SvgDraw._factory, Type('application/x-svgdraw'))