Revision 8d14b30d

b/lib/Makefile.am
4 4
pkgpython_PYTHON = __init__.py backend.py cli.py cmdlib.py config.py \
5 5
	objects.py errors.py logger.py ssh.py utils.py rpc.py \
6 6
	bdev.py hypervisor.py opcodes.py mcpu.py constants.py \
7
	ssconf.py locking.py luxi.py jqueue.py
7
	ssconf.py locking.py luxi.py jqueue.py serializer.py
8 8

  
9 9
all-local: _autoconf.py
10 10

  
b/lib/cmdlib.py
30 30
import tempfile
31 31
import re
32 32
import platform
33
import simplejson
34 33

  
35 34
from ganeti import rpc
36 35
from ganeti import ssh
......
43 42
from ganeti import objects
44 43
from ganeti import opcodes
45 44
from ganeti import ssconf
46

  
47

  
48
# Check whether the simplejson module supports indentation
49
_JSON_INDENT = 2
50
try:
51
  simplejson.dumps(1, indent=_JSON_INDENT)
52
except TypeError:
53
  _JSON_INDENT = None
45
from ganeti import serializer
54 46

  
55 47

  
56 48
class LogicalUnit(object):
......
3162 3154

  
3163 3155
    _IAllocatorAddNewInstance(al_data, op)
3164 3156

  
3165
    if _JSON_INDENT is None:
3166
      text = simplejson.dumps(al_data)
3167
    else:
3168
      text = simplejson.dumps(al_data, indent=_JSON_INDENT)
3157
    text = serializer.Dump(al_data)
3169 3158

  
3170 3159
    result = _IAllocatorRun(self.op.iallocator, text)
3171 3160

  
......
4876 4865

  
4877 4866
  """
4878 4867
  try:
4879
    rdict = simplejson.loads(data)
4868
    rdict = serializer.Load(data)
4880 4869
  except Exception, err:
4881 4870
    raise errors.OpExecError("Can't parse iallocator results: %s" % str(err))
4882 4871

  
......
4968 4957
    else:
4969 4958
      _IAllocatorAddRelocateInstance(data, self.op)
4970 4959

  
4971
    if _JSON_INDENT is None:
4972
      text = simplejson.dumps(data)
4973
    else:
4974
      text = simplejson.dumps(data, indent=_JSON_INDENT)
4960
    text = serializer.Dump(data)
4975 4961
    if self.op.direction == constants.IALLOCATOR_DIR_IN:
4976 4962
      result = text
4977 4963
    else:
b/lib/config.py
42 42
from ganeti import constants
43 43
from ganeti import rpc
44 44
from ganeti import objects
45
from ganeti import serializer
45 46

  
46 47

  
47 48
class ConfigWriter:
......
502 503
    f = open(self._cfg_file, 'r')
503 504
    try:
504 505
      try:
505
        data = objects.ConfigData.Load(f)
506
        data = objects.ConfigData.FromDict(serializer.Load(f.read()))
506 507
      except Exception, err:
507 508
        raise errors.ConfigurationError(err)
508 509
    finally:
......
560 561
    if destination is None:
561 562
      destination = self._cfg_file
562 563
    self._BumpSerialNo()
564
    txt = serializer.Dump(self._config_data.ToDict())
563 565
    dir_name, file_name = os.path.split(destination)
564 566
    fd, name = tempfile.mkstemp('.newconfig', file_name, dir_name)
565 567
    f = os.fdopen(fd, 'w')
566 568
    try:
567
      self._config_data.Dump(f)
569
      f.write(txt)
568 570
      os.fsync(f.fileno())
569 571
    finally:
570 572
      f.close()
b/lib/objects.py
27 27
"""
28 28

  
29 29

  
30
import simplejson
31 30
import ConfigParser
32 31
import re
33 32
from cStringIO import StringIO
......
40 39
           "OS", "Node", "Cluster"]
41 40

  
42 41

  
43
# Check whether the simplejson module supports indentation
44
_JSON_INDENT = 2
45
try:
46
  simplejson.dumps(1, indent=_JSON_INDENT)
47
except TypeError:
48
  _JSON_INDENT = None
49

  
50

  
51 42
class ConfigObject(object):
52 43
  """A generic config object.
53 44

  
......
90 81
      if name in self.__slots__:
91 82
        setattr(self, name, state[name])
92 83

  
93
  def Dump(self, fobj):
94
    """Dump to a file object.
95

  
96
    """
97
    data = self.ToDict()
98
    if _JSON_INDENT is None:
99
      simplejson.dump(data, fobj)
100
    else:
101
      simplejson.dump(data, fobj, indent=_JSON_INDENT)
102

  
103
  @classmethod
104
  def Load(cls, fobj):
105
    """Load data from the given stream.
106

  
107
    """
108
    return cls.FromDict(simplejson.load(fobj))
109

  
110
  def Dumps(self):
111
    """Dump and return the string representation."""
112
    buf = StringIO()
113
    self.Dump(buf)
114
    return buf.getvalue()
115

  
116
  @classmethod
117
  def Loads(cls, data):
118
    """Load data from a string."""
119
    return cls.Load(StringIO(data))
120

  
121 84
  def ToDict(self):
122 85
    """Convert to a dict holding only standard python types.
123 86

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

  
4
# Copyright (C) 2007, 2008 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
"""Serializer abstraction module
22

  
23
This module introduces a simple abstraction over the serialization
24
backend (currently json).
25

  
26
"""
27

  
28
import simplejson
29
import ConfigParser
30
import re
31

  
32
# Check whether the simplejson module supports indentation
33
_JSON_INDENT = 2
34
try:
35
  simplejson.dumps(1, indent=_JSON_INDENT)
36
except TypeError:
37
  _JSON_INDENT = None
38

  
39
_RE_EOLSP = re.compile('\s+$', re.MULTILINE)
40

  
41

  
42
def Dump(data):
43
  """Serialize a given object.
44

  
45
  """
46
  if _JSON_INDENT is None:
47
    txt = simplejson.dumps(data)
48
  else:
49
    txt = simplejson.dumps(data, indent=_JSON_INDENT)
50
  if not txt.endswith('\n'):
51
    txt += '\n'
52
  txt = _RE_EOLSP.sub("", txt)
53
  return txt
54

  
55

  
56
def Load(txt):
57
  """Unserialize data from a string.
58

  
59
  """
60
  return simplejson.loads(txt)

Also available in: Unified diff