| Trees | Indices | Help |
|
|---|
|
|
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
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
67 adapts(IGloballyIdentifiable)
68 implements(IGlobalIdentifier)
69
72
75
77 old = self.guid
78 setattr(self.context, GUID_ATTR_NAME, value)
79 notify(GUIDEvent(self.context, old, value, update_global_catalog))
80
83
84 guid = property(getGUID, setGUID)
85
90
91
93 implements(IGUIDManager)
94
95 _table_path = GUID_TABLE_PATH
96
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
109
111 path = self.getPath(guid)
112 if path is not None:
113 path = urllib.unquote(path)
114 return self.traverse(path)
115
118
121
125
135
| Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0.1.1812 on Tue Oct 11 12:52:02 2011 | http://epydoc.sourceforge.net |