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

# Source Code for Module Products.ZenRelations.schemadoc

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