Fred Drake | 85788ed | 2000-10-05 05:20:55 +0000 | [diff] [blame] | 1 | """Miscellaneous support code shared by some of the tool scripts. |
| 2 | |
| 3 | This includes option parsing code, HTML formatting code, and a couple of |
| 4 | useful helpers. |
| 5 | |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 6 | """ |
| 7 | __version__ = '$Revision$' |
| 8 | |
| 9 | |
| 10 | import getopt |
Fred Drake | b07216b | 2002-10-30 17:05:03 +0000 | [diff] [blame] | 11 | import os.path |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 12 | import sys |
| 13 | |
| 14 | |
| 15 | class Options: |
| 16 | __short_args = "a:c:ho:" |
| 17 | __long_args = [ |
| 18 | # script controls |
| 19 | "columns=", "help", "output=", |
| 20 | |
| 21 | # content components |
Fred Drake | b07216b | 2002-10-30 17:05:03 +0000 | [diff] [blame] | 22 | "address=", "iconserver=", "favicon=", |
Fred Drake | 5c0ebdc | 2004-11-05 04:05:06 +0000 | [diff] [blame] | 23 | "title=", "uplink=", "uptitle=", |
| 24 | "image-type=", |
| 25 | ] |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 26 | |
| 27 | outputfile = "-" |
| 28 | columns = 1 |
| 29 | letters = 0 |
Fred Drake | baf43c5 | 2002-02-04 21:15:42 +0000 | [diff] [blame] | 30 | uplink = "index.html" |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 31 | uptitle = "Python Documentation Index" |
Fred Drake | b07216b | 2002-10-30 17:05:03 +0000 | [diff] [blame] | 32 | favicon = None |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 33 | |
Fred Drake | e03e1fe | 2002-04-05 17:34:50 +0000 | [diff] [blame] | 34 | # The "Aesop Meta Tag" is poorly described, and may only be used |
| 35 | # by the Aesop search engine (www.aesop.com), but doesn't hurt. |
| 36 | # |
| 37 | # There are a number of values this may take to roughly categorize |
| 38 | # a page. A page should be marked according to its primary |
| 39 | # category. Known values are: |
| 40 | # 'personal' -- personal-info |
| 41 | # 'information' -- information |
| 42 | # 'interactive' -- interactive media |
| 43 | # 'multimedia' -- multimedia presenetation (non-sales) |
| 44 | # 'sales' -- sales material |
| 45 | # 'links' -- links to other information pages |
| 46 | # |
| 47 | # Setting the aesop_type value to one of these strings will cause |
| 48 | # get_header() to add the appropriate <meta> tag to the <head>. |
| 49 | # |
| 50 | aesop_type = None |
| 51 | |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 52 | def __init__(self): |
| 53 | self.args = [] |
| 54 | self.variables = {"address": "", |
| 55 | "iconserver": "icons", |
Fred Drake | 5c0ebdc | 2004-11-05 04:05:06 +0000 | [diff] [blame] | 56 | "imgtype": "png", |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 57 | "title": "Global Module Index", |
| 58 | } |
| 59 | |
| 60 | def add_args(self, short=None, long=None): |
| 61 | if short: |
Fred Drake | b258bed | 2001-02-12 15:30:22 +0000 | [diff] [blame] | 62 | self.__short_args = self.__short_args + short |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 63 | if long: |
Fred Drake | b258bed | 2001-02-12 15:30:22 +0000 | [diff] [blame] | 64 | self.__long_args = self.__long_args + long |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 65 | |
| 66 | def parse(self, args): |
| 67 | try: |
| 68 | opts, args = getopt.getopt(args, self.__short_args, |
| 69 | self.__long_args) |
| 70 | except getopt.error: |
| 71 | sys.stdout = sys.stderr |
| 72 | self.usage() |
| 73 | sys.exit(2) |
Fred Drake | b258bed | 2001-02-12 15:30:22 +0000 | [diff] [blame] | 74 | self.args = self.args + args |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 75 | for opt, val in opts: |
| 76 | if opt in ("-a", "--address"): |
Fred Drake | 071972e | 2002-10-16 15:30:17 +0000 | [diff] [blame] | 77 | val = val.strip() |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 78 | if val: |
| 79 | val = "<address>\n%s\n</address>\n" % val |
| 80 | self.variables["address"] = val |
| 81 | elif opt in ("-h", "--help"): |
| 82 | self.usage() |
| 83 | sys.exit() |
| 84 | elif opt in ("-o", "--output"): |
| 85 | self.outputfile = val |
| 86 | elif opt in ("-c", "--columns"): |
| 87 | self.columns = int(val) |
| 88 | elif opt == "--title": |
| 89 | self.variables["title"] = val.strip() |
| 90 | elif opt == "--uplink": |
| 91 | self.uplink = val.strip() |
| 92 | elif opt == "--uptitle": |
| 93 | self.uptitle = val.strip() |
| 94 | elif opt == "--iconserver": |
| 95 | self.variables["iconserver"] = val.strip() or "." |
Fred Drake | b07216b | 2002-10-30 17:05:03 +0000 | [diff] [blame] | 96 | elif opt == "--favicon": |
| 97 | self.favicon = val.strip() |
Fred Drake | 5c0ebdc | 2004-11-05 04:05:06 +0000 | [diff] [blame] | 98 | elif opt == "--image-type": |
| 99 | self.variables["imgtype"] = val.strip() |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 100 | else: |
| 101 | self.handle_option(opt, val) |
| 102 | if self.uplink and self.uptitle: |
| 103 | self.variables["uplinkalt"] = "up" |
| 104 | self.variables["uplinkicon"] = "up" |
| 105 | else: |
| 106 | self.variables["uplinkalt"] = "" |
| 107 | self.variables["uplinkicon"] = "blank" |
| 108 | self.variables["uplink"] = self.uplink |
| 109 | self.variables["uptitle"] = self.uptitle |
| 110 | |
| 111 | def handle_option(self, opt, val): |
| 112 | raise getopt.error("option %s not recognized" % opt) |
| 113 | |
| 114 | def get_header(self): |
Fred Drake | f10584c | 2001-10-22 15:07:16 +0000 | [diff] [blame] | 115 | s = HEAD % self.variables |
| 116 | if self.uplink: |
| 117 | if self.uptitle: |
Fred Drake | 4a47349 | 2002-10-30 21:32:40 +0000 | [diff] [blame] | 118 | link = ('<link rel="up" href="%s" title="%s">\n ' |
| 119 | '<link rel="start" href="%s" title="%s">' |
| 120 | % (self.uplink, self.uptitle, |
| 121 | self.uplink, self.uptitle)) |
Fred Drake | f10584c | 2001-10-22 15:07:16 +0000 | [diff] [blame] | 122 | else: |
Fred Drake | 4a47349 | 2002-10-30 21:32:40 +0000 | [diff] [blame] | 123 | link = ('<link rel="up" href="%s">\n ' |
| 124 | '<link rel="start" href="%s">' |
| 125 | % (self.uplink, self.uplink)) |
Fred Drake | f10584c | 2001-10-22 15:07:16 +0000 | [diff] [blame] | 126 | repl = " %s\n</head>" % link |
| 127 | s = s.replace("</head>", repl, 1) |
Fred Drake | e03e1fe | 2002-04-05 17:34:50 +0000 | [diff] [blame] | 128 | if self.aesop_type: |
Fred Drake | b07216b | 2002-10-30 17:05:03 +0000 | [diff] [blame] | 129 | meta = '<meta name="aesop" content="%s">\n ' % self.aesop_type |
Fred Drake | e03e1fe | 2002-04-05 17:34:50 +0000 | [diff] [blame] | 130 | # Insert this in the middle of the head that's been |
| 131 | # generated so far, keeping <meta> and <link> elements in |
| 132 | # neat groups: |
| 133 | s = s.replace("<link ", meta + "<link ", 1) |
Fred Drake | b07216b | 2002-10-30 17:05:03 +0000 | [diff] [blame] | 134 | if self.favicon: |
| 135 | ext = os.path.splitext(self.favicon)[1] |
| 136 | if ext in (".gif", ".png"): |
| 137 | type = ' type="image/%s"' % ext[1:] |
| 138 | else: |
| 139 | type = '' |
| 140 | link = ('<link rel="SHORTCUT ICON" href="%s"%s>\n ' |
| 141 | % (self.favicon, type)) |
| 142 | s = s.replace("<link ", link + "<link ", 1) |
Fred Drake | f10584c | 2001-10-22 15:07:16 +0000 | [diff] [blame] | 143 | return s |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 144 | |
| 145 | def get_footer(self): |
| 146 | return TAIL % self.variables |
| 147 | |
| 148 | def get_output_file(self, filename=None): |
| 149 | if filename is None: |
| 150 | filename = self.outputfile |
| 151 | if filename == "-": |
| 152 | return sys.stdout |
| 153 | else: |
| 154 | return open(filename, "w") |
| 155 | |
| 156 | |
| 157 | NAVIGATION = '''\ |
| 158 | <div class="navigation"> |
| 159 | <table width="100%%" cellpadding="0" cellspacing="2"> |
| 160 | <tr> |
| 161 | <td><img width="32" height="32" align="bottom" border="0" alt="" |
| 162 | src="%(iconserver)s/blank.%(imgtype)s"></td> |
| 163 | <td><a href="%(uplink)s" |
| 164 | title="%(uptitle)s"><img width="32" height="32" align="bottom" border="0" |
| 165 | alt="%(uplinkalt)s" |
| 166 | src="%(iconserver)s/%(uplinkicon)s.%(imgtype)s"></a></td> |
| 167 | <td><img width="32" height="32" align="bottom" border="0" alt="" |
| 168 | src="%(iconserver)s/blank.%(imgtype)s"></td> |
| 169 | <td align="center" width="100%%">%(title)s</td> |
| 170 | <td><img width="32" height="32" align="bottom" border="0" alt="" |
| 171 | src="%(iconserver)s/blank.%(imgtype)s"></td> |
| 172 | <td><img width="32" height="32" align="bottom" border="0" alt="" |
| 173 | src="%(iconserver)s/blank.%(imgtype)s"></td> |
| 174 | <td><img width="32" height="32" align="bottom" border="0" alt="" |
| 175 | src="%(iconserver)s/blank.%(imgtype)s"></td> |
| 176 | </tr></table> |
| 177 | <b class="navlabel">Up:</b> <span class="sectref"><a href="%(uplink)s" |
| 178 | title="%(uptitle)s">%(uptitle)s</A></span> |
| 179 | <br></div> |
| 180 | ''' |
| 181 | |
| 182 | HEAD = '''\ |
| 183 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 184 | <html> |
| 185 | <head> |
| 186 | <title>%(title)s</title> |
| 187 | <meta name="description" content="%(title)s"> |
| 188 | <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
| 189 | <link rel="STYLESHEET" href="lib/lib.css"> |
| 190 | </head> |
| 191 | <body> |
| 192 | ''' + NAVIGATION + '''\ |
| 193 | <hr> |
| 194 | |
| 195 | <h2>%(title)s</h2> |
| 196 | |
| 197 | ''' |
| 198 | |
| 199 | TAIL = "<hr>\n" + NAVIGATION + '''\ |
| 200 | %(address)s</body> |
| 201 | </html> |
| 202 | ''' |