sarugaku / shellingham

Tool to Detect Surrounding Shell
ISC License
222 stars 33 forks source link

============================================= Shellingham: Tool to Detect Surrounding Shell

.. image:: https://img.shields.io/pypi/v/shellingham.svg :target: https://pypi.org/project/shellingham/

Shellingham detects what shell the current Python executable is running in.

Usage

.. code-block:: python

>>> import shellingham
>>> shellingham.detect_shell()
('bash', '/bin/bash')

detect_shell pokes around the process's running environment to determine what shell it is run in. It returns a 2-tuple:

ShellDetectionFailure is raised if detect_shell fails to detect the surrounding shell.

Notes

Notes for Application Developers

Remember, your application's user is not necessarily using a shell. Shellingham raises ShellDetectionFailure if there is no shell to detect, but your application should almost never do this to your user.

A practical approach to this is to wrap detect_shell in a try block, and provide a sane default on failure

.. code-block:: python

try:
    shell = shellingham.detect_shell()
except shellingham.ShellDetectionFailure:
    shell = provide_default()

There are a few choices for you to choose from.

Here's a simple implementation to provide a default shell

.. code-block:: python

import os

def provide_default():
    if os.name == 'posix':
        return os.environ['SHELL']
    elif os.name == 'nt':
        return os.environ['COMSPEC']
    raise NotImplementedError(f'OS {os.name!r} support not available')