From f066fc663d674efc82f44d848e0a6a142bd9a0ab Mon Sep 17 00:00:00 2001 From: David Lynch Date: Thu, 2 Feb 2017 23:18:21 -0600 Subject: [PATCH] Use attrs --- requirements.txt | 1 + sites/__init__.py | 44 +++++++++++++++++++++++--------------------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/requirements.txt b/requirements.txt index c264135..a451cb7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +attrs==16.3.0 beautifulsoup4==4.4.1 html5lib==0.999 Pillow==3.0.0 diff --git a/sites/__init__.py b/sites/__init__.py index 768cefb..6615e75 100644 --- a/sites/__init__.py +++ b/sites/__init__.py @@ -3,31 +3,31 @@ import glob import os import argparse import uuid +import attr from bs4 import BeautifulSoup _sites = [] +def _default_uuid_string(*args): + return str(uuid.uuid4()) + + +@attr.s class Chapter: - def __init__(self, title, contents, date=False, chapterid=None): - if not chapterid: - chapterid = str(uuid.uuid4()) - self.id = chapterid - self.title = title - self.contents = contents - self.date = date + title = attr.ib() + contents = attr.ib() + date = attr.ib(default=False) + id = attr.ib(default=attr.Factory(_default_uuid_string), convert=str) +@attr.s class Section: - def __init__(self, title, author, sectionid=None): - if not sectionid: - sectionid = str(uuid.uuid4()) - self.id = sectionid - self.title = title - self.author = author - # Will contain a mix of Sections and Chapters - self.contents = [] - self.footnotes = [] + title = attr.ib() + author = attr.ib() + id = attr.ib(default=attr.Factory(_default_uuid_string), convert=str) + contents = attr.ib(default=attr.Factory(list)) + footnotes = attr.ib(default=attr.Factory(list)) def __iter__(self): return self.contents.__iter__() @@ -55,15 +55,17 @@ class Section: yield chapter.date +@attr.s class Site: """A Site handles checking whether a URL might represent a site, and then extracting the content of a story from said site. """ - def __init__(self, session, args=None): - super().__init__() - self.session = session - self.footnotes = [] - self.options = self._parse_args(args) + session = attr.ib() + args = attr.ib() + footnotes = attr.ib(default=attr.Factory(list), init=False) + + def __attrs_post_init__(self): + self.options = self._parse_args(self.args) @staticmethod def matches(url):