Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split tables.py into several files #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions pokedex/db/tables/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# encoding: utf8

u"""The Pokédex schema.

Columns have a info dictionary with these keys:
- official: True if the values appear in games or official material; False if
they are fan-created or fan-written. This flag is currently only set for
official text columns.
- format: The format of a text column. Can be one of:
- plaintext: Normal Unicode text (widely used in names)
- markdown: Veekun's Markdown flavor (generally used in effect descriptions)
- gametext: Transcription of in-game text that strives to be both
human-readable and represent the original text exactly.
- identifier: A fan-made identifier in the [-_a-z0-9]* format. Not intended
for translation.
- latex: A formula in LaTeX syntax.
- ripped: True for text that has been ripped from the games, and can be ripped
again for new versions or languages

- string_getter: for translation columns, a function taking (text, session,
language) that is used for properties on the main table. Used for Markdown
text.

See `pokedex.db.multilang` for how localizable text columns work. The session
classes in that module can be used to change the default language.
"""

from pokedex.db.tables.base import (
metadata, TableBase, mapped_classes, Language, create_translation_table)

from pokedex.db.tables.core import (
Ability, AbilityChangelog, AbilityFlavorText, Berry, BerryFirmness,
BerryFlavor, Characteristic, ContestCombo, ContestEffect, ContestType,
EggGroup, Encounter, EncounterCondition, EncounterConditionValue,
EncounterConditionValueMap, EncounterMethod, EncounterSlot, EvolutionChain,
EvolutionTrigger, Experience, Gender, Generation, GrowthRate, Item,
ItemCategory, ItemFlag, ItemFlagMap, ItemFlavorText, ItemFlingEffect,
ItemGameIndex, ItemPocket, Location, LocationArea,
LocationAreaEncounterRate, LocationGameIndex, Machine, Move,
MoveBattleStyle, MoveChangelog, MoveDamageClass, MoveEffect,
MoveEffectChangelog, MoveFlag, MoveFlagMap, MoveFlavorText, MoveMeta,
MoveMetaAilment, MoveMetaCategory, MoveMetaStatChange, MoveTarget, Nature,
NatureBattleStylePreference, NaturePokeathlonStat, PalPark, PalParkArea,
PokeathlonStat, Pokedex, PokedexVersionGroup, Pokemon, PokemonAbility,
PokemonColor, PokemonDexNumber, PokemonEggGroup, PokemonEvolution,
PokemonForm, PokemonFormGeneration, PokemonFormPokeathlonStat,
PokemonGameIndex, PokemonHabitat, PokemonItem, PokemonMove,
PokemonMoveMethod, PokemonShape, PokemonSpecies, PokemonSpeciesFlavorText,
PokemonStat, PokemonType, Region, Stat, SuperContestCombo,
SuperContestEffect, Type, TypeEfficacy, TypeGameIndex, Version,
VersionGroup, VersionGroupPokemonMoveMethod, VersionGroupRegion)

from pokedex.db.tables.conquest import (
ConquestEpisode, ConquestEpisodeWarrior, ConquestKingdom, ConquestMaxLink,
ConquestMoveData, ConquestMoveDisplacement, ConquestMoveEffect,
ConquestMoveRange, ConquestPokemonAbility, ConquestPokemonEvolution,
ConquestPokemonMove, ConquestPokemonStat, ConquestStat,
ConquestTransformationPokemon, ConquestTransformationWarrior,
ConquestWarrior, ConquestWarriorArchetype, ConquestWarriorRank,
ConquestWarriorRankStatMap, ConquestWarriorSkill, ConquestWarriorSpecialty,
ConquestWarriorStat, ConquestWarriorTransformation)
80 changes: 80 additions & 0 deletions pokedex/db/tables/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# encoding: utf8

from functools import partial

from sqlalchemy.ext.declarative import declarative_base, DeclarativeMeta
from sqlalchemy import Column, MetaData
from sqlalchemy.types import Boolean, Integer, Unicode

from pokedex.db import multilang


class TableSuperclass(object):
"""Superclass for declarative tables, to give them some generic niceties
like stringification.
"""
def __unicode__(self):
"""Be as useful as possible. Show the primary key, and an identifier
if we've got one.
"""
typename = u'.'.join((__name__, type(self).__name__))

pk_constraint = self.__table__.primary_key
if not pk_constraint:
return u"<%s object at %x>" % (typename, id(self))

pk = u', '.join(unicode(getattr(self, column.name))
for column in pk_constraint.columns)
try:
return u"<%s object (%s): %s>" % (typename, pk, self.identifier)
except AttributeError:
return u"<%s object (%s)>" % (typename, pk)

def __str__(self):
return unicode(self).encode('utf8')

def __repr__(self):
return unicode(self).encode('utf8')


mapped_classes = []
class TableMetaclass(DeclarativeMeta):
def __init__(cls, name, bases, attrs):
super(TableMetaclass, cls).__init__(name, bases, attrs)
if hasattr(cls, '__tablename__'):
mapped_classes.append(cls)
cls.translation_classes = []

metadata = MetaData()
TableBase = declarative_base(metadata=metadata, cls=TableSuperclass, metaclass=TableMetaclass)


### Need Language first, to create the partial() below

class Language(TableBase):
u"""A language the Pokémon games have been translated into."""
__tablename__ = 'languages'
__singlename__ = 'language'
id = Column(Integer, primary_key=True, nullable=False,
doc=u"A numeric ID")
iso639 = Column(Unicode(79), nullable=False,
doc=u"The two-letter code of the country where this language is spoken. Note that it is not unique.",
info=dict(format='identifier'))
iso3166 = Column(Unicode(79), nullable=False,
doc=u"The two-letter code of the language. Note that it is not unique.",
info=dict(format='identifier'))
identifier = Column(Unicode(79), nullable=False,
doc=u"An identifier",
info=dict(format='identifier'))
official = Column(Boolean, nullable=False, index=True,
doc=u"True iff games are produced in the language.")
order = Column(Integer, nullable=True,
doc=u"Order for sorting in foreign name lists.")

create_translation_table = partial(multilang.create_translation_table, language_class=Language)

create_translation_table('language_names', Language, 'names',
name = Column(Unicode(79), nullable=False, index=True,
doc=u"The name",
info=dict(format='plaintext', official=True)),
)
Loading