Ejemplo n.º 1
0
    def map(self, record, entities, **kwargs):
        kwargs.update(self.data)

        if self.entity is not None:
            entity = entities.get(self.entity)
            if entity is not None:
                return [entity.get('id')]
            return []

        # clean the values returned by the query, or by using literals, or
        # formats.
        values = []
        for value in self.record_values(record):
            value = self.type.clean(value, **kwargs)
            if value is not None:
                values.append(value)

        if self.join is not None:
            values = [self.join.join(values)]

        if self.split is not None:
            splote = []
            for value in values:
                splote = splote + value.split(self.split)
            values = splote

        return unique_list(values)
Ejemplo n.º 2
0
def merge_context(left, right):
    """When merging two entities, we make lists of all the
    duplicate context keys."""
    combined = {}
    keys = [*left.keys(), *right.keys()]
    for key in set(keys):
        lval = ensure_list(left.get(key))
        rval = ensure_list(right.get(key))
        combined[key] = unique_list([*lval, *rval])
    return combined
Ejemplo n.º 3
0
def merge_context(left: Dict[K, V], right: Dict[K, V]) -> Dict[K, List[V]]:
    """When merging two entities, make lists of all the duplicate context
    keys."""
    combined = {}
    keys = [*left.keys(), *right.keys()]
    for key in set(keys):
        lval: List[V] = [i for i in ensure_list(left.get(key)) if i is not None]
        rval: List[V] = [i for i in ensure_list(right.get(key)) if i is not None]
        combined[key] = unique_list([*lval, *rval])
    return combined
Ejemplo n.º 4
0
    def get_values(self, record):
        values = []
        if self.template is not None:
            values.append(self.formatter.apply(record))
        else:
            for ref in self.refs:
                values.append(record.get(ref))
        values.extend(self.literals)
        values = [self.schema.type.clean(v, record, self.data) for v in values]
        values = [v for v in values if v is not None]

        if self.join is not None:
            values = [self.join.join(values)]

        return unique_list(values)
Ejemplo n.º 5
0
def merge_docs(old, new):
    """Exend the values of the new doc with extra values from the old."""
    old = clean_dict(old)
    new = dict(clean_dict(new))
    for k, v in old.items():
        if k == 'created_at':
            new[k] = v
        elif k in new:
            if is_sequence(v):
                v = new[k] + v
                new[k] = unique_list(v)
            elif isinstance(v, dict):
                new[k] = merge_docs(v, new[k])
        else:
            new[k] = v
    return new
Ejemplo n.º 6
0
def merge_data(old, new):
    """Extend the values of the new doc with extra values from the old."""
    if is_sequence(old) or is_sequence(new):
        new = ensure_list(new)
        new.extend(ensure_list(old))
        return unique_list(new)
    if is_mapping(old) or is_mapping(new):
        old = old if is_mapping(old) else {}
        new = new if is_mapping(new) else {}
        keys = set(new.keys())
        keys.update(old.keys())
        combined = {}
        for key in keys:
            value = merge_data(old.get(key), new.get(key))
            if value is not None:
                combined[key] = value
        return combined
    return new or old