Abstract base class for MJCF attribute data types.
| 46 | |
| 47 | |
| 48 | class _Attribute(metaclass=abc.ABCMeta): |
| 49 | """Abstract base class for MJCF attribute data types.""" |
| 50 | |
| 51 | def __init__(self, name, required, parent, value, |
| 52 | conflict_allowed, conflict_behavior): |
| 53 | self._name = name |
| 54 | self._required = required |
| 55 | self._parent = parent |
| 56 | self._value = None |
| 57 | self._conflict_allowed = conflict_allowed |
| 58 | self._conflict_behavior = conflict_behavior |
| 59 | self._check_and_assign(value) |
| 60 | |
| 61 | def _check_and_assign(self, new_value): |
| 62 | if new_value is None: |
| 63 | self.clear() |
| 64 | elif isinstance(new_value, str): |
| 65 | self._assign_from_string(new_value) |
| 66 | else: |
| 67 | self._assign(new_value) |
| 68 | if debugging.debug_mode(): |
| 69 | self._last_modified_stack = debugging.get_current_stack_trace() |
| 70 | |
| 71 | @property |
| 72 | def last_modified_stack(self): |
| 73 | if debugging.debug_mode(): |
| 74 | return self._last_modified_stack |
| 75 | |
| 76 | @property |
| 77 | def value(self): |
| 78 | return self._value |
| 79 | |
| 80 | @value.setter |
| 81 | def value(self, new_value): |
| 82 | self._check_and_assign(new_value) |
| 83 | |
| 84 | @abc.abstractmethod |
| 85 | def _assign(self, value): |
| 86 | raise NotImplementedError # pragma: no cover |
| 87 | |
| 88 | def clear(self): |
| 89 | if self._required: |
| 90 | raise AttributeError( |
| 91 | 'Attribute {!r} of element <{}> is required' |
| 92 | .format(self._name, self._parent.tag)) |
| 93 | else: |
| 94 | self._force_clear() |
| 95 | |
| 96 | def _force_clear(self): |
| 97 | self._before_clear() |
| 98 | self._value = None |
| 99 | if debugging.debug_mode(): |
| 100 | self._last_modified_stack = debugging.get_current_stack_trace() |
| 101 | |
| 102 | def _before_clear(self): |
| 103 | pass |
| 104 | |
| 105 | def _assign_from_string(self, string): |
no outgoing calls
no test coverage detected
searching dependent graphs…