Skip to main content

Replacement for pexpect

Project description

Fast Start

Install the package:

pip install --user processcontroller

Import the main class:

from processcontroller import ProcessController

Create a new instance:

process_controller = ProcessController()

The ProcessController() instance

Methods

  • The run() method:

    process_controller.run(command, options)
    
    Run a program (command and options detailed below)
  • The send() method:

    process_controller.send(str|bytes)
    
    Send a string or bytearray to the subprogram stdin, you will usually use it inside a callback or in detached state
  • The close() method:

    process_controller.close()
    
    Close the stdin of the subprogram so it reaches EOF, you will usually use it inside a callback or in detached state
  • The kill() method:

    process_controller.kill()
    
    Sends signal SIGKILL to the subprogram, the return_value should be (pid, 9) after that.
    (this should not be so used to be honest)

Attributes

  • return_value

    is the return value of the subprogram it will be - (0, 0) if running - (pid, status) if terminated

Function Parameters

ProcessController.run()

command

The command parameter must be an array like:

['/usr/bin/python', 'file.py']

options

Currently, it supports the following keys, ‘when’, ‘input’, ‘echo’ and ‘detached’

  • when:

    This key is used to listen to events occuring on the STDOUT of the subprogram The value have to be an array of events The “event” is in fact a match for some pattern:

    'when': [
       ['^SomeRegex.*\n', callback],
       ['^An other one.\n', cb]
       ['^prompt>\$ $', cb]
    ]

    Be careful about the ending line, the match will be called each time a char is added to the buffer, this helps matching prompts maybe I’ll add an option to avoid such an expansive operation in the future Every time a ‘n’ char is found, the line is treated once and reseted to ‘’

    The callbacks will be called with two arguments: the ProcessController instance, and the matched string:

    def callback(processcontroller, string)

    You can automates user inputs in your callback when required by the subprogram using the send function of your ProcessController instance:

    def cb(p, s):
       c.send('some input')
  • input:’

    This key is used to pre-fill the stdin of a subprogram before running it:

    pc = ProcessController()
    pc.run(['/bin/bash'], {
       'input': 'echo test && exit'
    })

    You can set an array of input:

    'input': ['one', 'two', 'three']  # sends "one\ntwo\nthree\n"
    
    You can input str or bytes, conversion is handled for you
  • echo:

    This key is a boolean that defaults to False. When set to True, the ProcessController will print the input sent to your subprogram where it has been asked

  • detached:

    This key is used to make the program run in its own thread, making the call to run non-blocking:

    pc = ProcessController()
    pc.run(['/bin/bash'], {
       'detached': True
    })
    pc.send('echo test')  # will print test to stdout
    pc.close()  # close stdin of subprogram, so that bash will read EOF

Don’t forget that you have to create a new instance a ProcessController everytime you want to execute a new subprogram

Please feel free to read the tests and code for a better understanding

ENJOY

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

processcontroller-0.0.2.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

processcontroller-0.0.2-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file processcontroller-0.0.2.tar.gz.

File metadata

  • Download URL: processcontroller-0.0.2.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for processcontroller-0.0.2.tar.gz
Algorithm Hash digest
SHA256 1e777c55c16a5fc477db0590f61ac88596f5931d6fa1b4ec5d2599e41259c0d7
MD5 c758e79923fa801865df582c88acabc0
BLAKE2b-256 17c507736c9471746c6009780e3fc72096e72b15de5cc3b78c62f5e500f31faa

See more details on using hashes here.

File details

Details for the file processcontroller-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: processcontroller-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for processcontroller-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fe09bc93bb2f8f58b679f4273f1d11eb6a08c2369e2bf46d2f63e132c982aa06
MD5 ea13f0b7987cd36a61fe690e9e217b5d
BLAKE2b-256 a540ea20c8abab760bf11969e6f257217bf9e472299cdc00c302472ab1fd3695

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page