TextIOTrap
Simple class for trapping / capturing Python Text IO streams like from subprocess.popen, pexpect, sys.stderr and others; enabling the capture output of or dropping streams with cross platform DEVNULL helper.
Installation
python3 -m pip install tiotrap
Usage
This tool contains one class TextIOTrap and a helper DEVNULL.
Examples
(Ex1) Use TextIOTrap to capture stdout of a chatty process using store option:
_stdout_bk = sys.stdout # Store original stdout
ttrap = tiotrap.TextIOTrap(store=True)
try:
sys.stdout = ttrap # Map stdout to tiotrap
print("TEST1")
# call some chatty functions()
print("TEST2")
finally:
sys.stdout = _stdout_bk # Restore stdout
Output of print:
captured logs:
TEST1
<chatty outputs here>
TEST2
(Ex2) Use TextIOTrap to capture stdout using write_handler option:
aTrap = []
_stdout_bk = sys.stdout
try:
sys.stdout = tiotrap.TextIOTrap(write_handler=lambda s: aTrap.append(s))
print("TEST1")
print("TEST2")
finally:
sys.stdout = _stdout_bk
# print adds extra \n end so remove with rstrip()
print(f"aTrap:\n{''.join(aTrap).rstrip()}\n~end~\n")
Output of print:
aTrappedStdout = ['TEST1', 'TEST2']
You can substitute lambda with a function or method call to handle writes with your own code.
(Ex3) Use TextIOTrap grab output pexpect call :
ttrap = tiotrap.TextIOTrap(store=True)
p = pexpect.spawn('ls -la')
p.logfile = ttrap
p.expect(pexpect.EOF)
print(f"`ls -la` cmd output:\n{ttrap.entries()}\n~")
Output of print:
ls output:
<full directory listing here of cwd>
Other uses of TextIOTrap:
- Output the stdout of a
subprocess.popencall in real time to a secondary log file - ...
(Ex4) Use TextIOTrap grab output pexpect call :
ttrap = tiotrap.TextIOTrap(store=True)
p = pexpect.spawn('ls -la')
p.logfile = ttrap
p.expect(pexpect.EOF)
print("ls -la` cmd output (as was written):")
for write in ttrap:
print(write)
Output: Similar to Ex4
(Ex5) Use DEVNULL to drop all output of a TextIO Stream
_stdout_bk = sys.stdout
try:
sys.stdout = tiotrap.DEVNULL
print("THIS WILL NOT PRINT")
finally:
sys.stdout = _stdout_bk
print("THIS WILL PRINT")
This DEVNULL is very simple implementation and is fully cross platform unlike someother DEVNULL implementations.
Note: TextIOTrap has been set up to be compatible with the standard methods for a Text IO streams. I'll be glad to update if any edge cases are discovered.
Metadata
Release files for tiotrap 0.3.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tiotrap-0.3.3.tar.gz | 4.1 kB | Without provenance | Details for tiotrap-0.3.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tiotrap-0.3.3-py3-none-any.whl | Python 3 | none | any | Details for tiotrap-0.3.3-py3-none-any.whl |
Total release size: 8.3 kB
Release files / tiotrap-0.3.3.tar.gz
| Download URL | tiotrap-0.3.3.tar.gz |
|---|---|
| Size | 4.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b1b2b1c3dabc2800725ab34d256c9c6d03cde4e759688471f6120a09f047831e
|
|
BLAKE2b-256 checksum How to use checksums |
c4ba8789b170a6d288c29f8f257443fbec5b2bee2118bd3da7b76bb5dff79b42
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.13 CPython/3.7.9 Linux/5.13.0-37-generic
|
Release files / tiotrap-0.3.3-py3-none-any.whl
| Download URL | tiotrap-0.3.3-py3-none-any.whl |
|---|---|
| Size | 4.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ae021469ead5e7aafcb84223796472bbba2637563988695600fcd8821e7e9c9c
|
|
BLAKE2b-256 checksum How to use checksums |
b47bfc9b4e7625e18e096f5738b6703c998aed1822d13ee68b5633e30e584b1b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.13 CPython/3.7.9 Linux/5.13.0-37-generic
|
Security for tiotrap 0.3.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.