From 63e873aba36537711460ff72d3ffdbda44cb94a4 Mon Sep 17 00:00:00 2001 From: Kyle Bowman Date: Sun, 14 Apr 2024 22:13:43 -0400 Subject: [PATCH] Add Stdio. --- src/proto/infer.py | 2 ++ src/proto/io.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/proto/io.py diff --git a/src/proto/infer.py b/src/proto/infer.py index 98ff1be..31a8596 100644 --- a/src/proto/infer.py +++ b/src/proto/infer.py @@ -63,6 +63,8 @@ class _ArgSpec(dict): if isinstance(prm.annotation, type): # Basic types return get_argspecs(prm.annotation()) elif hasattr(prm.annotation, '__args__'): # Unions + # BUG: Unions, but also lists and other containers! + # Really, we want lists to be treated separately. # ASSUME: Order of types in signatures indicate order of preference. for type_ in prm.annotation.__args__: try: diff --git a/src/proto/io.py b/src/proto/io.py new file mode 100644 index 0000000..9132043 --- /dev/null +++ b/src/proto/io.py @@ -0,0 +1,35 @@ +from abc import abstractmethod +from pathlib import Path +import sys +from typing import Optional, Protocol + + +class IO(Protocol): + + @abstractmethod + def read(): + pass + + @abstractmethod + def write(): + pass + + +class Stdio(IO): + + @staticmethod + def read(file: Optional[Path])->str: + if file is None: + contents = sys.stdin.read() + else: + with open(file, "r") as f: + contents = f.read() + return contents + + @staticmethod + def write(contents, outfile: Optional[Path]=None): + if outfile is not None: + with open(outfile, "w") as out: + out.write(contents) + else: + sys.stdout.write(contents) \ No newline at end of file -- 2.39.5