append(): Fixing the test for convertability after consultation with
[python/dscho.git] / Doc / tools / support.py
blobbc17c6eccc69d349ae4ff9073b7624d4ab1bdedd
1 """Miscellaneous support code shared by some of the tool scripts.
3 This includes option parsing code, HTML formatting code, and a couple of
4 useful helpers.
6 """
7 __version__ = '$Revision$'
10 import getopt
11 import string
12 import sys
15 class Options:
16 __short_args = "a:c:ho:"
17 __long_args = [
18 # script controls
19 "columns=", "help", "output=",
21 # content components
22 "address=", "iconserver=",
23 "title=", "uplink=", "uptitle="]
25 outputfile = "-"
26 columns = 1
27 letters = 0
28 uplink = "index.html"
29 uptitle = "Python Documentation Index"
31 # The "Aesop Meta Tag" is poorly described, and may only be used
32 # by the Aesop search engine (www.aesop.com), but doesn't hurt.
34 # There are a number of values this may take to roughly categorize
35 # a page. A page should be marked according to its primary
36 # category. Known values are:
37 # 'personal' -- personal-info
38 # 'information' -- information
39 # 'interactive' -- interactive media
40 # 'multimedia' -- multimedia presenetation (non-sales)
41 # 'sales' -- sales material
42 # 'links' -- links to other information pages
44 # Setting the aesop_type value to one of these strings will cause
45 # get_header() to add the appropriate <meta> tag to the <head>.
47 aesop_type = None
49 def __init__(self):
50 self.args = []
51 self.variables = {"address": "",
52 "iconserver": "icons",
53 "imgtype": "gif",
54 "title": "Global Module Index",
57 def add_args(self, short=None, long=None):
58 if short:
59 self.__short_args = self.__short_args + short
60 if long:
61 self.__long_args = self.__long_args + long
63 def parse(self, args):
64 try:
65 opts, args = getopt.getopt(args, self.__short_args,
66 self.__long_args)
67 except getopt.error:
68 sys.stdout = sys.stderr
69 self.usage()
70 sys.exit(2)
71 self.args = self.args + args
72 for opt, val in opts:
73 if opt in ("-a", "--address"):
74 val = string.strip(val)
75 if val:
76 val = "<address>\n%s\n</address>\n" % val
77 self.variables["address"] = val
78 elif opt in ("-h", "--help"):
79 self.usage()
80 sys.exit()
81 elif opt in ("-o", "--output"):
82 self.outputfile = val
83 elif opt in ("-c", "--columns"):
84 self.columns = int(val)
85 elif opt == "--title":
86 self.variables["title"] = val.strip()
87 elif opt == "--uplink":
88 self.uplink = val.strip()
89 elif opt == "--uptitle":
90 self.uptitle = val.strip()
91 elif opt == "--iconserver":
92 self.variables["iconserver"] = val.strip() or "."
93 else:
94 self.handle_option(opt, val)
95 if self.uplink and self.uptitle:
96 self.variables["uplinkalt"] = "up"
97 self.variables["uplinkicon"] = "up"
98 else:
99 self.variables["uplinkalt"] = ""
100 self.variables["uplinkicon"] = "blank"
101 self.variables["uplink"] = self.uplink
102 self.variables["uptitle"] = self.uptitle
104 def handle_option(self, opt, val):
105 raise getopt.error("option %s not recognized" % opt)
107 def get_header(self):
108 s = HEAD % self.variables
109 if self.uplink:
110 if self.uptitle:
111 link = ('<link rel="up" href="%s" title="%s">'
112 % (self.uplink, self.uptitle))
113 else:
114 link = '<link rel="up" href="%s">' % self.uplink
115 repl = " %s\n</head>" % link
116 s = s.replace("</head>", repl, 1)
117 if self.aesop_type:
118 meta = '\n <meta name="aesop" content="%s">'
119 # Insert this in the middle of the head that's been
120 # generated so far, keeping <meta> and <link> elements in
121 # neat groups:
122 s = s.replace("<link ", meta + "<link ", 1)
123 return s
125 def get_footer(self):
126 return TAIL % self.variables
128 def get_output_file(self, filename=None):
129 if filename is None:
130 filename = self.outputfile
131 if filename == "-":
132 return sys.stdout
133 else:
134 return open(filename, "w")
137 NAVIGATION = '''\
138 <div class="navigation">
139 <table width="100%%" cellpadding="0" cellspacing="2">
140 <tr>
141 <td><img width="32" height="32" align="bottom" border="0" alt=""
142 src="%(iconserver)s/blank.%(imgtype)s"></td>
143 <td><a href="%(uplink)s"
144 title="%(uptitle)s"><img width="32" height="32" align="bottom" border="0"
145 alt="%(uplinkalt)s"
146 src="%(iconserver)s/%(uplinkicon)s.%(imgtype)s"></a></td>
147 <td><img width="32" height="32" align="bottom" border="0" alt=""
148 src="%(iconserver)s/blank.%(imgtype)s"></td>
149 <td align="center" width="100%%">%(title)s</td>
150 <td><img width="32" height="32" align="bottom" border="0" alt=""
151 src="%(iconserver)s/blank.%(imgtype)s"></td>
152 <td><img width="32" height="32" align="bottom" border="0" alt=""
153 src="%(iconserver)s/blank.%(imgtype)s"></td>
154 <td><img width="32" height="32" align="bottom" border="0" alt=""
155 src="%(iconserver)s/blank.%(imgtype)s"></td>
156 </tr></table>
157 <b class="navlabel">Up:</b> <span class="sectref"><a href="%(uplink)s"
158 title="%(uptitle)s">%(uptitle)s</A></span>
159 <br></div>
162 HEAD = '''\
163 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
164 <html>
165 <head>
166 <title>%(title)s</title>
167 <meta name="description" content="%(title)s">
168 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
169 <link rel="STYLESHEET" href="lib/lib.css">
170 </head>
171 <body>
172 ''' + NAVIGATION + '''\
173 <hr>
175 <h2>%(title)s</h2>
179 TAIL = "<hr>\n" + NAVIGATION + '''\
180 %(address)s</body>
181 </html>