| 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 |
|
|---|
| 6 | """
|
|---|
| 7 | __version__ = '$Revision: 37764 $'
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 | import getopt
|
|---|
| 11 | import os.path
|
|---|
| 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=", "favicon=",
|
|---|
| 23 | "title=", "uplink=", "uptitle=",
|
|---|
| 24 | "image-type=",
|
|---|
| 25 | ]
|
|---|
| 26 |
|
|---|
| 27 | outputfile = "-"
|
|---|
| 28 | columns = 1
|
|---|
| 29 | letters = 0
|
|---|
| 30 | uplink = "index.html"
|
|---|
| 31 | uptitle = "Python Documentation Index"
|
|---|
| 32 | favicon = None
|
|---|
| 33 |
|
|---|
| 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 |
|
|---|
| 52 | def __init__(self):
|
|---|
| 53 | self.args = []
|
|---|
| 54 | self.variables = {"address": "",
|
|---|
| 55 | "iconserver": "icons",
|
|---|
| 56 | "imgtype": "png",
|
|---|
| 57 | "title": "Global Module Index",
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | def add_args(self, short=None, long=None):
|
|---|
| 61 | if short:
|
|---|
| 62 | self.__short_args = self.__short_args + short
|
|---|
| 63 | if long:
|
|---|
| 64 | self.__long_args = self.__long_args + long
|
|---|
| 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)
|
|---|
| 74 | self.args = self.args + args
|
|---|
| 75 | for opt, val in opts:
|
|---|
| 76 | if opt in ("-a", "--address"):
|
|---|
| 77 | val = val.strip()
|
|---|
| 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 "."
|
|---|
| 96 | elif opt == "--favicon":
|
|---|
| 97 | self.favicon = val.strip()
|
|---|
| 98 | elif opt == "--image-type":
|
|---|
| 99 | self.variables["imgtype"] = val.strip()
|
|---|
| 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):
|
|---|
| 115 | s = HEAD % self.variables
|
|---|
| 116 | if self.uplink:
|
|---|
| 117 | if self.uptitle:
|
|---|
| 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))
|
|---|
| 122 | else:
|
|---|
| 123 | link = ('<link rel="up" href="%s">\n '
|
|---|
| 124 | '<link rel="start" href="%s">'
|
|---|
| 125 | % (self.uplink, self.uplink))
|
|---|
| 126 | repl = " %s\n</head>" % link
|
|---|
| 127 | s = s.replace("</head>", repl, 1)
|
|---|
| 128 | if self.aesop_type:
|
|---|
| 129 | meta = '<meta name="aesop" content="%s">\n ' % self.aesop_type
|
|---|
| 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)
|
|---|
| 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)
|
|---|
| 143 | return s
|
|---|
| 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 | '''
|
|---|