load_schema_file, load metaschema

This commit is contained in:
Tony Garnock-Jones 2021-08-16 11:18:54 -04:00
parent e056394ca6
commit a2ca133983
1 changed files with 18 additions and 14 deletions

View File

@ -376,32 +376,36 @@ class Compiler:
c._set_schema(self.root, module_path, n, d, None, None)
ns[n] = c
def load_schema_file(filename):
c = Compiler()
c.load(filename)
return c.root
# a decorator
def extend(cls):
return lambda f: setattr(cls, f.__name__, f)
if __name__ == '__main__':
c = Compiler()
schema_bin_filename = pathlib.Path(__file__).parent / '../../../schema/schema.bin'
c.load(schema_bin_filename)
with open(schema_bin_filename, 'rb') as f:
x = Decoder(f.read()).next()
print(c.root.schema.Schema.decode(x))
__metaschema_filename = pathlib.Path(__file__).parent / '../../../schema/schema.bin'
meta = load_schema_file(__metaschema_filename).schema
@extend(c.root.schema.Schema)
if __name__ == '__main__':
with open(__metaschema_filename, 'rb') as f:
x = Decoder(f.read()).next()
print(meta.Schema.decode(x))
@extend(meta.Schema)
def f(self, x):
return ['yay', self.embeddedType, x]
print(c.root.schema.Schema.decode(x).f(123))
print(meta.Schema.decode(x).f(123))
print(f)
print()
d = Compiler()
path_bin_filename = pathlib.Path(__file__).parent / '../../../path/path.bin'
d.load(path_bin_filename)
path = load_schema_file(path_bin_filename).path
with open(path_bin_filename, 'rb') as f:
x = Decoder(f.read()).next()
print(c.root.schema.Schema.decode(x))
print(c.root.schema.Schema.decode(x) == c.root.schema.Schema.decode(x))
print(meta.Schema.decode(x))
print(meta.Schema.decode(x) == meta.Schema.decode(x))
print()
print(d.root)
print(path)