klipper_utils.klipper_utils

Generic Klipper utility functions and classes.

exception klipper_utils.klipper_utils.KlipperUtilsError

Bases: Exception

Generic error for Klipper utility functions.

add_note(note, /)

Add a note to the exception

with_traceback(tb, /)

Set self.__traceback__ to tb and return self.

class klipper_utils.klipper_utils.KlipperConfigDict

Bases: ABC

Klipper dictionary type config file of comma separated key-values pairs and helper functions.

Klipper config file example:

config-name=<key_1>=<value_1>, <key_2>=<value_2>, ...

Class represents the config file as instance variables:

@dataclass
class KlipperConfImpl(KlipperConf):
    key_1: float
    key_2: float
abstractmethod static get_title()

Get the list of config variables.

This defines the which instance variables are the config variables, their stored order and naturally the name of the values. E.g., returning [‘low’, ‘high’] means the class has to have members ‘low’ and ‘high’.

Returns:

List of key names.

Return type:

list[str]

get_data()

Get the dictionary of all config variables.

Returns:

Dict of config variables.

Return type:

dict[str, float]

to_string()

Comma separated list of all config variables get_title() as <key>=<value> pairs.

I.e.:

<key_1>=<value_1>, <key_2>=<value_2>
Returns:

String of all config variables.

Return type:

str

class klipper_utils.klipper_utils.KlipperUtils

Bases: object

Collection of Klipper utility functions.

static call_command(gcmd, fun, syntax_prefix='')

Handle Klipper GCODE command automatically.

Parameters are checked for required/optional/unknown parameters and converted to the correct type.

Parameters:
  • gcmd (Gcmd) – G-code object.

  • fun (Command) – Command function to be called.

  • syntax_prefix (str) – Prefix shown on syntax error, e.g. the command name.

Raises:

gcmd.error – If the parameters are invalid or missing, or if there are unknown parameters.

Return type:

None

static klipper_from_config_string(class_entry, string)

Convert comma separated key-values pairs ‘<key>=<value>,…’ to an instance created with matching kwargs.

Parameters:
  • class_entry (type[T]) – Instance to create with key-value dict.

  • string (str) –

    Comma separated list of a key-value pair:

    <key 1>=<value 1>, <key 2>=<value 2>...
    

Returns:

Instance of [T] created with dict of key-value pairs.

Return type:

T

static klipper_map_from_config(class_entry, entries)

Convert a list of comma separated key-values pairs [‘<key>=<value>,…’, …] to an instance created with matching kwargs.

Klipper config file example:

config-name=<key 1a>=<value 1a>, <key 2a>=<value 2a>, ...
    <key 1b>=<value 1b>, <key 2a>=<value 2b>, ...
...
Parameters:
  • class_entry (type[T]) – Instance to create with key-value dict.

  • entries (list[str]) –

    Config as a list of strings, Example:

    config.getlists("config-name", seps='\n', default=[])
    

Returns:

List of objects with type [T]

Return type:

list[T]

static register_command(gcode, function, cmd_name, help_text, fail_on_error)

Register a command with Klipper G-code parser.

Parameters:
  • gcode (Gcmd) – G-code object to register the command with.

  • function (Command) – Function to handle the command. Signature should be function(gcmd, parameters…).

  • cmd_name (str) – Name of the command to register.

  • help_text (str) – Help text for the command shown in HELP G-code.

  • fail_on_error (bool) – True: raise an error on command failure. False: just log the error.

Return type:

None