Thing: Allow by_fullname and by_id to ignore missing Things.

This commit is contained in:
Andre D
2013-07-01 15:51:50 -07:00
committed by Chad Birch
parent 9b037e0a8f
commit 1c4ef53b4e

View File

@@ -382,7 +382,7 @@ class DataThing(object):
#TODO error when something isn't found?
@classmethod
def _byID(cls, ids, data=False, return_dict=True, extra_props=None,
stale=False):
stale=False, ignore_missing=False):
ids, single = tup(ids, True)
prefix = thing_prefix(cls.__name__)
@@ -407,17 +407,20 @@ class DataThing(object):
bases = sgm(cache, ids, items_db, prefix, stale=stale,
found_fn=count_found)
#check to see if we found everything we asked for
# Check to see if we found everything we asked for
missing = []
for i in ids:
if i not in bases:
missing = [i for i in ids if i not in bases]
raise NotFound, '%s %s' % (cls.__name__, missing)
if bases[i] and bases[i]._id != i:
missing.append(i)
elif bases[i] and bases[i]._id != i:
g.log.error("thing.py: Doppleganger on byID: %s got %s for %s" %
(cls.__name__, bases[i]._id, i))
bases[i] = items_db([i]).values()[0]
bases[i]._cache_myself()
if missing and not ignore_missing:
raise NotFound, '%s %s' % (cls.__name__, missing)
for i in missing:
ids.remove(i)
if data:
need = []
@@ -440,7 +443,7 @@ class DataThing(object):
bases[_id].__setattr__(k, v, False)
if single:
return bases[ids[0]]
return bases[ids[0]] if ids else None
elif return_dict:
return bases
else:
@@ -467,6 +470,7 @@ class DataThing(object):
@classmethod
def _by_fullname(cls, names,
return_dict = True,
ignore_missing=False,
**kw):
names, single = tup(names, True)
@@ -492,7 +496,7 @@ class DataThing(object):
# lookup ids for each type
identified = {}
for real_type, thing_ids in table.iteritems():
i = real_type._byID(thing_ids, **kw)
i = real_type._byID(thing_ids, ignore_missing=ignore_missing, **kw)
identified[real_type] = i
# interleave types in original order of the name
@@ -500,11 +504,13 @@ class DataThing(object):
for fullname in names:
if lookup.has_key(fullname):
real_type, thing_id = lookup[fullname]
res.append((fullname,
identified.get(real_type, {}).get(thing_id)))
thing = identified.get(real_type, {}).get(thing_id)
if not thing and ignore_missing:
continue
res.append((fullname, thing))
if single:
return res[0][1]
return res[0][1] if res else None
elif return_dict:
return dict(res)
else: