if obj is None: obj = factory(module) obj.extend(edt_map.get('value')) return obj def as_dict(self): return self.edt_encode(self) def __repr__(self): gcfg = [] for element in self: gcfg.append(repr(element)) return (repr(gcfg)) edtlib.register_class(GroupCfg) #register_marshaller(GroupCfg, AsDictMarshaller()) class GroupSetpointItem(EdtDataType): edt__cls = 'mpx.service.gsp.datatypes.GroupSetpointItem' __slots__ = [ 'setpoint_id', 'name', 'data_type', 'point_type', 'value', 'priority' ] def __init__(self, *args, **kw): super(GroupSetpointItem, self).__init__(*args, **kw) if self.priority is None: self.priority = 16 #defaults to lowest priority
del edt_map['edt__typ'] if obj is None: obj = factory(module) obj.extend(edt_map.get('value')) return obj def as_dict(self): return self.edt_encode(self) def __repr__(self): gcfg = [] for element in self: gcfg.append(repr(element)) return(repr(gcfg)) edtlib.register_class(GroupCfg) #register_marshaller(GroupCfg, AsDictMarshaller()) class GroupSetpointItem(EdtDataType): edt__cls = 'mpx.service.gsp.datatypes.GroupSetpointItem' __slots__ = ['setpoint_id', 'name', 'data_type', 'point_type', 'value', 'priority'] def __init__(self, *args, **kw): super(GroupSetpointItem, self).__init__(*args, **kw) if self.priority is None: self.priority = 16 #defaults to lowest priority edtlib.register_class(GroupSetpointItem) register_marshaller(GroupSetpointItem, AsDictMarshaller()) class EntityMapping(EdtDataType): edt__cls = 'mpx.service.gsp.datatypes.EntityMapping'
return self.edt_encode(self) def __repr__(self): return repr(self.as_dict()) from_dict = edt_decode def _get_reference(self): if not is_node(self._reference): try: self._reference = as_internal_node(self._reference) except: self._reference = None return self._reference reference = property(_get_reference) edtlib.register_class(PropertyDefinition) class EntityProp(PropertyDefinition): edt__cls = 'mpx.lib.entity.properties.EntityProp' ptype = 'Entity' def __init__(self, **kw): PropertyDefinition.__init__(self, **kw) self._attrs += ['url', 'entity'] self.url = kw.get('url', '') self.entity = kw.get('entity', '') edtlib.register_class(EntityProp) class PropWithUnits(PropertyDefinition): edt__cls = 'mpx.lib.entity.properties.PropWithUnits' def __init__(self, **kw): PropertyDefinition.__init__(self, **kw)
def test_ElementalObjectType(self): # Test "standalone" functionality: assert isinstance(ElementalObjectType().edt_get(), ElementalObjectType) assert ElementalObjectType().edt__typ is 'object' assert not hasattr(ElementalObjectType(), 'edt__cls') # Test marshaling an old-style class. class A: cls_var = 'class variable' _no_cls = 'hide me please!' def __init__(self, **kw): self.__dict__.update(kw) return an_obj = A(a=1, b='b', c=None, _d="don't do it!", f=lambda: "this either!") e = default_encoding(an_obj) encoded_attrs = ('edt__typ', 'cls_var', 'a', 'b', 'c') assert not filter(lambda k: k not in encoded_attrs, e.keys()) ignored_attrs = ('_d', 'f', 'no_cls_') assert not filter(lambda k: k in ignored_attrs, e.keys()) # Test unmarshaling it inplace via constructor. a = ElementalObjectType(e) assert not hasattr(a, 'edt__cls') assert a.edt__typ == 'object' assert a.cls_var == 'class variable' assert a.a == 1 assert a.b == 'b' assert a.c is None assert not e # Map is consumed in decoding. # Test unmarshaling it in a new object via the decode factory. e = default_encoding(an_obj) edt_decode(ObjDict(e)) a = ElementalObjectType.edt_decode(e) assert not hasattr(a, 'edt__cls') assert a.edt__typ == 'object' assert a.cls_var == 'class variable' assert a.a == 1 assert a.b == 'b' assert a.c is None # Test reinstanciating a 'named' class (but not registered). a.edt__cls = class_name(A) e = default_encoding(a) a = ElementalObjectType.edt_decode(e) assert a.edt__cls == class_name(A) # Class type conveyed. assert isinstance(a, ElementalObjectType) # But not used. # Test reinstanciating the actual class with edt_decode(). class B(A): @classmethod def edt_decode(cls, edt_map, obj=None): assert obj is None # Supporting obj == None is not important. # How the class uses edt_map is 'a local matter'. Could # be passed in toto to __init__, could use load_from_map(), # another factory. Whatever. edt_map.pop('edt__typ') # Don't need this. edt_map.pop('edt__cls') # Or this. return cls(**edt_map) # Can't reference B in B, but cls == B. register_class(B) b = B(a='b', b=2, c=math.pi) e = default_encoding(b) b = ElementalObjectType.edt_decode(e) assert isinstance(b, B) # assert b.edt__typ == 'object' assert b.edt__cls == class_name(b.__class__) assert b.cls_var == 'class variable' assert b.a == 'b' assert b.b == 2 assert b.c == math.pi # Test ability to override cls_name (dangerous, but may be # required for reverse compatibility after refactors that move # data classes.) class C(B): edt__typ = 'object' # Not required by edtlib, but OK. register_class(C, cls_name='_test_ElementalObjectType.C') c = C(a='c', b=3, c=math.e) e = default_encoding(c) c = ElementalObjectType.edt_decode(e) assert isinstance(c, C) assert c.edt__typ == 'object' assert c.edt__cls == '_test_ElementalObjectType.C' assert c.cls_var == 'class variable' assert c.a == 'c' assert c.b == 3 assert c.c == math.e # Test "integrated" marshalling assert edt_encode(a) == default_encoding(a) assert edt_encode(b) == default_encoding(b) assert edt_encode(c) == default_encoding(c) return
return repr(self.as_dict()) from_dict = edt_decode def _get_reference(self): if not is_node(self._reference): try: self._reference = as_internal_node(self._reference) except: self._reference = None return self._reference reference = property(_get_reference) edtlib.register_class(PropertyDefinition) class EntityProp(PropertyDefinition): edt__cls = 'mpx.lib.entity.properties.EntityProp' ptype = 'Entity' def __init__(self, **kw): PropertyDefinition.__init__(self, **kw) self._attrs += ['url', 'entity'] self.url = kw.get('url', '') self.entity = kw.get('entity', '') edtlib.register_class(EntityProp)
def test_ElementalObjectType(self): # Test "standalone" functionality: assert isinstance(ElementalObjectType().edt_get(), ElementalObjectType) assert ElementalObjectType().edt__typ is 'object' assert not hasattr(ElementalObjectType(),'edt__cls') # Test marshaling an old-style class. class A: cls_var='class variable' _no_cls='hide me please!' def __init__(self, **kw): self.__dict__.update(kw) return an_obj = A(a=1, b='b',c=None,_d="don't do it!", f=lambda : "this either!") e = default_encoding(an_obj) encoded_attrs=('edt__typ', 'cls_var','a','b','c') assert not filter(lambda k: k not in encoded_attrs, e.keys()) ignored_attrs=('_d','f','no_cls_') assert not filter(lambda k: k in ignored_attrs, e.keys()) # Test unmarshaling it inplace via constructor. a=ElementalObjectType(e) assert not hasattr(a, 'edt__cls') assert a.edt__typ == 'object' assert a.cls_var == 'class variable' assert a.a == 1 assert a.b == 'b' assert a.c is None assert not e # Map is consumed in decoding. # Test unmarshaling it in a new object via the decode factory. e = default_encoding(an_obj) edt_decode(ObjDict(e)) a=ElementalObjectType.edt_decode(e) assert not hasattr(a, 'edt__cls') assert a.edt__typ == 'object' assert a.cls_var == 'class variable' assert a.a == 1 assert a.b == 'b' assert a.c is None # Test reinstanciating a 'named' class (but not registered). a.edt__cls = class_name(A) e = default_encoding(a) a = ElementalObjectType.edt_decode(e) assert a.edt__cls == class_name(A) # Class type conveyed. assert isinstance(a,ElementalObjectType) # But not used. # Test reinstanciating the actual class with edt_decode(). class B(A): @classmethod def edt_decode(cls,edt_map,obj=None): assert obj is None # Supporting obj == None is not important. # How the class uses edt_map is 'a local matter'. Could # be passed in toto to __init__, could use load_from_map(), # another factory. Whatever. edt_map.pop('edt__typ') # Don't need this. edt_map.pop('edt__cls') # Or this. return cls(**edt_map) # Can't reference B in B, but cls == B. register_class(B) b = B(a='b',b=2,c=math.pi) e = default_encoding(b) b = ElementalObjectType.edt_decode(e) assert isinstance(b,B) # assert b.edt__typ == 'object' assert b.edt__cls == class_name(b.__class__) assert b.cls_var == 'class variable' assert b.a == 'b' assert b.b == 2 assert b.c == math.pi # Test ability to override cls_name (dangerous, but may be # required for reverse compatibility after refactors that move # data classes.) class C(B): edt__typ = 'object' # Not required by edtlib, but OK. register_class(C, cls_name='_test_ElementalObjectType.C') c = C(a='c',b=3,c=math.e) e = default_encoding(c) c = ElementalObjectType.edt_decode(e) assert isinstance(c,C) assert c.edt__typ == 'object' assert c.edt__cls == '_test_ElementalObjectType.C' assert c.cls_var == 'class variable' assert c.a == 'c' assert c.b == 3 assert c.c == math.e # Test "integrated" marshalling assert edt_encode(a) == default_encoding(a) assert edt_encode(b) == default_encoding(b) assert edt_encode(c) == default_encoding(c) return