Esempio n. 1
0
def get_sub_resource_path_by(resource, subresource_name):
    """Find subresource path.

    :param resource: Resource instance on which the name
        gets queried upon.
    :param subresource_name: name of the resource attribute.
    :returns: Resource path.
    """
    if not subresource_name:
        raise exceptions.InvalidInputError(error='subresource cannot be empty')

    if not isinstance(subresource_name, list):
        subresource_name = [subresource_name]

    body = resource.json
    for path_item in subresource_name:
        body = body.get(path_item, {})

    if not body:
        raise exceptions.MissingAttributeError(
            attribute='/'.join(subresource_name), resource=resource.path)

    try:
        return get_member_identity(body)

    except (TypeError, KeyError):
        attribute = '/'.join(subresource_name)
        raise exceptions.MissingAttributeError(attribute=attribute,
                                               resource=resource.path)
Esempio n. 2
0
    def find_by_field_value(self, name, value):
        for element in self:
            assigned_value = getattr(element, name, None)
            if assigned_value == value:
                return element

        raise exceptions.InvalidInputError(
            error='Component with field %s and value %s not '
            'found' % (name, value))
Esempio n. 3
0
    def __init__(self, field, mapping, required=False, default=None):
        if not isinstance(mapping, dict):
            raise exceptions.InvalidInputError(
                error="%s initializer must be a "
                "dict" % self.__class__.__name__)

        super(EnumerationField, self).__init__(field,
                                               required=required,
                                               default=default,
                                               converter=mapping.get)
Esempio n. 4
0
    def __init__(self, path, required=False, default=None, converter=None):
        if not isinstance(path, list):
            path = [path]

        elif not path:
            raise exceptions.InvalidInputError(error='Path cannot be empty')

        self._path = path
        self._required = required
        self._default = default
        self._converter = converter
Esempio n. 5
0
    def find_multiple_by_field_value(self, name, value):
        elements = []
        for element in self:
            assigned_value = getattr(element, name, None)
            if assigned_value == value:
                elements.append(element)

        if len(elements) > 0:
            return elements

        raise exceptions.InvalidInputError(
            error='Component with field %s and value %s not '
            'found' % (name, value))