Unverified Commit 35344012 authored by Martin Prudek's avatar Martin Prudek 🌀
Browse files

dump: Second version

parent 5f7cdbd7
......@@ -2,27 +2,26 @@ import json
import msgpack
class Storage:
class Dump:
def __init__(self, path):
self.path = path
self.file = open(path, "a+b")
self.unpacker = msgpack.Unpacker(self.file, raw=False)
self.read_position = 0
self.write_position = self.file.tell()
class DumpStorage(Storage):
def __init__(self, path):
super().__init__(path)
def dump(self, msg_type, payload):
def dump_message(self, msg_type, payload):
msg = {
"msg_type": msg_type,
"payload": payload,
}
j = json.dumps(msg)
with open(self.path, "ab") as f:
f.write(msgpack.packb(j, use_bin_type=True))
self.file.write(msgpack.packb(j, use_bin_type=True))
self.file.flush()
self.write_position = self.file.tell()
class ReplayStorage(Storage):
def __init__(self, path):
super().__init__(path)
f = open(self.path, "rb")
self.unpacker = msgpack.Unpacker(f, raw=False)
def load_message(self):
self.file.seek(self.read_position)
msg = json.loads(self.unpacker.unpack())
self.read_position = self.file.tell()
self.file.seek(self.write_position)
return msg
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment