Initial import to Tizen
[profile/ivi/python-pyOpenSSL.git] / doc / tools / support.py
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: 1.1.1.1 $'
8
9
10 import getopt
11 import string
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
28     uplink = "./"
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:
41             self.__short_args = self.__short_args + short
42         if long:
43             self.__long_args = self.__long_args + long
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)
53         self.args = self.args + args
54         for opt, val in opts:
55             if opt in ("-a", "--address"):
56                 val = string.strip(val)
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):
90         return HEAD % self.variables
91
92     def get_footer(self):
93         return TAIL % self.variables
94
95     def get_output_file(self, filename=None):
96         if filename is None:
97             filename = self.outputfile
98         if filename == "-":
99             return sys.stdout
100         else:
101             return open(filename, "w")
102
103
104 NAVIGATION = '''\
105 <div class="navigation">
106 <table width="100%%" cellpadding="0" cellspacing="2">
107 <tr>
108 <td><img width="32" height="32" align="bottom" border="0" alt=""
109  src="%(iconserver)s/blank.%(imgtype)s"></td>
110 <td><a href="%(uplink)s"
111  title="%(uptitle)s"><img width="32" height="32" align="bottom" border="0"
112  alt="%(uplinkalt)s"
113  src="%(iconserver)s/%(uplinkicon)s.%(imgtype)s"></a></td>
114 <td><img width="32" height="32" align="bottom" border="0" alt=""
115  src="%(iconserver)s/blank.%(imgtype)s"></td>
116 <td align="center" width="100%%">%(title)s</td>
117 <td><img width="32" height="32" align="bottom" border="0" alt=""
118  src="%(iconserver)s/blank.%(imgtype)s"></td>
119 <td><img width="32" height="32" align="bottom" border="0" alt=""
120  src="%(iconserver)s/blank.%(imgtype)s"></td>
121 <td><img width="32" height="32" align="bottom" border="0" alt=""
122  src="%(iconserver)s/blank.%(imgtype)s"></td>
123 </tr></table>
124 <b class="navlabel">Up:</b> <span class="sectref"><a href="%(uplink)s"
125  title="%(uptitle)s">%(uptitle)s</A></span>
126 <br></div>
127 '''
128
129 HEAD = '''\
130 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
131 <html>
132 <head>
133   <title>%(title)s</title>
134   <meta name="description" content="%(title)s">
135   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
136   <link rel="STYLESHEET" href="lib/lib.css">
137 </head>
138 <body>
139 ''' + NAVIGATION + '''\
140 <hr>
141
142 <h2>%(title)s</h2>
143
144 '''
145
146 TAIL = "<hr>\n" + NAVIGATION + '''\
147 %(address)s</body>
148 </html>
149 '''