|
1 | | -from typing import Any |
2 | | -from typing import Dict |
| 1 | +from typing import Callable |
| 2 | +from typing import NamedTuple |
| 3 | +from typing import Optional |
| 4 | +from typing import Sequence |
| 5 | +from typing import Tuple |
| 6 | +from typing import TYPE_CHECKING |
3 | 7 |
|
4 | 8 | from pre_commit.languages import conda |
5 | 9 | from pre_commit.languages import docker |
|
15 | 19 | from pre_commit.languages import script |
16 | 20 | from pre_commit.languages import swift |
17 | 21 | from pre_commit.languages import system |
| 22 | +from pre_commit.prefix import Prefix |
18 | 23 |
|
| 24 | +if TYPE_CHECKING: |
| 25 | + from pre_commit.repository import Hook |
19 | 26 |
|
20 | | -# A language implements the following constant and functions in its module: |
21 | | -# |
22 | | -# # Use None for no environment |
23 | | -# ENVIRONMENT_DIR = 'foo_env' |
24 | | -# |
25 | | -# def get_default_version(): |
26 | | -# """Return a value to replace the 'default' value for language_version. |
27 | | -# |
28 | | -# return 'default' if there is no better option. |
29 | | -# """ |
30 | | -# |
31 | | -# def healthy(prefix, language_version): |
32 | | -# """Return whether or not the environment is considered functional.""" |
33 | | -# |
34 | | -# def install_environment(prefix, version, additional_dependencies): |
35 | | -# """Installs a repository in the given repository. Note that the current |
36 | | -# working directory will already be inside the repository. |
37 | | -# |
38 | | -# Args: |
39 | | -# prefix - `Prefix` bound to the repository. |
40 | | -# version - A version specified in the hook configuration or 'default'. |
41 | | -# """ |
42 | | -# |
43 | | -# def run_hook(hook, file_args, color): |
44 | | -# """Runs a hook and returns the returncode and output of running that |
45 | | -# hook. |
46 | | -# |
47 | | -# Args: |
48 | | -# hook - `Hook` |
49 | | -# file_args - The files to be run |
50 | | -# color - whether the hook should be given a pty (when supported) |
51 | | -# |
52 | | -# Returns: |
53 | | -# (returncode, output) |
54 | | -# """ |
55 | 27 |
|
56 | | -languages: Dict[str, Any] = { |
57 | | - 'conda': conda, |
58 | | - 'docker': docker, |
59 | | - 'docker_image': docker_image, |
60 | | - 'fail': fail, |
61 | | - 'golang': golang, |
62 | | - 'node': node, |
63 | | - 'pygrep': pygrep, |
64 | | - 'python': python, |
65 | | - 'python_venv': python_venv, |
66 | | - 'ruby': ruby, |
67 | | - 'rust': rust, |
68 | | - 'script': script, |
69 | | - 'swift': swift, |
70 | | - 'system': system, |
| 28 | +class Language(NamedTuple): |
| 29 | + name: str |
| 30 | + # Use `None` for no installation / environment |
| 31 | + ENVIRONMENT_DIR: Optional[str] |
| 32 | + # return a value to replace `'default` for `language_version` |
| 33 | + get_default_version: Callable[[], str] |
| 34 | + # return whether the environment is healthy (or should be rebuilt) |
| 35 | + healthy: Callable[[Prefix, str], bool] |
| 36 | + # install a repository for the given language and language_version |
| 37 | + install_environment: Callable[[Prefix, str, Sequence[str]], None] |
| 38 | + # execute a hook and return the exit code and output |
| 39 | + run_hook: 'Callable[[Hook, Sequence[str], bool], Tuple[int, bytes]]' |
| 40 | + |
| 41 | + |
| 42 | +# TODO: back to modules + Protocol: https://github.com/python/mypy/issues/5018 |
| 43 | +languages = { |
| 44 | + # BEGIN GENERATED (testing/gen-languages-all) |
| 45 | + 'conda': Language(name='conda', ENVIRONMENT_DIR=conda.ENVIRONMENT_DIR, get_default_version=conda.get_default_version, healthy=conda.healthy, install_environment=conda.install_environment, run_hook=conda.run_hook), # noqa: E501 |
| 46 | + 'docker': Language(name='docker', ENVIRONMENT_DIR=docker.ENVIRONMENT_DIR, get_default_version=docker.get_default_version, healthy=docker.healthy, install_environment=docker.install_environment, run_hook=docker.run_hook), # noqa: E501 |
| 47 | + 'docker_image': Language(name='docker_image', ENVIRONMENT_DIR=docker_image.ENVIRONMENT_DIR, get_default_version=docker_image.get_default_version, healthy=docker_image.healthy, install_environment=docker_image.install_environment, run_hook=docker_image.run_hook), # noqa: E501 |
| 48 | + 'fail': Language(name='fail', ENVIRONMENT_DIR=fail.ENVIRONMENT_DIR, get_default_version=fail.get_default_version, healthy=fail.healthy, install_environment=fail.install_environment, run_hook=fail.run_hook), # noqa: E501 |
| 49 | + 'golang': Language(name='golang', ENVIRONMENT_DIR=golang.ENVIRONMENT_DIR, get_default_version=golang.get_default_version, healthy=golang.healthy, install_environment=golang.install_environment, run_hook=golang.run_hook), # noqa: E501 |
| 50 | + 'node': Language(name='node', ENVIRONMENT_DIR=node.ENVIRONMENT_DIR, get_default_version=node.get_default_version, healthy=node.healthy, install_environment=node.install_environment, run_hook=node.run_hook), # noqa: E501 |
| 51 | + 'pygrep': Language(name='pygrep', ENVIRONMENT_DIR=pygrep.ENVIRONMENT_DIR, get_default_version=pygrep.get_default_version, healthy=pygrep.healthy, install_environment=pygrep.install_environment, run_hook=pygrep.run_hook), # noqa: E501 |
| 52 | + 'python': Language(name='python', ENVIRONMENT_DIR=python.ENVIRONMENT_DIR, get_default_version=python.get_default_version, healthy=python.healthy, install_environment=python.install_environment, run_hook=python.run_hook), # noqa: E501 |
| 53 | + 'python_venv': Language(name='python_venv', ENVIRONMENT_DIR=python_venv.ENVIRONMENT_DIR, get_default_version=python_venv.get_default_version, healthy=python_venv.healthy, install_environment=python_venv.install_environment, run_hook=python_venv.run_hook), # noqa: E501 |
| 54 | + 'ruby': Language(name='ruby', ENVIRONMENT_DIR=ruby.ENVIRONMENT_DIR, get_default_version=ruby.get_default_version, healthy=ruby.healthy, install_environment=ruby.install_environment, run_hook=ruby.run_hook), # noqa: E501 |
| 55 | + 'rust': Language(name='rust', ENVIRONMENT_DIR=rust.ENVIRONMENT_DIR, get_default_version=rust.get_default_version, healthy=rust.healthy, install_environment=rust.install_environment, run_hook=rust.run_hook), # noqa: E501 |
| 56 | + 'script': Language(name='script', ENVIRONMENT_DIR=script.ENVIRONMENT_DIR, get_default_version=script.get_default_version, healthy=script.healthy, install_environment=script.install_environment, run_hook=script.run_hook), # noqa: E501 |
| 57 | + 'swift': Language(name='swift', ENVIRONMENT_DIR=swift.ENVIRONMENT_DIR, get_default_version=swift.get_default_version, healthy=swift.healthy, install_environment=swift.install_environment, run_hook=swift.run_hook), # noqa: E501 |
| 58 | + 'system': Language(name='system', ENVIRONMENT_DIR=system.ENVIRONMENT_DIR, get_default_version=system.get_default_version, healthy=system.healthy, install_environment=system.install_environment, run_hook=system.run_hook), # noqa: E501 |
| 59 | + # END GENERATED |
71 | 60 | } |
72 | 61 | all_languages = sorted(languages) |
0 commit comments