shell

repository·master·Indexed 41 days ago

https://github.com/toastdriven/shell

A Python library providing an intuitive interface for executing shell commands with less boilerplate than the standard subprocess module. It features a functional shell() shortcut and a Shell class for advanced control, supporting method chaining, input handling via .write(), and output/error capture. Compatible with Python 2.6+ and 3.3+.

Tokens
1.6K
Snippets
15
Records
16
Agent score
51%

What's inside shell

  1. Setup the shell testing environment for Python 3

    master

    To set up a local environment for running shell tests using Python 3, create a virtual environment specifying the python3 interpreter, activate it, and install the required dependencies mock==1.0.1 and nose==1.3.0.

    $ virtualenv -p python3 env3
    $ . env3/bin/activate
    $ pip install mock==1.0.1
    $ pip install nose==1.3.0
  2. Setup the shell testing environment for Python 2

    master

    To set up a local environment for running shell tests using Python 2, clone the repository, create a virtual environment, activate it, and install the required dependencies mock==1.0.1 and nose==1.3.0.

    $ git clone https://github.com/toastdriven/shell
    $ cd shell
    $ virtualenv env
    $ . env/bin/activate
    $ pip install mock==1.0.1
    $ pip install nose==1.3.0
  3. Chain shell commands with method chaining

    master

    The shell shortcut and Shell object support method chaining for concise command execution, especially when combining input and output operations.

    from shell import shell
    output = shell('cat -u', has_input=True).write('Hello, world!').output()
    print output
  4. Run quick shell commands with the shell() shortcut

    master

    For simple command execution, use the shell() shortcut function. It returns a command object that allows you to retrieve the command's output using the .output() method.

    from shell import shell
    ls = shell('ls')
    for file in ls.output():
        print file
  5. Handle interactive commands with input

    master

    To send input to an interactive command, initialize the shell with has_input=True (either via the shell() function or the Shell constructor) and use the .write() method.

    Note: You only get one shot at sending input; after writing, the command will finish. This is not intended for complex, multi-prompt interactive sessions.

    from shell import shell
    # Functional approach
    sh = shell('cat -u', has_input=True)
    sh.write('Hello, world!')
    print sh.output()
    
    # Class-based approach
    sh = Shell(has_input=True)
    sh.run('cat -u')
    sh.write('Hello, world!')
    print sh.output()