From 8daa14eb365cd737eddac8e8974cb53940fa0e37 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 27 Sep 2018 11:12:22 +0200 Subject: [PATCH 1/4] module: make Utils.create_uuid() working without pygobject Utils.create_uuid() only needs the "uuid" module, which is commonly available. Don't load it together with the NM module, as that requires pygobject. --- module_utils/network_lsr/utils.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/module_utils/network_lsr/utils.py b/module_utils/network_lsr/utils.py index ff16bfd..8f84c31 100644 --- a/module_utils/network_lsr/utils.py +++ b/module_utils/network_lsr/utils.py @@ -5,6 +5,7 @@ import os import socket import sys +import uuid # pylint: disable=import-error, no-name-in-module from ansible.module_utils.network_lsr import MyError @@ -40,8 +41,7 @@ class Util: @classmethod def create_uuid(cls): - cls.NM() - return str(cls._uuid.uuid4()) + return str(uuid.uuid4()) @classmethod def NM(cls): @@ -60,9 +60,6 @@ class Util: cls._Gio = Gio cls._GObject = GObject n = NM - import uuid - - cls._uuid = uuid return n @classmethod From 6856b5407d86cacd9c56c6b347d1ca326d6adcdd Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 27 Sep 2018 11:12:22 +0200 Subject: [PATCH 2/4] module: rename internal ArgValidator._validate() First a trivial renaming because the next commit will add a different "_validate" method. --- .../network_lsr/argument_validator.py | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/module_utils/network_lsr/argument_validator.py b/module_utils/network_lsr/argument_validator.py index 98b584a..38177ba 100644 --- a/module_utils/network_lsr/argument_validator.py +++ b/module_utils/network_lsr/argument_validator.py @@ -91,12 +91,12 @@ class ArgValidator: except Exception: # pylint: disable=broad-except return self.default_value - def _validate(self, value, name): + def _validate_impl(self, value, name): raise NotImplementedError() def validate(self, value, name=None): name = name or self.name or "" - validated = self._validate(value, name) + validated = self._validate_impl(value, name) return self._validate_post(value, name, validated) # pylint: disable=unused-argument,no-self-use @@ -117,7 +117,7 @@ class ArgValidatorStr(ArgValidator): self.enum_values = enum_values self.allow_empty = allow_empty - def _validate(self, value, name): + def _validate_impl(self, value, name): if not isinstance(value, Util.STRING_TYPE): raise ValidationError(name, "must be a string but is '%s'" % (value)) value = str(value) @@ -154,7 +154,7 @@ class ArgValidatorNum(ArgValidator): self.val_max = val_max self.numeric_type = numeric_type - def _validate(self, value, name): + def _validate_impl(self, value, name): v = None try: if isinstance(value, self.numeric_type): @@ -185,7 +185,7 @@ class ArgValidatorBool(ArgValidator): def __init__(self, name, required=False, default_value=False): ArgValidator.__init__(self, name, required, default_value) - def _validate(self, value, name): + def _validate_impl(self, value, name): try: if isinstance(value, bool): return value @@ -212,7 +212,7 @@ class ArgValidatorDict(ArgValidator): self.nested = {} self.all_missing_during_validate = all_missing_during_validate - def _validate(self, value, name): + def _validate_impl(self, value, name): result = {} seen_keys = set() try: @@ -247,7 +247,7 @@ class ArgValidatorList(ArgValidator): ArgValidator.__init__(self, name, required=False, default_value=default_value) self.nested = nested - def _validate(self, value, name): + def _validate_impl(self, value, name): if isinstance(value, Util.STRING_TYPE): # we expect a list. However, for convenience allow to @@ -273,8 +273,8 @@ class ArgValidatorIP(ArgValidatorStr): self.family = family self.plain_address = plain_address - def _validate(self, value, name): - v = ArgValidatorStr._validate(self, value, name) + def _validate_impl(self, value, name): + v = ArgValidatorStr._validate_impl(self, value, name) try: addr, family = Util.parse_ip(v, self.family) except Exception: @@ -293,8 +293,8 @@ class ArgValidatorMac(ArgValidatorStr): ArgValidatorStr.__init__(self, name, required, default_value, None) self.force_len = force_len - def _validate(self, value, name): - v = ArgValidatorStr._validate(self, value, name) + def _validate_impl(self, value, name): + v = ArgValidatorStr._validate_impl(self, value, name) try: addr = Util.mac_aton(v, self.force_len) except MyError: @@ -323,7 +323,7 @@ class ArgValidatorIPAddr(ArgValidatorDict): ) self.family = family - def _validate(self, value, name): + def _validate_impl(self, value, name): if isinstance(value, Util.STRING_TYPE): v = str(value) if not v: @@ -336,7 +336,7 @@ class ArgValidatorIPAddr(ArgValidatorDict): "value '%s' is not a valid IP%s address with prefix length" % (value, Util.addr_family_to_v(self.family)), ) - v = ArgValidatorDict._validate(self, value, name) + v = ArgValidatorDict._validate_impl(self, value, name) return { "address": v["address"]["address"], "family": v["address"]["family"], From 97e216c716415171e747bbd62572df333a4dca26 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 27 Sep 2018 11:30:24 +0200 Subject: [PATCH 3/4] module: don't expose "name" parameter for ArgValidator.validate() The "name" argument is an implementation detail, that is used by ArgValidatorDict and ArgValidatorList to pass a complex (nested) name of what is currently parsed. Callers are not supposed to see or use this argument. Hide it, by adding an internal helper method _validate(). --- module_utils/network_lsr/argument_validator.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/module_utils/network_lsr/argument_validator.py b/module_utils/network_lsr/argument_validator.py index 38177ba..5edc827 100644 --- a/module_utils/network_lsr/argument_validator.py +++ b/module_utils/network_lsr/argument_validator.py @@ -91,14 +91,16 @@ class ArgValidator: except Exception: # pylint: disable=broad-except return self.default_value - def _validate_impl(self, value, name): - raise NotImplementedError() + def validate(self, value): + return self._validate(value, self.name) - def validate(self, value, name=None): - name = name or self.name or "" + def _validate(self, value, name): validated = self._validate_impl(value, name) return self._validate_post(value, name, validated) + def _validate_impl(self, value, name): + raise NotImplementedError() + # pylint: disable=unused-argument,no-self-use def _validate_post(self, value, name, result): return result @@ -227,7 +229,7 @@ class ArgValidatorDict(ArgValidator): if validator is None: raise ValidationError(name, "invalid key '%s'" % (k)) try: - vv = validator.validate(v, name + "." + k) + vv = validator._validate(v, name + "." + k) except ValidationError as e: raise ValidationError(e.name, e.error_message) result[k] = vv @@ -258,7 +260,7 @@ class ArgValidatorList(ArgValidator): result = [] for (idx, v) in enumerate(value): try: - vv = self.nested.validate(v, name + "[" + str(idx) + "]") + vv = self.nested._validate(v, name + "[" + str(idx) + "]") except ValidationError as e: raise ValidationError(e.name, e.error_message) result.append(vv) From b4972517a4f408b3208da9ebf4399e062f4abb9d Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 27 Sep 2018 11:38:21 +0200 Subject: [PATCH 4/4] module: fix name for ArgValidator_DictConnection The name is actually not ever used, because ArgValidator_DictConnection() is never validated directly. Instead, it is always nested inside ArgValidator_ListConnections() which passes "connections[$IDX]" as name to self.nested._validate(). Anyway, still when looking at the name of a ArgValidator_DictConnection instance, it makes slightly more sense to call it just "connection". --- module_utils/network_lsr/argument_validator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/module_utils/network_lsr/argument_validator.py b/module_utils/network_lsr/argument_validator.py index 5edc827..413f246 100644 --- a/module_utils/network_lsr/argument_validator.py +++ b/module_utils/network_lsr/argument_validator.py @@ -628,7 +628,7 @@ class ArgValidator_DictConnection(ArgValidatorDict): def __init__(self): ArgValidatorDict.__init__( self, - name="connections[?]", + name="connection", nested=[ ArgValidatorStr("name"), ArgValidatorStr(