Beispiel #1
0
def _add_volume_from_secret(secret: Dict[Text, Text]):
    return gamla.compose_left(
        gamla.assoc_in(
            keys=["containers", 0, "volumeMounts"],
            value=[
                {
                    "name": secret["volume_name"],
                    "mountPath": secret["mount_path"],
                    "readOnly": True,
                },
            ],
        ),
        gamla.assoc_in(
            keys=["volumes"],
            value=[
                {
                    "name": secret["volume_name"],
                    "secret": {
                        "secretName": secret["secret_name"]
                    },
                },
            ],
        ),
    )
Beispiel #2
0
):
    return collection.sort(key, direction)


@gamla.curry
def count(collection: pymongo.collection.Collection, query: Dict[Text,
                                                                 Any]) -> int:
    return collection.count_documents(query)


def add_match_filter(f: Callable) -> Tuple[Dict, ...]:
    return gamla.compose_left(
        gamla.head,
        gamla.itemgetter("$match"),
        f,
        query_to_match_aggregation_stage,
        gamla.wrap_tuple,
    )


query_to_count_aggregation_stage = gamla.just({"$count": "count"})

query_to_match_aggregation_stage = gamla.value_to_dict("$match")

query_to_sample_aggregation_stage = gamla.compose_left(
    gamla.value_to_dict("size"),
    gamla.value_to_dict("$sample"),
)

query_to_sort_aggregation_stage = gamla.value_to_dict("$sort")
Beispiel #3
0
            gamla.just(update),
            gamla.compose_left(
                _time_since_last_updated(identifier),
                gamla.anyjuxt(
                    gamla.equals(None),
                    gamla.greater_than(datetime.timedelta(hours=ttl_hours)),
                ),
            ),
        ),
    )


_total_hours_since_update = gamla.ternary(
    gamla.equals(None),
    gamla.just(0),
    gamla.compose_left(lambda time_span: time_span.total_seconds() / 3600,
                       round),
)


def _get_cache_filename(
    cache_file_name: str,
    factory: Callable,
) -> str:

    cache_file = os.path.join(
        os.path.dirname(factory.__code__.co_filename),
        cache_file_name,
    )

    if not os.path.isfile(cache_file):
        with open(cache_file, "w") as f:
Beispiel #4
0
import ast
from typing import Iterable

import gamla

_gen_lambdas = gamla.compose_left(
    ast.walk,
    gamla.filter(gamla.is_instance(ast.Lambda)),
)


def _gen_lambda_arg_names(l: ast.Lambda) -> Iterable[str]:
    return gamla.pipe(l, lambda l: l.args.args,
                      gamla.map(gamla.attrgetter("arg")))


_is_unary_def = gamla.compose_left(_gen_lambda_arg_names, gamla.count,
                                   gamla.equals(1))


def _is_lambda_internal_invocation(l: ast.Lambda) -> bool:
    return gamla.pipe(l.body, gamla.is_instance(ast.Call))


def _get_call_arg_names(call: ast.Call) -> Iterable[str]:
    return gamla.pipe(
        gamla.concat([call.args, call.keywords]),
        gamla.map(
            gamla.ternary(
                # Generator expressions can also be given as arguments.
                gamla.is_instance(ast.Name),
Beispiel #5
0
detect = gamla.compose_left(
    ast.walk,
    gamla.bifurcate(
        gamla.compose_left(
            gamla.filter(
                gamla.anyjuxt(
                    gamla.is_instance(ast.AsyncFunctionDef),
                    gamla.is_instance(ast.FunctionDef),
                    gamla.is_instance(ast.ClassDef),
                ),
            ),
            gamla.map(gamla.attrgetter("name")),
        ),
        gamla.compose_left(
            gamla.filter(gamla.is_instance(ast.Name)),
            gamla.map(gamla.attrgetter("id")),
        ),
        gamla.compose_left(
            gamla.filter(gamla.is_instance(ast.Attribute)),
            gamla.map(gamla.attrgetter("attr")),
        ),
    ),
    gamla.concat,
    gamla.filter(lambda name: name.startswith("_")),
    gamla.remove(lambda name: name.startswith("__")),
    gamla.remove(gamla.equals("_")),
    gamla.count_by(gamla.identity),
    gamla.valfilter(gamla.equals(1)),
    dict.keys,
    gamla.map(gamla.wrap_str("`{}` is defined but unused.")),
)
Beispiel #6
0
import argparse
import ast
from typing import Optional, Sequence, Tuple

import gamla

from lint import comment, dead_code, redundant_lambda

_file_contents_to_messages = gamla.compose_left(
    gamla.log_text("{}"),
    open,
    lambda f: f.read(),
    gamla.juxtcat(
        gamla.compose_left(
            ast.parse,
            gamla.juxtcat(redundant_lambda.detect, dead_code.detect),
        ),
        comment.detect,
    ),
    tuple,
)


def _pretty_print_findings(findings: Tuple[str, ...], filename: str):
    print(filename)  # noqa
    for finding in findings:
        print(finding)  # noqa


def main(argv: Optional[Sequence[str]] = None) -> int:
    parser = argparse.ArgumentParser()
Beispiel #7
0
import gamla

detect = gamla.compose_left(
    lambda s: s.split("\n"),
    gamla.filter(
        gamla.compose_left(str.lower, lambda s: re.match(r"\s*#.*", s))),
    gamla.remove(lambda s: re.search(r"# type: ignore", s)),
    gamla.remove(lambda s: re.search(r"noqa", s)),
    gamla.bifurcate(
        gamla.compose_left(
            gamla.filter(
                gamla.alljuxt(
                    gamla.compose_left(str.lower,
                                       lambda s: re.search(r"\btodo\b", s)),
                    lambda s: not re.search(r"#\sTODO\([a-zA-Z]+\):\s.*", s),
                ), ),
            gamla.map(
                lambda s:
                f"Malformatted `TODO` syntax (should be `# TODO(name): ...`): [{s}]",
            ),
        ),
        gamla.compose_left(
            gamla.filter(lambda s: re.search(r"^\s*#[^\s:]", s)),
            gamla.map(lambda s:
                      f"Comment should start with a space or a colon: [{s}]"),
        ),
    ),
    gamla.concat,
)
Beispiel #8
0
def open_file(mode: Text):
    return gamla.compose_left(pathlib.Path, lambda p: p.open(mode=mode))