Skip to content

Commit

Permalink
Introduce Command dataclass
Browse files Browse the repository at this point in the history
  • Loading branch information
sevein committed Jul 11, 2024
1 parent 97aee4d commit 8087c0b
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions internal/runner/main.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
import json
import multiprocessing
import sys
from dataclasses import dataclass, field
from typing import Any, Dict

from bagit import Bag, make_bag
from bagit import BagError


class Runner:
def __init__(self, req, stdout):
self.req = req
self.stdout = stdout
@dataclass
class Command:
name: str
args: Dict[str, Any] = field(default_factory=dict)

@property
def name(self):
return self.req.get("name")

@property
def args(self):
return self.req.get("args")
class Runner:
def __init__(self, cmd, stdout):
self.cmd = cmd
self.stdout = stdout

def run(self):
resp = {}
name = self.cmd.name
args = self.cmd.args

resp = {}
try:
if self.name == "validate":
resp = self.validate(self.args)
elif self.name == "make":
resp = self.make(self.args)
if name == "validate":
resp = self.validate(args)
elif name == "make":
resp = self.make(args)
else:
raise Exception("Unknown command")
except BaseException as err:
Expand Down Expand Up @@ -56,15 +57,22 @@ def write_error(stdout, err):

def main():
while True:
cmd = sys.stdin.readline()
if not cmd:
line = sys.stdin.readline()
if not line:
break

req = json.loads(cmd)
if req.get("name") == "exit":
try:
payload = json.loads(line)
except ValueError as err:
Runner.write_error(sys.stdout, err)
continue

cmd = Command(name=payload.get("name"), args=payload.get("args"))

if cmd.name == "exit":
break

runner = Runner(req, sys.stdout)
runner = Runner(cmd, sys.stdout)
runner.run()


Expand Down

0 comments on commit 8087c0b

Please sign in to comment.