
import argparse
from bs4 import BeautifulSoup

_sites = []


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)

    @staticmethod
    def matches(url):
        raise NotImplementedError()

    def extract(self, url):
        """Download a story from a given URL

        Args:
            url (string): A valid URL for this Site
        Returns:
            story (dict) containing keys:
                title (string)
                author (string)
                chapters (list): list of tuples, in form (title, HTML, datetime)
        """
        raise NotImplementedError()

    def login(self, login_details):
        raise NotImplementedError()

    def _parse_args(self, args):
        parser = argparse.ArgumentParser()
        self._add_arguments(parser)
        return parser.parse_args(args)

    def _add_arguments(self, parser):
        pass

    def _soup(self, url, method='html5lib', **kw):
        page = self.session.get(url, **kw)
        if not page:
            raise SiteException("Couldn't fetch", url)
        return BeautifulSoup(page.text, method)

    def _new_tag(self, *args, **kw):
        soup = BeautifulSoup("", 'html5lib')
        return soup.new_tag(*args, **kw)

    def _footnote(self, contents, backlink_href=''):
        """Register a footnote and return a link to that footnote"""

        idx = len(self.footnotes) + 1

        # epub spec footnotes are all about epub:type on the footnote and the link
        # http://www.idpf.org/accessibility/guidelines/content/semantics/epub-type.php
        contents.name = 'div'
        contents.attrs['id'] = "footnote%d" % idx
        contents.attrs['epub:type'] = 'rearnote'

        # a backlink is essential for Kindle to think of this as a footnote
        # otherwise it doesn't get the inline-popup treatment
        # http://kindlegen.s3.amazonaws.com/AmazonKindlePublishingGuidelines.pdf
        # section 3.9.10
        backlink = self._new_tag('a', href="%s#noteback%d" % (backlink_href, idx))
        backlink.string = '^'
        contents.insert(0, backlink)

        self.footnotes.append(contents.prettify())

        # now build the link to the footnote to return, with appropriate
        # epub annotations.
        spoiler_link = self._new_tag('a')
        spoiler_link.attrs = {
            'id': 'noteback%d' % idx,
            'href': "footnotes.html#footnote%d" % idx,
            'epub:type': 'noteref',
        }
        spoiler_link.string = str(idx)

        return spoiler_link


class SiteException(Exception):
    pass


def register(site_class):
    _sites.append(site_class)
    return site_class


def get(url):
    for site_class in _sites:
        if site_class.matches(url):
            return site_class

# And now, the things that will use this:
from . import xenforo, fanfictionnet, deviantart, stash, ao3  # noqa

@@ -1,8 +1,30 @@
+%0Aimport glob%0Aimport os
 %0Aimport 
@@ -3021,16 +3021,17 @@
 _class%0A%0A
+%0A
 # And no
@@ -3037,99 +3037,332 @@
 ow, 
-the things that will use this:%0Afrom . import xenforo, fanfictionnet, deviantart, stash, ao3
+a particularly hacky take on a plugin system:%0A# Make an __all__ out of all the python files in this directory that don't start%0A# with __. Then import * them.%0A%0Amodules = glob.glob(os.path.join(os.path.dirname(__file__), %22*.py%22))%0A__all__ = %5Bos.path.basename(f)%5B:-3%5D for f in modules if not f.startswith(%22__%22)%5D%0A%0Afrom . import *
   # 

Make the sites system not require editing __init__.py