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 | b258bed | 2001-02-12 15:30:22 +0000 | [diff] [blame] | 11 | import string |
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 |
| 22 | "address=", "iconserver=", |
| 23 | "title=", "uplink=", "uptitle="] |
| 24 | |
| 25 | outputfile = "-" |
| 26 | columns = 1 |
| 27 | letters = 0 |
Fred Drake | baf43c5 | 2002-02-04 21:15:42 +0000 | [diff] [blame] | 28 | uplink = "index.html" |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 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 Drake | b258bed | 2001-02-12 15:30:22 +0000 | [diff] [blame] | 41 | self.__short_args = self.__short_args + short |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 42 | if long: |
Fred Drake | b258bed | 2001-02-12 15:30:22 +0000 | [diff] [blame] | 43 | self.__long_args = self.__long_args + long |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 44 | |
| 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 Drake | b258bed | 2001-02-12 15:30:22 +0000 | [diff] [blame] | 53 | self.args = self.args + args |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 54 | for opt, val in opts: |
| 55 | if opt in ("-a", "--address"): |
Fred Drake | b258bed | 2001-02-12 15:30:22 +0000 | [diff] [blame] | 56 | val = string.strip(val) |
Fred Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 57 | 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 Drake | f10584c | 2001-10-22 15:07:16 +0000 | [diff] [blame] | 90 | 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 Drake | ca2b2e0 | 2000-10-05 05:11:57 +0000 | [diff] [blame] | 100 | |
| 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 | |
| 113 | NAVIGATION = '''\ |
| 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 | |
| 138 | HEAD = '''\ |
| 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 | |
| 155 | TAIL = "<hr>\n" + NAVIGATION + '''\ |
| 156 | %(address)s</body> |
| 157 | </html> |
| 158 | ''' |