]> git.rocketbowman.com Git - proto.git/commitdiff
Add Stdio.
authorKyle Bowman <kylebowman14@gmail.com>
Mon, 15 Apr 2024 02:13:43 +0000 (22:13 -0400)
committerKyle Bowman <kylebowman14@gmail.com>
Mon, 15 Apr 2024 02:13:43 +0000 (22:13 -0400)
src/proto/infer.py
src/proto/io.py [new file with mode: 0644]

index 98ff1bec1f35ce140287e4ddd07dfab2555ac45c..31a8596a1dd109b9c269be71636cbb9f863c3fd9 100644 (file)
@@ -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 (file)
index 0000000..9132043
--- /dev/null
@@ -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