blob: bfd86cee61b87ff685a0947d9ee6db8c8fd6a494 [file] [log] [blame]
Fred Drake85788ed2000-10-05 05:20:55 +00001"""Miscellaneous support code shared by some of the tool scripts.
2
3This includes option parsing code, HTML formatting code, and a couple of
4useful helpers.
5
Fred Drakeca2b2e02000-10-05 05:11:57 +00006"""
7__version__ = '$Revision$'
8
9
10import getopt
Fred Drakeb258bed2001-02-12 15:30:22 +000011import string
Fred Drakeca2b2e02000-10-05 05:11:57 +000012import sys
13
14
15class Options:
16 __short_args = "a:c:ho:"
17 __long_args = [
18 # script controls
19 "columns=", "help", "output=",
20
21 # content components
22 "address=", "iconserver=",
23 "title=", "uplink=", "uptitle="]
24
25 outputfile = "-"
26 columns = 1
27 letters = 0
28 uplink = "./"
29 uptitle = "Python Documentation Index"
30
31 def __init__(self):
32 self.args = []
33 self.variables = {"address": "",
34 "iconserver": "icons",
35 "imgtype": "gif",
36 "title": "Global Module Index",
37 }
38
39 def add_args(self, short=None, long=None):
40 if short:
Fred Drakeb258bed2001-02-12 15:30:22 +000041 self.__short_args = self.__short_args + short
Fred Drakeca2b2e02000-10-05 05:11:57 +000042 if long:
Fred Drakeb258bed2001-02-12 15:30:22 +000043 self.__long_args = self.__long_args + long
Fred Drakeca2b2e02000-10-05 05:11:57 +000044
45 def parse(self, args):
46 try:
47 opts, args = getopt.getopt(args, self.__short_args,
48 self.__long_args)
49 except getopt.error:
50 sys.stdout = sys.stderr
51 self.usage()
52 sys.exit(2)
Fred Drakeb258bed2001-02-12 15:30:22 +000053 self.args = self.args + args
Fred Drakeca2b2e02000-10-05 05:11:57 +000054 for opt, val in opts:
55 if opt in ("-a", "--address"):
Fred Drakeb258bed2001-02-12 15:30:22 +000056 val = string.strip(val)
Fred Drakeca2b2e02000-10-05 05:11:57 +000057 if val:
58 val = "<address>\n%s\n</address>\n" % val
59 self.variables["address"] = val
60 elif opt in ("-h", "--help"):
61 self.usage()
62 sys.exit()
63 elif opt in ("-o", "--output"):
64 self.outputfile = val
65 elif opt in ("-c", "--columns"):
66 self.columns = int(val)
67 elif opt == "--title":
68 self.variables["title"] = val.strip()
69 elif opt == "--uplink":
70 self.uplink = val.strip()
71 elif opt == "--uptitle":
72 self.uptitle = val.strip()
73 elif opt == "--iconserver":
74 self.variables["iconserver"] = val.strip() or "."
75 else:
76 self.handle_option(opt, val)
77 if self.uplink and self.uptitle:
78 self.variables["uplinkalt"] = "up"
79 self.variables["uplinkicon"] = "up"
80 else:
81 self.variables["uplinkalt"] = ""
82 self.variables["uplinkicon"] = "blank"
83 self.variables["uplink"] = self.uplink
84 self.variables["uptitle"] = self.uptitle
85
86 def handle_option(self, opt, val):
87 raise getopt.error("option %s not recognized" % opt)
88
89 def get_header(self):
Fred Drakef10584c2001-10-22 15:07:16 +000090 s = HEAD % self.variables
91 if self.uplink:
92 if self.uptitle:
93 link = ('<link rel="up" href="%s" title="%s">'
94 % (self.uplink, self.uptitle))
95 else:
96 link = '<link rel="up" href="%s">' % self.uplink
97 repl = " %s\n</head>" % link
98 s = s.replace("</head>", repl, 1)
99 return s
Fred Drakeca2b2e02000-10-05 05:11:57 +0000100
101 def get_footer(self):
102 return TAIL % self.variables
103
104 def get_output_file(self, filename=None):
105 if filename is None:
106 filename = self.outputfile
107 if filename == "-":
108 return sys.stdout
109 else:
110 return open(filename, "w")
111
112
113NAVIGATION = '''\
114<div class="navigation">
115<table width="100%%" cellpadding="0" cellspacing="2">
116<tr>
117<td><img width="32" height="32" align="bottom" border="0" alt=""
118 src="%(iconserver)s/blank.%(imgtype)s"></td>
119<td><a href="%(uplink)s"
120 title="%(uptitle)s"><img width="32" height="32" align="bottom" border="0"
121 alt="%(uplinkalt)s"
122 src="%(iconserver)s/%(uplinkicon)s.%(imgtype)s"></a></td>
123<td><img width="32" height="32" align="bottom" border="0" alt=""
124 src="%(iconserver)s/blank.%(imgtype)s"></td>
125<td align="center" width="100%%">%(title)s</td>
126<td><img width="32" height="32" align="bottom" border="0" alt=""
127 src="%(iconserver)s/blank.%(imgtype)s"></td>
128<td><img width="32" height="32" align="bottom" border="0" alt=""
129 src="%(iconserver)s/blank.%(imgtype)s"></td>
130<td><img width="32" height="32" align="bottom" border="0" alt=""
131 src="%(iconserver)s/blank.%(imgtype)s"></td>
132</tr></table>
133<b class="navlabel">Up:</b> <span class="sectref"><a href="%(uplink)s"
134 title="%(uptitle)s">%(uptitle)s</A></span>
135<br></div>
136'''
137
138HEAD = '''\
139<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
140<html>
141<head>
142 <title>%(title)s</title>
143 <meta name="description" content="%(title)s">
144 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
145 <link rel="STYLESHEET" href="lib/lib.css">
146</head>
147<body>
148''' + NAVIGATION + '''\
149<hr>
150
151<h2>%(title)s</h2>
152
153'''
154
155TAIL = "<hr>\n" + NAVIGATION + '''\
156%(address)s</body>
157</html>
158'''