Skip to content

Commit

Permalink
Merge pull request #446 from kenrap/street_sweeping
Browse files Browse the repository at this point in the history
Street Sweeping (or whatever kind of refactoring you want to call it)
  • Loading branch information
MichaelSasser authored Dec 16, 2022
2 parents 75e5611 + 84b2a25 commit f294e31
Showing 1 changed file with 60 additions and 33 deletions.
93 changes: 60 additions & 33 deletions matrixctl/sanitizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,42 @@ def sanitize_message_type(
return False


def sanitize(
pattern: t.Pattern[str],
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.
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
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:
Expand Down Expand Up @@ -154,18 +190,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(
Expand Down Expand Up @@ -202,18 +235,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(
Expand Down Expand Up @@ -250,18 +280,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 :

0 comments on commit f294e31

Please sign in to comment.