commandkit package

Submodules

commandkit.EventManager module

This module contains a class responsible for managing event listeners and dispatching events.

class commandkit.EventManager.EventManager(events: dict[str, list[Callable]] | None = None)[source]

Bases: object

responsible for managing event listeners and dispatching events.

Parameters:

events (dict[str, list[Callable]])

add_listen(func: Callable, name: str | None = None)[source]

adds a new event listener

Parameters:
  • func (Callable) – the listener

  • name (str (default: None)) – the name of the listener. (uses the listeners name if None).

async dispatch(event_name: str, /, *args, **kwargs)[source]

dispatch an event

Parameters:
  • event_name (str) – the name of the event

  • *args – the arguments of the event

  • **kwargs – the keyword arguments of the event

event(func: Callable)[source]

a decorator to add an event listener

Parameters:

func (Callable) – the listener function

remove_listen(func: Callable, name: str | None = None)[source]

removes an event listener

Parameters:
  • func (Callable) – the listener

  • name (str (default: None)) – the name of the listener. (uses the listeners name if None).

async schedule_event(event: Callable, ev: str, /, *args, **kwargs)[source]

handles firing the event

Parameters:
  • event (Callable) – the event to fire

  • ev (str) – the event name

  • *args – the arguments of the event

  • **kwargs – the keyword arguments of the event

commandkit.commander module

module contains classes to manage and dispatch commands

class commandkit.commander.CommandManager(prefix: str = '')[source]

Bases: object

A class to manage and dispatch commands.

add_command(command: Command)[source]

add a new command

Parameters:

command (Command) – the command object to add

command(name: str | None = None, description: str | None = None, **kwargs)[source]

a decorator to register a command

Parameters:
  • name (str (default: None)) – the name of the command

  • description (str (default: None)) – a short description of the command

get_command(name: str) Command | None[source]

get command by name or alias

Parameters:

name (str) – the name or alias of the command

Returns:

the command object if found

Return type:

Optional[Command]

async invoke(command: Command, view: StringView) Any[source]

invokes a command by parsing arguments from a StringView

Parameters:
  • command (Command) – the command to invoke

  • view (StringView) – the view containing raw arguments

async process_command(string: str) Any[source]

processes a raw string to find and execute a command

Parameters:

string (str) – the raw command string to process

commandkit.core module

module containing Classes and functions to manage commands

exception commandkit.core.BadArgument(message, parameters, command)[source]

Bases: CommandError

when a parsing command arguments fails

class commandkit.core.Command(callback: Callable, name: str | None = None, description: str | None = None, **kwargs)[source]

Bases: object

Represents a command.

async invoke(*args, **kwargs) Any[source]

call the command function

Parameters:
  • args (tuple) – positional arguments

  • kwargs (dict) – keyword arguments

Returns:

whatever the command returns

Return type:

Any

exception commandkit.core.CommandError[source]

Bases: Exception

basic CommandError exception

exception commandkit.core.CommandNotFoundError(message, name)[source]

Bases: CommandError

when command doesn’t exist

commandkit.parsers module

Type conversion system for commandkit.

Converts string arguments to typed values based on annotations. Handles Union, Optional, custom converters, and callable fallback.

class commandkit.parsers.converter.Converter[source]

Bases: object

Base class for custom converters.

Example

class HexColor(Converter):
        def convert(self, argument: str) -> int:
                return int(argument.lstrip('#'), 16)
convert(argument: str) Any[source]
class commandkit.parsers.converter.Greedy(converter: Any)[source]

Bases: object

Marker class for greedy argument parsing.

Example: Greedy[int] consumes all successful int conversions.

commandkit.parsers.converter.register_converter(type_: type, converter: Any) None[source]

Register a custom converter (callable or Converter instance).

commandkit.parsers.converter.run_converters(converter: Any, argument: str) Any[source]

Convert argument using annotation. Unwraps Union/Optional first.

Parameters:
  • converter (Any) – Type annotation or Converter instance.

  • argument (str) – Raw string to convert.

Raises:

ConversionError – If all conversion attempts fail.

commandkit.parsers.converter.unregister_converter(type_: type) None[source]

Remove a registered converter.

The MIT License (MIT)

Copyright (c) 2015-present Rapptz

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

class commandkit.parsers.view.StringView(buffer: str)[source]

Bases: object

property current: str | None
property eof: bool
get() str | None[source]
get_quoted_word() str | None[source]
get_word() str[source]
read(n: int) str[source]
read_rest() str[source]
skip_string(string: str) bool[source]
skip_ws() bool[source]
undo() None[source]

Module contents

commandkit is a package to make managing and implementing events managers, commands easier

exception commandkit.BadArgument(message, parameters, command)[source]

Bases: CommandError

when a parsing command arguments fails

class commandkit.Command(callback: Callable, name: str | None = None, description: str | None = None, **kwargs)[source]

Bases: object

Represents a command.

async invoke(*args, **kwargs) Any[source]

call the command function

Parameters:
  • args (tuple) – positional arguments

  • kwargs (dict) – keyword arguments

Returns:

whatever the command returns

Return type:

Any

exception commandkit.CommandError[source]

Bases: Exception

basic CommandError exception

class commandkit.CommandManager(prefix: str = '')[source]

Bases: object

A class to manage and dispatch commands.

add_command(command: Command)[source]

add a new command

Parameters:

command (Command) – the command object to add

command(name: str | None = None, description: str | None = None, **kwargs)[source]

a decorator to register a command

Parameters:
  • name (str (default: None)) – the name of the command

  • description (str (default: None)) – a short description of the command

get_command(name: str) Command | None[source]

get command by name or alias

Parameters:

name (str) – the name or alias of the command

Returns:

the command object if found

Return type:

Optional[Command]

async invoke(command: Command, view: StringView) Any[source]

invokes a command by parsing arguments from a StringView

Parameters:
  • command (Command) – the command to invoke

  • view (StringView) – the view containing raw arguments

async process_command(string: str) Any[source]

processes a raw string to find and execute a command

Parameters:

string (str) – the raw command string to process

exception commandkit.CommandNotFoundError(message, name)[source]

Bases: CommandError

when command doesn’t exist

class commandkit.EventManager(events: dict[str, list[Callable]] | None = None)[source]

Bases: object

responsible for managing event listeners and dispatching events.

Parameters:

events (dict[str, list[Callable]])

add_listen(func: Callable, name: str | None = None)[source]

adds a new event listener

Parameters:
  • func (Callable) – the listener

  • name (str (default: None)) – the name of the listener. (uses the listeners name if None).

async dispatch(event_name: str, /, *args, **kwargs)[source]

dispatch an event

Parameters:
  • event_name (str) – the name of the event

  • *args – the arguments of the event

  • **kwargs – the keyword arguments of the event

event(func: Callable)[source]

a decorator to add an event listener

Parameters:

func (Callable) – the listener function

remove_listen(func: Callable, name: str | None = None)[source]

removes an event listener

Parameters:
  • func (Callable) – the listener

  • name (str (default: None)) – the name of the listener. (uses the listeners name if None).

async schedule_event(event: Callable, ev: str, /, *args, **kwargs)[source]

handles firing the event

Parameters:
  • event (Callable) – the event to fire

  • ev (str) – the event name

  • *args – the arguments of the event

  • **kwargs – the keyword arguments of the event

class commandkit.Greedy(converter: Any)[source]

Bases: object

Marker class for greedy argument parsing.

Example: Greedy[int] consumes all successful int conversions.