Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/plugins/rptlib.py
blob: 33dc936c479049f697972341b6d2e2e44cc850f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196

# rptlib.py - plugin function module
#
# Copyright (C) 1998, 1999 Albert Hopkins (marduk) <marduk@python.net>
# Copyright (C) 2002 Mike Meyer <mwm@mired.org>
# Copyright (C) 2005 Arthur de Jong <arthur@tiefighter.et.tudelft.nl>
#
# 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

__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.
# TODO: move fetching files to main
# TODO: use scheme system for opening files
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 <a>nchor to a url with title. If url is in the Linklist and
    is external, insert "class=external" in the <a> 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 '<a href="'+url+'" class="'+cssclass+'">'+title+'</a>'

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 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('<html>\n')
    fp.write('<head>\n')
    fp.write('<title>webcheck report for "%s"</title>\n' % get_title(site.linkMap[site.base]))
    fp.write('<style type="text/css">\n')
    fp.write('<!-- /* hide from old browsers */\n')
    fp.write(stylesheet+'\n')
    fp.write(' --> </style>\n')
    fp.write('</head>\n')
    fp.write('<frameset COLS="%s,*" border=0 framespacing=0>\n' % config.NAVBAR_WIDTH)
    fp.write('<frame name="navbar" src="%s" marginwidth=0 marginheight=0 frameborder=0>\n' \
             % config.NAVBAR_FILENAME)
    fp.write('<frame name="main" src="%s" frameborder=0>\n' % (config.PLUGINS[0]+'.html'))
    fp.write('</frameset>\n')
    fp.write('</html>\n')
    fp.close()

def nav_bar(fname, site, plugins):
    """ Write out the navigation bar frame. """
    fp=open_file(fname)
    # print page header
    fp.write('<html>\n')
    fp.write('<head>\n')
    fp.write('<title>navbar</title>\n')
    fp.write('<style type="text/css">\n')
    fp.write('<!-- /* hide from old browsers */\n')
    fp.write(stylesheet+'\n')
    fp.write(' --> </style>\n')
    fp.write('<base target="main">\n')
    fp.write('</head>\n')
    fp.write('<body class="navbar">\n')
    fp.write('<div align="center">\n')
    fp.write('<table cellpadding="%s" cellspacing="%s">\n' \
             % (config.NAVBAR_PADDING, config.NAVBAR_SPACING))
    # webcheck title with link to homepage
    fp.write('<tr><th class="home">\n')
    fp.write('<a target="_top" href="%s" onMouseOver="window.status=\'webcheck Home Page\'; return true;">webcheck %s</a></th></tr>\n' \
             % (version.home, version.webcheck))
    # labels pointing to each individual page
    for plugin in plugins:
        filename = plugin + '.html'
        fp.write('<tr><th>\n')
        report = __import__('plugins.' + plugin, globals(), locals(), [plugin])
        fp.write('<strong><a href="%s" onMouseOver="window.status=\'%s\'; return true">%s</a></strong>\n' \
              % (filename, report.__doc__, report.__title__))
        fp.write('</th></tr>\n')
    # print the page footer
    fp.write('</table>\n')
    fp.write('</div>\n')
    fp.write('</body>\n')
    fp.write('</html>\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('<html>\n')
    fp.write('<head><title>%s</title>\n' % report.__title__)
    fp.write('<style type="text/css">\n')
    fp.write('<!-- /* hide from old browsers */\n')
    fp.write(stylesheet+'\n')
    fp.write(' --> </style>\n')
    fp.write('<meta name="Generator" content="webcheck ' + version.webcheck + '">\n')
    fp.write('</head>\n')
    fp.write('<body class="%s">\n' % string.split(report.__name__,'.')[1])
    fp.write('<p class="logo"><a href="%s"><img src="%s" border="0" alt=""></a></p>\n' % (site.base, config.LOGO_HREF))
    fp.write('<h1 class="basename">%s</h1>\n' % make_link(site.base))
    fp.write('</h1>\n')
    fp.write('\n\n<table width="100%" cellpadding="4">\n')
    fp.write('  <tr><th class="title">%s</th></tr>\n</table>\n' % report.__title__)

def doBotMain(fp):
    """ Write bottom part of html file for main content frame. """
    import webcheck
    fp.write('<hr>\n');
    fp.write('<p class="footer">\n')
    fp.write('<em>Generated %s by <a target="_top" href="%s">webcheck %s</a></em></p>\n' % (webcheck.start_time,version.home, version.webcheck))
    fp.write('</body>\n')
    fp.write('</html>\n')