Пример #1
0
    def _load_and_index(self, d, object_index, substitute=False):
        """
        Load the class based on the type string and index it, if a BaseEntity.

        This function is used as the object hook when deserializing taurus objects

        :param d: dictionary to try to load into a registered class instance
        :param object_index: to add the object to if it is a BaseEntity
        :param substitute: whether to substitute LinkByUIDs when they are found in the index
        :return: the deserialized object, or the input dict if it wasn't recognized
        """
        if "type" not in d:
            return d
        typ = d.pop("type")

        if typ in self._clazz_index:
            clz = self._clazz_index[typ]
            obj = clz.from_dict(d)
        elif typ == LinkByUID.typ:
            obj = LinkByUID.from_dict(d)
            if substitute and (obj.scope.lower(), obj.id) in object_index:
                return object_index[(obj.scope.lower(), obj.id)]
            return obj
        else:
            raise TypeError("Unexpected base object type: {}".format(typ))

        if isinstance(obj, BaseEntity):
            for (scope, uid) in obj.uids.items():
                object_index[(scope.lower(), uid)] = obj
        return obj
Пример #2
0
def _loado(d, index):
    if "type" not in d:
        return d
    typ = d.pop("type")

    if typ in _clazz_index:
        clz = _clazz_index[typ]
        obj = clz.from_dict(d)
    elif typ == LinkByUID.typ:
        obj = LinkByUID.from_dict(d)
        return obj
    else:
        raise TypeError("Unexpected base object type: {}".format(typ))

    if isinstance(obj, BaseEntity):
        for (scope, id) in obj.uids.items():
            index[(scope.lower(), id)] = obj
    return obj