fix dependency check logic cause config always disabled
[lunaix-os.git] / lunaix-os / scripts / build-tools / shared / shconfig / main.py
1 import readline, textwrap
2
3 from shlex          import split as shsplit
4 from rlcompleter    import Completer
5 from lcfg2.config   import ConfigEnvironment
6 from .common        import ShconfigException, get_config_name
7 from .commands      import Commands
8
9 class ConfigNameCompleter(Completer):
10     def __init__(self, env: ConfigEnvironment):
11         super().__init__(None)
12
13         self.__options = []
14         self.__config_set= [
15             get_config_name(x._name)
16             for x in env.terms()
17         ]
18
19     def complete(self, text, state):
20         if state == 0:
21             text = text if text else ""
22             self.__options = [
23                 x for x in self.__config_set if x.startswith(text)]
24         
25         return None if not self.__options else self.__options[state]
26
27
28 def next_input(cmds: Commands):
29     line = input("shconfig> ")
30
31     if len(line) == 0:
32         return True
33     
34     parts = shsplit(line)
35     name, args = parts[0], parts[1:]
36
37     if name in ['q', 'exit']:
38         return False
39     
40     if name == "q!":
41         raise KeyboardInterrupt()
42     
43     if name.startswith("CONFIG_"):
44         cmds.call("opt", name)
45         return True
46
47     cmds.call(name, *args)
48     return True
49
50 def shconfig(env: ConfigEnvironment):
51     print(
52         "\n",
53         textwrap.dedent(
54             
55             """
56             Lunaix Interactive Configurator (shconfig)
57             
58             Type "help" to see all commands avaliables
59             Type "q" or "exit" to confirm and exit
60             Type "q!" or use ^C to discard and abort
61
62             """
63         ).strip(), "\n")
64
65     cmds = Commands(env)
66     cmpleter = ConfigNameCompleter(env)
67     readline.parse_and_bind('tab: complete')
68     readline.set_completer(cmpleter.complete)
69
70     while True:
71         try:
72             if not next_input(cmds):
73                 return True
74         except ShconfigException as e:
75             print(str(e))
76             continue
77         except KeyboardInterrupt as e:
78             return False
79         except Exception as e:
80             raise e