2017-09-20 22:30:38 +10:00
|
|
|
# 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/>.
|
|
|
|
|
2017-09-22 15:59:15 +10:00
|
|
|
import pymongo
|
|
|
|
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
# Database
|
|
|
|
# ========
|
|
|
|
|
|
|
|
client = pymongo.MongoClient()
|
|
|
|
db = client['test']
|
|
|
|
|
|
|
|
# Fields
|
|
|
|
# ======
|
2017-09-20 22:30:38 +10:00
|
|
|
|
|
|
|
class Field:
|
|
|
|
def __init__(self, *args, **kwargs):
|
2017-09-22 15:59:15 +10:00
|
|
|
self.default = kwargs.get('default', None)
|
|
|
|
self.hashed = kwargs.get('hashed', True)
|
2017-09-20 22:30:38 +10:00
|
|
|
|
|
|
|
class PrimitiveField(Field):
|
2017-09-22 15:59:15 +10:00
|
|
|
def serialise(self, value):
|
|
|
|
return value
|
|
|
|
|
|
|
|
def deserialise(self, value):
|
|
|
|
return value
|
|
|
|
|
|
|
|
DictField = PrimitiveField
|
|
|
|
IntField = PrimitiveField
|
|
|
|
ListField = PrimitiveField
|
|
|
|
StringField = PrimitiveField
|
|
|
|
|
|
|
|
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 value.serialise_and_wrap(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=[], *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]
|
|
|
|
|
|
|
|
EmbeddedObjectListField = ListField
|
|
|
|
|
|
|
|
class UUIDField(Field):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(default=uuid.uuid4, *args, **kwargs)
|
|
|
|
|
|
|
|
def serialise(self, value):
|
|
|
|
return str(uuid.uuid4)
|
|
|
|
|
|
|
|
def unserialise(self, value):
|
|
|
|
return uuid.uuid4(value)
|
2017-09-20 22:30:38 +10:00
|
|
|
|
2017-09-22 15:59:15 +10:00
|
|
|
# Objects
|
|
|
|
# =======
|
2017-09-20 22:30:38 +10:00
|
|
|
|
|
|
|
class EosObjectType(type):
|
2017-09-22 15:59:15 +10:00
|
|
|
def __new__(meta, name, bases, attrs):
|
|
|
|
#meta, name, bases, attrs = meta.before_new(meta, name, bases, attrs)
|
|
|
|
cls = type.__new__(meta, name, bases, attrs)
|
|
|
|
|
2017-09-20 22:30:38 +10:00
|
|
|
# Process fields
|
2017-09-22 15:59:15 +10:00
|
|
|
fields = cls._fields if hasattr(cls, '_fields') else {}
|
|
|
|
for attr in list(dir(cls)):
|
|
|
|
val = getattr(cls, attr)
|
2017-09-20 22:30:38 +10:00
|
|
|
if isinstance(val, Field):
|
|
|
|
fields[attr] = val
|
2017-09-22 15:59:15 +10:00
|
|
|
delattr(cls, attr)
|
|
|
|
cls._fields = fields
|
2017-09-20 22:30:38 +10:00
|
|
|
|
2017-09-22 15:59:15 +10:00
|
|
|
cls._name = cls.__module__ + '.' + cls.__qualname__
|
|
|
|
|
|
|
|
return cls
|
2017-09-20 22:30:38 +10:00
|
|
|
|
2017-09-22 15:59:15 +10:00
|
|
|
class EosObject(metaclass=EosObjectType):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
for attr, val in self._fields.items():
|
|
|
|
setattr(self, attr, kwargs.get(attr, val.default))
|
2017-09-20 22:30:38 +10:00
|
|
|
|
2017-09-22 15:59:15 +10:00
|
|
|
TopLevelObject = EosObject
|
|
|
|
EmbeddedObject = EosObject
|