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 |
| 27 | uplink = "./" |
| 28 | uptitle = "Python Documentation Index" |
| 29 | |
| 30 | def __init__(self): |
| 31 | self.args = [] |
| 32 | self.variables = {"address": "", |
| 33 | "iconserver": "icons", |
| 34 | "imgtype": "gif", |
| 35 | "title": "Global Module Index", |
| 36 | } |
| 37 | |
| 38 | def add_args(self, short=None, long=None): |
| 39 | if short: |
| 40 | self.__short_args += short |
| 41 | if long: |
| 42 | self.__long_args += long |
| 43 | |
| 44 | def parse(self, args): |
| 45 | try: |
| 46 | opts, args = getopt.getopt(args, self.__short_args, |
| 47 | self.__long_args) |
| 48 | except getopt.error: |
| 49 | sys.stdout = sys.stderr |
| 50 | self.usage() |
| 51 | sys.exit(2) |
| 52 | self.args += args |
| 53 | for opt, val in opts: |
| 54 | if opt in ("-a", "--address"): |
| 55 | val = val.strip() |
| 56 | if val: |
| 57 | val = "<address>\n%s\n</address>\n" % val |
| 58 | self.variables["address"] = val |
| 59 | elif opt in ("-h", "--help"): |
| 60 | self.usage() |
| 61 | sys.exit() |
| 62 | elif opt in ("-o", "--output"): |
| 63 | self.outputfile = val |
| 64 | elif opt in ("-c", "--columns"): |
| 65 | self.columns = int(val) |
| 66 | elif opt == "--title": |
| 67 | self.variables["title"] = val.strip() |
| 68 | elif opt == "--uplink": |
| 69 | self.uplink = val.strip() |
| 70 | elif opt == "--uptitle": |
| 71 | self.uptitle = val.strip() |
| 72 | elif opt == "--iconserver": |
| 73 | self.variables["iconserver"] = val.strip() or "." |
| 74 | else: |
| 75 | self.handle_option(opt, val) |
| 76 | if self.uplink and self.uptitle: |
| 77 | self.variables["uplinkalt"] = "up" |
| 78 | self.variables["uplinkicon"] = "up" |
| 79 | else: |
| 80 | self.variables["uplinkalt"] = "" |
| 81 | self.variables["uplinkicon"] = "blank" |
| 82 | self.variables["uplink"] = self.uplink |
| 83 | self.variables["uptitle"] = self.uptitle |
| 84 | |
| 85 | def handle_option(self, opt, val): |
| 86 | raise getopt.error("option %s not recognized" % opt) |
| 87 | |
| 88 | def get_header(self): |
| 89 | return HEAD % self.variables |
| 90 | |
| 91 | def get_footer(self): |
| 92 | return TAIL % self.variables |
| 93 | |
| 94 | def get_output_file(self, filename=None): |
| 95 | if filename is None: |
| 96 | filename = self.outputfile |
| 97 | if filename == "-": |
| 98 | return sys.stdout |
| 99 | else: |
| 100 | return open(filename, "w") |
| 101 | |
| 102 | |
| 103 | NAVIGATION = '''\ |
| 104 | <div class="navigation"> |
| 105 | <table width="100%%" cellpadding="0" cellspacing="2"> |
| 106 | <tr> |
| 107 | <td><img width="32" height="32" align="bottom" border="0" alt="" |
| 108 | src="%(iconserver)s/blank.%(imgtype)s"></td> |
| 109 | <td><a href="%(uplink)s" |
| 110 | title="%(uptitle)s"><img width="32" height="32" align="bottom" border="0" |
| 111 | alt="%(uplinkalt)s" |
| 112 | src="%(iconserver)s/%(uplinkicon)s.%(imgtype)s"></a></td> |
| 113 | <td><img width="32" height="32" align="bottom" border="0" alt="" |
| 114 | src="%(iconserver)s/blank.%(imgtype)s"></td> |
| 115 | <td align="center" width="100%%">%(title)s</td> |
| 116 | <td><img width="32" height="32" align="bottom" border="0" alt="" |
| 117 | src="%(iconserver)s/blank.%(imgtype)s"></td> |
| 118 | <td><img width="32" height="32" align="bottom" border="0" alt="" |
| 119 | src="%(iconserver)s/blank.%(imgtype)s"></td> |
| 120 | <td><img width="32" height="32" align="bottom" border="0" alt="" |
| 121 | src="%(iconserver)s/blank.%(imgtype)s"></td> |
| 122 | </tr></table> |
| 123 | <b class="navlabel">Up:</b> <span class="sectref"><a href="%(uplink)s" |
| 124 | title="%(uptitle)s">%(uptitle)s</A></span> |
| 125 | <br></div> |
| 126 | ''' |
| 127 | |
| 128 | HEAD = '''\ |
| 129 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 130 | <html> |
| 131 | <head> |
| 132 | <title>%(title)s</title> |
| 133 | <meta name="description" content="%(title)s"> |
| 134 | <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
| 135 | <link rel="STYLESHEET" href="lib/lib.css"> |
| 136 | </head> |
| 137 | <body> |
| 138 | ''' + NAVIGATION + '''\ |
| 139 | <hr> |
| 140 | |
| 141 | <h2>%(title)s</h2> |
| 142 | |
| 143 | ''' |
| 144 | |
| 145 | TAIL = "<hr>\n" + NAVIGATION + '''\ |
| 146 | %(address)s</body> |
| 147 | </html> |
| 148 | ''' |