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

# Source Code for Module Products.ZenUtils.guid

     1  ###########################################################################
     2  #
     3  # This program is part of Zenoss Core, an open source monitoring platform.
     4  # Copyright (C) 2007, 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 as published by
     8  # the Free Software Foundation.
     9  #
    10  # For complete information please visit: http://www.zenoss.com/oss/
    11  #
    12  ###########################################################################
    13  __doc__="""guid
    14
    15  Generate a globally unique id that is used for events.
    16  This is a wrapper around the library that is used in Python 2.5
    17  and higher.
    18  See http://zestyping.livejournal.com/157957.html for more info and
    19  the code is available from http://zesty.ca/python/
    20  """
    21
    22  from uuid import uuid1, uuid3, uuid4, uuid5
    23
    24  # Dictionary of known UUID types
    25  known_uuid_types= {
    26    1:uuid1,
    27    3:uuid3,
    28    4:uuid4,
    29    5:uuid5,
    30  }
    31
    32 -def generate( uuid_type=4, *args, **kwargs ):
    33      """
    34      Generate an Universally Unique ID (UUID), according to RFC 4122.
    35      If an unknown uuid_type is provided, uses the UUID4 algorithm.
    36
    37      >>> guids = [ generate() for x in range(100000) ]
    38      >>> guid_set = set( guids )
    39      >>> len(guids) == len(guid_set)
    40      True
    41      >>> len( str( generate() ) ) == 36
    42      True
    43
    44      @param uuid_type: the type of UUID to generate
    45      @type uuid_type: range from 0 - 5
    46      @return: UUID
    47      @type: string
    48      """
    49
    50      uuid_func= known_uuid_types.get( uuid_type, uuid4 )
    51      return str( uuid_func(*args, **kwargs) )
    52