Remove CompositePath

This commit is contained in:
Konsta Vesterinen
2014-01-04 02:43:38 +02:00
parent 795c3f2f45
commit afa4e984eb
2 changed files with 21 additions and 27 deletions

View File

@@ -27,6 +27,10 @@ class PathException(Exception):
pass pass
class DataException(Exception):
pass
class with_backrefs(object): class with_backrefs(object):
""" """
Marks given attribute path so that whenever its fetched with batch_fetch Marks given attribute path so that whenever its fetched with batch_fetch
@@ -37,9 +41,6 @@ class with_backrefs(object):
self.path = path self.path = path
class Path(object): class Path(object):
""" """
A class that represents an attribute path. A class that represents an attribute path.
@@ -79,7 +80,8 @@ class Path(object):
related_entities.extend(getattr(entity, attrs[0])) related_entities.extend(getattr(entity, attrs[0]))
if not related_entities: if not related_entities:
return raise DataException('No related entities.')
subpath = '.'.join(attrs[1:]) subpath = '.'.join(attrs[1:])
return Path.parse(related_entities, subpath, populate_backrefs) return Path.parse(related_entities, subpath, populate_backrefs)
else: else:
@@ -107,11 +109,6 @@ class Path(object):
return OneToManyFetcher return OneToManyFetcher
class CompositePath(object):
def __init__(self, *paths):
self.paths = paths
def batch_fetch(entities, *attr_paths): def batch_fetch(entities, *attr_paths):
""" """
Batch fetch given relationship attribute for collection of entities. Batch fetch given relationship attribute for collection of entities.
@@ -174,10 +171,16 @@ def batch_fetch(entities, *attr_paths):
if entities: if entities:
for path in attr_paths: for path in attr_paths:
try:
fetcher = fetcher_factory(entities, path) fetcher = fetcher_factory(entities, path)
if fetcher:
fetcher.fetch() fetcher.fetch()
fetcher.populate() fetcher.populate()
except DataException:
pass
def get_fetcher(entities, path, populate_backrefs):
return Path.parse(entities, path, populate_backrefs).fetcher
def fetcher_factory(entities, path): def fetcher_factory(entities, path):
@@ -186,20 +189,12 @@ def fetcher_factory(entities, path):
path = path.path path = path.path
populate_backrefs = True populate_backrefs = True
if isinstance(path, CompositePath): if isinstance(path, tuple):
fetchers = [] return CompositeFetcher(
for path in path.paths: *(get_fetcher(entities, p, populate_backrefs) for p in path)
path = Path.parse(entities, path, populate_backrefs)
if path:
fetchers.append(
path.fetcher
) )
return CompositeFetcher(*fetchers)
else: else:
path = Path.parse(entities, path, populate_backrefs) return get_fetcher(entities, path, populate_backrefs)
if path:
return path.fetcher
class CompositeFetcher(object): class CompositeFetcher(object):

View File

@@ -1,6 +1,5 @@
import sqlalchemy as sa import sqlalchemy as sa
from sqlalchemy_utils import batch_fetch from sqlalchemy_utils import batch_fetch
from sqlalchemy_utils.batch import CompositePath
from tests import TestCase from tests import TestCase
@@ -93,7 +92,7 @@ class TestCompoundOneToManyBatchFetching(TestCase):
batch_fetch( batch_fetch(
buildings, buildings,
'business_premises', 'business_premises',
CompositePath( (
'equipment', 'equipment',
'business_premises.equipment' 'business_premises.equipment'
) )
@@ -198,7 +197,7 @@ class TestCompoundManyToOneBatchFetching(TestCase):
batch_fetch( batch_fetch(
buildings, buildings,
'business_premises', 'business_premises',
CompositePath( (
'equipment', 'equipment',
'business_premises.equipment' 'business_premises.equipment'
) )