From: Kyle Bowman Date: Mon, 15 Apr 2024 02:13:43 +0000 (-0400) Subject: Add Stdio. X-Git-Url: https://git.rocketbowman.com/?a=commitdiff_plain;h=63e873aba36537711460ff72d3ffdbda44cb94a4;p=proto.git Add Stdio. --- 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