# Path: webcheck/db.py
# class Link(Base):
# class LinkProblem(Base):
# class PageProblem(Base):
# class Anchor(Base):
# class RequestedAnchor(Base):
#     def clean_url(url):
#     def get_or_create(session, url):
#     def _get_child(self, url):
#     def _mk_unicode(self, message):
#     def set_encoding(self, encoding):
#     def add_redirect(self, url):
#     def add_linkproblem(self, message):
#     def add_pageproblem(self, message):
#     def add_child(self, url):
#     def add_embed(self, url):
#     def add_anchor(self, anchor):
#     def add_reqanchor(self, parent, anchor):
#     def follow_link(self, visited=None):
#     def count_parents(self):
#     def parents(self):
#     def __unicode__(self):
#     def __unicode__(self):
#     def __unicode__(self):
#     def __unicode__(self):
# def setup_db(filename):
# def truncate_db():
# 
# Path: webcheck/output.py
# def render(output_file, **kwargs):
#     """Render the output file with the specified context variables."""
#     kwargs.setdefault('webcheck', webcheck)
#     kwargs.setdefault('output_file', output_file)
#     kwargs.setdefault('time', time.ctime(time.time()))
#     kwargs.setdefault('Link', Link)
#     kwargs.setdefault('config', config)
#     template = env.get_template(output_file)
#     fp = open_file(output_file)
#     fp.write(template.render(**kwargs))
#     fp.close()


from sqlalchemy.orm import joinedload
from webcheck.db import Session, Link
from webcheck.output import render
#
# The files produced as output from the software do not automatically fall
# under the copyright of the software, unless explicitly stated otherwise.

"""Present a listing of links that point to non-existent pages."""

__title__ = 'bad links'
__author__ = 'Arthur de Jong'
__outputfile__ = 'badlinks.html'




def postprocess(crawler):
    """Add all bad links as pageproblems on pages where they are linked."""
    session = Session()
    # find all links with link problems
    links = session.query(Link).filter(Link.linkproblems.any()).options(joinedload(Link.linkproblems))
    # TODO: probably make it a nicer query over all linkproblems
    for link in links:
        # add a reference to the problem map
        for problem in link.linkproblems:
            for parent in link.parents:
                parent.add_pageproblem('bad link: %s: %s' % (link.url, problem))
    session.commit()
    session.close()


def generate(crawler):
    """Present the list of bad links."""

# Path: webcheck/db.py
# class Link(Base):
# class LinkProblem(Base):
# class PageProblem(Base):
# class Anchor(Base):
# class RequestedAnchor(Base):
#     def clean_url(url):
#     def get_or_create(session, url):
#     def _get_child(self, url):
#     def _mk_unicode(self, message):
#     def set_encoding(self, encoding):
#     def add_redirect(self, url):
#     def add_linkproblem(self, message):
#     def add_pageproblem(self, message):
#     def add_child(self, url):
#     def add_embed(self, url):
#     def add_anchor(self, anchor):
#     def add_reqanchor(self, parent, anchor):
#     def follow_link(self, visited=None):
#     def count_parents(self):
#     def parents(self):
#     def __unicode__(self):
#     def __unicode__(self):
#     def __unicode__(self):
#     def __unicode__(self):
# def setup_db(filename):
# def truncate_db():
# 
# Path: webcheck/output.py
# def render(output_file, **kwargs):
#     """Render the output file with the specified context variables."""
#     kwargs.setdefault('webcheck', webcheck)
#     kwargs.setdefault('output_file', output_file)
#     kwargs.setdefault('time', time.ctime(time.time()))
#     kwargs.setdefault('Link', Link)
#     kwargs.setdefault('config', config)
#     template = env.get_template(output_file)
#     fp = open_file(output_file)
#     fp.write(template.render(**kwargs))
#     fp.close()


# Path: webcheck/plugins/badlinks.py
from sqlalchemy.orm import joinedload
from webcheck.db import Session, Link
from webcheck.output import render

#
# The files produced as output from the software do not automatically fall
# under the copyright of the software, unless explicitly stated otherwise.

"""Present a listing of links that point to non-existent pages."""

__title__ = 'bad links'
__author__ = 'Arthur de Jong'
__outputfile__ = 'badlinks.html'




def postprocess(crawler):
    """Add all bad links as pageproblems on pages where they are linked."""
    session = Session()
    # find all links with link problems
    links = session.query(Link).filter(Link.linkproblems.any()).options(joinedload(Link.linkproblems))
    # TODO: probably make it a nicer query over all linkproblems
    for link in links:
        # add a reference to the problem map
        for problem in link.linkproblems:
            for parent in link.parents:
                parent.add_pageproblem('bad link: %s: %s' % (link.url, problem))
    session.commit()
    session.close()


def generate(crawler):
    """Present the list of bad links."""

    session = Session()