예제 #1
0
    def adapt(self, obj, inplace=False, path=[]):
        # Check if obj is a dict
        if not pyson_t.is_object(obj):
            raise errors.AdaptationError(('%(path)s: not a mapping', path))

        new_obj = obj.copy()

        # Test the root key
        root_value = None
        if self.root_key is not None:
            try:
                root_value = new_obj.pop(self.root_key)
            except KeyError:
                raise errors.AdaptationError((None, path))
            else:
                root_value = self._root_schema.adapt(root_value, inplace, path)

        # Catches AdaptationError and raises PartialAdaptationError if root
        # exists
        try:
            for k, v in new_obj.items():
                try:
                    adapter = self[k].adapt
                except KeyError:
                    raise errors.AdaptationError((None, path))
                else:
                    new_obj[k] = adapter(v, inplace, path + [k])
        except errors.AdaptationError as ex:
            if self.root_key is not None:
                raise errors.PartialAdaptationError(ex.args[0], ex)

        # Writes root key back to the dictionary
        if self.root_key is not None:
            new_obj[self.root_key] = root_value

        # Checks validity
        if self.is_valid(new_obj):
            if inplace:
                obj.update(new_obj)
                return obj
            else:
                return new_obj
예제 #2
0
 def adapt(self, obj, inplace=False, path=[]):
     if not self.is_valid(obj):
         try:
             return int(obj)
         except TypeError:
             try:
                 return float(obj)
             except:
                 raise errors.AdaptationError((None, path))
     else:
         return obj
예제 #3
0
 def adapt(self, obj, inplace=False, path=[]):
     partial = None
     for v in self._schemas:
         try:
             return v.adapt(obj, inplace, path)
         except errors.PartialAdaptationError as ex:
             partial = ex
         except errors.AdaptationError:
             pass
     if partial is not None:
         raise partial
     else:
         raise errors.AdaptationError((None, path))
예제 #4
0
 def adapt(self, obj, inplace=False, path=[]):
     '''
     Adapts an object to be compatible with the schema. This is useful,
     for instance, to load JSON representations of objects into valid
     versions of these objects.
     
     Other common conversions are also implemented (e.g., ints to floats,
     str to unicode)
     
     Examples
     --------
     
     >>> from pyson.schema import *
     >>> schema = Schema({'name': Str(), 'age': Int()})
     >>> schema.adapt({'name': 'Arthur', 'age': 31.0})
     {'age': 31, 'name': 'Arthur'}
     '''
     if self.is_valid(obj):
         return obj
     else:
         msg = 'in %%(path)s: %s object not compatible with %s' % (type(obj), self)
         raise errors.AdaptationError((msg, path))
예제 #5
0
 def adapt(self, obj, inplace=False, path=[]):
     try:
         return int(obj)
     except TypeError:
         raise errors.AdaptationError((None, path))
예제 #6
0
 def adapt(self, obj, inplace=False, path=[]):
     if self.is_valid(obj):
         return unicode(obj)
     else:
         raise errors.AdaptationError((None, path))