Package Products :: Package ZenReports :: Module ReportLoader |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenReports.ReportLoader

     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  import os
    14  import transaction
    15  import logging
    16  import Globals
    17
    18  from Products.ZenUtils.ZCmdBase import ZCmdBase
    19  from Products.ZenModel.Report import Report
    20  from Products.Zuul.utils import CatalogLoggingFilter
    21
    22 -class ReportLoader(ZCmdBase):
    23
    24 -    def buildOptions(self):
    25          ZCmdBase.buildOptions(self)
    26          self.parser.add_option('-f', '--force', dest='force',
    27                                 action='store_true', default=0,
    28                                 help="Force load all the reports")
    29
    30 -    def loadDatabase(self):
    31          repdir = os.path.join(os.path.dirname(__file__),"reports")
    32          self.loadDirectory(repdir)
    33          transaction.commit()
    34
    35 -    def reports(self, directory):
    36          def normalize(f):
    37              return f.replace("_", " ")
    38          def toOrg(path):
    39              path = normalize(path).split("/")
    40              path = path[path.index("reports") + 1:]
    41              return "/" + "/".join(path)
    42          return [(toOrg(p), normalize(f[:-4]), os.path.join(p, f))
    43                  for p, ds, fs in os.walk(directory)
    44                  for f in fs
    45                  if f.endswith(".rpt")]
    46
    47 -    def unloadDirectory(self, repdir):
    48          self.log.info("removing reports from:%s", repdir)
    49          reproot = self.dmd.Reports
    50          for orgpath, fid, fullname in self.reports(repdir):
    51              rorg = reproot.createOrganizer(orgpath)
    52              if getattr(rorg, fid, False):
    53                  rorg._delObject(fid)
    54              while rorg.id != 'Reports':
    55                  if not rorg.objectValues():
    56                      id = rorg.id
    57                      rorg = rorg.getPrimaryParent()
    58                      rorg._delObject(id)
    59
    60
    61 -    def loadDirectory(self, repdir):
    62          self.log.info("loading reports from:%s", repdir)
    63          # If zencatalog hasn't finished yet, we get ugly messages that don't
    64          # mean anything. Hide them.
    65          logFilter = None
    66          if not getattr(self.dmd.zport, '_zencatalog_completed', False):
    67              logFilter = CatalogLoggingFilter()
    68              logging.getLogger('Zope.ZCatalog').addFilter(logFilter)
    69          try:
    70              reproot = self.dmd.Reports
    71              for orgpath, fid, fullname in self.reports(repdir):
    72                  rorg = reproot.createOrganizer(orgpath)
    73                  if getattr(rorg, fid, False):
    74                      if self.options.force:
    75                          rorg._delObject(fid)
    76                      else:
    77                          continue
    78                  self.log.info("loading: %s/%s", orgpath, fid)
    79                  self.loadFile(rorg, fid, fullname)
    80          finally:
    81              # Remove our logging filter so we don't hide anything important
    82              if logFilter is not None:
    83                  logging.getLogger('Zope.ZCatalog').removeFilter(logFilter)
    84
    85
    86 -    def loadFile(self, root, id, fullname):
    87          fdata = file(fullname).read()
    88          rpt = Report(id, text=fdata)
    89          root._setObject(id, rpt)
    90          return rpt
    91
    92
    93  if __name__ == "__main__":
    94      rl = ReportLoader()
    95      rl.loadDatabase()
    96