def convert(key, data, errors, context): value = data.get(key) #logger.debug('Processing field %s for reading (%r)', key[0], value) assert not value is missing assert isinstance(value, basestring) if not value: logger.warn('Read empty value for field %s' % (key[0])) # noop return
def _get_field_value(self, v, field): '''Get the value of a field considered a leaf. Serialize or format (not both!) this value, if requested so. ''' assert v is not None, 'This was supposed to be checked at dictize()' # Check if value needs to be serialized serializer_name = self.opts.get('serialize-values', False) if serializer_name: ser = serializer_for_field(field, serializer_name) if ser: try: v = ser.dumps(v) except Exception as ex: logger.warn( 'Failed to serialize value %r for field %r (%s): %s' % (v, field.__name__, field.__class__.__name__, ex)) v = None # Return here, no need to do anything more return v # Check if value needs to be formatted format_spec = self.opts.get('format-values', False) if format_spec: assert isinstance(format_spec, FormatSpec) fo = formatter_for_field(field, format_spec.name) if fo: fo_opts = format_spec.opts # Fetch any extra field-level extra options fo_conf = formatters.config_for_field( field, format_spec.name) if fo_conf and 'extra-opts' in fo_conf: fo_opts = copy.copy(fo_opts) fo_opts.update(fo_conf.get('extra-opts')) # Try to format try: v = fo.format(v, opts=fo_opts) except Exception as ex: logger.warn( 'Failed to format value %r for field %r (%s): %s' % (v, field.__name__, field.__class__.__name__, ex)) v = None return v
def _get_field_value(self, v, field): '''Get the value of a field considered a leaf. Serialize or format (not both!) this value, if requested so. ''' assert v is not None, 'This was supposed to be checked at dictize()' # Check if value needs to be serialized serializer_name = self.opts.get('serialize-values', False) if serializer_name: ser = serializer_for_field(field, serializer_name) if ser: try: v = ser.dumps(v) except Exception as ex: logger.warn( 'Failed to serialize value %r for field %r (%s): %s' % ( v, field.__name__, field.__class__.__name__, ex)) v = None # Return here, no need to do anything more return v # Check if value needs to be formatted format_spec = self.opts.get('format-values', False) if format_spec: assert isinstance(format_spec, FormatSpec) fo = formatter_for_field(field, format_spec.name) if fo: fo_opts = format_spec.opts # Fetch any extra field-level extra options fo_conf = formatters.config_for_field(field, format_spec.name) if fo_conf and 'extra-opts' in fo_conf: fo_opts = copy.copy(fo_opts) fo_opts.update(fo_conf.get('extra-opts')) # Try to format try: v = fo.format(v, opts=fo_opts) except Exception as ex: logger.warn( 'Failed to format value %r for field %r (%s): %s' % ( v, field.__name__, field.__class__.__name__, ex)) v = None return v
def _create_leaf_field(self, v, field): assert v is not None, 'This was supposed to be checked at load()' f = None ser_name = self.opts.get('unserialize-values', False) if ser_name: ser = serializer_for_field(field, ser_name) if ser: try: f = ser.loads(v) except: logger.warn( 'Failed to unserialize value %r for field %r' %(v, field)) if f is None: f = copy.copy(v) # or simply assign it ?? return f