From b1f9eb45d215868e904e84eb892e5cc953d2cc8a Mon Sep 17 00:00:00 2001 From: Andrew Elgert Date: Tue, 14 Mar 2023 11:45:30 -0400 Subject: [PATCH] Compiler can load from file-like objects --- implementations/python/.envrc | 1 + implementations/python/preserves/schema.py | 15 +++++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/implementations/python/.envrc b/implementations/python/.envrc index a500642..9ae9c9c 100644 --- a/implementations/python/.envrc +++ b/implementations/python/.envrc @@ -3,6 +3,7 @@ then python -m venv .venv . .venv/bin/activate pip install -U coverage setuptools setuptools_scm wheel + pip install -e . else . .venv/bin/activate fi diff --git a/implementations/python/preserves/schema.py b/implementations/python/preserves/schema.py index 3424f37..f887b15 100644 --- a/implementations/python/preserves/schema.py +++ b/implementations/python/preserves/schema.py @@ -456,15 +456,18 @@ class Compiler: def __init__(self): self.root = Namespace(()) + def load_filelike(self, module_path, f): + x = Decoder(f.read()).next() + if x.key == SCHEMA: + self.load_schema((Symbol(module_path),), x) + elif x.key == BUNDLE: + for (p, s) in x[0].items(): + self.load_schema(p, s) + def load(self, filename): filename = pathlib.Path(filename) with open(filename, 'rb') as f: - x = Decoder(f.read()).next() - if x.key == SCHEMA: - self.load_schema((Symbol(filename.stem),), x) - elif x.key == BUNDLE: - for (p, s) in x[0].items(): - self.load_schema(p, s) + self.load_filelike(filename.stem, f) def load_schema(self, module_path, schema): if schema[0][VERSION] != 1: