def to_larva(self, pipeline=True) -> None: """Display the log. Use the pipeline if `pipeline`, else print out the log.""" if pipeline: Utility.file_write(Utility.pipe_path("larva"), self.build(), "a") else: print(self.build())
def handle_kb_input(self, inp: str) -> None: inp = inp.strip().split(" ") if inp[0] in self.hardcodes.d: self.hardcodes.d[inp[0]](inp) elif inp[0] in self.scripts: Utility.file_write(Utility.pipe_path(inp[0]), ' '.join(inp[1:]), "a") else: print(f"Unknown command: {inp[0]}, try running 'help'")
def cfg(self) -> dict: """Return the cfg in the form of a dictionary. Meant to be called after valid_cfg().""" cfg = dict() contents = Utility.file_read(self.cfg_path).strip().split("\n") for line in contents: key, value = line.split(":") cfg[key] = value return cfg
def recfg(self, inp: list) -> None: if len(inp) == 1: print("recfg {script name}") else: if inp[1] in self.larva.scripts: print(f"Configuring new cfg file for {inp[1]}") new_cfg = [] for key in Script.REQUIRED_CFG_SETTINGS: while True: v = input( f"Select value for {key} {Script.REQUIRED_CFG_SETTINGS[key]}: " ) if v in Script.REQUIRED_CFG_SETTINGS[key]: new_cfg.append(f"{key}:{v}") break print( f"Value must be in {Script.REQUIRED_CFG_SETTINGS[key]}" ) Utility.file_write(self.larva.scripts[inp[1]].cfg_path, "\n".join(new_cfg)) else: print(f"{inp[1]} not found")
def valid_cfg(self) -> bool: """Check if the contents of the script's cfg file are valid.""" if not os.path.isfile(self.cfg_path): return False contents = Utility.file_read(self.cfg_path).strip().split("\n") keys_required = set(Script.REQUIRED_CFG_SETTINGS.keys()) for line in contents: line = line.split(":") if len(line) != 2: return False key, value = line if key in keys_required and value in Script.REQUIRED_CFG_SETTINGS[ key]: keys_required.remove(key) else: return False return not len(keys_required)
def larva_alive(self) -> None: """Check if Larva is still alive. If not, kill the process.""" if not Utility.pid_alive(self.larva_pid): exit(1)
def read_from_larva(self) -> list: """Check if there are new inputs from Larva. Returns a list where each element is a command (line) from Larva.""" return Utility.file_flush(Utility.pipe_path(self.name))
def handle_scripts_input(self) -> None: contents = Utility.file_flush(Utility.pipe_path("larva")) if contents: print("\n".join(contents))
import msvcrt import LarvaLibs.Utility as Utility from LarvaLibs.Larva import Larva manager = Larva() while Utility.tick(0.1): if msvcrt.kbhit(): manager.handle_kb_input(input(">")) manager.handle_scripts_input()
def alive(self) -> bool: if self.p is None: return False return Utility.pid_alive(self.p.pid)