Example #1
0
    def _dnf(where):
        """
        Constructs DNF of where tree consisting of terms in form:
            (alias, attribute, value, negation)
        meaning `alias.attribute = value`
         or `not alias.attribute = value` if negation is False

        Any conditions other then eq are dropped.
        """
        if isinstance(where, Lookup):
            # If where.lhs don't refer to a field then don't bother
            if not hasattr(where.lhs, 'target'):
                return SOME_TREE
            # Don't bother with complex right hand side either
            if isinstance(where.rhs, (QuerySet, Query)):
                return SOME_TREE
            # Skip conditions on non-serialized fields
            if isinstance(where.lhs.target, NOT_SERIALIZED_FIELDS):
                return SOME_TREE

            # 1.10: django.db.models.fields.related_lookups.RelatedExact

            attname = where.lhs.target.attname
            if isinstance(where, Exact):
                return [[(where.lhs.alias, attname, where.rhs, True)]]
            elif isinstance(where, IsNull):
                return [[(where.lhs.alias, attname, None, where.rhs)]]
            elif isinstance(where, In) and len(where.rhs) < LONG_DISJUNCTION:
                return [[(where.lhs.alias, attname, v, True)]
                        for v in where.rhs]
            else:
                return SOME_TREE
        elif isinstance(where, EverythingNode):
            return [[]]
        elif isinstance(where, NothingNode):
            return []
        elif isinstance(where, (ExtraWhere, SubqueryConstraint)):
            return SOME_TREE
        elif len(where) == 0:
            return [[]]
        else:
            chilren_dnfs = map(_dnf, where.children)

            if len(chilren_dnfs) == 0:
                return [[]]
            elif len(chilren_dnfs) == 1:
                result = chilren_dnfs[0]
            else:
                # Just unite children joined with OR
                if where.connector == OR:
                    result = cat(chilren_dnfs)
                # Use Cartesian product to AND children
                else:
                    result = map(cat, product(*chilren_dnfs))

            # Negating and expanding brackets
            if where.negated:
                result = [map(negate, p) for p in product(*result)]

            return result
Example #2
0
    def _dnf(where):
        """
        Constructs DNF of where tree consisting of terms in form:
            (alias, attribute, value, negation)
        meaning `alias.attribute = value`
         or `not alias.attribute = value` if negation is False

        Any conditions other then eq are dropped.
        """
        if isinstance(where, Lookup):
            # If where.lhs don't refer to a field then don't bother
            if not hasattr(where.lhs, 'target'):
                return SOME_TREE
            # Don't bother with complex right hand side either
            if isinstance(where.rhs, (QuerySet, Query, SQLEvaluator)):
                return SOME_TREE
            # Skip conditions on non-serialized fields
            if isinstance(where.lhs.target, NOT_SERIALIZED_FIELDS):
                return SOME_TREE

            attname = where.lhs.target.attname
            if isinstance(where, Exact):
                return [[(where.lhs.alias, attname, where.rhs, True)]]
            elif isinstance(where, IsNull):
                return [[(where.lhs.alias, attname, None, where.rhs)]]
            elif isinstance(where, In) and len(where.rhs) < LONG_DISJUNCTION:
                return [[(where.lhs.alias, attname, v, True)] for v in where.rhs]
            else:
                return SOME_TREE
        elif isinstance(where, EverythingNode):
            return [[]]
        elif isinstance(where, NothingNode):
            return []
        elif isinstance(where, (ExtraWhere, SubqueryConstraint)):
            return SOME_TREE
        elif len(where) == 0:
            return [[]]
        else:
            chilren_dnfs = map(_dnf, where.children)

            if len(chilren_dnfs) == 0:
                return [[]]
            elif len(chilren_dnfs) == 1:
                result = chilren_dnfs[0]
            else:
                # Just unite children joined with OR
                if where.connector == OR:
                    result = cat(chilren_dnfs)
                # Use Cartesian product to AND children
                else:
                    result = map(cat, product(*chilren_dnfs))

            # Negating and expanding brackets
            if where.negated:
                result = [map(negate, p) for p in product(*result)]

            return result
