Revision 8d14b30d lib/objects.py

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

  

Also available in: Unified diff