Skip to content

Python scripting

Python scripting may be achieved by importing the moulti.client module.

Main functions

  • moulti_connect() connects to a Moulti instance;
  • send_json_message() sends a message to a Moulti instance;
  • recv_json_message() receives a message from a Moulti instance;
  • send_to_moulti combines all three functions above: it connects to a Moulti instance, sends a single message, waits for a response, disconnects and returns the response.

A Moulti message is simply a Python dict reflecting moulti command-line arguments.

Example 1

This example creates and fills a new step from a string through two separate connections:

from moulti.client import send_to_moulti

# Create a new step:
# moulti step add python --title="Step created from Python" --no-collapsed
add_step_msg = {
    'command': 'step', 'action': 'add', 'id': 'python',
    'title': 'Step created from Python', 'collapsed': False,
}
send_to_moulti(add_step_msg)

# Generate text and update the step:
text = '\n'.join(['Python made this: ' + chr(i)*62 for i in range(33, 127)])
# moulti step update python --text="${text}"
set_text_msg = {
    'command': 'step', 'action': 'update', 'id': 'python',
    'text': text,
}
send_to_moulti(set_text_msg)

                                               Moulti                                                ▼ Step created from Python Python made this: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff Python made this: gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg Python made this: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh Python made this: iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii Python made this: jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj Python made this: kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk Python made this: llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll Python made this: mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm Python made this: nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn Python made this: oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo Python made this: pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp Python made this: qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq Python made this: rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr Python made this: ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss Python made this: tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt Python made this: uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu Python made this: vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv Python made this: wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww Python made this: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx▆▆ Python made this: yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy Python made this: zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz Python made this: {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ Python made this: |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Python made this: }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} Python made this: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  l  Lock scroll  s  Save  z  Console   x   Expand all  o  Collapse all  d  Light  h   Help   q  Quit 

Example 2

This examples creates and fills a new step through a single connection:

from moulti.client import moulti_connect, recv_json_message, send_json_message

filepath = '/etc/passwd'
with moulti_connect() as moulti_socket:
    # Create a new step:
    # moulti step add python --title="${filepath}:" --no-collapsed
    add_step_msg = {
        'command': 'step', 'action': 'add', 'id': 'python',
        'title': f'{filepath}:', 'collapsed': False,
    }
    send_json_message(moulti_socket, add_step_msg)
    recv_json_message(moulti_socket)

    # Fill that new step with the contents of a regular file: 
    with open(filepath) as filedesc:
        # moulti pass python < /etc/passwd
        pass_fd_msg = {'command': 'pass', 'id': 'python'}
        send_json_message(moulti_socket, pass_fd_msg, [filedesc.fileno()])
        recv_json_message(moulti_socket)

                                               Moulti                                                ▼ /etc/passwd: root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin sys:x:3:3:sys:/dev:/usr/sbin/nologin sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/usr/sbin/nologin man:x:6:12:man:/var/cache/man:/usr/sbin/nologin lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin mail:x:8:8:mail:/var/mail:/usr/sbin/nologin news:x:9:9:news:/var/spool/news:/usr/sbin/nologin uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin proxy:x:13:13:proxy:/bin:/usr/sbin/nologin www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin backup:x:34:34:backup:/var/backups:/usr/sbin/nologin list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin irc:x:39:39:ircd:/run/ircd:/usr/sbin/nologin _apt:x:42:65534::/nonexistent:/usr/sbin/nologin nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin  l  Lock scroll  s  Save  z  Console   x   Expand all  o  Collapse all  d  Light  h   Help   q  Quit 

Warning

Moulti is a young project and currently offers no guarantee whatsoever regarding the stability and availability of Python modules and functions.