Example #3
0
    def _dnf(where):
        """
        Constructs DNF of where tree consisting of terms in form:
            (alias, attribute, value, negation)
        meaning `alias.attribute = value`
         or `not alias.attribute = value` if negation is False

        Any conditions other then eq are dropped.
        """
        if isinstance(where, tuple):
            constraint, lookup, annotation, value = where
            attname = attname_of(model, constraint.col)
            if isinstance(value, (QuerySet, Query, SQLEvaluator)):
                return [[SOME_COND]]
            elif lookup == 'exact':
                if isinstance(constraint.field, NOT_SERIALIZED_FIELDS):
                    return [[SOME_COND]]
                else:
                    return [[(constraint.alias, attname, value, True)]]
            elif lookup == 'isnull':
                return [[(constraint.alias, attname, None, value)]]
            elif lookup == 'in' and len(value) < LONG_DISJUNCTION:
                return [[(constraint.alias, attname, v, True)] for v in value]
            else:
                return [[SOME_COND]]
        elif isinstance(where, EverythingNode):
            return [[]]
        elif isinstance(where, NothingNode):
            return []
        elif isinstance(where, (ExtraWhere, SubqueryConstraint)):
            return [[SOME_COND]]
        elif len(where) == 0:
            return [[]]
        else:
            chilren_dnfs = map(_dnf, where.children)

            if len(chilren_dnfs) == 0:
                return [[]]
            elif len(chilren_dnfs) == 1:
                result = chilren_dnfs[0]
            else:
                # Just unite children joined with OR
                if where.connector == OR:
                    result = cat(chilren_dnfs)
                # Use Cartesian product to AND children
                else:
                    result = map(cat, product(*chilren_dnfs))

            # Negating and expanding brackets
            if where.negated:
                result = [map(negate, p) for p in product(*result)]

            return result
Example #4
0
    def _dnf(where):
        """
        Constructs DNF of where tree consisting of terms in form:
            (alias, attribute, value, negation)
        meaning `alias.attribute = value`
         or `not alias.attribute = value` if negation is False

        Any conditions other then eq are dropped.
        """
        if isinstance(where, tuple):
            constraint, lookup, annotation, value = where
            attname = attname_of(model, constraint.col)
            if isinstance(value, (QuerySet, Query, SQLEvaluator)):
                return [[SOME_COND]]
            elif lookup == 'exact':
                if isinstance(constraint.field, NON_SERIALIZABLE_FIELDS):
                    return [[SOME_COND]]
                else:
                    return [[(constraint.alias, attname, value, True)]]
            elif lookup == 'isnull':
                return [[(constraint.alias, attname, None, value)]]
            elif lookup == 'in' and len(value) < LONG_DISJUNCTION:
                return [[(constraint.alias, attname, v, True)] for v in value]
            else:
                return [[SOME_COND]]
        elif isinstance(where, EverythingNode):
            return [[]]
        elif isinstance(where, NothingNode):
            return []
        elif isinstance(where, (ExtraWhere, SubqueryConstraint)):
            return [[SOME_COND]]
        elif len(where) == 0:
            return [[]]
        else:
            chilren_dnfs = map(_dnf, where.children)

            if len(chilren_dnfs) == 0:
                return [[]]
            elif len(chilren_dnfs) == 1:
                result = chilren_dnfs[0]
            else:
                # Just unite children joined with OR
                if where.connector == OR:
                    result = cat(chilren_dnfs)
                # Use Cartesian product to AND children
                else:
                    result = map(cat, product(*chilren_dnfs))

            # Negating and expanding brackets
            if where.negated:
                result = [map(negate, p) for p in product(*result)]

            return result
