This commit was manufactured by cvs2svn to create tag 'r234c1'.
[python/dscho.git] / Doc / tools / support.py
blobcd42fd0060d46a630eec50178149bdf161791229
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 os.path
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=", "favicon=",
23 "title=", "uplink=", "uptitle="]
25 outputfile = "-"
26 columns = 1
27 letters = 0
28 uplink = "index.html"
29 uptitle = "Python Documentation Index"
30 favicon = None
32 # The "Aesop Meta Tag" is poorly described, and may only be used
33 # by the Aesop search engine (www.aesop.com), but doesn't hurt.
35 # There are a number of values this may take to roughly categorize
36 # a page. A page should be marked according to its primary
37 # category. Known values are:
38 # 'personal' -- personal-info
39 # 'information' -- information
40 # 'interactive' -- interactive media
41 # 'multimedia' -- multimedia presenetation (non-sales)
42 # 'sales' -- sales material
43 # 'links' -- links to other information pages
45 # Setting the aesop_type value to one of these strings will cause
46 # get_header() to add the appropriate <meta> tag to the <head>.
48 aesop_type = None
50 def __init__(self):
51 self.args = []
52 self.variables = {"address": "",
53 "iconserver": "icons",
54 "imgtype": "gif",
55 "title": "Global Module Index",
58 def add_args(self, short=None, long=None):
59 if short:
60 self.__short_args = self.__short_args + short
61 if long:
62 self.__long_args = self.__long_args + long
64 def parse(self, args):
65 try:
66 opts, args = getopt.getopt(args, self.__short_args,
67 self.__long_args)
68 except getopt.error:
69 sys.stdout = sys.stderr
70 self.usage()
71 sys.exit(2)
72 self.args = self.args + args
73 for opt, val in opts:
74 if opt in ("-a", "--address"):
75 val = val.strip()
76 if val:
77 val = "<address>\n%s\n</address>\n" % val
78 self.variables["address"] = val
79 elif opt in ("-h", "--help"):
80 self.usage()
81 sys.exit()
82 elif opt in ("-o", "--output"):
83 self.outputfile = val
84 elif opt in ("-c", "--columns"):
85 self.columns = int(val)
86 elif opt == "--title":
87 self.variables["title"] = val.strip()
88 elif opt == "--uplink":
89 self.uplink = val.strip()
90 elif opt == "--uptitle":
91 self.uptitle = val.strip()
92 elif opt == "--iconserver":
93 self.variables["iconserver"] = val.strip() or "."
94 elif opt == "--favicon":
95 self.favicon = val.strip()
96 else:
97 self.handle_option(opt, val)
98 if self.uplink and self.uptitle:
99 self.variables["uplinkalt"] = "up"
100 self.variables["uplinkicon"] = "up"
101 else:
102 self.variables["uplinkalt"] = ""
103 self.variables["uplinkicon"] = "blank"
104 self.variables["uplink"] = self.uplink
105 self.variables["uptitle"] = self.uptitle
107 def handle_option(self, opt, val):
108 raise getopt.error("option %s not recognized" % opt)
110 def get_header(self):
111 s = HEAD % self.variables
112 if self.uplink:
113 if self.uptitle:
114 link = ('<link rel="up" href="%s" title="%s">\n '
115 '<link rel="start" href="%s" title="%s">'
116 % (self.uplink, self.uptitle,
117 self.uplink, self.uptitle))
118 else:
119 link = ('<link rel="up" href="%s">\n '
120 '<link rel="start" href="%s">'
121 % (self.uplink, self.uplink))
122 repl = " %s\n</head>" % link
123 s = s.replace("</head>", repl, 1)
124 if self.aesop_type:
125 meta = '<meta name="aesop" content="%s">\n ' % self.aesop_type
126 # Insert this in the middle of the head that's been
127 # generated so far, keeping <meta> and <link> elements in
128 # neat groups:
129 s = s.replace("<link ", meta + "<link ", 1)
130 if self.favicon:
131 ext = os.path.splitext(self.favicon)[1]
132 if ext in (".gif", ".png"):
133 type = ' type="image/%s"' % ext[1:]
134 else:
135 type = ''
136 link = ('<link rel="SHORTCUT ICON" href="%s"%s>\n '
137 % (self.favicon, type))
138 s = s.replace("<link ", link + "<link ", 1)
139 return s
141 def get_footer(self):
142 return TAIL % self.variables
144 def get_output_file(self, filename=None):
145 if filename is None:
146 filename = self.outputfile
147 if filename == "-":
148 return sys.stdout
149 else:
150 return open(filename, "w")
153 NAVIGATION = '''\
154 <div class="navigation">
155 <table width="100%%" cellpadding="0" cellspacing="2">
156 <tr>
157 <td><img width="32" height="32" align="bottom" border="0" alt=""
158 src="%(iconserver)s/blank.%(imgtype)s"></td>
159 <td><a href="%(uplink)s"
160 title="%(uptitle)s"><img width="32" height="32" align="bottom" border="0"
161 alt="%(uplinkalt)s"
162 src="%(iconserver)s/%(uplinkicon)s.%(imgtype)s"></a></td>
163 <td><img width="32" height="32" align="bottom" border="0" alt=""
164 src="%(iconserver)s/blank.%(imgtype)s"></td>
165 <td align="center" width="100%%">%(title)s</td>
166 <td><img width="32" height="32" align="bottom" border="0" alt=""
167 src="%(iconserver)s/blank.%(imgtype)s"></td>
168 <td><img width="32" height="32" align="bottom" border="0" alt=""
169 src="%(iconserver)s/blank.%(imgtype)s"></td>
170 <td><img width="32" height="32" align="bottom" border="0" alt=""
171 src="%(iconserver)s/blank.%(imgtype)s"></td>
172 </tr></table>
173 <b class="navlabel">Up:</b> <span class="sectref"><a href="%(uplink)s"
174 title="%(uptitle)s">%(uptitle)s</A></span>
175 <br></div>
178 HEAD = '''\
179 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
180 <html>
181 <head>
182 <title>%(title)s</title>
183 <meta name="description" content="%(title)s">
184 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
185 <link rel="STYLESHEET" href="lib/lib.css">
186 </head>
187 <body>
188 ''' + NAVIGATION + '''\
189 <hr>
191 <h2>%(title)s</h2>
195 TAIL = "<hr>\n" + NAVIGATION + '''\
196 %(address)s</body>
197 </html>