aboutsummaryrefslogtreecommitdiffstats
path: root/partmgr/core/entity.py
blob: 0187ee354bb3e88b5c568f604e3a6a8da8a16c99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# -*- coding: utf-8 -*-
#
# PartMgr - Abstract entity
#
# Copyright 2014-2022 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

from partmgr.core.timestamp import *
from partmgr.core.util import *


class Entity:
	"Abstract entity base class."

	FLG_OFFSET = 8	# Offset for user-flags

	NO_ID = -1	# Invalid ID

	# Set to PTYPE_..., if this entity supports parameters
	PARAMETER_PTYPE = None

	def __init__(self, name="", description="", flags=0,
		     createTimeStamp=None, modifyTimeStamp=None,
		     id=NO_ID, db=None, entityType="Entity"):
		self.name = name
		self.description = description
		self.flags = flags
		self.createTimeStamp = Timestamp(createTimeStamp)
		if not self.createTimeStamp.isValid():
			self.createTimeStamp.setNow()
		self.modifyTimeStamp = Timestamp(modifyTimeStamp)
		if not self.modifyTimeStamp.isValid():
			self.modifyTimeStamp.setStamp(self.createTimeStamp.getStamp())
		self.id = id
		self.db = db
		self.entityType = entityType

	def getId(self):
		return self.id

	def getName(self):
		return self.name

	def setName(self, newName):
		self.name = newName
		self.syncDatabase()

	def getDescription(self):
		return self.description

	def setDescription(self, newDescription):
		self.description = newDescription
		self.syncDatabase()

	def getCreateTimeStamp(self):
		return self.createTimeStamp.getStamp()

	def getModifyTimeStamp(self):
		return self.modifyTimeStamp.getStamp()

	def getAllParameters(self):
		assert(self.PARAMETER_PTYPE is not None)
		return self.db.getAllParametersByParent(self.PARAMETER_PTYPE, self)

	def addParameter(self, newParameter):
		assert(self.PARAMETER_PTYPE is not None)
		newParameter.setParentType(self.PARAMETER_PTYPE)
		newParameter.setParent(self)
		newParameter.id = Entity.NO_ID
		self.db.modifyParameter(newParameter)
		return newParameter

	@staticmethod
	def isValidId(entityId):
		if entityId is None:
			return False
		return entityId != Entity.NO_ID

	@staticmethod
	def toId(entity):
		if entity is None:
			return Entity.NO_ID
		if isinstance(entity, int):
			return entity
		assert(isinstance(entity, Entity))
		return entity.id

	def hasValidId(self):
		return Entity.isValidId(self.id)

	def getDatabase(self):
		return self.db

	def inDatabase(self, db):
		return self.db is db and self.hasValidId()

	def syncDatabase(self):
		pass # Override this in subclass, if required.

	def updateModifyTimeStamp(self):
		self.modifyTimeStamp.setNow()

	def delete(self):
		self.db = None
		self.id = Entity.NO_ID
		self.entityType = "Entity"

	def getEntityType(self):
		return self.entityType

	def __eq__(self, other):
		return isinstance(other, Entity) and\
		       self.entityType == other.entityType and\
		       self.db == other.db and\
		       self.id == other.id

	def __ne__(self, other):
		return not self.__eq__(other)

	def __hash__(self):
		return (hash(self.entityType) ^
			hash(self.db) ^
			hash(self.id))

	def __repr__(self):
		args = []
		args.append(str(self.name))
		args.append(str(self.description))
		args.append(str(self.flags))
		args.append(str(self.createTimeStamp))
		args.append(str(self.modifyTimeStamp))
		args.append(str(self.id))
		args.append(str(self.db))
		args.append(str(self.entityType))
		return "Entity(" + ", ".join(args) + ")"
bues.ch cgit interface