+VersionGroup.pokedex = relation(Pokedex, back_populates='version_groups')
+
+### Convenience function
+def all_tables():
+ u"""Yields all tables in the pokédex"""
+ for table in set(t for t in globals().values() if isclass(t)):
+ if issubclass(table, TableBase) and table is not TableBase:
+ yield table
+
+
+### Add name tables
+for table in all_tables():
+ if issubclass(table, OfficiallyNamed):
+ cls = TextColumn
+ info=dict(description="The name", format='plaintext', official=True)
+ elif issubclass(table, UnofficiallyNamed):
+ cls = ProseColumn
+ info=dict(description="The name", format='plaintext', official=False)
+ else:
+ continue
+ table.name = cls(Unicode(class_mapper(table).c.identifier.type.length),
+ plural='names', nullable=False, info=info)
+
+### Add text/prose tables
+
+def makeTextTable(object_table, name_plural, name_singular, columns, lazy):
+ # With "Language", we'd have two language_id. So, rename one to 'lang'
+ safe_name = object_table.__singlename__
+ if safe_name == 'language':
+ safe_name = 'lang'
+
+ fields = {
+ safe_name + '_id': Column(Integer,
+ ForeignKey(object_table.id), primary_key=True, nullable=False,
+ info=dict(description="ID of the object this table represents")
+ ),
+ '__tablename__': table.__singlename__ + '_' + name_plural,
+ '__singlename__': table.__singlename__ + '_' + name_singular,
+ 'is_%s_table' % name_singular: True,
+ }
+
+ fields.update((name, col) for name, plural, col in columns)
+
+ name = table.__name__ + name_singular.capitalize()
+
+ # There are some dynamic things that can only be set at class
+ # creation time because of declarative metaclass magic.
+ # So create class dynamically.
+ Strings = type(name, (TableBase, LanguageSpecific), fields)
+
+ # Alias the described thing to 'object', to make meta stuff easier
+ Strings.object_id = getattr(Strings, safe_name + '_id')
+
+ # The relation to the object
+ setattr(table, name_plural, relation(
+ Strings,
+ primaryjoin=(table.id == Strings.object_id),
+ backref=safe_name,
+ collection_class=attribute_mapped_collection('language'),
+ lazy=lazy,
+ ))
+
+ Strings.object = getattr(Strings, safe_name)
+
+ Strings.object_table = table
+ setattr(table, name_singular + '_table', Strings)
+
+ Strings.language = relation(
+ Language,
+ primaryjoin=Strings.language_id == Language.id,
+ )
+
+ for colname, pluralname, column in columns:
+ # Provide a relation with all the names, and an English accessor
+ # for backwards compatibility
+ def scope(colname, pluralname, column):
+ def get_string(self):
+ return dict(
+ (l, getattr(t, colname))
+ for l, t in getattr(self, name_plural).items()
+ )
+
+ def get_english_string(self):
+ try:
+ return get_string(self)['en']
+ except KeyError:
+ raise AttributeError(colname)
+
+ setattr(table, pluralname, property(get_string))
+ setattr(table, colname, property(get_english_string))
+ scope(colname, pluralname, column)
+
+ if colname == 'name':
+ table.name_table = Strings
+
+ return Strings
+
+for table in all_tables():
+ text_columns = []
+ prose_columns = []
+ for colname in dir(table):
+ column = getattr(table, colname)
+ if isinstance(column, TextColumn):
+ text_columns.append((colname, column.plural, column.makeSAColumn()))
+ elif isinstance(column, ProseColumn):
+ prose_columns.append((colname, column.plural, column.makeSAColumn()))
+ if text_columns:
+ string_table = makeTextTable(table, 'texts', 'text', text_columns, lazy=False)
+ globals()[string_table.__name__] = string_table
+ if prose_columns:
+ string_table = makeTextTable(table, 'prose', 'prose', prose_columns, lazy=True)
+ globals()[string_table.__name__] = string_table
+ if (text_columns or prose_columns) and issubclass(table, LanguageSpecific):
+ raise AssertionError("Language-specific table %s shouldn't have explicit language-specific columns" % table)
+
+### Add language relations
+for table in all_tables():
+ if issubclass(table, LanguageSpecific):
+ table.language = relation(Language, primaryjoin=table.language_id == Language.id)