ia64/xen-unstable

changeset 14934:26b80f4523e5

Change how XendStateStore store XML for persistance.

Make it explicity store empty dicts and list.

signed-off-by: Tom Wilkie <tom.wilkie@gmail.com>
author Tom Wilkie <tom.wilkie@gmail.com>
date Wed Apr 25 15:39:48 2007 +0100 (2007-04-25)
parents ca5bfcd32e13
children 2fe1f293e52d
files tools/python/xen/xend/XendStateStore.py
line diff
     1.1 --- a/tools/python/xen/xend/XendStateStore.py	Wed Apr 25 15:38:04 2007 +0100
     1.2 +++ b/tools/python/xen/xend/XendStateStore.py	Wed Apr 25 15:39:48 2007 +0100
     1.3 @@ -57,10 +57,12 @@ class XendStateStore:
     1.4  
     1.5      <hosts>
     1.6         <host uuid='49c01812-3c28-1ad4-a59d-2a3f81b13ec2'>
     1.7 -           <name type='string'>norwich</name>
     1.8 -           <description type='string'>Test Xen Host</description>
     1.9 -           <cpu uuid='6fc2d1ed-7eb0-4c9d-8006-3657d5483ae0' />
    1.10 -           <cpu uuid='669df3b8-62be-4e61-800b-bbe8ee63a760' />
    1.11 +          <name type='string'>norwich</name>
    1.12 +          <description type='string'>Test Xen Host</description>
    1.13 +          <cpu type='dict'>
    1.14 +             <item uuid='6fc2d1ed-7eb0-4c9d-8006-3657d5483ae0' />
    1.15 +             <item uuid='669df3b8-62be-4e61-800b-bbe8ee63a760' />
    1.16 +          </cpu>
    1.17         </host>
    1.18      </hosts>
    1.19  
    1.20 @@ -121,18 +123,20 @@ class XendStateStore:
    1.21                  if val_elem.firstChild:
    1.22                      val_text = val_elem.firstChild.nodeValue.strip()
    1.23                  
    1.24 -                if val_type == '' and val_uuid != '':
    1.25 -                    # this is a reference
    1.26 -                    if val_name not in cls_dict:
    1.27 -                        cls_dict[val_name] = {}
    1.28 -                    cls_dict[val_name][val_uuid] = None
    1.29 -                elif val_type == '':
    1.30 -                    # dictionary
    1.31 -                    k = val_elem.getAttribute('key').encode('utf8')
    1.32 -                    v = val_elem.getAttribute('value').encode('utf8')
    1.33 -                    if val_name not in cls_dict:
    1.34 -                        cls_dict[val_name] = {}
    1.35 -                    cls_dict[val_name][k] = v
    1.36 +                if val_type == 'list':
    1.37 +                    cls_dict[val_name] = []
    1.38 +                    for item in val_elem.childNodes:
    1.39 +                        if item.nodeType != Node.ELEMENT_NODE:
    1.40 +                            continue # skip non element nodes
    1.41 +                        cls_dict[val_name].append(item.getAttribute('uuid'))
    1.42 +                elif val_type == 'dict':
    1.43 +                    cls_dict[val_name] = {}
    1.44 +                    for item in val_elem.childNodes:
    1.45 +                        if item.nodeType != Node.ELEMENT_NODE:
    1.46 +                            continue # skip non element nodes
    1.47 +                        k = item.getAttribute('key').encode('utf8')
    1.48 +                        v = item.getAttribute('value').encode('utf8')
    1.49 +                        cls_dict[val_name][k] = v
    1.50                  elif val_type == 'string':
    1.51                      cls_dict[val_name] = val_text.encode('utf8')
    1.52                  elif val_type == 'float':
    1.53 @@ -158,8 +162,7 @@ class XendStateStore:
    1.54          @param state: a Xen API struct of the state of the class.
    1.55          @type  state: dict
    1.56          @rtype: None
    1.57 -        """
    1.58 -        
    1.59 +        """        
    1.60          xml_path = self._xml_file(cls)
    1.61  
    1.62          doc = minidom.getDOMImplementation().createDocument(None,
    1.63 @@ -191,7 +194,7 @@ class XendStateStore:
    1.64                      store_val = str(int(val))
    1.65                      store_type = 'bool'
    1.66  
    1.67 -                if store_type != None:
    1.68 +                if store_type is not None:
    1.69                      val_node = doc.createElement(key)
    1.70                      val_node.setAttribute('type', store_type)
    1.71                      node.appendChild(val_node)
    1.72 @@ -202,19 +205,25 @@ class XendStateStore:
    1.73  
    1.74                  # deal with dicts and lists
    1.75                  if type(val) == dict:
    1.76 -                    for val_uuid in val.keys():
    1.77 -                        val_node = doc.createElement(key)
    1.78 +                    val_node = doc.createElement(key)
    1.79 +                    val_node.setAttribute('type', 'dict')
    1.80 +                    for val_item in val.keys():
    1.81 +                        tmp = doc.createElement("item")
    1.82                          if key in ['other_config', 'device_config']:
    1.83 -                            val_node.setAttribute('key', str(val_uuid))
    1.84 -                            val_node.setAttribute('value', str(val[val_uuid]))
    1.85 +                            tmp.setAttribute('key', str(val_item))
    1.86 +                            tmp.setAttribute('value', str(val[val_item]))
    1.87                          else:
    1.88 -                            val_node.setAttribute('uuid', val_uuid)
    1.89 -                        node.appendChild(val_node)
    1.90 +                            tmp.setAttribute('uuid', val_uuid)
    1.91 +                        val_node.appendChild(tmp)
    1.92 +                    node.appendChild(val_node)
    1.93                  elif type(val) in (list, tuple):
    1.94 +                    val_node = doc.createElement(key)
    1.95 +                    val_node.setAttribute('type', 'list')
    1.96                      for val_uuid in val:
    1.97 -                        val_node = doc.createElement(key)
    1.98 -                        val_node.setAttribute('uuid', val_uuid)
    1.99 -                        node.appendChild(val_node)
   1.100 +                        tmp = doc.createElement("item")
   1.101 +                        tmp.setAttribute('uuid', val_uuid)
   1.102 +                        val_node.appendChild(tmp)
   1.103 +                    node.appendChild(val_node)
   1.104  
   1.105          open(xml_path, 'w').write(doc.toprettyxml())
   1.106