# XXX: Check if "gametext" is set correctly everywhere
import collections
+from functools import partial
from sqlalchemy import Column, ForeignKey, MetaData, PrimaryKeyConstraint, Table, UniqueConstraint
from sqlalchemy.ext.declarative import (
)
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.orm import (
- backref, eagerload_all, relation, class_mapper, synonym, mapper,
+ backref, compile_mappers, eagerload_all, relation, class_mapper, synonym, mapper,
)
from sqlalchemy.orm.session import Session, object_session
-from sqlalchemy.orm.collections import attribute_mapped_collection
-from sqlalchemy.ext.associationproxy import association_proxy
+from sqlalchemy.orm.interfaces import AttributeExtension
+from sqlalchemy.orm.collections import attribute_mapped_collection, MappedCollection, collection, collection_adapter
+from sqlalchemy.ext.associationproxy import _AssociationDict, association_proxy
from sqlalchemy.sql import and_
-from sqlalchemy.sql.expression import ColumnOperators
+from sqlalchemy.sql.expression import ColumnOperators, bindparam
from sqlalchemy.schema import ColumnDefault
from sqlalchemy.types import *
from inspect import isclass
if hasattr(cls, '__tablename__'):
table_classes.append(cls)
-metadata = MetaData()
-TableBase = declarative_base(metadata=metadata, metaclass=TableMetaclass)
-
-### Helper classes
-# XXX this stuff isn't covered anywhere; maybe put it in TableBase??
-class Named(object):
- """Mixin for objects that have names"""
+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 '<%s: %s>' % (type(self).__name__, self.identifier)
+ return u"<%s object (%s): %s>" % (typename, pk, self.identifier)
except AttributeError:
- return '<%s>' % type(self).__name__
+ return u"<%s object (%s)>" % (typename, pk)
def __str__(self):
- return unicode(self).encode('utf-8')
+ return unicode(self).encode('utf8')
- def __repr__(self):
- return str(self)
+metadata = MetaData()
+TableBase = declarative_base(metadata=metadata, cls=TableSuperclass, metaclass=TableMetaclass)
+### Helper classes
class LanguageSpecific(object):
"""Mixin for prose and text tables"""
@declared_attr
name = TextColumn(Unicode(16), nullable=False, index=True, plural='names',
info=dict(description="The name", format='plaintext', official=True))
- # Languages compare equal to its identifier, so a dictionary of
- # translations, with a Language as the key, can be indexed by the identifier
- def __eq__(self, other):
- try:
- return (
- self is other or
- self.identifier == other or
- self.identifier == other.identifier
- )
- except AttributeError:
- return NotImplemented
-
- def __ne__(self, other):
- return not (self == other)
-
- def __hash__(self):
- return hash(self.identifier)
-
class Location(TableBase):
u"""A place in the Pokémon world
"""
default_lang = u'en'
-def makeTextTable(object_table, name_plural, name_singular, columns, lazy):
+def create_translation_table(_table_name, foreign_class,
+ _language_class=Language, **kwargs):
+ """Creates a table that represents some kind of data attached to the given
+ foreign class, but translated across several languages. Returns the new
+ table's mapped class.
+TODO give it a __table__ or __init__?
+
+ `foreign_class` must have a `__singlename__`, currently only used to create
+ the name of the foreign key column.
+TODO remove this requirement
+
+ Also supports the notion of a default language, which is attached to the
+ session. This is English by default, for historical and practical reasons.
+
+ Usage looks like this:
+
+ class Foo(Base): ...
+
+ create_translation_table('foo_bars', Foo,
+ name = Column(...),
+ )
+
+ # Now you can do the following:
+ foo.name
+ foo.name_map['en']
+ foo.foo_bars['en']
+
+ foo.name_map['en'] = "new name"
+ del foo.name_map['en']
+
+ q.options(joinedload(Foo.default_translation))
+ q.options(joinedload(Foo.foo_bars))
+
+ In the above example, the following attributes are added to Foo:
+
+ - `foo_bars`, a relation to the new table. It uses a dict-based collection
+ class, where the keys are language identifiers and the values are rows in
+ the created tables.
+ - `foo_bars_local`, a relation to the row in the new table that matches the
+ current default language.
+
+ Note that these are distinct relations. Even though the former necessarily
+ includes the latter, SQLAlchemy doesn't treat them as linked; loading one
+ will not load the other. Modifying both within the same transaction has
+ undefined behavior.
+
+ For each column provided, the following additional attributes are added to
+ Foo:
+
+ - `(column)_map`, an association proxy onto `foo_bars`.
+ - `(column)`, an association proxy onto `foo_bars_local`.
+
+ Pardon the naming disparity, but the grammar suffers otherwise.
+
+ Modifying these directly is not likely to be a good idea.
+ """
+ # n.b.: _language_class only exists for the sake of tests, which sometimes
+ # want to create tables entirely separate from the pokedex metadata
+
+ foreign_key_name = foreign_class.__singlename__ + '_id'
+ # A foreign key "language_id" will clash with the language_id we naturally
+ # put in every table. Rename it something else
+ if foreign_key_name == 'language_id':
+ # TODO change language_id below instead and rename this
+ foreign_key_name = 'lang_id'
+
+ Translations = type(_table_name, (object,), {
+ '_language_identifier': association_proxy('language', 'identifier'),
+ })
+
+ # Create the table object
+ table = Table(_table_name, foreign_class.__table__.metadata,
+ Column(foreign_key_name, Integer, ForeignKey(foreign_class.id),
+ primary_key=True, nullable=False),
+ Column('language_id', Integer, ForeignKey(_language_class.id),
+ primary_key=True, nullable=False),
+ )
+
+ # Add ye columns
+ # Column objects have a _creation_order attribute in ascending order; use
+ # this to get the (unordered) kwargs sorted correctly
+ kwitems = kwargs.items()
+ kwitems.sort(key=lambda kv: kv[1]._creation_order)
+ for name, column in kwitems:
+ column.name = name
+ table.append_column(column)
+
+ # Construct ye mapper
+ mapper(Translations, table, properties={
+ # TODO change to foreign_id
+ 'object_id': synonym(foreign_key_name),
+ # TODO change this as appropriate
+ 'language': relation(_language_class,
+ primaryjoin=table.c.language_id == _language_class.id,
+ lazy='joined',
+ innerjoin=True),
+ # TODO does this need to join to the original table?
+ })
+
+ # Add full-table relations to the original class
+ # Class.foo_bars
+ setattr(foreign_class, _table_name, relation(Translations,
+ primaryjoin=foreign_class.id == Translations.object_id,
+ collection_class=attribute_mapped_collection('language'),
+ # TODO
+ lazy='select',
+ ))
+ # Class.foo_bars_local
+ # This is a bit clever; it uses bindparam() to make the join clause
+ # modifiable on the fly. db sessions know the current language identifier
+ # populates the bindparam.
+ local_relation_name = _table_name + '_local'
+ setattr(foreign_class, local_relation_name, relation(Translations,
+ primaryjoin=and_(
+ foreign_class.id == Translations.object_id,
+ Translations._language_identifier ==
+ bindparam('_default_language', required=True),
+ ),
+ uselist=False,
+ # TODO MORESO HERE
+ lazy='select',
+ ))
+
+ # Add per-column proxies to the original class
+ for name, column in kwitems:
+ # Class.(column) -- accessor for the default language's value
+ setattr(foreign_class, name,
+ association_proxy(local_relation_name, name))
+
+ # Class.(column)_map -- accessor for the language dict
+ # Need a custom creator since Translations doesn't have an init, and
+ # these are passed as *args anyway
+ def creator(language, value):
+ row = Translations()
+ row.language = language
+ setattr(row, name, value)
+ return row
+ setattr(foreign_class, name + '_map',
+ association_proxy(_table_name, name, creator=creator))
+
+ # Done
+ return Translations
+
+def makeTextTable(foreign_table_class, table_suffix_plural, table_suffix_singular, columns, lazy, Language=Language):
# 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'
+ foreign_key_name = foreign_table_class.__singlename__
+ if foreign_key_name == 'language':
+ foreign_key_name = 'lang'
- tablename = object_table.__singlename__ + '_' + name_plural
- singlename = object_table.__singlename__ + '_' + name_singular
+ table_name = foreign_table_class.__singlename__ + '_' + table_suffix_plural
- class Strings(object):
- __tablename__ = tablename
- __singlename__ = singlename
- _attrname = name_plural
+ class TranslatedStringsTable(object):
+ __tablename__ = table_name
+ _attrname = table_suffix_plural
_language_identifier = association_proxy('language', 'identifier')
- for name, plural, column in columns:
- column.name = name
+ for column_name, column_name_plural, column in columns:
+ column.name = column_name
if not column.nullable:
# A Python side default value, so that the strings can be set
# one by one without the DB complaining about missing values
column.default = ColumnDefault(u'')
- table = Table(tablename, metadata,
- Column(safe_name + '_id', Integer, ForeignKey(object_table.id),
+ table = Table(table_name, foreign_table_class.__table__.metadata,
+ Column(foreign_key_name + '_id', Integer, ForeignKey(foreign_table_class.id),
primary_key=True, nullable=False),
Column('language_id', Integer, ForeignKey(Language.id),
primary_key=True, index=True, nullable=False),
*(column for name, plural, column in columns)
)
- mapper(Strings, table,
+ mapper(TranslatedStringsTable, table,
properties={
- "object_id": synonym(safe_name + '_id'),
+ "object_id": synonym(foreign_key_name + '_id'),
"language": relation(Language,
primaryjoin=table.c.language_id == Language.id,
),
- safe_name: relation(object_table,
- primaryjoin=(object_table.id == table.c[safe_name + "_id"]),
- backref=backref(name_plural,
- collection_class=attribute_mapped_collection('language'),
+ foreign_key_name: relation(foreign_table_class,
+ primaryjoin=(foreign_table_class.id == table.c[foreign_key_name + "_id"]),
+ backref=backref(table_suffix_plural,
+ collection_class=attribute_mapped_collection('_language_identifier'),
lazy=lazy,
),
),
)
# The relation to the object
- Strings.object = getattr(Strings, safe_name)
+ TranslatedStringsTable.object = getattr(TranslatedStringsTable, foreign_key_name)
# Link the tables themselves, so we can get them if needed
- Strings.object_table = object_table
- setattr(object_table, name_singular + '_table', Strings)
+ TranslatedStringsTable.foreign_table_class = foreign_table_class
+ setattr(foreign_table_class, table_suffix_singular + '_table', TranslatedStringsTable)
- for colname, pluralname, column in columns:
+ for column_name, column_name_plural, column in columns:
# Provide a property with all the names, and an English accessor
# for backwards compatibility
- setattr(object_table, pluralname, StringProperty(
- object_table, Strings, colname,
- ))
- setattr(object_table, colname, DefaultLangProperty(pluralname))
+ def text_string_creator(language_code, string):
+ row = TranslatedStringsTable()
+ row._language_identifier = language_code
+ setattr(row, column_name, string)
+ return row
- if colname == 'name':
- object_table.name_table = Strings
+ setattr(foreign_table_class, column_name_plural,
+ association_proxy(table_suffix_plural, column_name, creator=text_string_creator))
+ setattr(foreign_table_class, column_name, DefaultLangProperty(column_name_plural))
- return Strings
-
-class StringProperty(object):
- def __init__(self, cls, stringclass, colname):
- self.cls = cls
- self.colname = colname
- self.stringclass = stringclass
-
- def __get__(self, instance, cls):
- if instance:
- return StringMapping(instance, self)
- else:
- return self
-
- def __getitem__(self, lang):
- return StringExpression(self, lang)
-
- def __str__(self):
- return '<StringDict %s.%s>' % (self.cls, self.colname)
-
-class StringMapping(collections.MutableMapping):
- def __init__(self, instance, prop):
- self.stringclass = prop.stringclass
- self.instance = instance
- self.strings = getattr(instance, prop.stringclass._attrname)
- self.colname = prop.colname
-
- def __len__(self):
- return len(self.strings)
-
- def __iter__(self):
- return iter(self.strings)
-
- def __contains__(self, lang):
- return lang in self.strings
-
- def __getitem__(self, lang):
- return getattr(self.strings[lang], self.colname)
-
- def __setitem__(self, lang, value):
- try:
- # Modifying an existing row
- row = self.strings[lang]
- except KeyError:
- # We need do add a whole row for the language
- row = self.stringclass()
- row.object_id = self.instance.id
- session = object_session(self.instance)
- if isinstance(lang, basestring):
- lang = session.query(Language).filter_by(
- identifier=lang).one()
- row.language = lang
- self.strings[lang] = row
- session.add(row)
- return setattr(row, self.colname, value)
-
- def __delitem__(self, lang):
- raise NotImplementedError('Cannot delete a single string. '
- 'Perhaps you wan to delete all of %s.%s?' %
- (self.instance, self.stringclass._attrname)
- )
-
-class StringExpression(ColumnOperators):
- def __init__(self, prop, lang):
- self.prop = prop
- self.column = getattr(prop.stringclass, prop.colname)
- self.lang_column = prop.stringclass._language_identifier
- if isinstance(lang, basestring):
- self.lang = lang
- else:
- self.lang = lang.identifier
+ if column_name == 'name':
+ foreign_table_class.name_table = TranslatedStringsTable
- def operate(self, op, *values, **kwargs):
- return getattr(self.prop.cls, self.prop.stringclass._attrname).any(and_(
- self.lang_column == self.lang,
- op(self.column, *values, **kwargs),
- ))
+ compile_mappers()
+ return TranslatedStringsTable
class DefaultLangProperty(object):
- def __init__(self, colname):
- self.colname = colname
+ def __init__(self, column_name):
+ self.column_name = column_name
def __get__(self, instance, cls):
if instance:
- return getattr(instance, self.colname)[default_lang]
+ return getattr(instance, self.column_name)[default_lang]
else:
- return getattr(cls, self.colname)[default_lang]
+ # TODO I think this is kind of broken
+ return getattr(cls, self.column_name)[default_lang]
def __set__(self, instance, value):
getattr(instance, self.colname)[default_lang] = value
if text_columns:
string_table = makeTextTable(table, 'texts', 'text', text_columns, lazy=False)
if prose_columns:
- string_table = makeTextTable(table, 'prose', 'prose', prose_columns, lazy=True)
+ string_table = makeTextTable(table, 'prose', 'prose', prose_columns, lazy='select')
### Add language relations
for table in list(table_classes):