"""$URL: svn://svn/repos/trunk/sitereaper/lib/link_report.py $
$Id: link_report.py 20952 2003-03-05 16:51:11Z dbinger $

Provides the LinkReport class, which exists mainly to make
accessing the link report pickles easier.
"""

class LinkReport:
    """

    Instance attributes:
      links_to : { string : [string] }
        maps the a URL of a page to the list of URLs that the page links to
      linked_from : { string : [string] }
        maps the URL of a page to the list of URLs of pages that link
        to that page

    """

    def __init__ (self):
        self.links_to = {}
        self.linked_from = {}

    def record_link (self, referring_url, target_url):
        self.links_to.setdefault(referring_url, [])
        self.linked_from.setdefault(target_url, [])

        self.links_to[referring_url].append(target_url)
        self.linked_from[target_url].append(referring_url)

    def report_targets (self, url, indent=""):
        for url in self.links_to.get(url, []):
            print indent + url

    def report_referrers (self, url, indent=""):
        for url in self.linked_from.get(url, []):
            print indent + url

    def report (self, url):
        print url
        print " links to:"
        self.report_targets(url, indent="  ")
        print " linked from:"
        self.report_referrers(url, indent="  ")
