subprocwriteread


Namesubprocwriteread JSON
Version 0.12 PyPI version JSON
download
home_pagehttps://github.com/hansalemaos/subprocwriteread
SummaryA class for interacting with subprocesses and capturing their input and output streams
upload_time2023-10-29 07:27:12
maintainer
docs_urlNone
authorJohannes Fischer
requires_python
licenseMIT
keywords subprocess write
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# A class for interacting with subprocesses and capturing their input and output streams

## Tested against Windows 10 / Python 3.11 / Anaconda

### pip install subprocwriteread

```python
SubProcInputOutput - A class for interacting with subprocesses and capturing their input and output streams.

This class allows you to run external commands as subprocesses and provides methods to read, write, and control
the standard input, standard output, and standard error streams. It also supports handling and printing of the
captured data.



Args:
    cmd (str or list): The command to run as a subprocess. If it's a string, the command is passed to the system shell.
        If it's a list, the command is executed directly.
    invisible (bool, optional): If True, the subprocess runs invisibly without showing its console window (Windows only).
    print_stdout (bool, optional): If True, capture and print the standard output of the subprocess.
    print_stderr (bool, optional): If True, capture and print the standard error of the subprocess.
    limit_stdout (int, optional): Maximum number of lines to capture in standard output.
    limit_stderr (int, optional): Maximum number of lines to capture in standard error.
    limit_stdin (int, optional): Maximum number of lines to capture in standard input.
    convert_to_83 (bool, optional): If True, convert long file paths to their 8.3 format (Windows only).
    separate_stdout_stderr_with_list (bool, optional): If True, separate standard output and standard error
        into lists; otherwise, use a single string for each.

Attributes:
    cmd (str or list): The command passed to the subprocess.
    stdout (deque): Captured standard output.
    stderr (deque): Captured standard error.
    stdin (deque): Captured standard input.
    print_stdout (bool): Indicates whether standard output is printed.
    print_stderr (bool): Indicates whether standard error is printed.

Methods:
    - kill_proc(press_ctrl_c=True, sleep_after_pipes=1, sleep_after_proc=1, shellkill=True):
        Terminate the subprocess, optionally sending a Ctrl+C signal and forcefully killing it.

    - disable_stderr_print(): Disable printing of standard error.
    - disable_stdout_print(): Disable printing of standard output.
    - enable_stderr_print(): Enable printing of standard error.
    - enable_stdout_print(): Enable printing of standard output.
    - write(cmd, wait_to_complete=0.1, convert_to_83=False): Write data to the standard input of the subprocess.

    - get_lock(): Acquire a lock to protect critical sections.
    - release_lock(): Release the lock acquired with 'get_lock()'.
    - flush_stdout(): Clear the captured standard output.
    - flush_stderr(): Clear the captured standard error.
    - flush_stdin(): Clear the captured standard input.
    - flush_all_pipes(): Clear the captured data from all streams.

    - send_ctrl_c(): Send a Ctrl+C signal to the subprocess.
    - send_ctrl_break(): Send a Ctrl+Break signal to the subprocess.
    - send_ctrl_close(): Send a Ctrl+Close signal to the subprocess.
    - send_ctrl_logoff(): Send a Ctrl+Logoff signal to the subprocess.
    - send_ctrl_shutdown(): Send a Ctrl+Shutdown signal to the subprocess.

    - isalive(): Check if the subprocess is still running (Windows only).

Note:
    Some methods are platform-specific and may not work on non-Windows systems. Make sure to handle platform
    compatibility when using this class.

Example:

from subprocwriteread import SubProcInputOutput

adbexe = r"C:\Android\android-sdk\platform-tools\adb.exe"

self = SubProcInputOutput(
cmd=[adbexe, "-s", "127.0.0.1:5555", "shell"],
invisible=True,
print_stdout=True,
print_stderr=True,
limit_stdout=None,
limit_stderr=None,
limit_stdin=None,
convert_to_83=True,
separate_stdout_stderr_with_list=False,
)
stdout, stderr = self.write("ls")
print(self.isalive())
stdout, stderr = self.write(
"ls -R -i -H -las -s", wait_to_complete=0.2, convert_to_83=False
)
self.disable_stdout_print()
self.write(
'''while true; do
    sleep 1
    echo oioioi
done
''',
wait_to_complete=0,
)
self.kill_proc(
press_ctrl_c=True, sleep_after_pipes=1, sleep_after_proc=1, shellkill=True
)
self.flush_all_pipes()
print(self.isalive())


Methods:
    - kill_proc(press_ctrl_c=True, sleep_after_pipes=1, sleep_after_proc=1, shellkill=True):
        Terminate the subprocess, optionally sending a Ctrl+C signal and forcefully killing it.

        Args:
            press_ctrl_c (bool, optional): Send a Ctrl+C signal to the subprocess before terminating it.
            sleep_after_pipes (float, optional): Time to sleep after closing input and output pipes.
            sleep_after_proc (float, optional): Time to sleep after terminating the subprocess.
            shellkill (bool, optional): If True (Windows only), use a shell command to forcefully kill the subprocess.

    - disable_stderr_print():
        Disable printing of standard error.

    - disable_stdout_print():
        Disable printing of standard output.

    - enable_stderr_print():
        Enable printing of standard error.

    - enable_stdout_print():
        Enable printing of standard output.

    - write(cmd, wait_to_complete=0.1, convert_to_83=False):
        Write data to the standard input of the subprocess.

        Args:
            cmd (str or bytes): The data to write to the subprocess's standard input.
            wait_to_complete (float, optional): Time to wait after writing data before returning.
            convert_to_83 (bool, optional): If True, convert long file paths to their 8.3 format (Windows only).

        Returns:
            list: A list containing lists of standard output and standard error data, size might change after executing if separate_stdout_stderr_with_list is True

    - get_lock():
        Acquire a lock to protect critical sections.

        Returns:
            self: The current instance with the lock acquired.

    - release_lock():
        Release the lock acquired with 'get_lock()'.

        Returns:
            self: The current instance with the lock released.

    - flush_stdout():
        Clear the captured standard output.

        Returns:
            self: The current instance with standard output cleared.

    - flush_stderr():
        Clear the captured standard error.

        Returns:
            self: The current instance with standard error cleared.

    - flush_stdin():
        Clear the captured standard input.

        Returns:
            self: The current instance with standard input cleared.

    - flush_all_pipes():
        Clear the captured data from all streams.

    - send_ctrl_c():
        Send a Ctrl+C signal to the subprocess.

        Returns:
            self: The current instance after sending the Ctrl+C signal.

    - send_ctrl_break():
        Send a Ctrl+Break signal to the subprocess.

        Returns:
            self: The current instance after sending the Ctrl+Break signal.

    - send_ctrl_close():
        Send a Ctrl+Close signal to the subprocess.

        Returns:
            self: The current instance after sending the Ctrl+Close signal.

    - send_ctrl_logoff():
        Send a Ctrl+Logoff signal to the subprocess.

        Returns:
            self: The current instance after sending the Ctrl+Logoff signal.

    - send_ctrl_shutdown():
        Send a Ctrl+Shutdown signal to the subprocess.

        Returns:
            self: The current instance after sending the Ctrl+Shutdown signal.

    - isalive():
        Check if the subprocess is still running (Windows only).

        Returns:
            bool: True if the subprocess is running, False otherwise.
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hansalemaos/subprocwriteread",
    "name": "subprocwriteread",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "subprocess,write",
    "author": "Johannes Fischer",
    "author_email": "aulasparticularesdealemaosp@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/33/05/ddaf6c0958c208d6184de6bff1f487436f8b3af7e2f0bd620b85752e6984/subprocwriteread-0.12.tar.gz",
    "platform": null,
    "description": "\r\n# A class for interacting with subprocesses and capturing their input and output streams\r\n\r\n## Tested against Windows 10 / Python 3.11 / Anaconda\r\n\r\n### pip install subprocwriteread\r\n\r\n```python\r\nSubProcInputOutput - A class for interacting with subprocesses and capturing their input and output streams.\r\n\r\nThis class allows you to run external commands as subprocesses and provides methods to read, write, and control\r\nthe standard input, standard output, and standard error streams. It also supports handling and printing of the\r\ncaptured data.\r\n\r\n\r\n\r\nArgs:\r\n    cmd (str or list): The command to run as a subprocess. If it's a string, the command is passed to the system shell.\r\n        If it's a list, the command is executed directly.\r\n    invisible (bool, optional): If True, the subprocess runs invisibly without showing its console window (Windows only).\r\n    print_stdout (bool, optional): If True, capture and print the standard output of the subprocess.\r\n    print_stderr (bool, optional): If True, capture and print the standard error of the subprocess.\r\n    limit_stdout (int, optional): Maximum number of lines to capture in standard output.\r\n    limit_stderr (int, optional): Maximum number of lines to capture in standard error.\r\n    limit_stdin (int, optional): Maximum number of lines to capture in standard input.\r\n    convert_to_83 (bool, optional): If True, convert long file paths to their 8.3 format (Windows only).\r\n    separate_stdout_stderr_with_list (bool, optional): If True, separate standard output and standard error\r\n        into lists; otherwise, use a single string for each.\r\n\r\nAttributes:\r\n    cmd (str or list): The command passed to the subprocess.\r\n    stdout (deque): Captured standard output.\r\n    stderr (deque): Captured standard error.\r\n    stdin (deque): Captured standard input.\r\n    print_stdout (bool): Indicates whether standard output is printed.\r\n    print_stderr (bool): Indicates whether standard error is printed.\r\n\r\nMethods:\r\n    - kill_proc(press_ctrl_c=True, sleep_after_pipes=1, sleep_after_proc=1, shellkill=True):\r\n        Terminate the subprocess, optionally sending a Ctrl+C signal and forcefully killing it.\r\n\r\n    - disable_stderr_print(): Disable printing of standard error.\r\n    - disable_stdout_print(): Disable printing of standard output.\r\n    - enable_stderr_print(): Enable printing of standard error.\r\n    - enable_stdout_print(): Enable printing of standard output.\r\n    - write(cmd, wait_to_complete=0.1, convert_to_83=False): Write data to the standard input of the subprocess.\r\n\r\n    - get_lock(): Acquire a lock to protect critical sections.\r\n    - release_lock(): Release the lock acquired with 'get_lock()'.\r\n    - flush_stdout(): Clear the captured standard output.\r\n    - flush_stderr(): Clear the captured standard error.\r\n    - flush_stdin(): Clear the captured standard input.\r\n    - flush_all_pipes(): Clear the captured data from all streams.\r\n\r\n    - send_ctrl_c(): Send a Ctrl+C signal to the subprocess.\r\n    - send_ctrl_break(): Send a Ctrl+Break signal to the subprocess.\r\n    - send_ctrl_close(): Send a Ctrl+Close signal to the subprocess.\r\n    - send_ctrl_logoff(): Send a Ctrl+Logoff signal to the subprocess.\r\n    - send_ctrl_shutdown(): Send a Ctrl+Shutdown signal to the subprocess.\r\n\r\n    - isalive(): Check if the subprocess is still running (Windows only).\r\n\r\nNote:\r\n    Some methods are platform-specific and may not work on non-Windows systems. Make sure to handle platform\r\n    compatibility when using this class.\r\n\r\nExample:\r\n\r\nfrom subprocwriteread import SubProcInputOutput\r\n\r\nadbexe = r\"C:\\Android\\android-sdk\\platform-tools\\adb.exe\"\r\n\r\nself = SubProcInputOutput(\r\ncmd=[adbexe, \"-s\", \"127.0.0.1:5555\", \"shell\"],\r\ninvisible=True,\r\nprint_stdout=True,\r\nprint_stderr=True,\r\nlimit_stdout=None,\r\nlimit_stderr=None,\r\nlimit_stdin=None,\r\nconvert_to_83=True,\r\nseparate_stdout_stderr_with_list=False,\r\n)\r\nstdout, stderr = self.write(\"ls\")\r\nprint(self.isalive())\r\nstdout, stderr = self.write(\r\n\"ls -R -i -H -las -s\", wait_to_complete=0.2, convert_to_83=False\r\n)\r\nself.disable_stdout_print()\r\nself.write(\r\n'''while true; do\r\n    sleep 1\r\n    echo oioioi\r\ndone\r\n''',\r\nwait_to_complete=0,\r\n)\r\nself.kill_proc(\r\npress_ctrl_c=True, sleep_after_pipes=1, sleep_after_proc=1, shellkill=True\r\n)\r\nself.flush_all_pipes()\r\nprint(self.isalive())\r\n\r\n\r\nMethods:\r\n    - kill_proc(press_ctrl_c=True, sleep_after_pipes=1, sleep_after_proc=1, shellkill=True):\r\n        Terminate the subprocess, optionally sending a Ctrl+C signal and forcefully killing it.\r\n\r\n        Args:\r\n            press_ctrl_c (bool, optional): Send a Ctrl+C signal to the subprocess before terminating it.\r\n            sleep_after_pipes (float, optional): Time to sleep after closing input and output pipes.\r\n            sleep_after_proc (float, optional): Time to sleep after terminating the subprocess.\r\n            shellkill (bool, optional): If True (Windows only), use a shell command to forcefully kill the subprocess.\r\n\r\n    - disable_stderr_print():\r\n        Disable printing of standard error.\r\n\r\n    - disable_stdout_print():\r\n        Disable printing of standard output.\r\n\r\n    - enable_stderr_print():\r\n        Enable printing of standard error.\r\n\r\n    - enable_stdout_print():\r\n        Enable printing of standard output.\r\n\r\n    - write(cmd, wait_to_complete=0.1, convert_to_83=False):\r\n        Write data to the standard input of the subprocess.\r\n\r\n        Args:\r\n            cmd (str or bytes): The data to write to the subprocess's standard input.\r\n            wait_to_complete (float, optional): Time to wait after writing data before returning.\r\n            convert_to_83 (bool, optional): If True, convert long file paths to their 8.3 format (Windows only).\r\n\r\n        Returns:\r\n            list: A list containing lists of standard output and standard error data, size might change after executing if separate_stdout_stderr_with_list is True\r\n\r\n    - get_lock():\r\n        Acquire a lock to protect critical sections.\r\n\r\n        Returns:\r\n            self: The current instance with the lock acquired.\r\n\r\n    - release_lock():\r\n        Release the lock acquired with 'get_lock()'.\r\n\r\n        Returns:\r\n            self: The current instance with the lock released.\r\n\r\n    - flush_stdout():\r\n        Clear the captured standard output.\r\n\r\n        Returns:\r\n            self: The current instance with standard output cleared.\r\n\r\n    - flush_stderr():\r\n        Clear the captured standard error.\r\n\r\n        Returns:\r\n            self: The current instance with standard error cleared.\r\n\r\n    - flush_stdin():\r\n        Clear the captured standard input.\r\n\r\n        Returns:\r\n            self: The current instance with standard input cleared.\r\n\r\n    - flush_all_pipes():\r\n        Clear the captured data from all streams.\r\n\r\n    - send_ctrl_c():\r\n        Send a Ctrl+C signal to the subprocess.\r\n\r\n        Returns:\r\n            self: The current instance after sending the Ctrl+C signal.\r\n\r\n    - send_ctrl_break():\r\n        Send a Ctrl+Break signal to the subprocess.\r\n\r\n        Returns:\r\n            self: The current instance after sending the Ctrl+Break signal.\r\n\r\n    - send_ctrl_close():\r\n        Send a Ctrl+Close signal to the subprocess.\r\n\r\n        Returns:\r\n            self: The current instance after sending the Ctrl+Close signal.\r\n\r\n    - send_ctrl_logoff():\r\n        Send a Ctrl+Logoff signal to the subprocess.\r\n\r\n        Returns:\r\n            self: The current instance after sending the Ctrl+Logoff signal.\r\n\r\n    - send_ctrl_shutdown():\r\n        Send a Ctrl+Shutdown signal to the subprocess.\r\n\r\n        Returns:\r\n            self: The current instance after sending the Ctrl+Shutdown signal.\r\n\r\n    - isalive():\r\n        Check if the subprocess is still running (Windows only).\r\n\r\n        Returns:\r\n            bool: True if the subprocess is running, False otherwise.\r\n```\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A class for interacting with subprocesses and capturing their input and output streams",
    "version": "0.12",
    "project_urls": {
        "Homepage": "https://github.com/hansalemaos/subprocwriteread"
    },
    "split_keywords": [
        "subprocess",
        "write"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e81cb6f7ab2195ee47ce5f9b0590c3ea20faeacd1264adb4974c5df2e927dff2",
                "md5": "04a96f186ae4379f7b5e18c2d3194b23",
                "sha256": "989d8f720e56bf64f09e6fb16db69db5f15732aba391026bfeca087c905e95d2"
            },
            "downloads": -1,
            "filename": "subprocwriteread-0.12-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "04a96f186ae4379f7b5e18c2d3194b23",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 12154,
            "upload_time": "2023-10-29T07:27:10",
            "upload_time_iso_8601": "2023-10-29T07:27:10.331078Z",
            "url": "https://files.pythonhosted.org/packages/e8/1c/b6f7ab2195ee47ce5f9b0590c3ea20faeacd1264adb4974c5df2e927dff2/subprocwriteread-0.12-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3305ddaf6c0958c208d6184de6bff1f487436f8b3af7e2f0bd620b85752e6984",
                "md5": "63e99d3af7c7a1ccd80dc1f01d616d67",
                "sha256": "da513c706fc695f8004db3bcf4c12e9521316dd5c07c3863ecbab9b5d8ac44a5"
            },
            "downloads": -1,
            "filename": "subprocwriteread-0.12.tar.gz",
            "has_sig": false,
            "md5_digest": "63e99d3af7c7a1ccd80dc1f01d616d67",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 9802,
            "upload_time": "2023-10-29T07:27:12",
            "upload_time_iso_8601": "2023-10-29T07:27:12.056597Z",
            "url": "https://files.pythonhosted.org/packages/33/05/ddaf6c0958c208d6184de6bff1f487436f8b3af7e2f0bd620b85752e6984/subprocwriteread-0.12.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-29 07:27:12",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "hansalemaos",
    "github_project": "subprocwriteread",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "subprocwriteread"
}
        
Elapsed time: 0.17209s