mirror of
https://github.com/veekun/pokedex.git
synced 2024-08-20 18:16:34 +00:00
Introspect relationships directly.
Possibly more fragile, but this way we don't need _set_relationships. SQLAlchemy version bump for AssociationProxy.remote_attr.
This commit is contained in:
parent
391fd1c1ac
commit
04b941755a
3 changed files with 44 additions and 53 deletions
|
@ -1829,7 +1829,8 @@ def add_relationships():
|
||||||
secondary=secondary)
|
secondary=secondary)
|
||||||
backref_info.update(backref_kwargs.pop('info', {}))
|
backref_info.update(backref_kwargs.pop('info', {}))
|
||||||
argument.relationship_info[backref_name] = backref_info
|
argument.relationship_info[backref_name] = backref_info
|
||||||
setattr(cls, name, relationship(argument, secondary=secondary,
|
doc = info.get('description', None)
|
||||||
|
setattr(cls, name, relationship(argument, secondary=secondary, doc=doc,
|
||||||
**kwargs))
|
**kwargs))
|
||||||
def add_association_proxy(name, target_collection, attr, **kwargs):
|
def add_association_proxy(name, target_collection, attr, **kwargs):
|
||||||
cls.relationship_info.setdefault('_order', [])
|
cls.relationship_info.setdefault('_order', [])
|
||||||
|
@ -1840,7 +1841,7 @@ def add_relationships():
|
||||||
type='association_proxy',
|
type='association_proxy',
|
||||||
target_collection=target_collection,
|
target_collection=target_collection,
|
||||||
attr=attr))
|
attr=attr))
|
||||||
setattr(cls, name, association_proxy(name, target_collection,
|
setattr(cls, name, association_proxy(target_collection, attr,
|
||||||
**kwargs))
|
**kwargs))
|
||||||
add_relationships(add_relationship=add_relationship,
|
add_relationships(add_relationship=add_relationship,
|
||||||
add_association_proxy=add_association_proxy)
|
add_association_proxy=add_association_proxy)
|
||||||
|
|
|
@ -20,7 +20,8 @@ from sphinx.ext.autodoc import ClassLevelDocumenter
|
||||||
|
|
||||||
from sqlalchemy import types
|
from sqlalchemy import types
|
||||||
from sqlalchemy.orm.attributes import InstrumentedAttribute
|
from sqlalchemy.orm.attributes import InstrumentedAttribute
|
||||||
from sqlalchemy.orm import configure_mappers
|
from sqlalchemy.orm.properties import RelationshipProperty
|
||||||
|
from sqlalchemy.orm import Mapper, configure_mappers
|
||||||
from sqlalchemy.ext.associationproxy import AssociationProxy
|
from sqlalchemy.ext.associationproxy import AssociationProxy
|
||||||
from pokedex.db.markdown import MoveEffectPropertyMap, MoveEffectProperty
|
from pokedex.db.markdown import MoveEffectPropertyMap, MoveEffectProperty
|
||||||
|
|
||||||
|
@ -196,55 +197,44 @@ def generate_strings(cls, remaining_attrs):
|
||||||
|
|
||||||
@with_header(u'Relationships')
|
@with_header(u'Relationships')
|
||||||
def generate_relationships(cls, remaining_attrs):
|
def generate_relationships(cls, remaining_attrs):
|
||||||
order = cls.relationship_info.get('_order', [])
|
for attr_name in sorted(remaining_attrs):
|
||||||
def sort_key((key, value)):
|
prop = getattr(cls, attr_name)
|
||||||
try:
|
def isrelationship(prop):
|
||||||
return 0, order.index(key)
|
return isinstance(prop, InstrumentedAttribute) and isinstance(prop.property, RelationshipProperty)
|
||||||
except ValueError:
|
if isrelationship(prop):
|
||||||
return 1, key
|
rel = prop.property
|
||||||
infos = sorted(cls.relationship_info.items(), key=sort_key)
|
yield u'%s.\ **%s**' % (cls.__name__, attr_name)
|
||||||
for rel_name, info in infos:
|
class_name = u':class:`~pokedex.db.tables.%s`' % rel.mapper.class_.__name__
|
||||||
if rel_name in remaining_attrs:
|
if rel.uselist:
|
||||||
info = cls.relationship_info.get(rel_name)
|
|
||||||
if info['type'] in ('relationship', 'backref'):
|
|
||||||
yield u'%s.\ **%s**' % (cls.__name__, rel_name)
|
|
||||||
class_name = u':class:`~pokedex.db.tables.%s`' % info['argument'].__name__
|
|
||||||
if info.get('uselist', True):
|
|
||||||
class_name = u'[%s]' % class_name
|
class_name = u'[%s]' % class_name
|
||||||
yield u'(→ %s)' % class_name
|
yield u'(→ %s)' % class_name
|
||||||
if 'description' in info:
|
if prop.__doc__:
|
||||||
yield u''
|
yield u''
|
||||||
yield u' ' + unicode(info['description'])
|
yield u' ' + unicode(prop.__doc__)
|
||||||
'''
|
if rel.secondary is not None:
|
||||||
if info.get('secondary') is not None:
|
|
||||||
yield u''
|
yield u''
|
||||||
yield ' Association table: ``%s``' % info['secondary']
|
yield ' Association table: ``%s``' % rel.secondary
|
||||||
if 'primaryjoin' in info:
|
#if rel.primaryjoin is not None:
|
||||||
yield u'')
|
# yield u''
|
||||||
yield ' Join condition: ``%s``' % info['primaryjoin']
|
# yield ' Join condition: ``%s``' % rel.primaryjoin
|
||||||
if 'secondaryjoin' in info:
|
# if rel.secondaryjoin is not None:
|
||||||
yield ' , ``%s``' % info['secondaryjoin']
|
# yield ' , ``%s``' % rel.secondaryjoin
|
||||||
'''
|
if rel.order_by:
|
||||||
if 'order_by' in info:
|
|
||||||
yield u''
|
yield u''
|
||||||
try:
|
|
||||||
order = iter(info['order_by'])
|
|
||||||
except TypeError:
|
|
||||||
order = [info['order_by']]
|
|
||||||
yield u' '
|
yield u' '
|
||||||
yield ' Ordered by: ' + u', '.join(
|
yield ' Ordered by: ' + u', '.join(
|
||||||
u'``%s``' % o for o in order)
|
u'``%s``' % o for o in rel.order_by)
|
||||||
elif info['type'] == 'association_proxy':
|
elif isinstance(prop, AssociationProxy):
|
||||||
yield u'%s.\ **%s**:' % (cls.__name__, rel_name)
|
yield u'%s.\ **%s**:' % (cls.__name__, attr_name)
|
||||||
yield '``{info[attr]}`` of ``self.{info[target_collection]}``'.format(
|
yield '``{prop.remote_attr.key}`` of ``self.{prop.target_collection}``'.format(
|
||||||
info=info)
|
prop=prop)
|
||||||
if 'description' in info:
|
'''if 'description' in info:
|
||||||
yield u''
|
yield u''
|
||||||
yield u' ' + unicode(info['description'])
|
yield u' ' + unicode(info['description'])'''
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
yield u''
|
yield u''
|
||||||
remaining_attrs.remove(rel_name)
|
remaining_attrs.remove(attr_name)
|
||||||
|
|
||||||
@with_header(u'Undocumented')
|
@with_header(u'Undocumented')
|
||||||
def generate_undocumented(cls, remaining_attrs):
|
def generate_undocumented(cls, remaining_attrs):
|
||||||
|
|
2
setup.py
2
setup.py
|
@ -8,7 +8,7 @@ setup(
|
||||||
'pokedex': ['data/csv/*.csv']
|
'pokedex': ['data/csv/*.csv']
|
||||||
},
|
},
|
||||||
install_requires=[
|
install_requires=[
|
||||||
'SQLAlchemy>=0.7',
|
'SQLAlchemy>=0.7.3',
|
||||||
'whoosh>=2.2.2',
|
'whoosh>=2.2.2',
|
||||||
'markdown',
|
'markdown',
|
||||||
'construct',
|
'construct',
|
||||||
|
|
Loading…
Reference in a new issue