From ca2210abdab35d1a84d1986b9f9f57016a98dee7 Mon Sep 17 00:00:00 2001 From: jantari Date: Thu, 7 Jul 2022 09:50:48 +0200 Subject: [PATCH 1/2] fix #808 by removing non-ascii characters and using strip before sub --- plugins/module_utils/netbox_utils.py | 4 +++- tests/unit/module_utils/test_data/normalize_data/data.json | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/plugins/module_utils/netbox_utils.py b/plugins/module_utils/netbox_utils.py index 134fefd37..dde0e3257 100644 --- a/plugins/module_utils/netbox_utils.py +++ b/plugins/module_utils/netbox_utils.py @@ -1162,8 +1162,10 @@ def _to_slug(self, value): return value else: removed_chars = re.sub(r"[^\-\.\w\s]", "", value) + removed_chars = removed_chars.encode('ascii', errors='ignore').decode() + removed_chars = removed_chars.strip() convert_chars = re.sub(r"[\-\.\s]+", "-", removed_chars) - return convert_chars.strip().lower() + return convert_chars.lower() def _normalize_data(self, data): """ diff --git a/tests/unit/module_utils/test_data/normalize_data/data.json b/tests/unit/module_utils/test_data/normalize_data/data.json index be7f861d2..f6d2438b0 100644 --- a/tests/unit/module_utils/test_data/normalize_data/data.json +++ b/tests/unit/module_utils/test_data/normalize_data/data.json @@ -217,7 +217,7 @@ }, { "before": { - "slug": "Test to_slug" + "slug": "Test to_slug© ⅛ äüöß 👀" }, "after": { "slug": "test-to_slug" @@ -345,4 +345,4 @@ "mac_address": "AA:BB:CC:DD:EE:FF" } } -] \ No newline at end of file +] From 80a890bc9e4f0d28ee498e7df0f91c3d0df27a6e Mon Sep 17 00:00:00 2001 From: jantari Date: Thu, 7 Jul 2022 16:52:02 +0200 Subject: [PATCH 2/2] address Black CI failure: use double quotes --- plugins/module_utils/netbox_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/module_utils/netbox_utils.py b/plugins/module_utils/netbox_utils.py index dde0e3257..e80b7a497 100644 --- a/plugins/module_utils/netbox_utils.py +++ b/plugins/module_utils/netbox_utils.py @@ -1162,7 +1162,7 @@ def _to_slug(self, value): return value else: removed_chars = re.sub(r"[^\-\.\w\s]", "", value) - removed_chars = removed_chars.encode('ascii', errors='ignore').decode() + removed_chars = removed_chars.encode('ascii', errors="ignore").decode() removed_chars = removed_chars.strip() convert_chars = re.sub(r"[\-\.\s]+", "-", removed_chars) return convert_chars.lower()