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

# Source Code for Module Products.ZenReports.ReportServer

      1  ###########################################################################
      2  #
      3  # This program is part of Zenoss Core, an open source monitoring platform.
      4  # Copyright (C) 2007, 2011 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 or (at your
      8  # option) any later version as published by the Free Software Foundation.
      9  #
     10  # For complete information please visit: http://www.zenoss.com/oss/
     11  #
     12  ###########################################################################
     13  __doc__ = """ReportServer
     14
     15  A front end to all the report plugins.
     16
     17  """
     18
     19
     20  import os
     21  import sys
     22  from glob import glob
     23  import logging
     24  log = logging.getLogger('zen.reportserver')
     25
     26  from Globals import InitializeClass
     27  from AccessControl import ClassSecurityInfo
     28
     29  from Products.ZenModel.ZenModelRM import ZenModelRM
     30  from Products.ZenUtils.Utils import importClass, zenPath
     31  from Products.ZenModel.ZenossSecurity import *
     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 dir in self._getPluginDirectories():
     55              plugins = [fn.replace('.py','') for fn in glob('%s/*.py' % dir) \
     56                           if not fn.endswith('__init__.py')]
     57              allPlugins.extend(plugins)
     58          return allPlugins
     59
     60 -    def _importPluginClass(self, name):
     61          """
     62          Find the named plugin and import it.
     63          """
     64          klass = None
     65          if name.startswith('/'):
     66              if name.endswith('.py'):
     67                  name = name.replace('.py', '')
     68              if os.path.exists(name + '.py'):
     69                  try:
     70                      d, name = name.rsplit('/', 1)
     71                      sys.path.insert(0, d)
     72                      klass = importClass(name)
     73                  finally:
     74                      sys.path.remove(d)
     75
     76          else:
     77              for d in self._getPluginDirectories():
     78                  if os.path.exists('%s/%s.py' % (d, name)):
     79                      try:
     80                          sys.path.insert(0, d)
     81                          klass = importClass(name)
     82                          break
     83                      finally:
     84                          sys.path.remove(d)
     85          return klass
     86
     87      security.declareProtected(ZEN_COMMON, 'plugin')
     88 -    def plugin(self, name, REQUEST, templateArgs = None):
     89          "Run a plugin to generate the report object"
     90          dmd = self.dmd
     91          args = dict(zip(REQUEST.keys(), REQUEST.values()))
     92
     93          # We don't want the response object getting passed to the plugin
     94          # because if it is stringified, it can modify the return code
     95          # and cause problems upstream.
     96          if 'RESPONSE' in args:
     97              del args['RESPONSE']
     98
     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          if templateArgs == None:
    105              return instance.run(dmd, args)
    106          else:
    107              return instance.run(dmd, args, templateArgs)
    108
    109 -def manage_addReportServer(context, id, REQUEST = None):
    110      """make a ReportServer"""
    111      rs = ReportServer(id)
    112      context._setObject(id, rs)
    113      if REQUEST is not None:
    114          REQUEST['RESPONSE'].redirect(context.absolute_url()+'/manage_main')
    115
    116
    117  InitializeClass(ReportServer)
    118