orchestrata_tools.query.dsl

Reference › orchestrata_tools › query › dsl

This module is a small fluent query builder for AST searches. Query is a dataclass with a target (methods by default), a filters dictionary and a limit. classes, methods, modules and functions choose the target, where merges keyword filters, and take sets a result limit; each returns the query so calls chain, for example methods where tested=False and public=True, or modules where lines__gt=100. The suffixes __gt, __lt and __contains are interpreted by evaluator, which runs the query against an index. The CLI in __main__ builds queries with this class.

At a glance

Source src/orchestrata_tools/query/dsl.py
Lines of code 45
Classes 1
Functions 0
Imports (internal) none
Imported by orchestrata_tools.query, orchestrata_tools.query.evaluator
Tests that import it tests/test_orchestrata_tools_reviewer_query_indexer.py

Class diagram

Class diagram orchestrata_tools.query.dsl

Dependency diagram

Dependency diagram orchestrata_tools.query.dsl

API

Classes

Query (dataclass)

Decorators: dataclass · Line 16

A query builder for AST searches.

Attribute Type Default
target str 'methods'
filters dict[str, Any] field(default_factory=dict)
limit int 0
Method Signature Summary
classes classes() -> Query
methods methods() -> Query
modules modules() -> Query
functions functions() -> Query
where where(****kwargs: Any) -> Query
take take(n: int) -> Query

Source

Permalink: https://gitlab.com/MSamy/orchestrata-ultimate-architecture/-/blob/a4cbc2bdf/src/orchestrata_tools/query/dsl.py

"""Simple query DSL for AST searches.

Examples:
    Query().classes().where(protocol=True)
    Query().methods().where(tested=False, public=True)
    Query().modules().where(lines__gt=100)
"""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Any


@dataclass
class Query:
    """A query builder for AST searches."""

    target: str = "methods"  # methods, classes, modules, functions
    filters: dict[str, Any] = field(default_factory=dict)
    limit: int = 0

    def classes(self) -> Query:
        self.target = "classes"
        return self

    def methods(self) -> Query:
        self.target = "methods"
        return self

    def modules(self) -> Query:
        self.target = "modules"
        return self

    def functions(self) -> Query:
        self.target = "functions"
        return self

    def where(self, **kwargs: Any) -> Query:
        self.filters.update(kwargs)
        return self

    def take(self, n: int) -> Query:
        self.limit = n
        return self