-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathautocomplete
73 lines (57 loc) · 2.06 KB
/
autocomplete
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
72
73
#!/usr/bin/env python3
"""
Python3 module to generate autocomplete feature
"""
import importlib.machinery
import yaml
from jinja2 import Environment, FileSystemLoader
# Constants
CLI_COMMAND = 'nita'
TEMPLATES = 'templates/'
CLI_TEMPLATE = CLI_COMMAND + '.j2'
TMP = 'tmp/'
VARS = TMP + 'vars.yml'
COMPLETION = 'bash_completion.d/'
CLI_SCRIPT = COMPLETION + CLI_COMMAND
AUTOCOMPLETE = {}
loader = importlib.machinery.SourceFileLoader(CLI_COMMAND, CLI_COMMAND)
source = loader.load_module()
def autocomplete_values(dictionary, root):
"""
Function that returns a dictionary with list of possible
autocompletion values in a nested dictionary.
"""
if isinstance(dictionary, dict):
for key, value in dictionary.items():
# Dive into a nested level.
if isinstance(value, dict):
try:
if AUTOCOMPLETE[root][key]:
for item in value.keys():
AUTOCOMPLETE[root][key].append(item)
except KeyError:
AUTOCOMPLETE[root][key] = list(value.keys())
value = autocomplete_values(value, root)
else:
try:
if key not in AUTOCOMPLETE[root]:
AUTOCOMPLETE[root][key] = ''
except KeyError:
pass
else:
print('It is not dictionary')
return dictionary
if __name__ == '__main__':
AUTOCOMPLETE['opts'] = list(source.COMMANDS.get(CLI_COMMAND).keys())
AUTOCOMPLETE[CLI_COMMAND] = {}
autocomplete = autocomplete_values(source.COMMANDS, CLI_COMMAND)
with open(VARS, 'w') as yaml_file:
yaml.dump(AUTOCOMPLETE, yaml_file, default_flow_style=False)
config_data = yaml.safe_load(open(VARS))
env = Environment(loader=FileSystemLoader(TEMPLATES),
trim_blocks=True, lstrip_blocks=True)
template = env.get_template(CLI_TEMPLATE)
# print(template.render(config_data))
output = template.render(config_data)
with open(CLI_SCRIPT, 'w') as f:
f.write(output)