def pre_dump(self, record, data, **kwargs): """Called before a record is dumped in a secondary storage system.""" if self._dump: dict_set( data, self.attr_name, getattr(record, self.attr_name) )
def set_default_value(record_dict, value, path, default_prefix="metadata"): """Set the value with the specified dot-separated path in the record. :param record_dict: The dict in which to set the value. :param value: The value to set (can be callable). :param path: The dot-separated path to the value. :param default_prefix: The default path prefix to assume. """ if callable(value): value = value() # if the path explicitly starts with a dot, we know that we shouldn't # prepend the default_prefix to the path if path.startswith("."): path = path[1:] else: path = "{}.{}".format(default_prefix, path) dict_set(record_dict, path, value)
def expand(self, hit): """Return the expanded fields for the given hit.""" results = dict() for field in self._fields: try: value = dict_lookup(hit, field.field_name) except KeyError: continue else: # value is not None v, service = field.get_value_service(value) resolved_rec = field.get_dereferenced_record(service, v) if not resolved_rec: continue output = field.pick(resolved_rec) # transform field name (potentially dotted) to nested dicts # to keep the nested structure of the field d = dict() dict_set(d, field.field_name, output) # merge dict with previous results dict_merge(results, d) return results
def pre_dump(self, record, data, **kwargs): """Called before a record is dumped in a secondary storage system.""" dict_set(data, self.key, record.has_draft)