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

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

      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  __doc__="""guid
     12
     13  Generate a globally unique id that is used for events.
     14  This is a wrapper around the library that is used in Python 2.5
     15  and higher.
     16  See http://zestyping.livejournal.com/157957.html for more info and
     17  the code is available from http://zesty.ca/python/
     18  """
     19  import urllib
     20  from uuid import uuid1, uuid3, uuid4, uuid5
     21  from BTrees.OOBTree import OOBTree
     22  from zope.event import notify
     23  from zope.interface import implements
     24  from zope.component import adapts
     25  from .interfaces import IGloballyIdentifiable, IGlobalIdentifier, IGUIDManager
     26
     27  from Products.ZenUtils.guid.event import GUIDEvent
     28  from Products.ZCatalog.interfaces import ICatalogBrain
     29
     30  # Dictionary of known UUID types
     31  known_uuid_types= {
     32    1:uuid1,
     33    3:uuid3,
     34    4:uuid4,
     35    5:uuid5,
     36  }
     37
     38 -def generate( uuid_type=4, *args, **kwargs ):
     39      """
     40      Generate an Universally Unique ID (UUID), according to RFC 4122.
     41      If an unknown uuid_type is provided, uses the UUID4 algorithm.
     42
     43      >>> guids = [ generate() for x in range(100000) ]
     44      >>> guid_set = set( guids )
     45      >>> len(guids) == len(guid_set)
     46      True
     47      >>> len( str( generate() ) ) == 36
     48      True
     49
     50      @param uuid_type: the type of UUID to generate
     51      @type uuid_type: range from 0 - 5
     52      @return: UUID
     53      @type: string
     54      """
     55      uuid_func = known_uuid_types.get(uuid_type, uuid4)
     56      return str(uuid_func(*args, **kwargs))
     57
     58
     59  GUID_ATTR_NAME = '_guid'
     60  GUID_TABLE_PATH = '/zport/dmd/guid_table'
     61
     62
     63 -class GlobalIdentifier(object):
     64      adapts(IGloballyIdentifiable)
     65      implements(IGlobalIdentifier)
     66
     67 -    def __init__(self, context):
     68          self.context = context
     69
     70 -    def getGUID(self):
     71          return getattr(self.context, GUID_ATTR_NAME, None)
     72
     73 -    def _setGUID(self, value, update_global_catalog=True):
     74          old = self.guid
     75          setattr(self.context, GUID_ATTR_NAME, value)
     76          notify(GUIDEvent(self.context, old, value, update_global_catalog))
     77
     78 -    def setGUID(self, value):
     79          self._setGUID(value)
     80
     81      guid = property(getGUID, setGUID)
     82
     83 -    def create(self, force=False, update_global_catalog=True):
     84          if self.guid is None or force:
     85              self._setGUID(generate(), update_global_catalog=update_global_catalog)
     86          return self.guid
     87
     88
     89 -class GUIDManager(object):
     90      implements(IGUIDManager)
     91
     92      _table_path = GUID_TABLE_PATH
     93
     94 -    def __init__(self, context):
     95          self.context = context
     96          self.traverse = self.context.unrestrictedTraverse
     97          try:
     98              self.table = self.traverse(self._table_path)
     99          except (AttributeError, KeyError), e:
    100              parent, name = self._table_path.rsplit('/', 1)
    101              self.table = OOBTree()
    102              setattr(self.traverse(parent), name, self.table)
    103
    104 -    def getPath(self, guid):
    105          return self.table.get(guid, None)
    106
    107 -    def getObject(self, guid):
    108          path = self.getPath(guid)
    109          if path is not None:
    110              path = urllib.unquote(path)
    111              return self.traverse(path)
    112
    113 -    def setPath(self, guid, path):
    114          self.table[guid] = path
    115
    116 -    def setObject(self, guid, object):
    117          self.setPath(guid, object.getPrimaryUrlPath())
    118
    119 -    def remove(self, guid):
    120          if guid in self.table:
    121              del self.table[guid]
    122
    123 -class BrainGlobalIdentifier(object):
    124      adapts(ICatalogBrain)
    125      implements(IGlobalIdentifier)
    126
    127 -    def __init__(self, context):
    128          self.context = context
    129
    130 -    def getGUID(self):
    131          return self.context.uuid
    132