Package ZenRelations :: Module schemadoc
[hide private]
[frames] | no frames]

Source Code for Module ZenRelations.schemadoc

  1  ########################################################################### 
  2  # 
  3  # This program is part of Zenoss Core, an open source monitoring platform. 
  4  # Copyright (C) 2007, Zenoss Inc. 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify it 
  7  # under the terms of the GNU General Public License version 2 as published by 
  8  # the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 11  # 
 12  ########################################################################### 
 13   
 14  import sys, os, shutil 
 15  import logging 
 16  logging.basicConfig() 
 17  root = logging.getLogger() 
 18  root.setLevel(logging.CRITICAL) 
 19   
 20  import Globals 
 21   
 22  from utils import importClasses 
 23  from Products.ZenUtils.Utils import zenPath 
 24   
25 -class HtmlGenerator(object):
26
27 - def __init__(self, baseModule, classList, outdir="docs"):
28 self.outdir = outdir 29 self.baseModule = baseModule 30 self.classList = classList 31 self._classnames = {}
32 33
34 - def generate(self):
35 """Generate all html docs for the classList we were given.""" 36 for cls in self.classList: 37 self.genHtml(cls) 38 self.genIndexHtml()
39 40 41
42 - def genIndexHtml(self):
43 """ 44 Generate our index file must be called after all classes are processed. 45 """ 46 self.of = open(os.path.join(self.outdir, "index.html"),"w") 47 self.writeHeader() 48 keys = self._classnames.keys() 49 keys.sort() 50 self.writeTableHeader("Classes", ("ClassName", )) 51 for clsname in keys: 52 filepath = self._classnames[clsname] 53 self.of.write("""<tr class="tablevalues">""") 54 self.td("""<a href="%s">%s</a>""" % (filepath, clsname)) 55 self.of.write("</table>") 56 self.writeFooter() 57 self.of.close()
58 59
60 - def genHtml(self, cls):
61 """Generate html documentaion for a class's schema.""" 62 logging.info("generating doc for class %s...", cls.__name__) 63 filename = self.getFileName(cls) 64 self._classnames[cls.__name__] = self.getHref(cls) 65 self.of = open(filename, "w") 66 self.writeHeader() 67 self.genPropertiesTable(cls) 68 self.genRelationsTable(cls) 69 self.writeFooter() 70 self.of.close()
71 72
73 - def genPropertiesTable(self, cls):
74 props = getattr(cls, "_properties", False) 75 if not props: return 76 self.writeTableHeader("Properties", ("name", "type", "mode", "setter")) 77 for prop in props: 78 self.of.write("""<tr class="tablevalues">""") 79 for key in ("id", "type", "mode", "setter"): 80 self.of.write("""<td>%s</td>""" % prop.get(key, "")) 81 self.of.write("</tr>") 82 self.of.write("</table>")
83 84
85 - def genRelationsTable(self, cls):
86 rels = getattr(cls, "_relations", False) 87 if not rels: return 88 self.writeTableHeader("Relations", 89 ("Local Name", "Local Type", 90 "Remote Type", "Remote Class", "Remote Name")) 91 for name, rel in rels: 92 self.of.write("""<tr class="tablevalues">""") 93 self.td(name) 94 self.td(rel.__class__.__name__) 95 self.td(rel.remoteType.__name__) 96 self.td("""<a href="%s">%s</a>""" % 97 (self.getHref(rel.remoteClass), rel.remoteClass)) 98 99 self.td(rel.remoteName) 100 self.of.write("</tr>") 101 self.of.write("</table>")
102 103
104 - def writeTableHeader(self, title, colnames):
105 self.of.write("<table>") 106 self.of.write( 107 """<tr class="tabletitle"><td colspan="%s">%s</td></tr>""" % ( 108 len(colnames), title)) 109 self.of.write("""<tr class="tableheader">""") 110 for colname in colnames: 111 self.of.write("<td>%s</td>" % colname)
112 113
114 - def writeHeader(self):
115 self.of.write("<html>") 116 self.of.write("<head>") 117 self.of.write("""<link rel="stylesheet" href="schemadoc.css" """ 118 """type="text/css" />""") 119 self.of.write("</head>") 120 self.of.write("<body>")
121 122
123 - def writeFooter(self):
124 self.of.write("</body></html>")
125 126
127 - def getHref(self, cls):
128 name = getattr(cls, "__name__", cls) 129 return ".".join((self.baseModule, name)) + ".html"
130 131
132 - def getFileName(self, cls, outdir=True):
133 filename = os.path.join(self.outdir, self.getHref(cls)) 134 dirname = os.path.dirname(filename) 135 if not os.path.isdir(dirname): os.makedirs(dirname, 0750) 136 return filename
137 138
139 - def td(self, value):
140 self.of.write("<td>%s</td>" % value)
141 142 143 baseModule = None 144 if len(sys.argv) > 1: 145 baseModule = sys.argv[1] 146 docdir = zenPath("zendocs/schema") 147 classList = importClasses(basemodule=baseModule, 148 skipnames=("ZentinelPortal", "ZDeviceLoader")) 149 htmlGen = HtmlGenerator(baseModule, classList, docdir) 150 htmlGen.generate() 151 cssfile = os.path.join(os.path.dirname(__file__),"schemadoc.css") 152 shutil.copy2(cssfile, docdir) 153