Small improvements
This commit is contained in:
parent
abe60b3506
commit
e45ff6b020
|
@ -1,5 +1,6 @@
|
||||||
from .preserves import *
|
from .preserves import *
|
||||||
import pathlib
|
import pathlib
|
||||||
|
import keyword
|
||||||
|
|
||||||
AND = Symbol('and')
|
AND = Symbol('and')
|
||||||
ANY = Symbol('any')
|
ANY = Symbol('any')
|
||||||
|
@ -33,6 +34,7 @@ class SchemaEntity:
|
||||||
SCHEMA = None
|
SCHEMA = None
|
||||||
MODULE_PATH = None
|
MODULE_PATH = None
|
||||||
NAME = None
|
NAME = None
|
||||||
|
VARIANT = None
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def decode(cls, v):
|
def decode(cls, v):
|
||||||
|
@ -132,9 +134,7 @@ class SchemaEntity:
|
||||||
raise Exception('Subclass responsibility')
|
raise Exception('Subclass responsibility')
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
n = self.NAME.name
|
n = self._constructor_name()
|
||||||
if self.VARIANT:
|
|
||||||
n = n + '.' + self.VARIANT.name
|
|
||||||
if self.SIMPLE:
|
if self.SIMPLE:
|
||||||
return n + '(' + repr(self.value) + ')'
|
return n + '(' + repr(self.value) + ')'
|
||||||
else:
|
else:
|
||||||
|
@ -161,6 +161,7 @@ class Enumeration(SchemaEntity):
|
||||||
c = pretty_subclass(Definition, module_path_str(module_path + (name,)), n.name)
|
c = pretty_subclass(Definition, module_path_str(module_path + (name,)), n.name)
|
||||||
c._set_schema(rootns, module_path, name, d, n, cls)
|
c._set_schema(rootns, module_path, name, d, n, cls)
|
||||||
cls.VARIANTS.append((n, c))
|
cls.VARIANTS.append((n, c))
|
||||||
|
safesetattr(cls, n.name, c)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def try_decode(cls, v):
|
def try_decode(cls, v):
|
||||||
|
@ -172,22 +173,46 @@ class Enumeration(SchemaEntity):
|
||||||
def _encode(self):
|
def _encode(self):
|
||||||
raise Exception('Cannot encode instance of Enumeration')
|
raise Exception('Cannot encode instance of Enumeration')
|
||||||
|
|
||||||
|
def safesetattr(o, k, v):
|
||||||
|
if keyword.iskeyword(k):
|
||||||
|
setattr(o, k + '_', v)
|
||||||
|
else:
|
||||||
|
setattr(o, k, v)
|
||||||
|
|
||||||
class Definition(SchemaEntity):
|
class Definition(SchemaEntity):
|
||||||
SIMPLE = False
|
SIMPLE = False
|
||||||
FIELD_NAMES = []
|
FIELD_NAMES = []
|
||||||
VARIANT = None
|
|
||||||
ENUMERATION = None
|
ENUMERATION = None
|
||||||
|
|
||||||
|
def _constructor_name(self):
|
||||||
|
if self.VARIANT is None:
|
||||||
|
return self.NAME.name
|
||||||
|
else:
|
||||||
|
return self.NAME.name + '.' + self.VARIANT.name
|
||||||
|
|
||||||
def __init__(self, *args):
|
def __init__(self, *args):
|
||||||
self._fields = args
|
self._fields = args
|
||||||
if self.SIMPLE:
|
if self.SIMPLE:
|
||||||
|
if len(args) != 1:
|
||||||
|
raise Exception('%s needs exactly one argument' % (self._constructor_name(),))
|
||||||
self.value = args[0]
|
self.value = args[0]
|
||||||
else:
|
else:
|
||||||
|
if len(args) != len(self.FIELD_NAMES):
|
||||||
|
raise Exception('%s needs argument(s) %r' % (self._constructor_name(), self.FIELD_NAMES))
|
||||||
i = 0
|
i = 0
|
||||||
for k in self.FIELD_NAMES:
|
for k in self.FIELD_NAMES:
|
||||||
setattr(self, k, args[i])
|
safesetattr(self, k, args[i])
|
||||||
i = i + 1
|
i = i + 1
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
return (other.__class__ is self.__class__) and (self._fields == other._fields)
|
||||||
|
|
||||||
|
def __ne__(self, other):
|
||||||
|
return not self.__eq__(other)
|
||||||
|
|
||||||
|
def __hash__(self):
|
||||||
|
return hash(self._fields) ^ hash(self.__class_)
|
||||||
|
|
||||||
def _accept(self, visitor):
|
def _accept(self, visitor):
|
||||||
if self.VARIANT is None:
|
if self.VARIANT is None:
|
||||||
return visitor(*self._fields)
|
return visitor(*self._fields)
|
||||||
|
@ -226,7 +251,7 @@ class Definition(SchemaEntity):
|
||||||
return getattr(self, name)
|
return getattr(self, name)
|
||||||
|
|
||||||
def __setitem__(self, name, value):
|
def __setitem__(self, name, value):
|
||||||
return setattr(self, name, value)
|
return safesetattr(self, name, value)
|
||||||
|
|
||||||
def module_path_str(mp):
|
def module_path_str(mp):
|
||||||
return '.'.join([e.name for e in mp])
|
return '.'.join([e.name for e in mp])
|
||||||
|
@ -343,6 +368,11 @@ if __name__ == '__main__':
|
||||||
x = Decoder(f.read()).next()
|
x = Decoder(f.read()).next()
|
||||||
print(c.root.schema.Schema.decode(x))
|
print(c.root.schema.Schema.decode(x))
|
||||||
|
|
||||||
|
def m(self, x):
|
||||||
|
return ['yay', self.embeddedType, x]
|
||||||
|
c.root.schema.Schema.f = m
|
||||||
|
print(c.root.schema.Schema.decode(x).f(123))
|
||||||
|
|
||||||
print()
|
print()
|
||||||
|
|
||||||
d = Compiler()
|
d = Compiler()
|
||||||
|
@ -351,5 +381,6 @@ if __name__ == '__main__':
|
||||||
with open(path_bin_filename, 'rb') as f:
|
with open(path_bin_filename, 'rb') as f:
|
||||||
x = Decoder(f.read()).next()
|
x = Decoder(f.read()).next()
|
||||||
print(c.root.schema.Schema.decode(x))
|
print(c.root.schema.Schema.decode(x))
|
||||||
|
print(c.root.schema.Schema.decode(x) == c.root.schema.Schema.decode(x))
|
||||||
print()
|
print()
|
||||||
print(d.root)
|
print(d.root)
|
||||||
|
|
Loading…
Reference in New Issue