~soren/nova/iptables-security-groups

« back to all changes in this revision

Viewing changes to vendor/carrot/utils.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from uuid import UUID, uuid4, _uuid_generate_random
 
2
try:
 
3
    import ctypes
 
4
except ImportError:
 
5
    ctypes = None
 
6
 
 
7
 
 
8
def gen_unique_id():
 
9
    """Generate a unique id, having - hopefully - a very small chance of
 
10
    collission.
 
11
 
 
12
    For now this is provided by :func:`uuid.uuid4`.
 
13
    """
 
14
    # Workaround for http://bugs.python.org/issue4607
 
15
    if ctypes and _uuid_generate_random:
 
16
        buffer = ctypes.create_string_buffer(16)
 
17
        _uuid_generate_random(buffer)
 
18
        return str(UUID(bytes=buffer.raw))
 
19
    return str(uuid4())
 
20
 
 
21
 
 
22
def _compat_rl_partition(S, sep, direction=str.split):
 
23
    items = direction(S, sep, 1)
 
24
    if len(items) == 1:
 
25
        return items[0], sep, ''
 
26
    return items[0], sep, items[1]
 
27
 
 
28
 
 
29
def _compat_partition(S, sep):
 
30
    """``partition(S, sep) -> (head, sep, tail)``
 
31
 
 
32
    Search for the separator ``sep`` in ``S``, and return the part before
 
33
    it, the separator itself, and the part after it. If the separator is not
 
34
    found, return ``S`` and two empty strings.
 
35
 
 
36
    """
 
37
    return _compat_rl_partition(S, sep, direction=str.split)
 
38
 
 
39
 
 
40
def _compat_rpartition(S, sep):
 
41
    """``rpartition(S, sep) -> (tail, sep, head)``
 
42
 
 
43
    Search for the separator ``sep`` in ``S``, starting at the end of ``S``,
 
44
    and return the part before it, the separator itself, and the part
 
45
    after it. If the separator is not found, return two empty
 
46
    strings and ``S``.
 
47
 
 
48
    """
 
49
    return _compat_rl_partition(S, sep, direction=str.rsplit)
 
50
 
 
51
try:
 
52
    partition = str.partition
 
53
    rpartition = str.rpartition
 
54
except AttributeError: # Python <= 2.4
 
55
    partition = _compat_partition
 
56
    rpartition = _compat_rpartition