-
Notifications
You must be signed in to change notification settings - Fork 23
/
tab_completion.py
71 lines (52 loc) · 2.28 KB
/
tab_completion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Copyright (C) 2022 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
"""This module provides tab completion if the dependencies are installed"""
from pathlib import Path
from kit.utils.constants import PluginsConfig, PluginState
from kit.utils.config import load_config, load_toml
from kit.utils.files import list_dirs
try:
# Tab completion is an optional feature, this means that
# hekit can be executed without enabling this functionality.
# But if the user installs argcomplete and registers the
# hekit script, tab completion will be available
from argcomplete import autocomplete
except ImportError as e:
def autocomplete(arg): # type: ignore[misc] # pylint: disable=unused-argument
"""Continue with the execution"""
def enable_tab_completion(parser):
"""Enables tab completion feature if dependencies are fulfilled"""
autocomplete(parser)
def components_completer(
prefix, parsed_args, **kwargs # pylint: disable=unused-argument
) -> list[str]:
"""Returns the components that were installed with the hekit"""
config = load_config(parsed_args.config)
return list_dirs(config.repo_location)
def instances_completer(
prefix, parsed_args, **kwargs # pylint: disable=unused-argument
) -> list[str]:
"""Returns the instances of a component that
was installed with the hekit"""
config = load_config(parsed_args.config)
comp_name_path = f"{config.repo_location}/{parsed_args.component}"
return list_dirs(comp_name_path)
def get_plugins_by_state(
state: str, source_file: Path = PluginsConfig.FILE
) -> list[str]:
"""Return a list of plugins with a specific state"""
try:
plugin_dict = load_toml(source_file)[PluginsConfig.KEY]
return [k for k, v in plugin_dict.items() if v["state"] == state]
except (FileNotFoundError, KeyError):
return []
def plugins_enable_completer(
prefix, parsed_args, **kwargs # pylint: disable=unused-argument
) -> list[str]:
"""Return a list of plugins that are enabled"""
return get_plugins_by_state(PluginState.ENABLE)
def plugins_disable_completer(
prefix, parsed_args, **kwargs # pylint: disable=unused-argument
) -> list[str]:
"""Return a list of plugins that are disabled"""
return get_plugins_by_state(PluginState.DISABLE)