Multi-language markdown move properties
authorPetr Viktorin <encukou@gmail.com>
Fri, 4 Feb 2011 04:22:44 +0000 (06:22 +0200)
committerEevee <git@veekun.com>
Sun, 13 Mar 2011 22:10:11 +0000 (15:10 -0700)
Singular property name for English text
Plural property name for dict of texts keyed by language

pokedex/db/markdown.py
pokedex/db/tables.py

index 928c3d4..2bef876 100644 (file)
@@ -57,6 +57,26 @@ class MarkdownString(object):
         return self.source_text
 
 
+class _MoveEffects(object):
+    def __init__(self, effect_column, move):
+        self.effect_column = effect_column
+        self.move = move
+
+    def __contains__(self, lang):
+        return lang in self.move.move_effect.prose
+
+    def __getitem__(self, lang):
+        try:
+            effect_text = getattr(self.move.move_effect.prose[lang], self.effect_column)
+        except AttributeError:
+            return None
+        effect_text = effect_text.replace(
+            u'$effect_chance',
+            unicode(self.move.effect_chance),
+        )
+
+        return MarkdownString(effect_text)
+
 class MoveEffectProperty(object):
     """Property that wraps a move effect.  Used like this:
 
@@ -73,13 +93,28 @@ class MoveEffectProperty(object):
         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),
-        )
+        if move is None:
+            # Don't crash with getattr on the class
+            return NotImplemented
+        return _MoveEffects(self.effect_column, move)['en']
 
-        return MarkdownString(effect_text)
+class MoveEffectsProperty(object):
+    """Property that wraps move effects.  Used like this:
+
+        MoveClass.effects = MoveEffectProperty('effect')
+
+        some_move.effects[lang]            # returns a MarkdownString
+        some_move.effects[lang].as_html    # returns a chunk of HTML
+
+    This class also performs simple substitution on the effect, replacing
+    `$effect_chance` with the move's actual effect chance.
+    """
+
+    def __init__(self, effect_column):
+        self.effect_column = effect_column
+
+    def __get__(self, move, move_class):
+        return _MoveEffects(self.effect_column, move)
 
 class MarkdownColumn(sqlalchemy.types.TypeDecorator):
     """Generic SQLAlchemy column type for Markdown text.
index 8ea9586..cf9b780 100644 (file)
@@ -1521,11 +1521,20 @@ Move.super_contest_combo_prev = association_proxy('super_contest_combo_second',
 Move.target = relation(MoveTarget, backref='moves')
 Move.type = relation(Type, back_populates='moves')
 
+Move.effect = markdown.MoveEffectProperty('effect')
+Move.effects = markdown.MoveEffectsProperty('effect')
+Move.short_effect = markdown.MoveEffectProperty('short_effect')
+Move.short_effects = markdown.MoveEffectsProperty('short_effect')
 
 MoveChangelog.changed_in = relation(VersionGroup, backref='move_changelog')
 MoveChangelog.move_effect = relation(MoveEffect, backref='move_changelog')
 MoveChangelog.type = relation(Type, backref='move_changelog')
 
+MoveChangelog.effect = markdown.MoveEffectProperty('effect')
+MoveChangelog.effects = markdown.MoveEffectsProperty('effect')
+MoveChangelog.short_effect = markdown.MoveEffectProperty('short_effect')
+MoveChangelog.short_effects = markdown.MoveEffectsProperty('short_effect')
+
 MoveEffect.category_map = relation(MoveEffectCategoryMap)
 MoveEffect.categories = association_proxy('category_map', 'category')
 MoveEffect.changelog = relation(MoveEffectChangelog,