Name | morbin JSON |
Version |
1.0.0
JSON |
| download |
home_page | |
Summary | Base class for creating modules that are bindings for command line tools. |
upload_time | 2024-02-08 23:47:12 |
maintainer | |
docs_url | None |
author | |
requires_python | <3.12,>=3.10 |
license | |
keywords |
binding
bindings
cli
morbin
morbius
morph
terminal
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# morbin
Base class for creating bindings for command line tools.
## Installation
Install with:
<pre>
pip install morbin
</pre>
## 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:
<pre>
from morbin import Morbin, Output
class Pip(Morbin):
@property
def program(self) -> str:
return "pip"
</pre>
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:
<pre>
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")
</pre>
It can be used like:
<pre>
pip = Pip()
output = pip.install_requirements()
</pre>
The `Output` object each function returns is a `dataclass` with three fields: `return_code`, `stdout`, and `stderr`.
<pre>
@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,
)
</pre>
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`:
<pre>
pip = Pip(capture_output=True)
text = pip.install_requirements().stdout
</pre>
or
<pre>
pip = Pip()
with pip.capturing_output():
text = pip.install_requirements().stdout
</pre>
Raw data
{
"_id": null,
"home_page": "",
"name": "morbin",
"maintainer": "",
"docs_url": null,
"requires_python": "<3.12,>=3.10",
"maintainer_email": "",
"keywords": "binding,bindings,cli,morbin,morbius,morph,terminal",
"author": "",
"author_email": "Matt Manes <mattmanes@pm.me>",
"download_url": "https://files.pythonhosted.org/packages/90/76/88129312f7b8f7be229130f416d97816b6c6fc9c9b72212559fca6f970ed/morbin-1.0.0.tar.gz",
"platform": null,
"description": "# morbin\n\nBase class for creating bindings for command line tools.\n\n## Installation\n\nInstall with:\n\n<pre>\npip install morbin\n</pre>\n\n## Usage\n\nThe easiest way to start is to use the bundled template generator. \nThe only argument it requires is the name of the program you want to create bindings for.\n\nAs an example we'll do `Pip`.\n\nRunning `morbin pip` in your terminal will produce a file named `pip.py` in your current directory. \nIt should look like this:\n<pre>\nfrom morbin import Morbin, Output\n\n\nclass Pip(Morbin):\n @property\n def program(self) -> str:\n return \"pip\"\n</pre>\n\nAdditional functions should be built on top of the `run` function and return its output. \n\nAfter adding functions for `install` and `upgrade` the class should look like this:\n<pre>\nfrom morbin import Morbin, Output\n\n\nclass Pip(Morbin):\n @property\n def program(self) -> str:\n return \"pip\"\n \n def install(self, package:str, *args:str)->Output:\n return self.run(\"install\", package, *args)\n \n def upgrade(self, package:str)->Output:\n return self.install(package, \"--upgrade\")\n \n def install_requirements(self)->Output:\n return self.install(\"-r\", \"requirements.txt\")\n</pre>\n\nIt can be used like:\n<pre>\npip = Pip()\noutput = pip.install_requirements()\n</pre>\n\n\nThe `Output` object each function returns is a `dataclass` with three fields: `return_code`, `stdout`, and `stderr`. \n<pre>\n@dataclass\nclass Output:\n \"\"\"Dataclass representing the output of a terminal command.\n\n #### Fields:\n * `return_code: list[int]`\n * `stdout: str`\n * `stderr: str`\"\"\"\n\n return_code: list[int]\n stdout: str = \"\"\n stderr: str = \"\"\n\n def __add__(self, output: Self) -> Self:\n return Output(\n self.return_code + output.return_code,\n self.stdout + output.stdout,\n self.stderr + output.stderr,\n )\n</pre>\n\n\nBy default `stdout` and `stderr` are not captured. \nThey are sent to wherever they normally would be and the `stdout` and `stderr` fields of the `Output` object will be empty strings. \n`stdout` and `stderr` can be captured by either setting the `capture_output` property of a class instance to `True` \nor by using the `capturing_output` context manager. \nTo get the output of `install_requirements`:\n<pre>\npip = Pip(capture_output=True)\ntext = pip.install_requirements().stdout\n</pre>\nor\n<pre>\npip = Pip()\nwith pip.capturing_output():\n text = pip.install_requirements().stdout\n</pre>\n",
"bugtrack_url": null,
"license": "",
"summary": "Base class for creating modules that are bindings for command line tools.",
"version": "1.0.0",
"project_urls": {
"Documentation": "https://github.com/matt-manes/morbin/tree/main/docs",
"Homepage": "https://github.com/matt-manes/morbin",
"Source code": "https://github.com/matt-manes/morbin/tree/main/src/morbin"
},
"split_keywords": [
"binding",
"bindings",
"cli",
"morbin",
"morbius",
"morph",
"terminal"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "201dcf7259a306912f7e07db521746e55ef6a38c024d76f79a6463bd08ba3006",
"md5": "fad60109c2fa806ee655441b75ba7671",
"sha256": "d9ff3017e3d69cdfb217049713f63b61a6776015c28da08e21eaf655eca31873"
},
"downloads": -1,
"filename": "morbin-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "fad60109c2fa806ee655441b75ba7671",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<3.12,>=3.10",
"size": 5421,
"upload_time": "2024-02-08T23:47:09",
"upload_time_iso_8601": "2024-02-08T23:47:09.659030Z",
"url": "https://files.pythonhosted.org/packages/20/1d/cf7259a306912f7e07db521746e55ef6a38c024d76f79a6463bd08ba3006/morbin-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "907688129312f7b8f7be229130f416d97816b6c6fc9c9b72212559fca6f970ed",
"md5": "35908642249105b3ebb77be3ae2c8c4a",
"sha256": "f6694327654eebbc061c3e06712c2acf31de4b6678cbb4516e4ba59612af4bcc"
},
"downloads": -1,
"filename": "morbin-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "35908642249105b3ebb77be3ae2c8c4a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<3.12,>=3.10",
"size": 53987,
"upload_time": "2024-02-08T23:47:12",
"upload_time_iso_8601": "2024-02-08T23:47:12.406147Z",
"url": "https://files.pythonhosted.org/packages/90/76/88129312f7b8f7be229130f416d97816b6c6fc9c9b72212559fca6f970ed/morbin-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-02-08 23:47:12",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "matt-manes",
"github_project": "morbin",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "morbin"
}