Package horizons :: Package util :: Module worldobject
[hide private]
[frames] | no frames]

Source Code for Module horizons.util.worldobject

 1  # ################################################### 
 2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
 3  # team@unknown-horizons.org 
 4  # This file is part of Unknown Horizons. 
 5  # 
 6  # Unknown Horizons is free software; you can redistribute it and/or modify 
 7  # it under the terms of the GNU General Public License as published by 
 8  # the Free Software Foundation; either version 2 of the License, or 
 9  # (at your option) any later version. 
10  # 
11  # This program is distributed in the hope that it will be useful, 
12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
14  # GNU General Public License for more details. 
15  # 
16  # You should have received a copy of the GNU General Public License 
17  # along with this program; if not, write to the 
18  # Free Software Foundation, Inc., 
19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
20  # ################################################### 
21   
22  import logging 
23  import weakref 
24  from typing import Any, MutableMapping 
25   
26  from horizons.messaging import WorldObjectDeleted 
27  from horizons.util.changelistener import ChangeListener 
28 29 30 -class WorldObjectNotFound(KeyError):
31 pass
32
33 34 -class WorldObject(ChangeListener):
35 """Gives every instance a unique id. 36 """ 37 __next_id = 1 38 __objects = weakref.WeakValueDictionary() # type: MutableMapping[int, Any] 39 log = logging.getLogger("util.worldobject") 40
41 - def __init__(self, worldid=None, **kwargs):
42 """ 43 @param worldid: worldid to assign. Use None to get an autogenerated one. 44 """ 45 super().__init__(**kwargs) 46 self.__init(worldid)
47
48 - def __init(self, worldid=None):
49 self.worldid = worldid if worldid is not None else WorldObject.__next_id 50 assert self.worldid not in WorldObject.__objects, "WorldObject ID(" + str(worldid) + ") already in use by " + str(self.get_object_by_id(worldid)) 51 WorldObject.__objects[self.worldid] = self 52 # Make sure that new WorldIDs are always higher than every other WorldObject 53 WorldObject.__next_id = max(WorldObject.__next_id, self.worldid + 1)
54 55 @classmethod
56 - def get_object_by_id(cls, id):
57 """Returns the worldobject with id id 58 Throws WorldObjectNotFound with the worldid as arg. 59 """ 60 try: 61 return cls.__objects[id] 62 except KeyError as e: 63 raise WorldObjectNotFound(e.args[0])
64 65 @classmethod
66 - def reset(cls):
67 cls.__next_id = 1 68 cls.__objects.clear()
69
70 - def save(self, db):
71 pass
72
73 - def load(self, db, worldid):
74 super().load(db, worldid) 75 self.__init(worldid) 76 self.log.debug('Loading worldobject %s %s', worldid, self)
77
78 - def remove(self):
79 WorldObjectDeleted.broadcast(self, self, self.worldid) 80 super().remove() 81 self.log.debug("Removing WorldObject %s %s", self.worldid, self) 82 del WorldObject.__objects[self.worldid]
83
84 - def __lt__(self, other):
85 return self.worldid < other.worldid
86 87 # for testing: 88 @classmethod
89 - def get_objs(cls):
90 return cls.__objects
91