# rptlib.py - plugin function module # # Copyright (C) 1998, 1999 Albert Hopkins (marduk) # Copyright (C) 2002 Mike Meyer # Copyright (C) 2005 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., 675 Mass Ave, Cambridge, MA 02139, USA. __version__ = '1.0' __author__ = 'mwm@mired.org' import sys import urllib import string import debugio import version import config problem_db = {} # get the stylesheet for insertion, # Note that I do it this way for two reasons. One is that Netscape reportedly # handles stylesheets better when they are inlined. Two is that people often # forget to put webcheck.css in the output directory. if config.PROXIES is None: config.PROXIES = urllib.getproxies() opener = urllib.FancyURLopener(config.PROXIES) opener.addheaders = [('User-agent','Webcheck ' + version.webcheck)] try: stylesheet = opener.open(config.STYLESHEET).read() except: stylesheet = '' def get_title(link): """Returns the title of a link if it is set otherwise returns url.""" if link.title is None: return url return link.title def make_link(url,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.""" # try to fetch the link object for this url cssclass='internal' try: global mySite link=mySite.linkMap[url] if link.external: cssclass='external' if title is None: title=link.title except KeyError: pass if title is None or title == "": title=url return ''+title+'' def add_problem(type,link): """ Add a problem link to the 'problems' database. Will not add external links """ if link.external: return global problem_db author = link.author if problem_db.has_key(author): problem_db[author].append((type,link)) else: problem_db[author]=[(type,link)] def sort_by_age(a,b): """ Sort helper for url's age. a and b are urls in linkMap """ global mySite aage, bage = mySite.linkMap[a].age, mySite.linkMap[b].age if aage < bage: return -1 elif aage == bage: return sort_by_author(a,b) else: return 1 def sort_by_rev_age(a,b): global mySite aage, bage = mySite.linkMap[a].age, mySite.linkMap[b].age if aage > bage: return -1 elif aage == bage: return sort_by_author(a,b) else: return 1 def sort_by_author(a,b): global mySite aauthor,bauthor = mySite.linkMap[a].author, mySite.linkMap[b].author if aauthor < bauthor: return -1 elif aauthor == bauthor: return 0 else: return 1 def sort_by_size(a,b): global mySite asize, bsize = mySite.linkMap[a].totalSize, mySite.linkMap[b].totalSize if asize > bsize: return -1 elif asize == bsize: return 0 else: return 1 def open_file(filename): """ given config.OUTPUT_DIR checks if the directory already exists; if not, it creates it, and then opens filename for writing and returns the file object """ import os if os.path.isdir(config.OUTPUT_DIR) == 0: os.mkdir(config.OUTPUT_DIR) fname = config.OUTPUT_DIR + filename if os.path.exists(fname) and not config.OVERWRITE_FILES: # mv: overwrite `/tmp/b'? # cp: overwrite `/tmp/b'? ow = raw_input('File %s already exists. Overwrite? y[es]/a[ll]/Q[uit] ' % fname) ow = ow.lower() + " " if ow[0] == 'a': config.OVERWRITE_FILES = 1 elif ow[0] != 'y': print 'Aborted.' sys.exit(0) return open(fname,'w') def main_index(fname, site): """ Write out the frameset. """ # FIXME: get rid of this once we have a better way of passing this information global mySite mySite=site fp = open_file(fname) fp.write('\n') fp.write('\n') fp.write('Webcheck report for "%s"\n' % get_title(site.linkMap[site.base])) fp.write('\n') fp.write('\n') fp.write('\n' % config.NAVBAR_WIDTH) fp.write('\n' \ % config.NAVBAR_FILENAME) fp.write('\n' % (config.PLUGINS[0]+'.html')) fp.write('\n') fp.write('\n') fp.close() def nav_bar(fname, site, plugins): """ Write out the navigation bar frame. """ fp=open_file(fname) # print page header fp.write('\n') fp.write('\n') fp.write('navbar\n') fp.write('\n') fp.write('\n') fp.write('\n') fp.write('\n') fp.write('
\n') fp.write('\n' \ % (config.NAVBAR_PADDING, config.NAVBAR_SPACING)) # webcheck title with link to homepage fp.write('\n' \ % (version.home, version.webcheck)) # labels pointing to each individual page for plugin in plugins: filename = plugin + '.html' fp.write('\n') # print the page footer fp.write('
\n') fp.write('Webcheck %s
\n') report = __import__('plugins.' + plugin, globals(), locals(), [plugin]) fp.write('%s\n' \ % (filename, report.__doc__, report.__title__)) fp.write('
\n') fp.write('
\n') fp.write('\n') fp.write('\n') # close file fp.close() def gen_plugins(site,plugins): """ Generate pages for plugins. """ for plugin in plugins: debugio.info(' ' + plugin) filename = plugin + '.html' report = __import__('plugins.' + plugin, globals(), locals(), [plugin]) fp = open_file(filename) doTopMain(fp,site,report) report.generate(fp,site) doBotMain(fp) fp.close() def doTopMain(fp,site,report): """ Write top part of html file for main content frame. """ fp.write('\n') fp.write('%s\n' % report.__title__) fp.write('\n') fp.write('\n') fp.write('\n') fp.write('\n' % string.split(report.__name__,'.')[1]) fp.write('\n' % (site.base, config.LOGO_HREF)) fp.write('

%s

\n' % make_link(site.base)) fp.write('\n') fp.write('\n\n\n') fp.write(' \n
%s
\n' % report.__title__) def doBotMain(fp): """ Write bottom part of html file for main content frame. """ import webcheck fp.write('
\n'); fp.write('\n' % (webcheck.start_time,version.home, version.webcheck)) fp.write('\n') fp.write('\n')