|
1
|
1 |
|
from .attributesholder import AttributesHolder |
|
2
|
|
|
|
|
3
|
1 |
|
class TypedAttributesHolder(AttributesHolder): |
|
4
|
|
|
"""AttributesHolder with a special attributes holding type.""" |
|
5
|
1 |
|
__slots__ = () |
|
6
|
1 |
|
_type = None |
|
7
|
1 |
|
def __init__(self, *args, **attributes): |
|
8
|
|
|
# Sanity checks |
|
9
|
1 |
|
if self._type is None or self._possible_attributes is None: |
|
10
|
1 |
|
raise TypeError('%s is an abstract class.' % self.__class__) |
|
11
|
1 |
|
super(TypedAttributesHolder, self).__init__(*args, **attributes) |
|
12
|
|
|
|
|
13
|
1 |
|
self._attributes['type'] = self.type |
|
14
|
|
|
|
|
15
|
1 |
|
def _check_attributes(self, attributes, extra=None): |
|
16
|
1 |
|
if 'type' in attributes: |
|
17
|
1 |
|
assert attributes.pop('type') == self.type |
|
18
|
1 |
|
return super(TypedAttributesHolder, self) \ |
|
19
|
|
|
._check_attributes(attributes, extra) |
|
20
|
|
|
|
|
21
|
1 |
|
@property |
|
22
|
|
|
def type(self): |
|
23
|
|
|
"""Type of the node.""" |
|
24
|
1 |
|
return self._type |
|
25
|
|
|
|
|
26
|
1 |
|
def __repr__(self): |
|
27
|
1 |
|
return '<PPP node "%s" %r>' % (self.type, |
|
28
|
|
|
{x:y for (x,y) in self._attributes.items() if x != 'type'}) |
|
29
|
|
|
|
|
30
|
1 |
|
def __eq__(self, other): |
|
31
|
|
|
"""Tests equality with another AttributesHolder instance.""" |
|
32
|
1 |
|
if isinstance(other, dict): |
|
33
|
|
|
return self.as_dict() == other |
|
34
|
1 |
|
elif isinstance(other, TypedAttributesHolder) and \ |
|
35
|
|
|
self._type == other._type: |
|
36
|
1 |
|
return super(TypedAttributesHolder, self).__eq__(other) |
|
37
|
|
|
else: |
|
38
|
1 |
|
return NotImplemented |
|
39
|
|
|
|
|
40
|
1 |
|
def __hash__(self): |
|
41
|
1 |
|
return hash(frozenset(self._attributes.items())) |
|
42
|
|
|
|
|
43
|
|
|
|