コード例 #1
0
 def _from_xml(self, datastring):
     if datastring is None:
         return None
     plurals = set(self.metadata.get('plurals', {}))
     try:
         node = etree.fromstring(datastring)
         root_tag = self._get_key(node.tag)
         links = self._get_links(root_tag, node)
         result = self._from_xml_node(node, plurals)
         # There is no case where root_tag = constants.VIRTUAL_ROOT_KEY
         # and links is not None because of the way data are serialized
         if root_tag == constants.VIRTUAL_ROOT_KEY:
             return result
         return dict({root_tag: result}, **links)
     except Exception as e:
         parseError = False
         # Python2.7
         if (hasattr(etree, 'ParseError')
                 and isinstance(e, getattr(etree, 'ParseError'))):
             parseError = True
         # Python2.6
         elif isinstance(e, expat.ExpatError):
             parseError = True
         if parseError:
             msg = _("Cannot understand XML")
             raise exception.MalformedResponseBody(reason=msg)
         else:
             raise
コード例 #2
0
 def _from_json(self, datastring):
     try:
         return jsonutils.loads(datastring)
     except ValueError:
         msg = _("Cannot understand JSON")
         raise exception.MalformedResponseBody(reason=msg)