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