Basic abstraction layer for executables.
Project description
pyTooling.CLIAbstraction
pyTooling.CLIAbstraction is an abstraction layer and wrapper for command line programs, so they can be used easily in
Python. All parameters like --value=42
are described as parameters of the executable.
Main Goals
- Offer access to CLI programs as Python classes.
- Abstract CLI arguments (a.k.a. parameter, option, flag, ...) as members on such a Python class.
- Derive program variants from existing programs.
- Assemble parameters in list format for handover to
subprocess.Popen
with proper escaping and quoting. - Launch a program with
subprocess.Popen
and hide the complexity of Popen. - Get a generator object for line-by-line output reading to enable postprocessing of outputs.
Use Cases
- Wrap command line interfaces of EDA tools (Electronic Design Automation) in Python classes.
Example
The following example implements a portion of the git
program and its commit
sub-command.
Git program defining commit
argument:
from pyTooling.CLIAbstraction import CLIArgument, Executable
from pyTooling.CLIAbstraction.Command import CommandArgument
from pyTooling.CLIAbstraction.Flag import LongFlag
from pyTooling.CLIAbstraction.ValuedTupleFlag import ShortTupleFlag
class Git(Executable):
_executableNames = {
"Windows": "git.exe",
"Linux": "git",
"Darwin": "git"
}
@CLIArgument()
class FlagVerbose(LongFlag, name="verbose"):
"""Print verbose messages."""
@CLIArgument()
class CommandCommit(CommandArgument, name="commit"):
"""Command to commit staged files."""
@CLIArgument()
class ValueCommitMessage(ShortTupleFlag, name="m"):
"""Specify the commit message."""
def GetCommitTool(self):
"""Derive a new program from a configured program."""
tool = self.__class__(executablePath=self._executablePath)
tool[tool.CommandCommit] = True
self._CopyParameters(tool)
return tool
Usage:
# Create a program instance and set common parameters.
git = Git()
git[git.FlagVerbose] = True
# Derive a variant of that pre-configured program.
commit = git.getCommitTool()
commit[commit.ValueCommitMessage] = "Bumped dependencies."
# Launch the program and parse outputs line-by-line.
commit.StartProcess()
for line in commit.GetLineReader():
print(line)
Consumers
This layer is used by:
Contributors
- Patrick Lehmann (Maintainer)
- Unai Martinez-Corral
- and more...
License
This Python package (source code) licensed under Apache License 2.0.
The accompanying documentation is licensed under Creative Commons - Attribution 4.0 (CC-BY 4.0).
SPDX-License-Identifier: Apache-2.0
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
Built Distribution
Close
Hashes for pyTooling.CLIAbstraction-0.4.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | addbffbe7e7bc77f5f36751e0ca967e7645118dfaa5cb135e3b71b7aa8176103 |
|
MD5 | 543d17e74da2e5bc8ce2861c5dd01b9d |
|
BLAKE2b-256 | ad03e9e69cb8b0db160d200f00865721f69b7714946a0d3c1caabc307b189bc1 |
Close
Hashes for pyTooling.CLIAbstraction-0.4.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 306fcf20b942c2fba9110cf1d9f0406ec3e5529749f1a78f6b9d50d83fdfb67e |
|
MD5 | abe1b4f37ef9d0c8663b331f78153677 |
|
BLAKE2b-256 | 6dde08fee7f3fd4d1a8a6d22539345af508c56aa36b4707075cc5f3be926f84c |