Bring JS version of eos.core.objects up to date with Python
This commit is contained in:
parent
f2b1112896
commit
90865eaaca
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,5 +1,6 @@
|
|||||||
/.coverage
|
/.coverage
|
||||||
/.python-version
|
/.python-version
|
||||||
/htmlcov
|
/htmlcov
|
||||||
|
/venv
|
||||||
__javascript__
|
__javascript__
|
||||||
__pycache__
|
__pycache__
|
||||||
|
20
build_js.sh
20
build_js.sh
@ -15,13 +15,19 @@
|
|||||||
# You should have received a copy of the GNU Affero General Public License
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
transcrypt -b -n eos.js.py
|
set +e
|
||||||
|
|
||||||
# Javascript identifiers cannot contain dots
|
FLAGS=-k
|
||||||
perl -0777 -pi -e 's/eos.js/eosjs/g' eos/__javascript__/eos.js.js
|
|
||||||
|
|
||||||
# __pragma__ sometimes stops working???
|
for f in eos.js eos.js_tests; do
|
||||||
perl -0777 -pi -e "s/__pragma__ \('.*?'\)//gs" eos/__javascript__/eos.js.js
|
transcrypt -b -n $FLAGS $f.py
|
||||||
|
|
||||||
# Transcrypt by default suppresses stack traces for some reason??
|
# Javascript identifiers cannot contain dots
|
||||||
perl -0777 -pi -e "s/__except0__.__cause__ = null;//g" eos/__javascript__/eos.js.js
|
perl -0777 -pi -e 's/eos.js/eosjs/g' eos/__javascript__/$f.js
|
||||||
|
|
||||||
|
# __pragma__ sometimes stops working???
|
||||||
|
perl -0777 -pi -e "s/__pragma__ \('.*?'\)//gs" eos/__javascript__/$f.js
|
||||||
|
|
||||||
|
# Transcrypt by default suppresses stack traces for some reason??
|
||||||
|
perl -0777 -pi -e "s/__except0__.__cause__ = null;//g" eos/__javascript__/$f.js
|
||||||
|
done
|
||||||
|
@ -14,45 +14,166 @@
|
|||||||
# You should have received a copy of the GNU Affero General Public License
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
# Fields
|
||||||
|
# ======
|
||||||
|
|
||||||
class Field:
|
class Field:
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
if 'default' in kwargs:
|
self.default = kwargs.get('default', None)
|
||||||
self.required = False
|
self.hashed = kwargs.get('hashed', True)
|
||||||
self.default = kwargs['default']
|
|
||||||
else:
|
|
||||||
self.required = True
|
|
||||||
self.default = None
|
|
||||||
|
|
||||||
StringField = Field
|
class PrimitiveField(Field):
|
||||||
|
def serialise(self, value):
|
||||||
|
return value
|
||||||
|
|
||||||
|
def deserialise(self, value):
|
||||||
|
return value
|
||||||
|
|
||||||
|
DictField = PrimitiveField
|
||||||
|
IntField = PrimitiveField
|
||||||
|
ListField = PrimitiveField
|
||||||
|
StringField = PrimitiveField
|
||||||
|
UUIDField = PrimitiveField # Different to Python
|
||||||
|
|
||||||
|
class EmbeddedObjectField(Field):
|
||||||
|
def __init__(self, object_type=None, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
self.object_type = object_type
|
||||||
|
|
||||||
|
def serialise(self, value):
|
||||||
|
return EosObject.serialise_and_wrap(value, self.object_type)
|
||||||
|
|
||||||
|
def deserialise(self, value):
|
||||||
|
return EosObject.deserialise_and_unwrap(value, self.object_type)
|
||||||
|
|
||||||
|
class ListField(Field):
|
||||||
|
def __init__(self, element_field=None, *args, **kwargs):
|
||||||
|
super().__init__(default=EosList, *args, **kwargs)
|
||||||
|
self.element_field = element_field
|
||||||
|
|
||||||
|
def serialise(self, value):
|
||||||
|
return [self.element_field.serialise(x) for x in value]
|
||||||
|
|
||||||
|
def deserialise(self, value):
|
||||||
|
return [self.element_field.deserialise(x) for x in value]
|
||||||
|
|
||||||
|
class EmbeddedObjectListField(Field):
|
||||||
|
def __init__(self, object_type=None, *args, **kwargs):
|
||||||
|
super().__init__(default=EosList, *args, **kwargs)
|
||||||
|
self.object_type = object_type
|
||||||
|
|
||||||
|
def serialise(self, value):
|
||||||
|
return [EosObject.serialise_and_wrap(x, self.object_type) for x in value]
|
||||||
|
|
||||||
|
def deserialise(self, value):
|
||||||
|
return [EosObject.deserialise_and_unwrap(x, self.object_type) for x in value]
|
||||||
|
|
||||||
|
# Objects
|
||||||
|
# =======
|
||||||
|
|
||||||
class EosObjectType(type):
|
class EosObjectType(type):
|
||||||
def before_new(meta, name, bases, attrs):
|
|
||||||
# Process fields
|
|
||||||
fields = {}
|
|
||||||
for attr, val in dict(attrs).items():
|
|
||||||
if isinstance(val, Field):
|
|
||||||
fields[attr] = val
|
|
||||||
attrs[attr] = val.to_python()
|
|
||||||
attrs['_fields'] = fields
|
|
||||||
|
|
||||||
return meta, name, bases, attrs
|
|
||||||
|
|
||||||
def __new__(meta, name, bases, attrs):
|
def __new__(meta, name, bases, attrs):
|
||||||
meta, name, bases, attrs = meta.before_new(meta, name, bases, attrs)
|
cls = type.__new__(meta, name, bases, attrs)
|
||||||
#return super().__new__(meta, name, bases, attrs)
|
cls._name = cls.__module__ + '.' + cls.__qualname__
|
||||||
return type.__new__(meta, name, bases, attrs)
|
if name != 'EosObject':
|
||||||
|
EosObject.objects[cls._name] = cls
|
||||||
|
return cls
|
||||||
|
|
||||||
class EosObject():
|
class EosObject(metaclass=EosObjectType):
|
||||||
|
objects = {}
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self._instance = (None, None)
|
||||||
|
self._inited = False
|
||||||
|
|
||||||
|
def post_init(self):
|
||||||
|
self._inited = True
|
||||||
|
|
||||||
|
def recurse_parents(self, cls):
|
||||||
|
if isinstance(self, cls):
|
||||||
|
return self
|
||||||
|
if self._instance[0]:
|
||||||
|
return self._instance[0].recurse_parents(cls)
|
||||||
|
return None
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def serialise_and_wrap(value, object_type=None):
|
||||||
|
if object_type:
|
||||||
|
return value.serialise()
|
||||||
|
return {'type': value._name, 'value': value.serialise()}
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def deserialise_and_unwrap(value, object_type=None):
|
||||||
|
if object_type:
|
||||||
|
return object_type.deserialise(value)
|
||||||
|
return EosObject.objects[value['type']].deserialise(value['value'])
|
||||||
|
|
||||||
|
class EosList(EosObject, list):
|
||||||
|
def append(self, value):
|
||||||
|
if isinstance(value, EosObject):
|
||||||
|
value._instance = (self, len(self))
|
||||||
|
if not value._inited:
|
||||||
|
value.post_init()
|
||||||
|
return super().append(value)
|
||||||
|
|
||||||
|
class DocumentObjectType(EosObjectType):
|
||||||
|
def __new__(meta, name, bases, attrs):
|
||||||
|
cls = EosObjectType.__new__(meta, name, bases, attrs)
|
||||||
|
|
||||||
|
# Process fields
|
||||||
|
fields = Object.create(cls._fields) if hasattr(cls, '_fields') else {} # Different to Python: TNYI dict.copy not implemented
|
||||||
|
for attr in list(dir(cls)):
|
||||||
|
val = getattr(cls, attr)
|
||||||
|
if isinstance(val, Field):
|
||||||
|
val._instance = (cls, name)
|
||||||
|
fields[attr] = val
|
||||||
|
delattr(cls, attr)
|
||||||
|
cls._fields = fields
|
||||||
|
|
||||||
|
# Make properties
|
||||||
|
def make_property(name, field):
|
||||||
|
def field_getter(self):
|
||||||
|
return self._field_values[name]
|
||||||
|
def field_setter(self, value):
|
||||||
|
if isinstance(value, EosObject):
|
||||||
|
value._instance = (self, name)
|
||||||
|
if not value._inited:
|
||||||
|
value.post_init()
|
||||||
|
|
||||||
|
self._field_values[name] = value
|
||||||
|
return property(field_getter, field_setter)
|
||||||
|
|
||||||
|
for attr, val in fields.items():
|
||||||
|
setattr(cls, attr, make_property(attr, val))
|
||||||
|
|
||||||
|
return cls
|
||||||
|
|
||||||
|
class DocumentObject(EosObject, metaclass=DocumentObjectType):
|
||||||
|
_ver = StringField(default='0.1')
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
self._field_values = {}
|
||||||
|
|
||||||
|
for attr, val in self._fields.items():
|
||||||
|
if attr in kwargs:
|
||||||
|
setattr(self, attr, kwargs[attr])
|
||||||
|
else:
|
||||||
|
default = val.default
|
||||||
|
if callable(default):
|
||||||
|
default = default()
|
||||||
|
setattr(self, attr, default)
|
||||||
|
|
||||||
|
def serialise(self):
|
||||||
|
return {attr: val.serialise(getattr(self, attr)) for attr, val in self._fields.items()}
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def deserialise(cls, value):
|
||||||
|
return cls(**{attr: val.deserialise(value[attr]) for attr, val in cls._fields.items()})
|
||||||
|
|
||||||
|
class TopLevelObject(DocumentObject):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class DocumentObject(EosObject, metaclass=EosObjectType):
|
class EmbeddedObject(DocumentObject):
|
||||||
def __init__(self, *args, **kwargs):
|
pass
|
||||||
# Process fields
|
|
||||||
for name, field in self._fields.items():
|
|
||||||
if name not in kwargs:
|
|
||||||
continue
|
|
||||||
setattr(self, name, kwargs.pop(name, field.default))
|
|
||||||
|
|
||||||
# MongoDB distinguishes between these two, but we don't care
|
|
||||||
TopLevelObject = DocumentObject
|
|
||||||
EmbeddedObject = DocumentObject
|
|
||||||
|
@ -16,3 +16,4 @@
|
|||||||
|
|
||||||
import eos.core.objects
|
import eos.core.objects
|
||||||
import eos.core.bigint
|
import eos.core.bigint
|
||||||
|
import eos.core.bitstring
|
||||||
|
19
eos/js_tests.py
Normal file
19
eos/js_tests.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# Eos - Verifiable elections
|
||||||
|
# Copyright © 2017 RunasSudo (Yingtong Li)
|
||||||
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Affero General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import eos.js
|
||||||
|
|
||||||
|
import eos.core.test_code
|
2
js.html
2
js.html
@ -1 +1 @@
|
|||||||
<script src="eos/__javascript__/eos.js.js"></script>
|
<script src="eos/__javascript__/eos.js_tests.js"></script>
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
coverage==4.4.1
|
||||||
mypy==0.521
|
mypy==0.521
|
||||||
pymongo==3.5.1
|
pymongo==3.5.1
|
||||||
Transcrypt==3.6.50
|
Transcrypt==3.6.50
|
||||||
|
Loading…
Reference in New Issue
Block a user