Revision 32683096 lib/objects.py

b/lib/objects.py
44 44
from ganeti import errors
45 45
from ganeti import constants
46 46
from ganeti import netutils
47
from ganeti import objectutils
47 48
from ganeti import utils
48 49

  
49 50
from socket import AF_INET
......
191 192
    ])
192 193

  
193 194

  
194
class ConfigObject(object):
195
class ConfigObject(objectutils.ValidatedSlots):
195 196
  """A generic config object.
196 197

  
197 198
  It has the following properties:
......
206 207
  """
207 208
  __slots__ = []
208 209

  
209
  def __init__(self, **kwargs):
210
    for k, v in kwargs.iteritems():
211
      setattr(self, k, v)
212

  
213 210
  def __getattr__(self, name):
214
    if name not in self._all_slots():
211
    if name not in self.GetAllSlots():
215 212
      raise AttributeError("Invalid object attribute %s.%s" %
216 213
                           (type(self).__name__, name))
217 214
    return None
218 215

  
219 216
  def __setstate__(self, state):
220
    slots = self._all_slots()
217
    slots = self.GetAllSlots()
221 218
    for name in state:
222 219
      if name in slots:
223 220
        setattr(self, name, state[name])
224 221

  
225
  @classmethod
226
  def _all_slots(cls):
227
    """Compute the list of all declared slots for a class.
222
  def Validate(self):
223
    """Validates the slots.
228 224

  
229 225
    """
230
    slots = []
231
    for parent in cls.__mro__:
232
      slots.extend(getattr(parent, "__slots__", []))
233
    return slots
234

  
235
  #: Public getter for the defined slots
236
  GetAllSlots = _all_slots
237 226

  
238 227
  def ToDict(self):
239 228
    """Convert to a dict holding only standard python types.
......
246 235

  
247 236
    """
248 237
    result = {}
249
    for name in self._all_slots():
238
    for name in self.GetAllSlots():
250 239
      value = getattr(self, name, None)
251 240
      if value is not None:
252 241
        result[name] = value

Also available in: Unified diff