From dbf3649583d39f2c5993dc08528ab30373c2fe9f Mon Sep 17 00:00:00 2001 From: Wesley Kerfoot Date: Fri, 7 Jun 2019 01:07:05 -0400 Subject: [PATCH] Refactor in progress --- deletefb/tools/archive.py | 57 +++++++++++++++++++++++++++++++++++++++ deletefb/tools/common.py | 42 ----------------------------- 2 files changed, 57 insertions(+), 42 deletions(-) create mode 100644 deletefb/tools/archive.py diff --git a/deletefb/tools/archive.py b/deletefb/tools/archive.py new file mode 100644 index 0000000..107ee38 --- /dev/null +++ b/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)) diff --git a/deletefb/tools/common.py b/deletefb/tools/common.py index 8cc4385..a831fc3 100644 --- a/deletefb/tools/common.py +++ b/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