From 4b6b3d70807f014a751d6cad7ac8919df9b840cc Mon Sep 17 00:00:00 2001 From: Kenneth Raplee Date: Wed, 14 Dec 2022 00:29:04 -0800 Subject: [PATCH 1/3] Consolidate sanitizer code into a reusable function --- matrixctl/sanitizers.py | 72 ++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/matrixctl/sanitizers.py b/matrixctl/sanitizers.py index 74846edb..6dd65d55 100644 --- a/matrixctl/sanitizers.py +++ b/matrixctl/sanitizers.py @@ -120,6 +120,21 @@ def sanitize_message_type( return False +def sanitize( + pattern: t.Pattern[str], + identifier: t.Any | None, + error_message: str, +) -> str | t.Literal[False] | None: + if identifier is None: + return None + with suppress(TypeError, AttributeError): + identifier = str(identifier).strip() + if pattern.match(identifier): + return t.cast(str, identifier) + logger.error(error_message) + return False + + def sanitize_event_identifier( event_identifier: t.Any, ) -> str | t.Literal[False] | None: @@ -154,18 +169,15 @@ def sanitize_event_identifier( the sanitized string, when it is valid, otherwise ``False`` """ - if event_identifier is None: - return None - with suppress(TypeError, AttributeError): - event_identifier = str(event_identifier).strip() - if EVENT_ID_PATTERN.match(event_identifier): - return t.cast(str, event_identifier) - logger.error( - "The given event identifier has an invalid format. Please make" - " sure you use one with the correct format. For example:" - " $tjeDdqYAk9BDLAUcniGUy640e_D9TrWU2RmCksJQQEQ" + return sanitize( + pattern=EVENT_ID_PATTERN, + identifier=event_identifier, + error_message=( + "The given event identifier has an invalid format. Please make" + " sure you use one with the correct format. For example:" + " $tjeDdqYAk9BDLAUcniGUy640e_D9TrWU2RmCksJQQEQ" + ), ) - return False def sanitize_user_identifier( @@ -202,18 +214,15 @@ def sanitize_user_identifier( the sanitized string, when it is valid, otherwise ``False`` """ - if user_identifier is None: - return None - with suppress(TypeError, AttributeError): - user_identifier = str(user_identifier).strip() - if USER_ID_PATTERN.match(user_identifier): - return t.cast(str, user_identifier) - logger.error( - "The given user identifier has an invalid format. Please make sure" - " you use one with the correct format. For example:" - " @username:domain.tld" + return sanitize( + pattern=USER_ID_PATTERN, + identifier=user_identifier, + error_message=( + "The given user identifier has an invalid format. Please make sure" + " you use one with the correct format. For example:" + " @username:domain.tld" + ), ) - return False def sanitize_room_identifier( @@ -250,18 +259,15 @@ def sanitize_room_identifier( the sanitized string, when it is valid, otherwise ``False`` """ - if room_identifier is None: - return None - with suppress(TypeError, AttributeError): - room_identifier = str(room_identifier).strip() - if ROOM_ID_PATTERN.match(room_identifier): - return t.cast(str, room_identifier) - logger.error( - "The given room identifier has an invalid format. Please make sure" - " you use one with the correct format. For example:" - " !iuyQXswfjgxQMZGrfQ:matrix.org" + return sanitize( + pattern=ROOM_ID_PATTERN, + identifier=room_identifier, + error_message=( + "The given room identifier has an invalid format. Please make sure" + " you use one with the correct format. For example:" + " !iuyQXswfjgxQMZGrfQ:matrix.org" + ), ) - return False # vim: set ft=python : From bd7dd1b455ead5ad0fc8fa11beb47408b564ab75 Mon Sep 17 00:00:00 2001 From: Kenneth Raplee Date: Wed, 14 Dec 2022 22:34:10 -0800 Subject: [PATCH 2/3] Add required docstring to `sanitize` function --- matrixctl/sanitizers.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/matrixctl/sanitizers.py b/matrixctl/sanitizers.py index 6dd65d55..48e25d7c 100644 --- a/matrixctl/sanitizers.py +++ b/matrixctl/sanitizers.py @@ -125,6 +125,12 @@ def sanitize( identifier: t.Any | None, error_message: str, ) -> str | t.Literal[False] | None: + """Create a new sanitizer based on compiled RegEx expressions. + + A helper function for simplifying the latter sanitize identifier specific + functions. + + """ if identifier is None: return None with suppress(TypeError, AttributeError): From 84b2a257e25d23f50aad8618293ad1f03842dbd1 Mon Sep 17 00:00:00 2001 From: Kenneth Raplee Date: Thu, 15 Dec 2022 20:21:35 -0800 Subject: [PATCH 3/3] Fix the docstring for the `sanitize` function --- matrixctl/sanitizers.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/matrixctl/sanitizers.py b/matrixctl/sanitizers.py index 48e25d7c..d7257dd2 100644 --- a/matrixctl/sanitizers.py +++ b/matrixctl/sanitizers.py @@ -130,6 +130,21 @@ def sanitize( A helper function for simplifying the latter sanitize identifier specific functions. + Parameters + ---------- + pattern : typing.Pattern + The RegEx pattern used for the specific sanitizing + identifier : typing.Any | None + The identifier to sanitize based on the pattern + error_message : str + The error string used for logging errors + + Returns + ------- + result : typing.Literal[False] or str, optional + The function returns ``None`` if ``identifier`` is ``None``, + the sanitized string, when it is valid, otherwise ``False`` + """ if identifier is None: return None