Helpers
Helpers or helper function are common functions used throughout the project.
Command
Use this module to dynamically create a commands structure.
- class matrixctl.command.SubCommand(*values)[source]
Bases:
EnumUse to enumerate possible subcommands for the command-line interface.
Each member represents a distinct subcommand that can be invoked by the user.
- MEDIA = 3
- MOD = 5
- ROOM = 1
- SELF = 6
- SERVER = 4
- USER = 2
- static generate_commands()[source]
Generate a dictionary of subcommands with empty lists.
- Return type:
dict[SubCommand,list[Callable[[_SubParsersAction,ArgumentParser],None]]]
- matrixctl.command.create_error_handler(parser)[source]
Create a custom error handler for each subparser to display its help.
Creates a custom error handler for an argparse subparser that prints the parser’s help message and exits with status code 2 when called.
- matrixctl.command.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.command.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.command.subparser(subcommand)[source]
Decorate subparsers with, to add them to (global) commands on import.
- Return type:
Callable[[Callable[[_SubParsersAction,ArgumentParser],None]],Callable[[_SubParsersAction,ArgumentParser],None]]- Parameters:
- fn
matrixctl.addon_manager.SubParserType The supparser to be wrapped.
- subcommand
matrixctl.addon_manager.SubCommand The subcommand (or category) to which the subparser belongs.
- ..Note:
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
- fn
- 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.EventType(*values)[source]
Bases:
EnumUse this enum for describing message types.
Supported events:
message_type
Usage
m.room.message
This event to send messages to 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 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.reaction
This event is a reaction to another event
m.room.encrypted
This event is used for encrypted messages
m.room.history_visibility
This event sets the history visibility
m.room.guest_access
This event sets the guest access
- M_REACTION = 'm.reaction'
- M_ROOM_AVATAR = 'm.room.avatar'
- M_ROOM_CREATE = 'm.room.create'
- M_ROOM_ENCRYPTED = 'm.room.encrypted'
- M_ROOM_GUEST_ACCESS = 'm.room.guest_access'
- M_ROOM_HISTORY_VISIBILITY = 'm.room.history_visibility'
- 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, fn_alias=None)[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
- fn_alias
typing.Callable,optional A function that can be used to convert an alias to an identifier
- pattern
- Returns:
- result
typing.Literal[False]orstr,optional The function returns
NoneifidentifierisNone, the sanitized string, when it is valid, otherwiseFalse
- result
- matrixctl.sanitizers.sanitize_event_identifier(event_identifier, _=None)[source]
Sanitize an event identifier.
- Return type:
- Parameters:
- event_identifier
typeing.Any The event identifier to sanitize
- event_identifier
- Returns:
- result
typing.Literal[False]orstr,optional The function returns
Noneifevent_identifierisNone, 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_event_type(event_type, _=None)[source]
Sanitize an event type.
- Return type:
- Parameters:
- event_type
typing.Any The event identifier to sanitize
- event_type
- Returns:
- event_type_sanitized
typing.Literal[False]orMessageType,optional The function returns
Noneifmessage_typeisNone,MessageType, if it is valid, otherwiseFalse
- event_type_sanitized
Examples
>>> sanitize_event_type("m.room.message") <EventType.M_ROOM_MESSAGE: 'm.room.message'>
>>> sanitize_event_type("M.RooM.MeSsAgE") <EventType.M_ROOM_MESSAGE: 'm.room.message'>
>>> sanitize_event_type(" m.room.message ") <EventType.M_ROOM_MESSAGE: 'm.room.message'>
>>> sanitize_event_type(EventType.M_ROOM_MESSAGE) <EventType.M_ROOM_MESSAGE: 'm.room.message'>
>>> sanitize_event_type("something invalid") False
>>> sanitize_event_type(None)
- matrixctl.sanitizers.sanitize_mxc(uri, _=None)[source]
Sanitize an room identifier.
- Return type:
- Parameters:
- mxc
typing.Any The media URI to sanitize
- mxc
- Returns:
- mxc_sanitized
typing.Literal[False]orstr,optional The function returns
NoneifmxcisNone, the sanitized string, when it is valid, otherwiseFalse
- mxc_sanitized
Examples
>>> sanitize_mxc("mxc://matrix.org/asdfDfjskksjdiIlakjidjLAjdj") 'mxc://matrix.org/asdfDfjskksjdiIlakjidjLAjdj'
>>> sanitize_mxc(" mxc://matrix.org/asdfDfjskksjdiIlakjidjLAjdj ") 'mxc://matrix.org/asdfDfjskksjdiIlakjidjLAjdj'
>>> sanitize_mxc("something invalid") False
>>> sanitize_mxc(None)
- matrixctl.sanitizers.sanitize_room_identifier(room_identifier_or_alias, fn_alias=None)[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]orstr,optional The function returns
Noneifroom_identifierisNone, 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_sequence(sanitizer, args, fn_alias=None)[source]
Run a sanitizer against a sequence.
- matrixctl.sanitizers.sanitize_user_identifier(user_identifier, _=None)[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]orstr,optional The function returns
Noneifuser_identifierisNone, 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.
- matrixctl.print_helpers.human_readable_bool(b)[source]
Use this helper function to get a “yes” or “no” string from a “bool”.
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
Trueif the answer wasy/j, orFalseif 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