From 5596b2ed6b7109db1ddfd474d77e24cac647fd66 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Sat, 21 Mar 2020 12:44:11 +0100 Subject: Split instruction between builtin and command Signed-off-by: Olivier Gayot --- vish.py | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) (limited to 'vish.py') diff --git a/vish.py b/vish.py index 8d8f6e1..43c4090 100755 --- a/vish.py +++ b/vish.py @@ -30,12 +30,20 @@ class Pipeline(): class Instruction(): - def __init__(self, tokens: list=[], stdout=None, stderr=None): - self.prog = tokens[0] - self.args = tokens[1:] + def __init__(self, stdout=None, stderr=None): self.stdout = stdout self.stderr = stderr + def execute(self): + pass + + +class Command(Instruction): + def __init__(self, prog: str, args: list=[], **kwargs): + super().__init__(**kwargs) + + self.prog = prog + self.args = args def execute(self): cpid = os.fork() @@ -53,6 +61,33 @@ class Instruction(): return status +class Builtin(Instruction): + def __init__(self, function, args: list=[], **kwargs): + super().__init__(**kwargs) + + self.function = function + self.args = args + + def execute(self): + # TODO Rewrite using with statements + if self.stdout is not None: + stdout = open(self.stdout, mode="w", encoding="utf-8") + else: + stdout = sys.stdout + + if self.stderr is not None: + stderr = open(self.stderr, mode="w", encoding="utf-8") + else: + stderr = sys.stderr + + try: + self.function(self.args, stdout=stdout) + except Exception as e: + print(e.__repr__(), file=stderr) + return 1 + return 0 + + class AndInstruction(Instruction): def __init__(self, instruction1: Instruction, instruction2: Instruction): self.instruction1 = instruction1 -- cgit v1.2.3