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

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

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