Imported Upstream version 2008.1+svn1656
[opeanno-debian-packaging.git] / game / util / living.py
blob303e826893bc86ebe16cbcb34600af5b942587e8
1 # ###################################################
2 # Copyright (C) 2008 The OpenAnno Team
3 # team@openanno.org
4 # This file is part of OpenAnno.
6 # OpenAnno 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.
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.
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 # ###################################################
22 class livingProperty(object):
23 def __init__(self):
24 self.values = {}
26 def __get__(self, obj, cls=None):
27 return self.values.get(obj, None)
29 def __set__(self, obj, value):
30 if obj in self.values and hasattr(self.values[obj], 'end'):
31 self.values[obj].end()
32 if value is None:
33 del self.values[obj]
34 else:
35 self.values[obj] = value
36 if hasattr(value, 'begin'):
37 print 'Beginning:', value
38 tmp1, tmp2 = value._livingObject_args, value._livingObject_kwargs
39 del value._livingObject_args
40 del value._livingObject_kwargs
41 value.begin(*tmp1, **tmp2)
43 def __delete__(self, obj):
44 self.__set__(obj, None)
46 class livingObject(object):
47 def __init__(self, *args, **kwargs):
48 print 'Initing:', self
49 super(livingObject, self).__init__()
50 self._livingObject_args = args
51 self._livingObject_kwargs = kwargs
53 def begin(self):
54 pass
56 def end(self):
57 for p in self.__dict__.keys()[:]:
58 delattr(self, p)
59 self._is_ended = True
61 def __del__(self):
62 if not (hasattr(self, '_is_ended') and self._is_ended):
63 print "Warning: Object %s is not ended but no reference is left." % (repr(self),)