Revision 473d87a3

b/Makefile.am
261 261
	lib/mcpu.py \
262 262
	lib/netutils.py \
263 263
	lib/objects.py \
264
	lib/objectutils.py \
265 264
	lib/opcodes.py \
265
	lib/outils.py \
266 266
	lib/ovf.py \
267 267
	lib/pathutils.py \
268 268
	lib/qlang.py \
......
1094 1094
	test/py/ganeti.mcpu_unittest.py \
1095 1095
	test/py/ganeti.netutils_unittest.py \
1096 1096
	test/py/ganeti.objects_unittest.py \
1097
	test/py/ganeti.objectutils_unittest.py \
1098 1097
	test/py/ganeti.opcodes_unittest.py \
1098
	test/py/ganeti.outils_unittest.py \
1099 1099
	test/py/ganeti.ovf_unittest.py \
1100 1100
	test/py/ganeti.qlang_unittest.py \
1101 1101
	test/py/ganeti.query_unittest.py \
b/lib/masterd/iallocator.py
1 1
#
2 2
#
3 3

  
4
# Copyright (C) 2012 Google Inc.
4
# Copyright (C) 2012, 2013 Google Inc.
5 5
#
6 6
# This program is free software; you can redistribute it and/or modify
7 7
# it under the terms of the GNU General Public License as published by
......
25 25
from ganeti import constants
26 26
from ganeti import errors
27 27
from ganeti import ht
28
from ganeti import objectutils
28
from ganeti import outils
29 29
from ganeti import opcodes
30 30
from ganeti import rpc
31 31
from ganeti import serializer
......
60 60
_INST_NAME = ("name", ht.TNonEmptyString)
61 61

  
62 62

  
63
class _AutoReqParam(objectutils.AutoSlots):
63
class _AutoReqParam(outils.AutoSlots):
64 64
  """Meta class for request definitions.
65 65

  
66 66
  """
......
73 73
    return [slot for (slot, _) in params]
74 74

  
75 75

  
76
class IARequestBase(objectutils.ValidatedSlots):
76
class IARequestBase(outils.ValidatedSlots):
77 77
  """A generic IAllocator request object.
78 78

  
79 79
  """
......
92 92
    REQ_PARAMS attribute for this class.
93 93

  
94 94
    """
95
    objectutils.ValidatedSlots.__init__(self, **kwargs)
95
    outils.ValidatedSlots.__init__(self, **kwargs)
96 96

  
97 97
    self.Validate()
98 98

  
b/lib/objects.py
1 1
#
2 2
#
3 3

  
4
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012 Google Inc.
4
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Google Inc.
5 5
#
6 6
# This program is free software; you can redistribute it and/or modify
7 7
# it under the terms of the GNU General Public License as published by
......
45 45
from ganeti import errors
46 46
from ganeti import constants
47 47
from ganeti import netutils
48
from ganeti import objectutils
48
from ganeti import outils
49 49
from ganeti import utils
50 50

  
51 51
from socket import AF_INET
......
193 193
    ])
194 194

  
195 195

  
196
class ConfigObject(objectutils.ValidatedSlots):
196
class ConfigObject(outils.ValidatedSlots):
197 197
  """A generic config object.
198 198

  
199 199
  It has the following properties:
/dev/null
1
#
2
#
3

  
4
# Copyright (C) 2012 Google Inc.
5
#
6
# This program 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, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# 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 Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

  
21
"""Module for object related utils."""
22

  
23

  
24
class AutoSlots(type):
25
  """Meta base class for __slots__ definitions.
26

  
27
  """
28
  def __new__(mcs, name, bases, attrs):
29
    """Called when a class should be created.
30

  
31
    @param mcs: The meta class
32
    @param name: Name of created class
33
    @param bases: Base classes
34
    @type attrs: dict
35
    @param attrs: Class attributes
36

  
37
    """
38
    assert "__slots__" not in attrs, \
39
      "Class '%s' defines __slots__ when it should not" % name
40

  
41
    attrs["__slots__"] = mcs._GetSlots(attrs)
42

  
43
    return type.__new__(mcs, name, bases, attrs)
44

  
45
  @classmethod
46
  def _GetSlots(mcs, attrs):
47
    """Used to get the list of defined slots.
48

  
49
    @param attrs: The attributes of the class
50

  
51
    """
52
    raise NotImplementedError
53

  
54

  
55
class ValidatedSlots(object):
56
  """Sets and validates slots.
57

  
58
  """
59
  __slots__ = []
60

  
61
  def __init__(self, **kwargs):
62
    """Constructor for BaseOpCode.
63

  
64
    The constructor takes only keyword arguments and will set
65
    attributes on this object based on the passed arguments. As such,
66
    it means that you should not pass arguments which are not in the
67
    __slots__ attribute for this class.
68

  
69
    """
70
    slots = self.GetAllSlots()
71
    for (key, value) in kwargs.items():
72
      if key not in slots:
73
        raise TypeError("Object %s doesn't support the parameter '%s'" %
74
                        (self.__class__.__name__, key))
75
      setattr(self, key, value)
76

  
77
  @classmethod
78
  def GetAllSlots(cls):
79
    """Compute the list of all declared slots for a class.
80

  
81
    """
82
    slots = []
83
    for parent in cls.__mro__:
84
      slots.extend(getattr(parent, "__slots__", []))
85
    return slots
86

  
87
  def Validate(self):
88
    """Validates the slots.
89

  
90
    This method must be implemented by the child classes.
91

  
92
    """
93
    raise NotImplementedError
