Esempio n. 1
0
def evaluate(value, context, freeze=True):
    list_type = tuple if freeze else list
    dict_type = yaqlutils.FrozenDict if freeze else dict
    set_type = frozenset if freeze else set

    if isinstance(value, (dsl_types.YaqlExpression,
                          yaql.language.expressions.Statement)):
        return value(context)
    elif isinstance(value, yaqlutils.MappingType):
        return dict_type(
            (evaluate(d_key, context, freeze),
             evaluate(d_value, context, freeze))
            for d_key, d_value in six.iteritems(value))
    elif yaqlutils.is_sequence(value):
        return list_type(evaluate(t, context, freeze) for t in value)
    elif isinstance(value, yaqlutils.SetType):
        return set_type(evaluate(t, context, freeze) for t in value)
    elif yaqlutils.is_iterable(value):
        return list_type(
            evaluate(t, context, freeze)
            for t in yaqlutils.limit_iterable(
                value, constants.ITERATORS_LIMIT))
    elif isinstance(value, dsl_types.MuranoObjectInterface):
        return value.object
    else:
        return value
Esempio n. 2
0
def evaluate(value, context, freeze=True):
    list_type = tuple if freeze else list
    dict_type = yaqlutils.FrozenDict if freeze else dict
    set_type = frozenset if freeze else set

    if isinstance(
            value,
        (dsl_types.YaqlExpression, yaql.language.expressions.Statement)):
        return value(context)
    elif isinstance(value, yaqlutils.MappingType):
        return dict_type((evaluate(d_key, context, freeze),
                          evaluate(d_value, context, freeze))
                         for d_key, d_value in value.items())
    elif yaqlutils.is_sequence(value):
        return list_type(evaluate(t, context, freeze) for t in value)
    elif isinstance(value, yaqlutils.SetType):
        return set_type(evaluate(t, context, freeze) for t in value)
    elif yaqlutils.is_iterable(value):
        return list_type(
            evaluate(t, context, freeze) for t in yaqlutils.limit_iterable(
                value, CONF.murano.dsl_iterators_limit))
    elif isinstance(value, dsl_types.MuranoObjectInterface):
        return value.object
    else:
        return value
Esempio n. 3
0
def flatten(collection):
    for t in collection:
        if utils.is_iterable(t):
            for t2 in flatten(t):
                yield t2
        else:
            yield t
Esempio n. 4
0
def flatten(collection):
    for t in collection:
        if utils.is_iterable(t):
            for t2 in flatten(t):
                yield t2
        else:
            yield t
Esempio n. 5
0
def select_many(collection, selector):
    for item in collection:
        inner = selector(item)
        if utils.is_iterable(inner):
            for t in inner:
                yield t
        else:
            yield inner
Esempio n. 6
0
def traverse(seed, producer=None, track_visited=True):
    if not yaqlutils.is_iterable(seed):
        seed = [seed]
    visited = None if not track_visited else set()
    queue = collections.deque(seed)
    while queue:
        item = queue.popleft()
        if track_visited:
            if item in visited:
                continue
            visited.add(item)
        produced = (yield item)
        if produced is None and producer:
            produced = producer(item)
        if produced:
            queue.extend(produced)
Esempio n. 7
0
def traverse(seed, producer=None, track_visited=True):
    if not yaqlutils.is_iterable(seed):
        seed = [seed]
    visited = None if not track_visited else set()
    queue = collections.deque(seed)
    while queue:
        item = queue.popleft()
        if track_visited:
            if item in visited:
                continue
            visited.add(item)
        produced = (yield item)
        if produced is None and producer:
            produced = producer(item)
        if produced:
            queue.extend(produced)
Esempio n. 8
0
def evaluate(value, context):
    if isinstance(value, (dsl_types.YaqlExpression, yaql.language.expressions.Statement)):
        return value(context)
    elif isinstance(value, yaqlutils.MappingType):
        return yaqlutils.FrozenDict(
            (evaluate(d_key, context), evaluate(d_value, context)) for d_key, d_value in six.iteritems(value)
        )
    elif yaqlutils.is_sequence(value):
        return tuple(evaluate(t, context) for t in value)
    elif isinstance(value, yaqlutils.SetType):
        return frozenset(evaluate(t, context) for t in value)
    elif yaqlutils.is_iterable(value):
        return tuple(evaluate(t, context) for t in yaqlutils.limit_iterable(value, constants.ITERATORS_LIMIT))
    elif isinstance(value, dsl_types.MuranoObjectInterface):
        return value.object
    else:
        return value
Esempio n. 9
0
def flatten(collection):
    """:yaql:flatten

    Returns an iterator to the recursive traversal of collection.

    :signature: collection.flatten()
    :receiverArg collection: collection to be traversed
    :argType collection: iterable
    :returnType: list

    .. code::

        yaql> ["a", ["b", [2,3]]].flatten()
        ["a", "b", 2, 3]
    """
    for t in collection:
        if utils.is_iterable(t):
            for t2 in flatten(t):
                yield t2
        else:
            yield t
Esempio n. 10
0
def flatten(collection):
    """:yaql:flatten

    Returns an iterator to the recursive traversal of collection.

    :signature: collection.flatten()
    :receiverArg collection: collection to be traversed
    :argType collection: iterable
    :returnType: list

    .. code::

        yaql> ["a", ["b", [2,3]]].flatten()
        ["a", "b", 2, 3]
    """
    for t in collection:
        if utils.is_iterable(t):
            for t2 in flatten(t):
                yield t2
        else:
            yield t
Esempio n. 11
0
def evaluate(value, context):
    if isinstance(value, (dsl_types.YaqlExpression,
                          yaql.language.expressions.Statement)):
        return value(context)
    elif isinstance(value, yaqlutils.MappingType):
        return yaqlutils.FrozenDict(
            (evaluate(d_key, context),
             evaluate(d_value, context))
            for d_key, d_value in six.iteritems(value))
    elif yaqlutils.is_sequence(value):
        return tuple(evaluate(t, context) for t in value)
    elif isinstance(value, yaqlutils.SetType):
        return frozenset(evaluate(t, context) for t in value)
    elif yaqlutils.is_iterable(value):
        return tuple(
            evaluate(t, context)
            for t in yaqlutils.limit_iterable(
                value, constants.ITERATORS_LIMIT))
    elif isinstance(value, dsl_types.MuranoObjectInterface):
        return value.object
    else:
        return value
Esempio n. 12
0
def get_limited_if_need(data, engine):
    if (yaqlutils.is_iterable(data) or yaqlutils.is_sequence(data) or
            isinstance(data, (yaqlutils.MappingType, yaqlutils.SetType))):
        return yaqlutils.limit_iterable(data, engine)
    return data
Esempio n. 13
0
def is_iterable(value):
    return utils.is_iterable(value)