cmmit
This commit is contained in:
parent
7c25607f33
commit
5a0d446b43
4 changed files with 81 additions and 21 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
__pycache__/
|
35
main.py
35
main.py
|
@ -1,25 +1,18 @@
|
||||||
from dataclasses import dataclass
|
from notepad import Shard, Entry
|
||||||
from io import BufferedRandom
|
|
||||||
|
|
||||||
@dataclass
|
print("loading shart...")
|
||||||
class Shard:
|
shard = Shard.load_shard('myfile')
|
||||||
size: int
|
print("Size:", shard.size)
|
||||||
file: BufferedRandom
|
|
||||||
|
|
||||||
@staticmethod
|
print("now creating entry")
|
||||||
def load_shard(filename: str) -> 'Shard':
|
entry = Entry(shard)
|
||||||
file = open(filename, 'rb+')
|
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())
|
||||||
size = int.from_bytes(file.read(8), 'big')
|
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
|
shard.close()
|
||||||
class Entry:
|
|
||||||
shard: Shard
|
|
||||||
|
|
||||||
content: bytearray
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
shart = Shard.load_shard('myfile')
|
|
||||||
print(shart.size)
|
|
BIN
myfile
BIN
myfile
Binary file not shown.
66
notepad.py
Normal file
66
notepad.py
Normal file
|
@ -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
|
Loading…
Reference in a new issue