From 1aeb503b1d348b428315d1ff8ddb0acc9559dbad Mon Sep 17 00:00:00 2001 From: changzhi1990 Date: Wed, 6 Apr 2016 11:46:03 +0800 Subject: [PATCH] Add uuid utils Change-Id: Iae18676b91218dc5d7f0f74aa64056811cd82776 --- steth/stethclient/utils.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/steth/stethclient/utils.py b/steth/stethclient/utils.py index 67d2626..ccde4e5 100644 --- a/steth/stethclient/utils.py +++ b/steth/stethclient/utils.py @@ -18,6 +18,7 @@ import jsonrpclib import six import socket import sys +import uuid def safe_decode(text, incoming=None, errors='strict'): @@ -134,3 +135,32 @@ def get_ip_from_agent(node, net_type): return 1 except Exception as e: print "Can't get ip! Because: %s" % e + + +def generate_uuid(): + """Creates a random uuid string. + + :returns: string + """ + return str(uuid.uuid4()) + + +def _format_uuid_string(string): + return (string.replace('urn:', '') + .replace('uuid:', '') + .strip('{}') + .replace('-', '') + .lower()) + + +def is_uuid_like(val): + """Returns validation of a value as a UUID. + + :param val: Value to verify + :type val: string + :returns: bool + """ + try: + return str(uuid.UUID(val)).replace('-', '') == _format_uuid_string(val) + except (TypeError, ValueError, AttributeError): + return False