| Trees | Indices | Help |
|
|---|
|
|
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
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
64 adapts(IGloballyIdentifiable)
65 implements(IGlobalIdentifier)
66
69
72
74 old = self.guid
75 setattr(self.context, GUID_ATTR_NAME, value)
76 notify(GUIDEvent(self.context, old, value, update_global_catalog))
77
80
81 guid = property(getGUID, setGUID)
82
87
88
90 implements(IGUIDManager)
91
92 _table_path = GUID_TABLE_PATH
93
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
106
108 path = self.getPath(guid)
109 if path is not None:
110 path = urllib.unquote(path)
111 return self.traverse(path)
112
115
118
122
132
| Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0.1.1812 on Mon Jul 30 17:11:23 2012 | http://epydoc.sourceforge.net |