b/lib/opcodes.py
1 1
#
2 2
#
3 3

  
4
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012 Google Inc.
4
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Google Inc.
5 5
#
6 6
# This program is free software; you can redistribute it and/or modify
7 7
# it under the terms of the GNU General Public License as published by
......
41 41
from ganeti import errors
42 42
from ganeti import ht
43 43
from ganeti import objects
44
from ganeti import objectutils
44
from ganeti import outils
45 45

  
46 46

  
47 47
# Common opcode attributes
......
417 417
_TMaybeAddr4List = ht.TMaybe(ht.TListOf(_TIpAddress4))
418 418

  
419 419

  
420
class _AutoOpParamSlots(objectutils.AutoSlots):
420
class _AutoOpParamSlots(outils.AutoSlots):
421 421
  """Meta class for opcode definitions.
422 422

  
423 423
  """
......
443 443

  
444 444
    attrs["OP_ID"] = _NameToId(name)
445 445

  
446
    return objectutils.AutoSlots.__new__(mcs, name, bases, attrs)
446
    return outils.AutoSlots.__new__(mcs, name, bases, attrs)
447 447

  
448 448
  @classmethod
449 449
  def _GetSlots(mcs, attrs):
......
457 457
    return [pname for (pname, _, _, _) in params]
458 458

  
459 459

  
460
class BaseOpCode(objectutils.ValidatedSlots):
460
class BaseOpCode(outils.ValidatedSlots):
461 461
  """A simple serializable object.
462 462

  
463 463
  This object serves as a parent class for OpCode without any custom
b/lib/outils.py
1
#
2
#
3

  
4
# Copyright (C) 2012 Google Inc.
5
#
6
# This program 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, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# 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 Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

  
21
"""Module for object related utils."""
22

  
23

  
24
class AutoSlots(type):
25
  """Meta base class for __slots__ definitions.
26

  
27
  """
28
  def __new__(mcs, name, bases, attrs):
29
    """Called when a class should be created.
30

  
31
    @param mcs: The meta class
32
    @param name: Name of created class
33
    @param bases: Base classes
34
    @type attrs: dict
35
    @param attrs: Class attributes
36

  
37
    """
38
    assert "__slots__" not in attrs, \
39
      "Class '%s' defines __slots__ when it should not" % name
40

  
41
    attrs["__slots__"] = mcs._GetSlots(attrs)
42

  
43
    return type.__new__(mcs, name, bases, attrs)
44

  
45
  @classmethod
46
  def _GetSlots(mcs, attrs):
47
    """Used to get the list of defined slots.
48

  
49
    @param attrs: The attributes of the class
50

  
51
    """
52
    raise NotImplementedError
53

  
54

  
55
class ValidatedSlots(object):
56
  """Sets and validates slots.
57

  
58
  """
59
  __slots__ = []
60

  
61
  def __init__(self, **kwargs):
62
    """Constructor for BaseOpCode.
63

  
64
    The constructor takes only keyword arguments and will set
65
    attributes on this object based on the passed arguments. As such,
66
    it means that you should not pass arguments which are not in the
67
    __slots__ attribute for this class.
68

  
69
    """
70
    slots = self.GetAllSlots()
71
    for (key, value) in kwargs.items():
72
      if key not in slots:
73
        raise TypeError("Object %s doesn't support the parameter '%s'" %
74
                        (self.__class__.__name__, key))
75
      setattr(self, key, value)
76

  
77
  @classmethod
78
  def GetAllSlots(cls):
79
    """Compute the list of all declared slots for a class.
80

  
81
    """
82
    slots = []
83
    for parent in cls.__mro__:
84
      slots.extend(getattr(parent, "__slots__", []))
85
    return slots
86

  
87
  def Validate(self):
88
    """Validates the slots.
89

  
90
    This method must be implemented by the child classes.
91

  
92
    """
93
    raise NotImplementedError
/dev/null
1
#!/usr/bin/python
2
#
3

  
4
# Copyright (C) 2012 Google Inc.
5
#
6
# This program 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, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# 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 Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

  
21

  
22
"""Script for unittesting the objectutils module"""
23

  
24

  
25
import unittest
26

  
27
from ganeti import objectutils
28

  
29
import testutils
30

  
31

  
32
class SlotsAutoSlot(objectutils.AutoSlots):
33
  @classmethod
34
  def _GetSlots(mcs, attr):
35
    return attr["SLOTS"]
36

  
37

  
38
class AutoSlotted(object):
39
  __metaclass__ = SlotsAutoSlot
40

  
41
  SLOTS = ["foo", "bar", "baz"]
42

  
43

  
44
class TestAutoSlot(unittest.TestCase):
45
  def test(self):
46
    slotted = AutoSlotted()
47
    self.assertEqual(slotted.__slots__, AutoSlotted.SLOTS)
48

  
49
if __name__ == "__main__":
50
  testutils.GanetiTestProgram()
b/test/py/ganeti.outils_unittest.py
1
#!/usr/bin/python
2
#
3

  
4
# Copyright (C) 2012, 2013 Google Inc.
5
#
6
# This program 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, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# 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 Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

  
21

  
22
"""Script for unittesting the outils module"""
23

  
24

  
25
import unittest
26

  
27
from ganeti import outils
28

  
29
import testutils
30

  
31

  
32
class SlotsAutoSlot(outils.AutoSlots):
33
  @classmethod
34
  def _GetSlots(mcs, attr):
35
    return attr["SLOTS"]
36

  
37

  
38
class AutoSlotted(object):
39
  __metaclass__ = SlotsAutoSlot
40

  
41
  SLOTS = ["foo", "bar", "baz"]
42

  
43

  
44
class TestAutoSlot(unittest.TestCase):
45
  def test(self):
46
    slotted = AutoSlotted()
47
    self.assertEqual(slotted.__slots__, AutoSlotted.SLOTS)
48

  
49
if __name__ == "__main__":
50
  testutils.GanetiTestProgram()

Also available in: Unified diff