Skip to main content

Subprocess replacement with fancy API to build commands, typing and, hopefully, autocompletion support ✨

Project description

Shhell

Subprocess replacement done right with fancy API to build commands, autocompletion and typing support ✨

I was not satisfied with API of subprocess/sh/plumbum, so I decided to at least define how the perfect API should look like for me.

Coming soon, sometime or just never.

How I want API to look like:

import shhell

shhell.sh(c=shhell.curl(*'fsSL', 'https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh')).run()

which will be translated to:

sh -c "$(curl -fsSL 'https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh')"

shhell.<cmd>(...) will produce an instance of special class, which can invoke and get result from command both synchronously and asynchronously, allowing flexibility:

import asyncio
import shhell

OUTPUT_FILE = "example.mp4"

async def download():
    await shhell.curl(
        *'fsSL',
        "https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_1920_18MG.mp4",
        output=OUTPUT_FILE
    )

async def print_file_size():
    while True:
        await shhell.echo(f'\t{await shhell.stat(OUTPUT_FILE, printf="%s")}')
        await asyncio.sleep(0.1)

asyncio.run(
    asyncio.wait(
        [
            asyncio.create_task(download()), 
            asyncio.create_task(print_file_size())
        ],
        return_when=asyncio.FIRST_COMPLETED
    )
)

Pipes, redirects, subshells?

I like how plumbum deos this, i.e. using python magic methods allowing |, > support. In fact, if I'm going to write this library, I'm going to steal a lot of ideas from plumbum, changing little things that I don't like.

How autocompletion would even work?

Code generation, of course! However, it's not going to be easy, if achievable at all...

One of the options is generating signatures upon package build for executables available in PATH (expected args, types, docs) from -h/--help/man output for the command for linux/macOS. This option is only viable on CI/CD stages, however.

There should be api available for users to generate code structures for their own non-standard executables.

Note: all the generated code will serve only cosmetic purpose, whether a signature was generated for a specific executable or not, the runtime result should always remain the same.

So shhell/executables/cowsay.py would look like this:

from typing import Any, Literal
from shhell.command import Command
from shhell.executable import Executable

@Executable.from_dummy
def cowsay(
    *args: Literal["b", "d", "g", "p", "s", "t", "w", "y", "l", "n"],
    # __message this is positional only optional argument
    __message: str = ...,
    # these arguments are keyword only (e='??' will be converted to -e '??')
    e: Any = "eyes",
    f: Any = "cowfile",
    T: Any = "tongue",
    W: Any = "wrapcolumn",
) -> Command:
    """
    cow{say,think} version 3.03, (c) 1999 Tony Monroe
    Usage: cowsay [-bdgpstwy] [-h] [-e eyes] [-f cowfile]
          [-l] [-n] [-T tongue] [-W wrapcolumn] [message]
    """
...

However, I think it's better to have a separate file for each executable, and just add imports for them

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

shhell-0.0.0.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

shhell-0.0.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file shhell-0.0.0.tar.gz.

File metadata

  • Download URL: shhell-0.0.0.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.9.5 Darwin/20.5.0

File hashes

Hashes for shhell-0.0.0.tar.gz
Algorithm Hash digest
SHA256 4d932cd59a2f10ca17828909177b47532340aa58e9b484ee442ea97533b582ba
MD5 92c41c9dd5ac74f27fef54d522cd328c
BLAKE2b-256 165c4f99e98163d0649c43b640563fc5a6dbdeffb9eb85b3143289ec0181c918

See more details on using hashes here.

File details

Details for the file shhell-0.0.0-py3-none-any.whl.

File metadata

  • Download URL: shhell-0.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.9.5 Darwin/20.5.0

File hashes

Hashes for shhell-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e98bfac178ce43d46af7951a24cf712bac5b7ae3fb9cfe3c8772dc22dfc4787c
MD5 ba7658dcf667206d65afc6655e6be83d
BLAKE2b-256 7e693d609a1464281f4b467f3b852ee3b2304018067f1fc8d7d7d1841162ac8d

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