Package Products :: Package ZenModel :: Module ZenPackPersistence |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenModel.ZenPackPersistence

      1  ###########################################################################
      2  #
      3  # This program is part of Zenoss Core, an open source monitoring platform.
      4  # Copyright (C) 2008, 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
     14  __doc__='''
     15  ZenPackPersistence
     16  '''
     17
     18  from zope.interface import implements
     19  from Products.ZenModel.interfaces import IIndexed
     20
     21  ZENPACK_PERSISTENCE_CATALOG = 'zenPackPersistence'
     22
     23  import logging
     24  log = logging.getLogger('ZenPackPersistence')
     25
     26 -def CreateZenPackPersistenceCatalog(dmd):
     27      '''
     28      Create the zenPackPersistence catalog if it does not exist.
     29      Return the catalog
     30      '''
     31      from Products.ZCatalog.ZCatalog import manage_addZCatalog
     32      from Products.ZenUtils.Search import makeCaseSensitiveFieldIndex
     33      zcat = getattr(dmd, ZENPACK_PERSISTENCE_CATALOG, None)
     34      if zcat is None:
     35          manage_addZCatalog(dmd, ZENPACK_PERSISTENCE_CATALOG,
     36                                  ZENPACK_PERSISTENCE_CATALOG)
     37          zcat = dmd._getOb(ZENPACK_PERSISTENCE_CATALOG)
     38          cat = zcat._catalog
     39          cat.addIndex('getZenPackName',makeCaseSensitiveFieldIndex('getZenPackName'))
     40          cat.addColumn('id')
     41          cat.addColumn('getPrimaryId')
     42      return zcat
     43
     44
     45 -def GetCatalogedObjects(dmd, packName):
     46      """
     47      Return a list of all the objects in the zenPackPersistence catalog
     48      for the given zenPack name.
     49      If the catalog is not found, return None.
     50      """
     51      zcat = getattr(dmd, ZENPACK_PERSISTENCE_CATALOG, None)
     52      if zcat is None:
     53          result = None
     54      else:
     55          result = []
     56          brains = zcat(dict(getZenPackName=packName))
     57          for brain in brains:
     58              try:
     59                  obj = brain.getObject()
     60                  result.append(obj)
     61              except KeyError, e:
     62                  log.warn('catalog object %s not found in system', e)
     63      return result
     64
     65
     66 -class ZenPackPersistence(object):
     67      '''
     68      This is a mix-in class that should be used whenever a ZenPack-supplied
     69      class is going to be stored persistently in the zodb.  It provides
     70      for a catalog to associate objects in zodb with the ZenPacks that provide
     71      those objects' classes.
     72
     73      The motivation for this is that we usually need to delete all instances
     74      of zenpack-supplied classes when that zenpack is deleted.  This is
     75      because the class for those objects no longer exists and they are just
     76      sad, broken, unloved objects in the zodb at that point.  This is
     77      undesirable.
     78
     79      IMPORTANT: This should be the first class listed in any subclasses's
     80      list of parents.  Otherwise the manage_* methods of the other classes
     81      will likely be called and these skipped.
     82      '''
     83      implements(IIndexed)
     84
     85      # Subclasses should set this to the id of the ZenPack or they
     86      # should override getZenPackName()
     87      # ZENPACKID = 'ZenPacks.my.name'
     88
     89 -    def getZenPackName(self):
     90          '''
     91          '''
     92          if not self.ZENPACKID:
     93              from ZenPack import ZenPackException
     94              raise ZenPackException('The class %s must define ZENPACKID ' %
     95                      str(self.__class__) +
     96                      'or override getZenPackName().')
     97          # Should we check to make sure ZENPACKID matches the name of an
     98          # installed ZenPack?
     99          return self.ZENPACKID
    100
    101
    102 -    def getZenPack(self, context):
    103          """
    104          Return the ZenPack instance that provides this object.
    105          """
    106          return context.dmd.ZenPackManager.packs._getOb(
    107                                              self.getZenPackName(), None)
    108
    109
    110 -    def path(self, *parts):
    111          """
    112          Return the path to the installed ZenPack directory or a subdirectory.
    113          Example: zenpack.path('libexec') would return something like
    114          $ZENHOME/ZenPacks/ZenPacks.Me.MyZenPack/ZenPacks/Me/MyZenPack/libexec
    115          """
    116          zp = self.getZenPack(self)
    117          return zp.path(*parts)
    118
    119
    120      # index_object and unindex_object are overridden so that instances
    121      # can participate in other catalogs, not just the
    122      # ZENPACK_PERSISTENCE_CATALOG.
    123      # If we used the standard method of just setting default_catalog in
    124      # this class then ZenPacks would not be able to override Zenoss
    125      # classes that already participate in catalogs, eg DeviceClass.
    126
    127 -    def index_object(self):
    128          """A common method to allow Findables to index themselves."""
    129          cat = getattr(self, ZENPACK_PERSISTENCE_CATALOG, None)
    130          if cat is not None:
    131              cat.catalog_object(self, self.getPrimaryId())
    132          super(ZenPackPersistence, self).index_object()
    133
    134
    135 -    def unindex_object(self):
    136          """A common method to allow Findables to unindex themselves."""
    137          #FIXME THIS WON'T WORK IF WE DELETE FROM THE ZENPACK PAGE BECAUSE WE CAN'T FIND THE CATALOG -EAD
    138          cat = getattr(self, ZENPACK_PERSISTENCE_CATALOG, None)
    139          if cat is not None:
    140              cat.uncatalog_object(self.getPrimaryId())
    141          super(ZenPackPersistence, self).unindex_object()
    142