Structure handlers to allow .get() to return lists

See #1154. In the end I didn't modify the Attributehandler and
TagHandler like this, instead I added the `return_list` argument
for cases when one wants a guaranteed return.
This commit is contained in:
Griatch 2017-08-27 14:56:05 +02:00
parent 05a3d0435d
commit 92df3ce5ae
13 changed files with 86 additions and 1608 deletions

View file

@ -387,7 +387,7 @@ class AttributeHandler(object):
def get(self, key=None, default=None, category=None, return_obj=False,
strattr=False, raise_exception=False, accessing_obj=None,
default_access=True):
default_access=True, return_list=False):
"""
Get the Attribute.
@ -398,7 +398,8 @@ class AttributeHandler(object):
category (str, optional): the category within which to
retrieve attribute(s).
default (any, optional): The value to return if an
Attribute was not defined.
Attribute was not defined. If set, it will be returned in
a one-item list.
return_obj (bool, optional): If set, the return is not the value of the
Attribute but the Attribute object itself.
strattr (bool, optional): Return the `strvalue` field of
@ -410,13 +411,15 @@ class AttributeHandler(object):
accessing_obj (object, optional): If set, an `attrread`
permission lock will be checked before returning each
looked-after Attribute.
default_access (bool, optional):
default_access (bool, optional): If no `attrread` lock is set on
object, this determines if the lock should then be passed or not.
return_list (bool, optional):
Returns:
result (any, Attribute or list): This will be the value of the found
Attribute unless `return_obj` is True, at which point it will be
the attribute object or None. If multiple keys are given, this
will be a list of values or attribute objects/None.
result (any or list): One or more matches for keys and/or categories. Each match will be
the value of the found Attribute(s) unless `return_obj` is True, at which point it
will be the attribute object itself or None. If `return_list` is True, this will
always be a list, regardless of the number of elements.
Raises:
AttributeError: If `raise_exception` is set and no matching Attribute
@ -453,7 +456,10 @@ class AttributeHandler(object):
ret = ret if return_obj else [attr.strvalue for attr in ret if attr]
else:
ret = ret if return_obj else [attr.value for attr in ret if attr]
if not ret:
if return_list:
return ret if ret else [default] if default is not None else []
elif not ret:
return ret if len(key) > 1 else default
return ret[0] if len(ret) == 1 else ret