From 55591cce430b1300050fff22942c5c2898b34f5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vincent=20Fran=C3=A7oise?= Date: Fri, 23 Sep 2016 11:12:26 +0200 Subject: [PATCH] Watcher utils cleanup As some of our utils function are actually available in oslo.utils, this patchset simply gets rid of our local copy and instead references the oslo.utils one. This will help us deport the code maintenance of these to a 3rd party library. Change-Id: I3760ebab69d35c37bbe78500918d4e46b5bd9119 --- watcher/common/utils.py | 47 ++++++++++++----------------------------- 1 file changed, 13 insertions(+), 34 deletions(-) diff --git a/watcher/common/utils.py b/watcher/common/utils.py index 44fef6bbf..c2f3b1e81 100644 --- a/watcher/common/utils.py +++ b/watcher/common/utils.py @@ -16,17 +16,19 @@ """Utilities and helper functions.""" +import re + from jsonschema import validators from oslo_config import cfg from oslo_log import log as logging -from watcher.common import exception - -import re +from oslo_utils import strutils +from oslo_utils import timeutils +from oslo_utils import uuidutils import six -import uuid - from watcher._i18n import _LW +from watcher.common import exception + UTILS_OPTS = [ cfg.StrOpt('rootwrap_config', @@ -66,6 +68,12 @@ class Struct(dict): raise AttributeError(name) +generate_uuid = uuidutils.generate_uuid +is_uuid_like = uuidutils.is_uuid_like +is_int_like = strutils.is_int_like +strtime = timeutils.strtime + + def safe_rstrip(value, chars=None): """Removes trailing characters from a string if that does not make it empty @@ -83,31 +91,6 @@ def safe_rstrip(value, chars=None): return value.rstrip(chars) or value -def generate_uuid(): - return str(uuid.uuid4()) - - -def is_int_like(val): - """Check if a value looks like an int.""" - try: - return str(int(val)) == str(val) - except Exception: - return False - - -def is_uuid_like(val): - """Returns validation of a value as a UUID. - - For our purposes, a UUID is a canonical form string: - aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa - - """ - try: - return str(uuid.UUID(val)) == val - except (TypeError, ValueError, AttributeError): - return False - - def is_hostname_safe(hostname): """Determine if the supplied hostname is RFC compliant. @@ -133,10 +116,6 @@ def get_cls_import_path(cls): return module + '.' + cls.__name__ -def strtime(at): - return at.strftime("%Y-%m-%dT%H:%M:%S.%f") - - # Default value feedback extension as jsonschema doesn't support it def extend_with_default(validator_class): validate_properties = validator_class.VALIDATORS["properties"]