Package Products :: Package ZenUtils :: Package guid :: Module event |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenUtils.guid.event

     1  ##############################################################################
     2  #
     3  # Copyright (C) Zenoss, Inc. 2010, 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  import logging
    12  from zope.event import notify
    13  from zope.component import adapter
    14  from zope.interface import implements
    15  from zope.component.interfaces import ObjectEvent
    16  from zope.container.interfaces import IObjectMovedEvent, IObjectRemovedEvent
    17  from OFS.interfaces import IObjectWillBeMovedEvent, IObjectWillBeAddedEvent
    18  from .interfaces import IGUIDEvent, IGUIDManager, IGloballyIdentifiable
    19  from .interfaces import IGlobalIdentifier
    20
    21  log = logging.getLogger('zen.UUID')
    22
    23
    24 -class GUIDEvent(ObjectEvent):
    25      implements(IGUIDEvent)
    26 -    def __init__(self, object, old, new, update_global_catalog=True):
    27          super(GUIDEvent, self).__init__(object)
    28          self.old = old
    29          self.new = new
    30          self.update_global_catalog = update_global_catalog
    31
    32
    33  @adapter(IGloballyIdentifiable, IGUIDEvent)
    34 -def registerGUIDToPathMapping(object, event):
    35      mgr = IGUIDManager(object)
    36      if event.new:
    37          mgr.setObject(event.new, object)
    38          if event.update_global_catalog:
    39              try:
    40                  catalog = object.global_catalog
    41                  catalog.catalog_object(object, idxs=(), update_metadata=True)
    42              except Exception:
    43                  log.exception('Encountered a guid exception')
    44      if event.old and event.old != event.new:
    45          # When we move a component around,
    46          # we don't want to remove the guid
    47          # from the catalog
    48          if mgr.getPath(event.old) == object.getPrimaryUrlPath():
    49              mgr.remove(event.old)
    50
    51
    52  @adapter(IGloballyIdentifiable, IObjectMovedEvent)
    53 -def refireEventOnObjectAddOrMove(object, event):
    54      if not IObjectRemovedEvent.providedBy(event):
    55          oldguid = IGlobalIdentifier(object).getGUID()
    56          if oldguid is None:
    57              IGlobalIdentifier(object).create()
    58          else:
    59              # Refire in the case where an object already has a guid
    60              # but that guid has been removed from the guid table
    61              notify(GUIDEvent(object, oldguid, oldguid))
    62
    63
    64  @adapter(IGloballyIdentifiable, IObjectWillBeMovedEvent)
    65 -def refireEventOnObjectBeforeRemove(object, event):
    66      if not IObjectWillBeAddedEvent.providedBy(event):
    67          guid = IGlobalIdentifier(object).guid
    68          notify(GUIDEvent(object, guid, None))
    69