项目记录 -- config2html 理解
html 代码:
1 <table width=1280 border=0 cellspacing=1 cellpadding=1> 2 <tr id=tblhdr> 3 <td><b>pool: {{ d['pool']}} </b></td> 4 </tr> 5 <tr id=row1><td>state: {{ d['state']}} </td></tr> 6 <tr id=row1><td>errors: {{ d['errors'] }} </td></tr> 7 <tr id=row1><td>scan: {{ ''.join(d['scan']) }} </td></tr> 8 <tr id=row1><td>config: </td></tr> 9 <table width=800> 10 {% raw config2html(d['pool'], d['config']['topvdev'], 0, None) %} 11 {% if d['config']['logs'] %} 12 {% raw config2html(d['pool'], d['config']['logs'], 0, None) %} 13 {% end %} 14 {% if d['config']['cache'] %} 15 {% raw config2html(d['pool'], d['config']['cache'], 0, None) %} 16 {% end %} 17 {% if d['config']['spares'] %} 18 {% raw config2html(d['pool'], d['config']['spares'], 0, None) %} 19 {% end %} 20 </table> 21 </table>
app.py 代码:
1 class ZpoolMoreHandler(tornado.web.RequestHandler): 2 def config2html(self, poolname, vdev, depth, l, vtype=None): 3 d = vdev['this'] 4 if depth == 0: 5 depth = 8 6 if d['name'] == 'logs' or d['name'] == 'cache'\ 7 or d['name'] == 'spares': 8 l = [] 9 elif d['name'] == poolname: 10 l = ['<tr id=row1>', 11 '<td>', ' '*depth, 'NAME', '</td>', 12 '<td>', 'STATE', '</td>', 13 '</tr>' 14 ] 15 if d['name'] == poolname: 16 action = '' 17 vtype = 'root' 18 elif d['name'] in ['logs', 'cache', 'spares']: 19 action = '' 20 vtype = 'log' 21 elif d['name'].startswith('raidz'): 22 action = '' 23 vtype = 'raidz' 24 elif d['name'].startswith('mirror'): 25 action = '' 26 vtype = 'mirror' 27 elif d['name'].startswith('replac'): 28 action = '' 29 vtype = 'mirror' 30 elif d['name'].startswith('spar'): 31 action = '' 32 vtype = 'mirror' 33 elif vtype == 'root': 34 action = '<a style="color:#0000ff" \ 35 href="/Manage/vdevreplace?poolname=%s&old=%s"> \ 36 Replace</a> ' %(poolname, d['name']) 37 if d['state'] == 'ONLINE': 38 action += '<a style="color:#0000ff" \ 39 href="/Manage/vdevoffline?poolname=%s&vdev=%s"> \ 40 Offline</a> ' %(poolname, d['name']) 41 else: 42 action += '<a style="color:#0000ff" \ 43 href="/Manage/vdevonline?poolname=%s&vdev=%s"> \ 44 Online</a> ' %(poolname, d['name']) 45 action += '<a style="color:#0000ff" \ 46 href="/Manage/vdevattach?poolname=%s&old=%s"> \ 47 Attach</a> ' %(poolname, d['name']) 48 elif vtype == 'raidz': 49 action = '<a style="color:#0000ff" \ 50 href="/Manage/vdevreplace?poolname=%s&old=%s"> \ 51 Replace</a> ' %(poolname, d['name']) 52 if d['state'] == 'ONLINE': 53 action += '<a style="color:#0000ff" \ 54 href="/Manage/vdevoffline?poolname=%s&vdev=%s"> \ 55 Offline</a> ' %(poolname, d['name']) 56 else: 57 action += '<a style="color:#0000ff" \ 58 href="/Manage/vdevonline?poolname=%s&vdev=%s"> \ 59 Online</a> ' %(poolname, d['name']) 60 elif vtype == 'mirror': 61 action = '<a style="color:#0000ff" \ 62 href="/Manage/vdevreplace?poolname=%s&old=%s"> \ 63 Replace</a> ' %(poolname, d['name']) 64 if d['state'] == 'ONLINE': 65 action += '<a style="color:#0000ff" \ 66 href="/Manage/vdevoffline?poolname=%s&vdev=%s"> \ 67 Offline</a> ' %(poolname, d['name']) 68 else: 69 action += '<a style="color:#0000ff" \ 70 href="/Manage/vdevonline?poolname=%s&vdev=%s"> \ 71 Online</a> ' %(poolname, d['name']) 72 action += '<a style="color:#0000ff" \ 73 href="/Manage/vdevattach?poolname=%s&old=%s"> \ 74 Attach</a> ' %(poolname, d['name']) 75 action += '<a style="color:#0000ff" \ 76 href="/Manage/vdevdetach?poolname=%s&vdev=%s"> \ 77 Detach</a> ' %(poolname, d['name']) 78 elif vtype == 'log': 79 action = '<a style="color:#0000ff" \ 80 href="/Manage/vdevreplace?poolname=%s&old=%s"> \ 81 Replace</a> ' %(poolname, d['name']) 82 if d['state'] == 'ONLINE' or d['state'] == 'AVAIL': 83 action += '<a style="color:#0000ff" \ 84 href="/Manage/vdevoffline?poolname=%s&vdev=%s"> \ 85 Offline</a> ' %(poolname, d['name']) 86 else: 87 action += '<a style="color:#0000ff" \ 88 href="/Manage/vdevonline?poolname=%s&vdev=%s"> \ 89 Online</a> ' %(poolname, d['name']) 90 action += '<a style="color:#0000ff" \ 91 href="/Manage/vdevremove?poolname=%s&vdev=%s"> \ 92 Remove</a> ' %(poolname, d['name']) 93 94 if 'disk' in d: 95 diskinfo = d['disk'] 96 vdevname = 'e'+diskinfo['enclosure']+'s'+diskinfo['bay']+ ', '\ 97 + diskinfo['logical-disk'] + ', ' 98 if diskinfo['capacity'] != "N/A": 99 vdevname += str(int(diskinfo['capacity'])>>30) + 'GiB' 100 else: 101 vdevname = d['name'] 102 103 l.append('<tr id=row1>' + 104 '<td>' + ' '*depth + vdevname + '</td>' + 105 '<td>' + d['state'] + '</td>' + 106 '<td>' + action + '</td>' + 107 '</tr>') 108 for c in vdev['children']: 109 self.config2html(poolname, c, depth+4, l, vtype) 110 if depth == 8: 111 return ''.join(l) 112 113 def get(self): 114 import xmlrpc.client 115 s = xmlrpc.client.ServerProxy(LocalRpcSvcAddr) 116 name = self.get_argument('name', '') 117 if name: 118 status = s.zpoolstatus(name) 119 if not status: 120 message='状态信息不可用,请重试.' 121 self.write("<script language=javascript> alert('%s');</script>" 122 % message) 123 return 124 125 self.render("zpoolmore.htm", d=status, config2html=self.config2html)
注重理解:config2html这段代码
显示页面如下: