| 1 |
|
| 2 |
) |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
from . import util |
| 11 |
|
| 12 |
|
| 13 |
class Entity: |
| 14 |
|
| 15 |
def __init__(self, nixfile, nixparent, h5group): |
| 16 |
util.check_entity_id(h5group.get_attr("entity_id")) |
| 17 |
self._h5group = h5group |
| 18 |
self._parent = nixparent |
| 19 |
self._file = nixfile |
| 20 |
|
| 21 |
@classmethod |
| 22 |
def create_new(cls, nixfile, nixparent, h5parent, name=None, type_=None): |
| 23 |
if name and type_: |
| 24 |
id_ = util.create_id() |
| 25 |
if not name: |
| 26 |
name = util.create_id() |
| 27 |
id_ = name |
| 28 |
util.check_entity_name_and_type(name, type_) |
| 29 |
|
| 30 |
h5group = h5parent.open_group(name) |
| 31 |
h5group.set_attr("name", name) |
| 32 |
h5group.set_attr("type", type_) |
| 33 |
h5group.set_attr("entity_id", id_) |
| 34 |
|
| 35 |
newentity = cls(nixfile, nixparent, h5group) |
| 36 |
newentity.force_created_at() |
| 37 |
newentity.force_updated_at() |
| 38 |
return newentity |
| 39 |
|
| 40 |
@property |
| 41 |
def id(self): |
| 42 |
""" |
| 43 |
A property providing the ID of the Entity. The id is generated |
| 44 |
automatically, therefore the property is read-only. |
| 45 |
|
| 46 |
:rtype: str |
| 47 |
""" |
| 48 |
return self._h5group.get_attr("entity_id") |
| 49 |
|
| 50 |
@property |
| 51 |
def created_at(self): |
| 52 |
""" |
| 53 |
The creation time of the entity. This is a read-only property. |
| 54 |
Use `force_created_at` in order to change the creation time. |
| 55 |
|
| 56 |
:rtype: int |
| 57 |
""" |
| 58 |
return util.str_to_time(self._h5group.get_attr("created_at")) |
| 59 |
|
| 60 |
@property |
| 61 |
def file(self): |
| 62 |
""" |
| 63 |
Reference to the NIX File object. |
| 64 |
This is a read-only property. |
| 65 |
|
| 66 |
:rtype: nixio.File |
| 67 |
""" |
| 68 |
return self._file |
| 69 |
|
| 70 |
def force_created_at(self, time=None): |
| 71 |
""" |
| 72 |
Sets the creation time `created_at` to the given time |
| 73 |
(default: current time). |
| 74 |
|
| 75 |
:param time: The time to set. |
| 76 |
:type time: int |
| 77 |
""" |
| 78 |
if time is None: |
| 79 |
time = util.now_int() |
| 80 |
else: |
| 81 |
util.check_attr_type(time, int) |
| 82 |
self._h5group.set_attr("created_at", util.time_to_str(time)) |
| 83 |
|
| 84 |
@property |
| 85 |
def updated_at(self): |
| 86 |
""" |
| 87 |
The time of the last update of the entity. This is a read-only |
| 88 |
property. Use `force_updated_at` in order to change the update |
| 89 |
time. |
| 90 |
|
| 91 |
:rtype: int |
| 92 |
""" |
| 93 |
return util.str_to_time(self._h5group.get_attr("updated_at")) |
| 94 |
|
| 95 |
def force_updated_at(self, time=None): |
| 96 |
""" |
| 97 |
Sets the update time `updated_at` to the given time. |
| 98 |
(default: current time) |
| 99 |
|
| 100 |
:param time: The time to set. |
| 101 |
:type time: int |
| 102 |
""" |
| 103 |
if time is None: |
| 104 |
time = util.now_int() |
| 105 |
else: |
| 106 |
util.check_attr_type(time, int) |
| 107 |
self._h5group.set_attr("updated_at", util.time_to_str(time)) |
| 108 |
|
| 109 |
@property |
| 110 |
def definition(self): |
| 111 |
""" |
| 112 |
The definition of the entity. The definition can contain a textual |
| 113 |
description of the entity. This is an optional read-write |
| 114 |
property, and can be None if no definition is available. |
| 115 |
|
| 116 |
:type: str |
| 117 |
""" |
| 118 |
return self._h5group.get_attr("definition") |
| 119 |
|
| 120 |
@definition.setter |
| 121 |
def definition(self, definition): |
| 122 |
util.check_attr_type(definition, str) |
| 123 |
self._h5group.set_attr("definition", definition) |
| 124 |
if self.file.auto_update_timestamps: |
| 125 |
self.force_updated_at() |
| 126 |
|
| 127 |
@property |
| 128 |
def name(self): |
| 129 |
""" |
| 130 |
The name of an entity. The name serves as a human readable |
| 131 |
identifier. This is a read-only property; entities cannot be |
| 132 |
renamed. |
| 133 |
|
| 134 |
:type: str |
| 135 |
""" |
| 136 |
return self._h5group.get_attr("name") |
| 137 |
|
| 138 |
@property |
| 139 |
def type(self): |
| 140 |
""" |
| 141 |
The type of the entity. The type is used in order to add semantic |
| 142 |
meaning to the entity. This is a read-write property, but it can't |
| 143 |
be set to None. |
| 144 |
|
| 145 |
:type: str |
| 146 |
""" |
| 147 |
return self._h5group.get_attr("type") |
| 148 |
|
| 149 |
@type.setter |
| 150 |
def type(self, typ): |
| 151 |
if typ is None: |
| 152 |
raise AttributeError("type can't be None") |
| 153 |
util.check_attr_type(typ, str) |
| 154 |
self._h5group.set_attr("type", typ) |
| 155 |
if self.file.auto_update_timestamps: |
| 156 |
self.force_updated_at() |
| 157 |
|
| 158 |
def __eq__(self, other): |
| 159 |
""" |
| 160 |
Two Entities are considered equal when they have the same id. |
| 161 |
""" |
| 162 |
if hasattr(other, "id"): |
| 163 |
return self.id == other.id |
| 164 |
return False |
| 165 |
|
| 166 |
def __ne__(self, other): |
| 167 |
return not self.__eq__(other) |
| 168 |
|
| 169 |
def __hash__(self): |
| 170 |
""" |
| 171 |
overwriting method __eq__ blocks inheritance of __hash__ in Python 3 |
| 172 |
hash has to be either explicitly inherited from parent class, |
| 173 |
implemented or escaped |
| 174 |
""" |
| 175 |
return hash(self.id) |
| 176 |
|
| 177 |
def __str__(self): |
| 178 |
return "{}: {{name = {}, type = {}}}".format( |
| 179 |
type(self).__name__, self.name, self.type |
| 180 |
) |
| 181 |
|
| 182 |
def __repr__(self): |
| 183 |
return self.__str__() |