Browse Source

some minimail implementation

PYADPT-55
Volker Birk 6 years ago
parent
commit
53ae834511
  1. 52
      test/minimail.py
  2. 6
      test/sync_test.py

52
test/minimail.py

@ -6,15 +6,61 @@
# this file is under GNU General Public License 3.0
import pEp
from secrets import token_urlsafe
from itertools import compress
from functools import partial
from time import sleep
def unlock(inbox):
lockfile = inbox / "lock"
try:
lockfile.unlink()
except:
pass
class Lock:
def __init__(self, inbox):
self.inbox = inbox
def __exit__(self, *exc):
unlock(self.inbox)
def __enter__(self):
lockfile = self.inbox / "lock"
while lockfile.is_file():
sleep(1)
lockfile.touch()
def send(inbox, msg):
with Lock(inbox):
name = token_urlsafe(16) + ".eml"
with open(inbox / name, "wb") as f:
f.write(str(msg).encode())
def recv(inbox):
pass
def newer(file1, file2):
if not file2.is_file():
return False
elif not file1.is_file():
return True
stat1 = file1.stat()
stat2 = file2.stat()
return stat2.st_mtime > stat1.st_mtime
def recv_all(inbox, marker):
with Lock(inbox):
r = []
while not r:
for f in compress(inbox.glob("*.eml"), partial(newer, file1=marker)):
t = f.readall()
r.append(t)
if not r:
sleep(1)
marker.touch(exist_ok=True)
return r

6
test/sync_test.py

@ -60,7 +60,6 @@ EINTR = 4
if __name__ == "__main__":
from optparse import OptionParser
from multiprocessing import Process
optParser = OptionParser()
optParser.add_option("-c", "--clean", action="store_true", dest="clean",
@ -68,11 +67,16 @@ if __name__ == "__main__":
options, args = optParser.parse_args()
if options.clean:
from minimail import unlock
rmrf("TestInbox")
unlock(pathlib.Path("TestInbox"))
rmrf("Alice")
rmrf("Barbara")
else:
from multiprocessing import Process
os.makedirs("TestInbox", exist_ok=True)
setup("Alice")
setup("Barbara")

Loading…
Cancel
Save