Ejemplo n.º 1
0
    def read(self):
        ret = {}
        if not os.path.isdir(self.storage):
            return ret
        files = os.listdir(self.storage)
        if len(files) is 0:
            return ret
        for f in files:
            file = open(self.storage + "/" + f, "rb")
            json_obj = pickle.load(file)
            file.close()
            qos = json_obj["qos"]
            path = base64_decode(f)
            sub = {
                "path": path,
                "qos": qos
            }
            ret[path] = sub
            if qos is 2:
                value = Value()
                value.type = json_obj["type"]
                value.updated_at = json_obj["ts"]
                value.value = json_obj["value"]
                self.store(sub, value)
                self.update_cache[path] = value
            elif qos is 3:
                try:
                    import_queue = json_obj["queue"]
                except KeyError:
                    continue

                if path not in self.updates_cache or sub["path"] not in self.updates_cache:
                    queue = []
                elif path in self.updates_cache:
                    queue = self.updates_cache[path]
                elif sub["path"] in self.updates_cache:
                    queue = self.updates_cache[sub["path"]]
                else:
                    continue

                for array in import_queue:
                    value = Value()
                    value.type = array[0]
                    value.updated_at = array[1]
                    value.value = array[2]
                    queue.append(value)
        return ret