Exemplo n.º 1
0
    def has(self, key=None, category=None):
        """
        Checks if the given Attribute (or list of Attributes) exists on
        the object.

        Args:
            key (str or iterable): The Attribute key or keys to check for.
                If `None`, search by category.
            category (str or None): Limit the check to Attributes with this
                category (note, that `None` is the default category).

        Returns:
            has_attribute (bool or list): If the Attribute exists on
                this object or not. If `key` was given as an iterable then
                the return is a list of booleans.

        """
        ret = super(SharedAttributeHandler, self).has(key=key,
                                                      category=category)
        if isinstance(ret, list):
            # In this case, we just return the list, as it would be
            # too complicated to handle individual checks.
            return ret

        if not ret:
            prototype = AttributeHandler.get(self, "prototype")
            if prototype:
                return prototype.attributes.has(key=key, category=category)

        return ret
Exemplo n.º 2
0
 def attributes(self):
     return AttributeHandler(self)
Exemplo n.º 3
0
    def get(self,
            key=None,
            default=None,
            category=None,
            return_obj=False,
            strattr=False,
            raise_exception=False,
            accessing_obj=None,
            default_access=True,
            return_list=False):
        """
        Get the Attribute.

        Args:
            key (str or list, optional): the attribute identifier or
                multiple attributes to get. if a list of keys, the
                method will return a list.
            category (str, optional): the category within which to
                retrieve attribute(s).
            default (any, optional): The value to return if an
                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
                the Attribute rather than the usual `value`, this is a
                string-only value for quick database searches.
            raise_exception (bool, optional): When an Attribute is not
                found, the return from this is usually `default`. If this
                is set, an exception is raised instead.
            accessing_obj (object, optional): If set, an `attrread`
                permission lock will be checked before returning each
                looked-after Attribute.
            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 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
                was found matching `key`.

        """
        ret = super(SharedAttributeHandler,
                    self).get(key=key,
                              default=None,
                              category=category,
                              return_obj=return_obj,
                              strattr=strattr,
                              raise_exception=raise_exception,
                              accessing_obj=accessing_obj,
                              default_access=default_access,
                              return_list=return_list)
        if isinstance(ret, list):
            # In this case, we just return the list, as it would be
            # too complicated to handle individual checks.
            return ret

        if ret is None:
            prototype = AttributeHandler.get(self, "prototype")
            if prototype:
                return prototype.attributes.get(
                    key=key,
                    default=default,
                    category=category,
                    return_obj=return_obj,
                    strattr=strattr,
                    raise_exception=raise_exception,
                    accessing_obj=accessing_obj,
                    default_access=default_access,
                    return_list=return_list)

        return ret