Browse Source

Refactor in progress

pull/53/head
Wesley Kerfoot 5 years ago
parent
commit
dbf3649583
  1. 57
      deletefb/tools/archive.py
  2. 42
      deletefb/tools/common.py

57
deletefb/tools/archive.py

@ -0,0 +1,57 @@
import attr
import datetime
import uuid
import json
from contextlib import contextmanager
from pathlib import Path
# Used to avoid duplicates in the log
from pybloom_live import BloomFilter
def acquire_path():
log_file = open(log_path, mode="ta", buffering=1)
bfilter = BloomFilter(
capacity=settings["MAX_POSTS"],
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=uuid.uuid4)
@attr.s
class Comment:
commenter = attr.ib()
content = attr.ib()
date = attr.ib(factory=datetime.datetime.now)
name = attr.ib(factory=uuid.uuid4)
@attr.s
class Archive:
archive_type = attr.ib()
# We give the Archive class a file handle
# This is better because the archive function
# should not know about anything related to filesystem paths
archive_file = attr.ib()
def archive(self, content):
# do something
print("Archiving type {0} with content {1} to directory {2}".format(self.archive_type, content, self.archive_dir))
self.archive_file.write(json.dumps(content.asdict()))
wall_archive = Archive(archive_type="wall")
comments = [Comment(commenter="Bob", content="Nice post!"),
Comment(commenter="Alice", content="I hate this")]
wall_archive.archive(Post(content="A post!", comments=comments))

42
deletefb/tools/common.py

@ -6,9 +6,6 @@ import time
from .config import settings
# Used to avoid duplicates in the log
from pybloom_live import BloomFilter
from os.path import abspath, relpath, split, isfile
from selenium.common.exceptions import (
NoSuchElementException,
@ -52,45 +49,6 @@ def logger(name):
logging.config.dictConfig(config["logging"])
return logging.getLogger(name)
def archiver(category):
"""
Log content to file. Call using `archive("some content")`
Args:
category: str The category of logs you want to log
Returns:
(log_file_handle, archiver)
"""
log_path = "{0}.log".format(abspath(relpath(split(category)[-1], ".")))
log_file = open(log_path, mode="ta", buffering=1)
bfilter = BloomFilter(
capacity=settings["MAX_POSTS"],
error_rate=0.001
)
def log(content, timestamp=False):
if not settings["ARCHIVE"]:
return
if content in bfilter:
# This was already archived
return
structured_content = {
"category" : category,
"content" : content,
"timestamp" : timestamp
}
log_file.write("{0}\n".format(json.dumps(structured_content)))
bfilter.add(content)
return (log_file, log)
NO_CHROME_DRIVER = """
You need to install the chromedriver for Selenium\n

Loading…
Cancel
Save