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

# Source Code for Module Products.ZenModel.ZenPackPersistence

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