Skip to main content

Base class for creating modules that are bindings for command line tools.

Project description

morbin

Base class for creating bindings for command line tools.

Installation

Install with:

pip install morbin

Usage

The easiest way to start is to use the bundled template generator.
The only argument it requires is the name of the program you want to create bindings for.

As an example we'll do Pip.

Running morbin pip in your terminal will produce a file named pip.py in your current directory.
It should look like this:

from morbin import Morbin, Output


class Pip(Morbin):
    @property
    def program(self) -> str:
    return "pip"

Additional functions should be built on top of the run function and return its output.

After adding functions for install and upgrade the class should look like this:

from morbin import Morbin, Output


class Pip(Morbin):
    @property
    def program(self) -> str:
    return "pip"
    
    def install(self, package:str, *args:str)->Output:
        return self.run("install", package, *args)
    
    def upgrade(self, package:str)->Output:
        return self.install(package, "--upgrade")
    
    def install_requirements(self)->Output:
        return self.install("-r", "requirements.txt")

It can be used like:

pip = Pip()
output = pip.install_requirements()

The Output object each function returns is a dataclass with three fields: return_code, stdout, and stderr.

@dataclass
class Output:
    """Dataclass representing the output of a terminal command.

    #### Fields:
    * `return_code: list[int]`
    * `stdout: str`
    * `stderr: str`"""

    return_code: list[int]
    stdout: str = ""
    stderr: str = ""

    def __add__(self, output: Self) -> Self:
        return Output(
            self.return_code + output.return_code,
            self.stdout + output.stdout,
            self.stderr + output.stderr,
        )

By default stdout and stderr are not captured.
They are sent to wherever they normally would be and the stdout and stderr fields of the Output object will be empty strings.
stdout and stderr can be captured by either setting the capture_output property of a class instance to True or by using the capturing_output context manager.
To get the output of install_requirements:

pip = Pip(capture_output=True)
text = pip.install_requirements().stdout

or

pip = Pip()
with pip.capturing_output():
    text = pip.install_requirements().stdout

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

morbin-1.0.0.tar.gz (54.0 kB view details)

Uploaded Source

Built Distribution

morbin-1.0.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file morbin-1.0.0.tar.gz.

File metadata

  • Download URL: morbin-1.0.0.tar.gz
  • Upload date:
  • Size: 54.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for morbin-1.0.0.tar.gz
Algorithm Hash digest
SHA256 f6694327654eebbc061c3e06712c2acf31de4b6678cbb4516e4ba59612af4bcc
MD5 35908642249105b3ebb77be3ae2c8c4a
BLAKE2b-256 907688129312f7b8f7be229130f416d97816b6c6fc9c9b72212559fca6f970ed

See more details on using hashes here.

Provenance

File details

Details for the file morbin-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: morbin-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for morbin-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d9ff3017e3d69cdfb217049713f63b61a6776015c28da08e21eaf655eca31873
MD5 fad60109c2fa806ee655441b75ba7671
BLAKE2b-256 201dcf7259a306912f7e07db521746e55ef6a38c024d76f79a6463bd08ba3006

See more details on using hashes here.

Provenance

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