Example #5
0
File: django.py Project: Suor/flaws
def _parse_urlconf(files, urlconf):
    # Old patterns() call, TODO: drop them
    patterns = [n for n in ast.walk(urlconf.tree) if is_call(n, 'patterns')]
    if patterns:
        return cat(_parse_patterns(files, p) for p in patterns)

    # NOTE that we don't support mixing patterns() and new style in single file
    refs = [n.args[1].s for n in ast.walk(urlconf.tree) if is_call(n, 'url') and
            len(n.args) >= 2 and isinstance(n.args[1], ast.Str)]

    included = [n.args[0].s for n in ast.walk(urlconf.tree) if is_call(n, 'include') and
                len(n.args) >= 1 and isinstance(n.args[0], ast.Str)]
    for mod in included:
        if mod in files:
            refs.append('%s.urlpatterns' % mod)
            refs.extend(_parse_urlconf(files, files[mod]))

    return refs
Example #6
0
def _parse_urlconf(files, urlconf):
    # Old patterns() call, TODO: drop them
    patterns = [n for n in ast.walk(urlconf.tree) if is_call(n, 'patterns')]
    if patterns:
        return cat(_parse_patterns(files, p) for p in patterns)

    # NOTE that we don't support mixing patterns() and new style in single file
    refs = [
        n.args[1].s for n in ast.walk(urlconf.tree) if is_call(n, 'url')
        and len(n.args) >= 2 and isinstance(n.args[1], ast.Str)
    ]

    included = [
        n.args[0].s for n in ast.walk(urlconf.tree) if is_call(n, 'include')
        and len(n.args) >= 1 and isinstance(n.args[0], ast.Str)
    ]
    for mod in included:
        if mod in files:
            refs.append('%s.urlpatterns' % mod)
            refs.extend(_parse_urlconf(files, files[mod]))

    return refs
Example #7
0
    def _dnf(where):
        """
        Constructs DNF of where tree consisting of terms in form:
            (alias, attribute, value, negation)
        meaning `alias.attribute = value`
         or `not alias.attribute = value` if negation is False

        Any conditions other then eq are dropped.
        """
        # Lookups appeared in Django 1.7
        if isinstance(where, Lookup):
            # If where.lhs don't refer to a field then don't bother
            if not hasattr(where.lhs, 'target'):
                return SOME_TREE
            # Don't bother with complex right hand side either
            if isinstance(where.rhs, (QuerySet, Query, SQLEvaluator)):
                return SOME_TREE
            # Skip conditions on non-serialized fields
            if isinstance(where.lhs.target, NOT_SERIALIZED_FIELDS):
                return SOME_TREE

            attname = where.lhs.target.attname
            if isinstance(where, Exact):
                return [[(where.lhs.alias, attname, where.rhs, True)]]
            elif isinstance(where, IsNull):
                return [[(where.lhs.alias, attname, None, where.rhs)]]
            elif isinstance(where, In) and len(where.rhs) < LONG_DISJUNCTION:
                return [[(where.lhs.alias, attname, v, True)] for v in where.rhs]
            else:
                return SOME_TREE
        # Django 1.6 and earlier used tuples to encode conditions
        elif isinstance(where, tuple):
            constraint, lookup, annotation, value = where
            # Don't bother with complex right hand side
            if isinstance(value, (QuerySet, Query, SQLEvaluator)):
                return SOME_TREE
            # Skip conditions on non-serialized fields
            if isinstance(constraint.field, NOT_SERIALIZED_FIELDS):
                return SOME_TREE

            attname = attname_of(model, constraint.col)
            if lookup == 'isnull':
                return [[(constraint.alias, attname, None, value)]]
            elif lookup == 'exact':
                return [[(constraint.alias, attname, value, True)]]
            elif lookup == 'in' and len(value) < LONG_DISJUNCTION:
                return [[(constraint.alias, attname, v, True)] for v in value]
            else:
                return SOME_TREE
        elif isinstance(where, EverythingNode):
            return [[]]
        elif isinstance(where, NothingNode):
            return []
        elif isinstance(where, (ExtraWhere, SubqueryConstraint)):
            return SOME_TREE
        elif len(where) == 0:
            return [[]]
        else:
            chilren_dnfs = map(_dnf, where.children)

            if len(chilren_dnfs) == 0:
                return [[]]
            elif len(chilren_dnfs) == 1:
                result = chilren_dnfs[0]
            else:
                # Just unite children joined with OR
                if where.connector == OR:
                    result = cat(chilren_dnfs)
                # Use Cartesian product to AND children
                else:
                    result = map(cat, product(*chilren_dnfs))

            # Negating and expanding brackets
            if where.negated:
                result = [map(negate, p) for p in product(*result)]

            return result
