diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba0430d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__/ \ No newline at end of file diff --git a/main.py b/main.py index 69e1224..a2d3c4d 100644 --- a/main.py +++ b/main.py @@ -1,25 +1,18 @@ -from dataclasses import dataclass -from io import BufferedRandom +from notepad import Shard, Entry -@dataclass -class Shard: - size: int - file: BufferedRandom +print("loading shart...") +shard = Shard.load_shard('myfile') +print("Size:", shard.size) - @staticmethod - def load_shard(filename: str) -> 'Shard': - file = open(filename, 'rb+') - size = int.from_bytes(file.read(8), 'big') +print("now creating entry") +entry = Entry(shard) +entry.add_content("Lorm Ipsum dolor sit ammet, consectetuer adipiscng elit, We're no strangers to luv You know the rules and so do I A full commitment's wat I'm thinkin of You wouldn't get this from any other guy very good very nice".encode()) +print("entry created, its length:", len(entry.to_bytes()), "now writing it") - return Shard(size, file) +try: + shard.write_entry(entry) + print("entry written") +except EOFError: + print("oops, looks like the file wont fit this") -@dataclass -class Entry: - shard: Shard - - content: bytearray - - - -shart = Shard.load_shard('myfile') -print(shart.size) \ No newline at end of file +shard.close() \ No newline at end of file diff --git a/myfile b/myfile deleted file mode 100644 index 98222a1..0000000 Binary files a/myfile and /dev/null differ diff --git a/notepad.py b/notepad.py new file mode 100644 index 0000000..14d8315 --- /dev/null +++ b/notepad.py @@ -0,0 +1,66 @@ +from dataclasses import dataclass, field +from io import BufferedRandom +from os.path import getsize +from time import time + +@dataclass +class Shard: + size: int + file: BufferedRandom + file_size: int + + @staticmethod + def load_shard(filename: str) -> 'Shard': + file = open(filename, 'r+b') + size = int.from_bytes(file.read(8), 'big') + + file.seek(0, 2) + file_size = file.tell() + + return Shard(size, file, file_size) + + def write_entry(self, entry: 'Entry'): + data = entry.to_bytes() + data_length = len(data) + + if self.size + 1 + data_length > self.file_size: + raise EOFError() + + self.file.seek(self.size + 1) + self.file.write(data) + + self.size += data_length + self.file.seek(0) + self.file.write(self.size.to_bytes(8, 'big')) + + def close(self): + self.file.close() + + +@dataclass +class Entry: + shard: Shard + timestamp_start: int = field(default_factory=lambda: int(time())) + timestamp_end: int = field(default_factory=lambda: int(time())) + content: bytes = field(default_factory=bytes) + + @staticmethod + def from_bytes(shard: Shard, data: BufferedRandom) -> 'Entry': + timestamp_start = int.from_bytes(data.read(8), 'big') + timestamp_end = int.from_bytes(data.read(8), 'big') + content_length = int.from_bytes(data.read(8), 'big') + content = data.read(content_length) + + return Entry(shard, timestamp_start, timestamp_end, content) + + def to_bytes(self) -> bytes: + data = self.timestamp_start.to_bytes(8, 'big') + data += self.timestamp_end.to_bytes(8, 'big') + data += len(self.content).to_bytes(8, 'big') + data += self.content + + return data + + def add_content(self, content: bytes): + self.timestamp_end = int(time()) + self.content += content