autocommit.utils

Miscellaneous utilities for autocommit

Functions

compute_truncation(lengths, max_total_length)

Figure out the truncation of the lengths to fit in max_total_length

create_argument_parser(**argparse_kwargs)

Decorator that potentially creates an argument parser and passes it down

get_api_key([key_file, storage_dir])

Get the Mistral API key

take_argument_annotation_from(this)

Take the argument annotations from another function

walk_tree(tree, *[, base_path])

Walk a tree recursively

Classes

CreateArgumentParserReturnSignature(*args, ...)

FileIsBinaryReturnableError(file)

Returned by the tool calls if a file cannot be printed

FileNewError(file)

Returned by the tool calls if a file has no diff because it is new

FileNotFoundReturnableError(file)

Returned by the tool calls if a file is not found in the repository

FileUnchangedError(file)

Returned by the tool calls if a file has no diff because it did not change

autocommit.utils.get_api_key(key_file: Path | None = None, storage_dir: Path | None = None) str[source]

Get the Mistral API key

class autocommit.utils.FileNotFoundReturnableError(file: str)[source]

Returned by the tool calls if a file is not found in the repository

class autocommit.utils.FileIsBinaryReturnableError(file: str)[source]

Returned by the tool calls if a file cannot be printed

class autocommit.utils.FileUnchangedError(file: str)[source]

Returned by the tool calls if a file has no diff because it did not change

class autocommit.utils.FileNewError(file: str)[source]

Returned by the tool calls if a file has no diff because it is new

autocommit.utils.walk_tree(tree, *, base_path=()) Iterator[tuple[tuple[str, ...], Blob]][source]

Walk a tree recursively

Yield all blobs in the tree, and their path relative to the root tree

Parameters:
  • tree (Tree) – The tree to walk

  • base_path (tuple, optional) – The path to the current tree. Defaults to ().

autocommit.utils.take_argument_annotation_from(this: Callable[[P], Any]) Callable[[Callable[[...], T]], Callable[[P], T]][source]

Take the argument annotations from another function

Decorator stating that the function it decorates should have the same annotations as the function passed as argument.

Inspired from https://stackoverflow.com/a/71262408/4948719

class autocommit.utils.CreateArgumentParserReturnSignature(*args, **kwargs)[source]
autocommit.utils.create_argument_parser(**argparse_kwargs) Callable[[Callable[[ArgumentParser], None]], CreateArgumentParserReturnSignature][source]

Decorator that potentially creates an argument parser and passes it down

This is a helper function that allows to have a function that either

  • populates an already existing parser

    (if passed as argument, generally for use with subparsers)

  • creates an all new parser

It decorates a function that takes a single argument, an ArgumentParser, and populates it.

usage:

# @create_argument_parser
# accepts all the arguments of ArgumentParser.
# They will be passed to the ArgumentParser constructor
# if a new parser is created
@create_argument_parser(description="This is the description")
def my_argument_parser(parser: ArgumentParser):
    parser.add_argument("--my-arg", help="This is the help")

specific_parser = my_argument_parser()
# or
parser = ArgumentParser()
subparsers = parser.add_subparsers()
my_argument_parser(subparsers.add_parser("my_subcommand"))

Note

The input function should only take a single argument parser, and does not need to return anything. The output function will return the parser.

autocommit.utils.compute_truncation(lengths, max_total_length)[source]

Figure out the truncation of the lengths to fit in max_total_length

Given a list of lengths [l_1...l_n], and a maximum total length L, figure out the maximum truncation index t, such that the sum of the lengths truncated at t is less than L, ie

\[\sum_{i=1}^n min(l_i, t) < L\]
Parameters:
  • lengths (list[int]) – The list of lengths

  • max_total_length (int) – The maximum total length