2018-07-10 20:54:17 +02:00
|
|
|
from collections import OrderedDict
|
|
|
|
|
2020-03-22 03:22:36 +01:00
|
|
|
from .base import _cls_init
|
2021-01-09 04:08:33 +01:00
|
|
|
from .registration import (
|
2020-03-22 03:22:36 +01:00
|
|
|
_register_marking, _register_object, _register_observable,
|
|
|
|
_register_observable_extension,
|
2018-07-13 17:10:05 +02:00
|
|
|
)
|
2018-07-10 20:54:17 +02:00
|
|
|
|
|
|
|
|
2020-04-02 20:15:45 +02:00
|
|
|
def _get_properties_dict(properties):
|
|
|
|
try:
|
|
|
|
return OrderedDict(properties)
|
|
|
|
except TypeError as e:
|
2021-02-18 18:26:54 +01:00
|
|
|
raise ValueError(
|
|
|
|
"properties must be dict-like, e.g. a list "
|
|
|
|
"containing tuples. For example, "
|
|
|
|
"[('property1', IntegerProperty())]",
|
|
|
|
) from e
|
2018-07-10 20:54:17 +02:00
|
|
|
|
|
|
|
|
2020-04-02 20:15:45 +02:00
|
|
|
def _custom_object_builder(cls, type, properties, version, base_class):
|
|
|
|
prop_dict = _get_properties_dict(properties)
|
|
|
|
|
|
|
|
class _CustomObject(cls, base_class):
|
2020-03-19 15:40:35 +01:00
|
|
|
|
2018-07-10 20:54:17 +02:00
|
|
|
_type = type
|
2020-04-02 20:15:45 +02:00
|
|
|
_properties = prop_dict
|
2018-07-10 20:54:17 +02:00
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
2020-03-22 03:22:36 +01:00
|
|
|
base_class.__init__(self, **kwargs)
|
2018-10-17 13:34:15 +02:00
|
|
|
_cls_init(cls, self, kwargs)
|
2018-07-10 20:54:17 +02:00
|
|
|
|
2020-07-27 03:08:19 +02:00
|
|
|
_CustomObject.__name__ = cls.__name__
|
|
|
|
|
2018-07-10 20:54:17 +02:00
|
|
|
_register_object(_CustomObject, version=version)
|
|
|
|
return _CustomObject
|
|
|
|
|
|
|
|
|
2020-03-22 03:22:36 +01:00
|
|
|
def _custom_marking_builder(cls, type, properties, version, base_class):
|
2020-04-02 20:15:45 +02:00
|
|
|
prop_dict = _get_properties_dict(properties)
|
2020-03-19 19:16:48 +01:00
|
|
|
|
2020-03-22 03:22:36 +01:00
|
|
|
class _CustomMarking(cls, base_class):
|
2018-07-10 20:54:17 +02:00
|
|
|
|
|
|
|
_type = type
|
2020-04-02 20:15:45 +02:00
|
|
|
_properties = prop_dict
|
2018-07-10 20:54:17 +02:00
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
2020-03-22 03:22:36 +01:00
|
|
|
base_class.__init__(self, **kwargs)
|
2018-10-17 13:34:15 +02:00
|
|
|
_cls_init(cls, self, kwargs)
|
2018-07-10 20:54:17 +02:00
|
|
|
|
2020-07-27 03:08:19 +02:00
|
|
|
_CustomMarking.__name__ = cls.__name__
|
|
|
|
|
2018-07-10 20:54:17 +02:00
|
|
|
_register_marking(_CustomMarking, version=version)
|
|
|
|
return _CustomMarking
|
|
|
|
|
|
|
|
|
2020-03-22 03:22:36 +01:00
|
|
|
def _custom_observable_builder(cls, type, properties, version, base_class, id_contrib_props=None):
|
2020-03-04 20:46:55 +01:00
|
|
|
if id_contrib_props is None:
|
|
|
|
id_contrib_props = []
|
|
|
|
|
2020-04-02 20:15:45 +02:00
|
|
|
prop_dict = _get_properties_dict(properties)
|
2018-07-10 20:54:17 +02:00
|
|
|
|
2020-04-02 20:15:45 +02:00
|
|
|
class _CustomObservable(cls, base_class):
|
2018-07-10 20:54:17 +02:00
|
|
|
|
|
|
|
_type = type
|
2020-04-02 20:15:45 +02:00
|
|
|
_properties = prop_dict
|
2020-02-25 03:11:42 +01:00
|
|
|
if version != '2.0':
|
|
|
|
_id_contributing_properties = id_contrib_props
|
2018-07-10 20:54:17 +02:00
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
2020-03-22 03:22:36 +01:00
|
|
|
base_class.__init__(self, **kwargs)
|
2018-10-17 13:34:15 +02:00
|
|
|
_cls_init(cls, self, kwargs)
|
2018-07-10 20:54:17 +02:00
|
|
|
|
2020-07-27 03:08:19 +02:00
|
|
|
_CustomObservable.__name__ = cls.__name__
|
|
|
|
|
2018-07-10 20:54:17 +02:00
|
|
|
_register_observable(_CustomObservable, version=version)
|
|
|
|
return _CustomObservable
|
|
|
|
|
|
|
|
|
2020-03-22 03:22:36 +01:00
|
|
|
def _custom_extension_builder(cls, observable, type, properties, version, base_class):
|
2020-04-02 20:15:45 +02:00
|
|
|
prop_dict = _get_properties_dict(properties)
|
2018-07-10 20:54:17 +02:00
|
|
|
|
2020-03-22 03:22:36 +01:00
|
|
|
class _CustomExtension(cls, base_class):
|
2018-07-10 20:54:17 +02:00
|
|
|
|
|
|
|
_type = type
|
2020-03-11 01:24:53 +01:00
|
|
|
_properties = prop_dict
|
2018-07-10 20:54:17 +02:00
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
2020-03-22 03:22:36 +01:00
|
|
|
base_class.__init__(self, **kwargs)
|
2018-10-17 13:34:15 +02:00
|
|
|
_cls_init(cls, self, kwargs)
|
2018-07-10 20:54:17 +02:00
|
|
|
|
2020-07-27 03:08:19 +02:00
|
|
|
_CustomExtension.__name__ = cls.__name__
|
|
|
|
|
2018-07-10 20:54:17 +02:00
|
|
|
_register_observable_extension(observable, _CustomExtension, version=version)
|
|
|
|
return _CustomExtension
|