Helpers
Helpers or helper function are common functions used throughout the project.
Addon Manager
Use this module as addon manager.
- matrixctl.addon_manager.import_commands_from(addon_directory, addon_module, parser_name)[source]
Import commands in (global) commands.
- Return type:
- Parameters:
- addon_directory
str
The absolute path as string to the addon directory.
- addon_module
str
The import path (with dots
.
not slashes/
) to the commands from project root e.g. “matrixctl.commands”.- parser_name
str
The name of the module the subparser is in.
- ..Note:
The nothing will be imported, when the subparser is not in (global) commands. To add the subparse to commands you need to decorate the subparsers with
matrixctl.addon_manager.subparser
- addon_directory
- Returns:
- none
None
The function always returns
None
.
- none
- matrixctl.addon_manager.setup(func)[source]
Add subparsers to the (main) parser.
- Return type:
- Parameters:
- func
matrixctl.addon_manager.ParserSetupType
A callback to the main parser.
- func
- Returns:
- parser
argparse.ArgumentParser
The parser which includes all subparsers.
- parser
- matrixctl.addon_manager.subparser(func)[source]
Decorate subparsers with, to add them to (global) commands on import.
- Return type:
- Parameters:
- func
matrixctl.addon_manager.SubParserType
A subparser.
- ..Note:
The nothing will be imported, when the subparser is not in (global) commands. To add the subparse to commands you need to decorate the subparsers with
matrixctl.addon_manager.subparser
- func
- Returns:
- decorated_func
matrixctl.addon_manager.SubParserType
The same subparser which was used as argument. (Without any changes)
- decorated_func
Package Version
Get the packages version.
The package’s version is determined by first checking if a
pyproject.toml``exists. If this is given, the version variable is
searched line by line in the file using a regular expression. When a
match occurs, the version is returned. If the ``pyproject.toml
does
not exist, e.g. because the package was installed, it uses the version
stored in the package’s metadata. In any case, if the version could not
be determined, it will return None
.
- matrixctl.package_version.get_version(name, file)[source]
Get the version of a Python package.
- Return type:
- Parameters:
- Returns:
Examples
# file: __init__.py from .package_version import get_version __version__: str | None = get_version(__name__, __file__) # or __version__: str = get_version(__name__, __file__) or "Unknown" # Optional: if __version__ is None: raise ValueError("Could not find the version of the package.")
# file: conf.py (sphinx) import sys sys.path.insert(0, os.path.abspath("../")) sys.path.insert(0, os.path.abspath("../..")) from matrixctl.package_version import get_version __version__: str = ( get_version("matrixctl", Path(__file__).parent) or "Unknown" )
Sanitizers
Use the functions of this module as printing helpers.
- class matrixctl.sanitizers.MessageType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
Enum
Use this enum for describing message types.
Supported events:
message_type
Usage
m.room.message
This event is used when sending messages in a room
m.room.name
This event sets the name of an room
m.room.topic
This events sets the room topic
m.room.avatar
This event sets the room avatar
m.room.pinned_events
This event pins events
m.room.member
Adjusts the membership state for a user in a room
m.room.join_rules
This event sets the join rules
m.room.create
This event creates a room
m.room.power_levels
This event sets a rooms power levels
m.room.redaction
This event redacts other events
- M_ROOM_AVATAR = 'm.room.avatar'
- M_ROOM_CREATE = 'm.room.create'
- M_ROOM_JOIN_RULES = 'm.room.join_rules'
- M_ROOM_MEMBER = 'm.room.member'
- M_ROOM_MESSAGE = 'm.room.message'
- M_ROOM_NAME = 'm.room.name'
- M_ROOM_PINNED_EVENTS = 'm.room.pinned_events'
- M_ROOM_POWER_LEVELS = 'm.room.power_levels'
- M_ROOM_REDACTION = 'm.room.redaction'
- M_ROOM_TOPIC = 'm.room.topic'
- matrixctl.sanitizers.sanitize(pattern, identifier, error_message)[source]
Create a new sanitizer based on compiled RegEx expressions.
A helper function for simplifying the latter sanitize identifier specific functions.
- Return type:
- Parameters:
- pattern
typing.Pattern
The RegEx pattern used for the specific sanitizing
- identifier
typing.Any
,optional
The identifier to sanitize based on the pattern
- error_message
str
The error string used for logging errors
- pattern
- Returns:
- result
typing.Literal
[False
]or
str
,optional
The function returns
None
ifidentifier
isNone
, the sanitized string, when it is valid, otherwiseFalse
- result
- matrixctl.sanitizers.sanitize_event_identifier(event_identifier)[source]
Sanitize an event identifier.
- Return type:
- Parameters:
- event_identifier
typeing.Any
The event identifier to sanitize
- event_identifier
- Returns:
- result
typing.Literal
[False
]or
str
,optional
The function returns
None
ifevent_identifier
isNone
, the sanitized string, when it is valid, otherwiseFalse
- result
Examples
>>> sanitize_event_identifier( ... "$event-abcdefghijklmH4omLrEumu7Pd01Qp-LySpK_Y" ... ) '$event-abcdefghijklmH4omLrEumu7Pd01Qp-LySpK_Y'
>>> sanitize_event_identifier( ... " $event-abcdefghijklmH4omLrEumu7Pd01Qp-LySpK_Y " ... ) '$event-abcdefghijklmH4omLrEumu7Pd01Qp-LySpK_Y'
>>> sanitize_event_identifier("something invalid") False
>>> sanitize_event_identifier(None)
- matrixctl.sanitizers.sanitize_message_type(message_type)[source]
Sanitize an message type.
- Return type:
Union
[MessageType
,Literal
[False
],None
]- Parameters:
- message_type
typing.Any
The event identifier to sanitize
- message_type
- Returns:
- message_type_sanitized
typing.Literal
[False
]or
MessageType
,optional
The function returns
None
ifmessage_type
isNone
,MessageType
, if it is valid, otherwiseFalse
- message_type_sanitized
Examples
>>> sanitize_message_type("m.room.message") <MessageType.M_ROOM_MESSAGE: 'm.room.message'>
>>> sanitize_message_type("M.RooM.MeSsAgE") <MessageType.M_ROOM_MESSAGE: 'm.room.message'>
>>> sanitize_message_type(" m.room.message ") <MessageType.M_ROOM_MESSAGE: 'm.room.message'>
>>> sanitize_message_type(MessageType.M_ROOM_MESSAGE) <MessageType.M_ROOM_MESSAGE: 'm.room.message'>
>>> sanitize_message_type("something invalid") False
>>> sanitize_message_type(None)
- matrixctl.sanitizers.sanitize_room_identifier(room_identifier)[source]
Sanitize an room identifier.
- Return type:
- Parameters:
- room_identifier
typing.Any
The room identifier to sanitize
- room_identifier
- Returns:
- room_identifier_sanitized
typing.Literal
[False
]or
str
,optional
The function returns
None
ifroom_identifier
isNone
, the sanitized string, when it is valid, otherwiseFalse
- room_identifier_sanitized
Examples
>>> sanitize_room_identifier("!room:domain.tld") '!room:domain.tld'
>>> sanitize_room_identifier(" !room:domain.tld ") '!room:domain.tld'
>>> sanitize_room_identifier("something invalid") False
>>> sanitize_room_identifier(None)
- matrixctl.sanitizers.sanitize_user_identifier(user_identifier)[source]
Sanitize an user identifier.
- Return type:
- Parameters:
- user_identifier
typing.Any
The user identifier to sanitize
- user_identifier
- Returns:
- event_identifier_sanitized
typing.Literal
[False
]or
str
,optional
The function returns
None
ifuser_identifier
isNone
, the sanitized string, when it is valid, otherwiseFalse
- event_identifier_sanitized
Examples
>>> sanitize_user_identifier("@user:domain.tld") '@user:domain.tld'
>>> sanitize_user_identifier(" @user:domain.tld ") '@user:domain.tld'
>>> sanitize_user_identifier("something invalid") False
>>> sanitize_user_identifier(None)
Print
Use the functions of this module as printing helpers.
Password
Use the functions of this module as helpers for passwords.
- matrixctl.password_helpers.ask_password()[source]
Ask the user to create a password.
The user will be asked twice for a password. After that the function compares the two entered passwords. If they are the same, and not empty, the function will return the password.
- matrixctl.password_helpers.ask_question(question='Is everything correct?')[source]
Asks the user a simple yes/no a question.
- Return type:
- Parameters:
- question
str
The yes/no question the user should be asked
- question
- Returns:
- answerbool
True
if the answer wasy
/j
, orFalse
if the answer wasn
Notes
The user entered value is case-insensitive.
If the user answered with an invalid answer (not
y
/j
/n
) the function asks again.
- matrixctl.password_helpers.create_user(user, admin=None)[source]
Ask the user to create a password.
The user will be asked twice for a password. After that the function compares the two entered passwords. If they are the same, and not empty, the function will ask the user if the data is correct without disclosing the password.
- Return type:
- Parameters:
- Returns:
- password
str
The user entered password.
- password