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

# Source Code for Module Products.ZenReports.ReportServer

      1  ##############################################################################
      2  #
      3  # Copyright (C) Zenoss, Inc. 2007, 2011, 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  __doc__ = """ReportServer
     12
     13  A front end to all the report plugins.
     14
     15  """
     16
     17
     18  import logging
     19  import os
     20  import sys
     21
     22  from glob import glob
     23
     24  from Globals import InitializeClass
     25  from AccessControl import ClassSecurityInfo
     26
     27  from Products.ZenModel.ZenModelRM import ZenModelRM
     28  from Products.ZenModel.ZenossSecurity import ZEN_COMMON
     29  from Products.ZenUtils.Utils import importClass, zenPath
     30
     31  log = logging.getLogger('zen.reportserver')
     32
     33
     34 -class ReportServer(ZenModelRM):
     35      security = ClassSecurityInfo()
     36      security.setDefaultAccess('allow')
     37
     38 -    def _getPluginDirectories(self):
     39          directories = []
     40          for p in self.ZenPackManager.packs():
     41              if p.id == 'broken':
     42                  continue
     43              try:
     44                  pluginpath = p.path('reports', 'plugins')
     45                  directories.append(pluginpath)
     46              except AttributeError:
     47                  log.warn("Unable to load report plugins for ZenPack %s",
     48                            p.id)
     49          directories.append(zenPath('Products/ZenReports/plugins'))
     50          return directories
     51
     52 -    def listPlugins(self):
     53          allPlugins = []
     54          for dirpath in self._getPluginDirectories():
     55              allPlugins.extend(
     56                  fn.replace('.py', '')
     57                      for fn in glob('%s/*.py' % dirpath)
     58                          if not fn.endswith('__init__.py')
     59              )
     60          return allPlugins
     61
     62 -    def _importPluginClass(self, name):
     63          """
     64          Find the named plugin and import it.
     65          """
     66          klass = None
     67          if name.startswith('/'):
     68              if name.endswith('.py'):
     69                  name = name.replace('.py', '')
     70              if os.path.exists(name + '.py'):
     71                  try:
     72                      d, name = name.rsplit('/', 1)
     73                      sys.path.insert(0, d)
     74                      klass = importClass(name)
     75                  finally:
     76                      sys.path.remove(d)
     77          else:
     78              for d in self._getPluginDirectories():
     79                  if os.path.exists('%s/%s.py' % (d, name)):
     80                      try:
     81                          sys.path.insert(0, d)
     82                          klass = importClass(name)
     83                          break
     84                      finally:
     85                          sys.path.remove(d)
     86          return klass
     87
     88      security.declareProtected(ZEN_COMMON, 'plugin')
     89 -    def plugin(self, name, REQUEST, templateArgs=None):
     90          "Run a plugin to generate the report object"
     91          dmd = self.dmd
     92          args = dict(zip(REQUEST.keys(), REQUEST.values()))
     93
     94          # We don't want the response object getting passed to the plugin
     95          # because if it is stringified, it can modify the return code
     96          # and cause problems upstream.
     97          if 'RESPONSE' in args:
     98              del args['RESPONSE']
     99          klass = self._importPluginClass(name)
    100          if not klass:
    101              raise IOError('Unable to find plugin named "%s"' % name)
    102          instance = klass()
    103          log.debug("Running plugin %s", name)
    104          try:
    105              if templateArgs is None:
    106                  return instance.run(dmd, args)
    107              return instance.run(dmd, args, templateArgs)
    108          except Exception:
    109              log.exception("Failed to run plugin %s (%s)", name, instance)
    110              return []
    111
    112
    113 -def manage_addReportServer(context, id, REQUEST=None):
    114      """make a ReportServer"""
    115      rs = ReportServer(id)
    116      context._setObject(id, rs)
    117      if REQUEST is not None:
    118          REQUEST['RESPONSE'].redirect(context.absolute_url() + '/manage_main')
    119
    120
    121  InitializeClass(ReportServer)
    122