|
@ -1,40 +1,18 @@ |
|
|
import attr |
|
|
|
|
|
import datetime |
|
|
|
|
|
import uuid |
|
|
|
|
|
import json |
|
|
|
|
|
|
|
|
|
|
|
from contextlib import contextmanager |
|
|
from contextlib import contextmanager |
|
|
from pathlib import Path |
|
|
from pathlib import Path |
|
|
|
|
|
|
|
|
|
|
|
import attr |
|
|
|
|
|
import json |
|
|
|
|
|
|
|
|
# Used to avoid duplicates in the log |
|
|
# Used to avoid duplicates in the log |
|
|
from pybloom_live import BloomFilter |
|
|
from pybloom_live import BloomFilter |
|
|
|
|
|
|
|
|
def acquire_path(): |
|
|
def make_filter(): |
|
|
log_file = open(log_path, mode="ta", buffering=1) |
|
|
return BloomFilter( |
|
|
|
|
|
|
|
|
bfilter = BloomFilter( |
|
|
|
|
|
capacity=settings["MAX_POSTS"], |
|
|
capacity=settings["MAX_POSTS"], |
|
|
error_rate=0.001 |
|
|
error_rate=0.001 |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
|
|
@attr.s |
|
|
|
|
|
class Post: |
|
|
|
|
|
content = attr.ib() |
|
|
|
|
|
comments = attr.ib(default=[]) |
|
|
|
|
|
date = attr.ib(factory=datetime.datetime.now) |
|
|
|
|
|
name = attr.ib(factory=lambda: uuid.uuid4().hex) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@attr.s |
|
|
|
|
|
class Comment: |
|
|
|
|
|
commenter = attr.ib() |
|
|
|
|
|
content = attr.ib() |
|
|
|
|
|
date = attr.ib(factory=datetime.datetime.now) |
|
|
|
|
|
name = attr.ib(factory=lambda: uuid.uuid4().hex) |
|
|
|
|
|
|
|
|
|
|
|
@attr.s |
|
|
@attr.s |
|
|
class Archive: |
|
|
class Archive: |
|
|
archive_type = attr.ib() |
|
|
archive_type = attr.ib() |
|
@ -44,25 +22,29 @@ class Archive: |
|
|
# should not know about anything related to filesystem paths |
|
|
# should not know about anything related to filesystem paths |
|
|
archive_file = attr.ib() |
|
|
archive_file = attr.ib() |
|
|
|
|
|
|
|
|
|
|
|
_bloom_filter = attr.ib(factory=make_filter) |
|
|
|
|
|
|
|
|
def archive(self, content): |
|
|
def archive(self, content): |
|
|
# do something |
|
|
""" |
|
|
print("Archiving type {0} with content {1} to file".format(self.archive_type, content)) |
|
|
Archive an object |
|
|
self.archive_file.write(json.dumps(attr.asdict(content))) |
|
|
""" |
|
|
|
|
|
print("Archiving {0}".format(content)) |
|
|
|
|
|
|
|
|
def close(self): |
|
|
if content.name not in self._bloom_filter: |
|
|
self.archive_file.close() |
|
|
self.archive_file.write(json.dumps(attr.asdict(content))) |
|
|
|
|
|
self._bloom_filter.add(content.name) |
|
|
|
|
|
return |
|
|
|
|
|
|
|
|
@contextmanager |
|
|
@contextmanager |
|
|
def archiver(archive_type): |
|
|
def archiver(archive_type): |
|
|
archiver_instance = Archive(archive_type=archive_type, |
|
|
archive_file = open("./%s.log" % archive_type, mode="ta", buffering=1) |
|
|
archive_file=open( |
|
|
|
|
|
"./%s.log" % archive_type, |
|
|
archiver_instance = Archive( |
|
|
mode="ta", |
|
|
archive_type=archive_type, |
|
|
buffering=1 |
|
|
archive_file=archive_file |
|
|
) |
|
|
|
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
try: |
|
|
try: |
|
|
yield archiver_instance |
|
|
yield archiver_instance |
|
|
finally: |
|
|
finally: |
|
|
archiver_instance.close() |
|
|
archive_file.close() |
|
|