Example #8
0
def test_full_py2():
    assert sorted(funcy.__all__) == sorted(cat(m.__all__ for m in modules))
Example #9
0
def test_full_py2():
    assert sorted(funcy.__all__) == sorted(cat(m.__all__ for m in modules))
Example #10
0
    def _dnf(where):
        """
        Constructs DNF of where tree consisting of terms in form:
            (alias, attribute, value, negation)
        meaning `alias.attribute = value`
         or `not alias.attribute = value` if negation is False

        Any conditions other then eq are dropped.
        """
        # Lookups appeared in Django 1.7
        if isinstance(where, Lookup):
            # If where.lhs don't refer to a field then don't bother
            if not hasattr(where.lhs, 'target'):
                return [[SOME_COND]]
            # TODO: check if all of this are possible
            if isinstance(where.rhs, (QuerySet, Query, SQLEvaluator)):
                return [[SOME_COND]]

            attname = where.lhs.target.attname
            if isinstance(where, Exact):
                if isinstance(where.lhs.target, NOT_SERIALIZED_FIELDS):
                    return [[SOME_COND]]
                else:
                    return [[(where.lhs.alias, attname, where.rhs, True)]]
            elif isinstance(where, IsNull):
                return [[(where.lhs.alias, attname, None, where.rhs)]]
            elif isinstance(where, In) and len(where.rhs) < LONG_DISJUNCTION:
                return [[(where.lhs.alias, attname, v, True)] for v in where.rhs]
            else:
                return [[SOME_COND]]
        # Django 1.6 and earlier used tuples to encode conditions
        elif isinstance(where, tuple):
            constraint, lookup, annotation, value = where
            attname = attname_of(model, constraint.col)
            if isinstance(value, (QuerySet, Query, SQLEvaluator)):
                return [[SOME_COND]]
            elif lookup == 'exact':
                # TODO: check for non-serialized for both exact and in
                if isinstance(constraint.field, NOT_SERIALIZED_FIELDS):
                    return [[SOME_COND]]
                else:
                    return [[(constraint.alias, attname, value, True)]]
            elif lookup == 'isnull':
                return [[(constraint.alias, attname, None, value)]]
            elif lookup == 'in' and len(value) < LONG_DISJUNCTION:
                return [[(constraint.alias, attname, v, True)] for v in value]
            else:
                return [[SOME_COND]]
        elif isinstance(where, EverythingNode):
            return [[]]
        elif isinstance(where, NothingNode):
            return []
        elif isinstance(where, (ExtraWhere, SubqueryConstraint)):
            return [[SOME_COND]]
        elif len(where) == 0:
            return [[]]
        else:
            chilren_dnfs = map(_dnf, where.children)

            if len(chilren_dnfs) == 0:
                return [[]]
            elif len(chilren_dnfs) == 1:
                result = chilren_dnfs[0]
            else:
                # Just unite children joined with OR
                if where.connector == OR:
                    result = cat(chilren_dnfs)
                # Use Cartesian product to AND children
                else:
                    result = map(cat, product(*chilren_dnfs))

            # Negating and expanding brackets
            if where.negated:
                result = [map(negate, p) for p in product(*result)]

            return result