Whoops; preserve column order.
[zzz-pokedex.git] / pokedex / db / markdown.py
index d8ac683..dd86746 100644 (file)
@@ -8,6 +8,7 @@ http://michelf.com/projects/php-markdown/extra/ respectively.
 Pokédex links are represented with the extended syntax `[name]{type}`, e.g.,
 `[Eevee]{pokemon}`.  The actual code that parses these is in spline-pokedex.
 """
+from __future__ import absolute_import
 
 import markdown
 import sqlalchemy.types
@@ -55,15 +56,26 @@ class MarkdownString(object):
         """
         return self.source_text
 
+def _markdownify_effect_text(move, effect_text):
+    effect_text = effect_text.replace(
+        u'$effect_chance',
+        unicode(move.effect_chance),
+    )
+
+    return MarkdownString(effect_text)
 
 class MoveEffectProperty(object):
-    """Property that wraps a move effect.  Used like this:
+    """Property that wraps move effects.  Used like this:
 
         MoveClass.effect = MoveEffectProperty('effect')
 
         some_move.effect            # returns a MarkdownString
         some_move.effect.as_html    # returns a chunk of HTML
 
+    This class attempts to detect if the wrapped property is a dict-based
+    association proxy, and will act like such a dict if so.  Don't rely on it
+    for querying, of course.
+
     This class also performs simple substitution on the effect, replacing
     `$effect_chance` with the move's actual effect chance.
     """
@@ -71,14 +83,17 @@ class MoveEffectProperty(object):
     def __init__(self, effect_column):
         self.effect_column = effect_column
 
-    def __get__(self, move, move_class):
-        effect_text = getattr(move.move_effect, self.effect_column)
-        effect_text = effect_text.replace(
-            u'$effect_chance',
-            unicode(move.effect_chance),
-        )
-
-        return MarkdownString(effect_text)
+    def __get__(self, obj, cls):
+        prop = getattr(obj.move_effect, self.effect_column)
+        if isinstance(prop, dict):
+            # Looks like a dict proxy; markdownify everyone
+            newdict = dict(prop)
+            for key in newdict:
+                newdict[key] = _markdownify_effect_text(obj, newdict[key])
+            return newdict
+
+        # Otherwise, scalar prop.  Boring
+        return _markdownify_effect_text(obj, prop)
 
 class MarkdownColumn(sqlalchemy.types.TypeDecorator):
     """Generic SQLAlchemy column type for Markdown text.