# __init__.py - plugin function module # # Copyright (C) 1998, 1999 Albert Hopkins (marduk) # Copyright (C) 2002 Mike W. Meyer # Copyright (C) 2005, 2006, 2007, 2009, 2011 Arthur de Jong # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # The files produced as output from the software do not automatically fall # under the copyright of the software, unless explicitly stated otherwise. """This package groups all the plugins. When generating the report each plugin is called in turn with the generate() function. Each plugin should export the following fields: generate(site) Based on the site generate all the output files as needed. __title__ A short description of the plugin that is used when linking to the output from the plugin. __author__ The author(s) of the plugin. __outputfile__ The file the plugin generates (for linking to). docstring The docstring is used as description of the plugin in the report. Pluings can use the functions exported by this module.""" import sys import time from sqlalchemy.orm import joinedload from sqlalchemy.orm.session import object_session import config import db import debugio import parsers.html # reference function from html module htmlescape = parsers.html.htmlescape def _floatformat(f): """Return a float as a string while trying to keep it within three characters.""" txt = '%.1f' % f # remove period from too long strings if len(txt) > 3: txt = txt[:txt.find('.')] return txt def get_size(i): """Return the size in bytes as a readble string.""" K = 1024 M = K * 1024 G = M * 1024 if i > 1024 * 1024 * 999: return _floatformat(float(i) / float(G)) + 'G' elif i > 1024 * 999: return _floatformat(float(i) / float(M)) + 'M' elif i >= 1024: return _floatformat(float(i) / float(K)) + 'K' else: return '%d' % i def _get_info(link): """Return a string with a summary of the information in the link.""" info = u'url: %s\n' % link.url if link.status: info += u'%s\n' % link.status if link.title: info += u'title: %s\n' % link.title.strip() if link.author: info += u'author: %s\n' % link.author.strip() if link.is_internal: info += u'internal link' else: info += u'external link' if link.yanked: info += u', not checked (%s)\n' % link.yanked else: info += u'\n' if link.redirectdepth: if link.children.count() > 0: info += u'redirect: %s\n' % link.children.first().url else: info += u'redirect (not followed)\n' count = link.count_parents if count == 1: info += u'linked from 1 page\n' elif count > 1: info += u'linked from %d pages\n' % count if link.mtime: info += u'last modified: %s\n' % time.ctime(link.mtime) if link.size: info += u'size: %s\n' % get_size(link.size) if link.mimetype: info += u'mime-type: %s\n' % link.mimetype if link.encoding: info += u'encoding: %s\n' % link.encoding for problem in link.linkproblems: info += u'problem: %s\n' % problem.message # trim trailing newline return info.strip() def make_link(link, title=None): """Return an nchor to a url with title. If url is in the Linklist and is external, insert "class=external" in the tag.""" return '%(title)s' % \ dict(url=htmlescape(link.url), target='target="_blank" ' if config.REPORT_LINKS_IN_NEW_WINDOW else '', cssclass='internal' if link.is_internal else 'external', info=htmlescape(_get_info(link)).replace('\n', ' '), title=htmlescape(title or link.title or link.url)) def print_parents(fp, link, indent=' '): """Write a list of parents to the output file descriptor. The output is indeted with the specified indent.""" # if there are no parents print nothing count = link.count_parents if not count: return parents = link.parents.order_by(db.Link.title, db.Link.url).options(joinedload(db.Link.linkproblems))[:config.PARENT_LISTLEN] fp.write( indent + '