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

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

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