Skip to content

Commit

Permalink
Fixes #35: Adding support for loading configs from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
Janos Bonic committed Aug 12, 2022
1 parent 3709fe8 commit df12dad
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/arcaflow_plugin_sdk/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ def run(
"-f",
"--file",
dest="filename",
help="Configuration file to read configuration from.",
help="Configuration file to read configuration from. Pass - to read from stdin.",
metavar="FILE",
)
parser.add_option(
Expand Down Expand Up @@ -535,7 +535,7 @@ def run(
else:
step_id = list(s.steps.keys())[0]
if options.filename is not None:
return _execute_file(step_id, s, options, stdout, stderr)
return _execute_file(step_id, s, options, stdin, stdout, stderr)
elif options.json_schema is not None:
return _print_json_schema(step_id, s, options, stdout)
else:
Expand Down Expand Up @@ -567,9 +567,19 @@ def build_schema(*args: schema.StepSchema) -> schema.Schema:
)


def _execute_file(step_id, s, options, stdout, stderr) -> int:
def _execute_file(
step_id: str,
s: schema.StepSchema,
options,
stdin: io.TextIOWrapper,
stdout: io.TextIOWrapper,
stderr: io.TextIOWrapper
) -> int:
filename: str = options.filename
data = serialization.load_from_file(filename)
if filename == "-":
data = serialization.load_from_stdin(stdin)
else:
data = serialization.load_from_file(filename)
original_stdout = sys.stdout
original_stderr = sys.stderr
if options.debug:
Expand Down
27 changes: 27 additions & 0 deletions src/arcaflow_plugin_sdk/serialization.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import io
import json
from typing import Any

Expand Down Expand Up @@ -26,6 +27,32 @@ def load_from_file(file_name: str) -> Any:
raise LoadFromFileException("Unsupported file extension: {}".format(file_name))


def load_from_stdin(stdin: io.TextIOWrapper) -> Any:
"""
This function reads from the standard input and returns a Python data structure.
:param stdin: the standard input
:return: the decoded structure.
"""
stdin_data = stdin.read()
if stdin_data.startswith("{"):
try:
return json.loads(stdin_data)
except BaseException as e:
raise LoadFromStdinException("Failed to load JSON from stdin: {}".format(e.__str__())) from e
else:
try:
return yaml.safe_load(stdin_data)
except BaseException as e:
raise LoadFromStdinException("Failed to load YAML from stdin: {}".format(e.__str__())) from e


class LoadFromStdinException(Exception):
msg: str

def __str__(self) -> str:
return self.msg


class LoadFromFileException(Exception):
msg: str

Expand Down

0 comments on commit df12dad

Please sign in to comment.