{ "info": { "author": "Brian Skinn", "author_email": "bskinn@alum.mit.edu", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Natural Language :: English", "Operating System :: OS Independent", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3 :: Only", "Topic :: Software Development :: Libraries :: Python Modules", "Topic :: Software Development :: Testing" ], "description": "stdio Manager: Context manager for mocking/wrapping ``stdin``/``stdout``/``stderr``\n===================================================================================\n\n**Current Development Version:**\n\n.. image:: https://travis-ci.org/bskinn/stdio-mgr.svg?branch=dev\n :target: https://travis-ci.org/bskinn/stdio-mgr\n\n.. image:: https://codecov.io/gh/bskinn/stdio-mgr/branch/dev/graph/badge.svg\n :target: https://codecov.io/gh/bskinn/stdio-mgr\n\n**Most Recent Stable Release:**\n\n.. image:: https://img.shields.io/pypi/v/stdio_mgr.svg\n :target: https://pypi.org/project/stdio-mgr\n\n.. image:: https://img.shields.io/pypi/pyversions/stdio-mgr.svg\n\n**Info:**\n\n.. image:: https://img.shields.io/github/license/mashape/apistatus.svg\n :target: https://github.com/bskinn/stdio-mgr/blob/master/LICENSE.txt\n\n.. image:: https://img.shields.io/badge/code%20style-black-000000.svg\n :target: https://github.com/ambv/black\n\n----\n\n**Have a CLI Python application?**\n\n**Want to automate testing of the actual console input & output\nof your user-facing components?**\n\n`stdio Manager` can help.\n\nWhile some functionality here is more or less duplicative of\n``redirect_stdout`` and ``redirect_stderr`` in ``contextlib``\n`within the standard library `__,\nit provides (i) a much more concise way to mock both ``stdout`` and ``stderr`` at the same time,\nand (ii) a mechanism for mocking ``stdin``, which is not available in ``contextlib``.\n\n**First, install:**\n\n.. code::\n\n $ pip install stdio-mgr\n\nThen use!\n\nAll of the below examples assume ``stdio_mgr`` has already\nbeen imported via:\n\n.. code::\n\n from stdio_mgr import stdio_mgr\n\n**Mock** ``stdout``\\ **:**\n\n.. code::\n\n >>> with stdio_mgr() as (in_, out_, err_):\n ... print('foobar')\n ... out_cap = out_.getvalue()\n >>> out_cap\n 'foobar\\n'\n >>> in_.closed and out_.closed and err_.closed\n True\n\nBy default ``print``\n`appends a newline `__\nafter each argument, which is why ``out_cap`` is ``'foobar\\n'``\nand not just ``'foobar'``.\n\nAs currently implemented, ``stdio_mgr`` closes all three mocked streams\nupon exiting the managed context.\n\n\n**Mock** ``stderr``\\ **:**\n\n.. code ::\n\n >>> import warnings\n >>> with stdio_mgr() as (in_, out_, err_):\n ... warnings.warn(\"'foo' has no 'bar'\")\n ... err_cap = err_.getvalue()\n >>> err_cap\n \"...UserWarning: 'foo' has no 'bar'\\n...\"\n\n\n**Mock** ``stdin``\\ **:**\n\nThe simulated user input has to be pre-loaded to the mocked stream.\n**Be sure to include newlines in the input to correspond to\neach mocked** `Enter` **keypress!**\nOtherwise, ``input`` will hang, waiting for a newline\nthat will never come.\n\nIf the entirety of the input is known in advance,\nit can just be provided as an argument to ``stdio_mgr``.\nOtherwise, ``.append()`` mocked input to ``in_``\nwithin the managed context as needed:\n\n.. code::\n\n >>> with stdio_mgr('foobar\\n') as (in_, out_, err_):\n ... print('baz')\n ... in_cap = input('??? ')\n ...\n ... _ = in_.append(in_cap[:3] + '\\n')\n ... in_cap2 = input('??? ')\n ...\n ... out_cap = out_.getvalue()\n >>> in_cap\n 'foobar'\n >>> in_cap2\n 'foo'\n >>> out_cap\n 'baz\\n??? foobar\\n??? foo\\n'\n\nThe ``_ =`` assignment suppresses ``print``\\ ing of the return value\nfrom the ``in_.append()`` call--otherwise, it would be interleaved\nin ``out_cap``, since this example is shown for an interactive context.\nFor non-interactive execution, as with ``unittest``, ``pytest``, etc.,\nthese 'muting' assignments should not be necessary.\n\n**Both** the ``'??? '`` prompts for ``input``\n**and** the mocked input strings\nare echoed to ``out_``, mimicking what a CLI user would see.\n\nA subtlety: While the trailing newline on, e.g., ``'foobar\\n'`` is stripped\nby ``input``, it is *retained* in ``out_``.\nThis is because ``in_`` tees the content read from it to ``out_``\n*before* that content is passed to ``input``.\n\n\n**Want to modify internal** ``print`` **calls\nwithin a function or method?**\n\nIn addition to mocking, ``stdio_mgr`` can also be used to\nwrap functions that directly output to ``stdout``/``stderr``. A ``stdout`` example:\n\n.. code::\n\n >>> def emboxen(func):\n ... def func_wrapper(s):\n ... from stdio_mgr import stdio_mgr\n ...\n ... with stdio_mgr() as (in_, out_, err_):\n ... func(s)\n ... content = out_.getvalue()\n ...\n ... max_len = max(map(len, content.splitlines()))\n ... fmt_str = '| {{: <{0}}} |\\n'.format(max_len)\n ...\n ... newcontent = '=' * (max_len + 4) + '\\n'\n ... for line in content.splitlines():\n ... newcontent += fmt_str.format(line)\n ... newcontent += '=' * (max_len + 4)\n ...\n ... print(newcontent)\n ...\n ... return func_wrapper\n\n >>> @emboxen\n ... def testfunc(s):\n ... print(s)\n\n >>> testfunc(\"\"\"\\\n ... Foo bar baz quux.\n ... Lorem ipsum dolor sit amet.\"\"\")\n ===============================\n | Foo bar baz quux. |\n | Lorem ipsum dolor sit amet. |\n ===============================\n\n----\n\nAvailable on `PyPI `__\n(``pip install stdio-mgr``).\n\nSource on `GitHub `__. Bug reports\nand feature requests are welcomed at the\n`Issues `__ page there.\n\nCopyright \\(c) 2018-2019 Brian Skinn\n\nLicense: The MIT License. See `LICENSE.txt `__\nfor full license terms.\n\n\n\n", "description_content_type": "", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://www.github.com/bskinn/stdio-mgr", "keywords": "", "license": "MIT License", "maintainer": "", "maintainer_email": "", "name": "stdio-mgr", "package_url": "https://pypi.org/project/stdio-mgr/", "platform": "", "project_url": "https://pypi.org/project/stdio-mgr/", "project_urls": { "Homepage": "https://www.github.com/bskinn/stdio-mgr" }, "release_url": "https://pypi.org/project/stdio-mgr/1.0.1/", "requires_dist": [ "attrs (>=17.1)" ], "requires_python": ">=3", "summary": "Context manager for mocking/wrapping stdin/stdout/stderr", "version": "1.0.1" }, "last_serial": 4808144, "releases": { "1.0": [ { "comment_text": "", "digests": { "md5": "a6208cb9319a435c0cf4ab7643136987", "sha256": "ec8da381a000f42bcaea699508fc0631bbe7f1965e2b8d9820d339af35a289f3" }, "downloads": -1, "filename": "stdio_mgr-1.0-py3-none-any.whl", "has_sig": false, "md5_digest": "a6208cb9319a435c0cf4ab7643136987", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3", "size": 8826, "upload_time": "2018-04-02T03:29:48", "url": "https://files.pythonhosted.org/packages/a9/3c/90b6e7e957a6ad8d134b4d4687cb639539172a32611bf7c8dc4937be46f7/stdio_mgr-1.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "5c9a5dfbab4e5422d5e8ed128273b7fe", "sha256": "3e8398ebba9e22c69d373b02676fba618172ea418ea57c1f2a72e45d62361d6d" }, "downloads": -1, "filename": "stdio-mgr-1.0.tar.gz", "has_sig": false, "md5_digest": "5c9a5dfbab4e5422d5e8ed128273b7fe", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3", "size": 6723, "upload_time": "2018-04-02T03:29:59", "url": "https://files.pythonhosted.org/packages/ea/c4/744db436f530b5d7e9a7c96e6e7e8c0684a1a26c90dbac92419a68ea6c11/stdio-mgr-1.0.tar.gz" } ], "1.0.1": [ { "comment_text": "", "digests": { "md5": "81625e1bbacb2b0e4016330f93838f40", "sha256": "c8e6c9568709560dfc3e57fa322fe6d72c505ac7e666b12f22aea1589006da95" }, "downloads": -1, "filename": "stdio_mgr-1.0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "81625e1bbacb2b0e4016330f93838f40", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3", "size": 7386, "upload_time": "2019-02-11T22:01:13", "url": "https://files.pythonhosted.org/packages/f5/49/5a7e6b42fb3b2bca6cc3dedc6e935f224bf0e4ac9fdd1e368d16d540fa7f/stdio_mgr-1.0.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "027b7e83918676ce44698883cab3d37c", "sha256": "781c3b51632a5e098ecad316e1fa64959ae540e1d66d8ea8a9db9c9b7b9f4186" }, "downloads": -1, "filename": "stdio-mgr-1.0.1.tar.gz", "has_sig": false, "md5_digest": "027b7e83918676ce44698883cab3d37c", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3", "size": 7222, "upload_time": "2019-02-11T22:01:24", "url": "https://files.pythonhosted.org/packages/dc/e2/5a1e907d5ecdcadaca69663d50426a19e513dc1cdbaefbfab9e6212009b4/stdio-mgr-1.0.1.tar.gz" } ], "1.0rc1": [ { "comment_text": "", "digests": { "md5": "08f3bc37b58692f7c1150119ab578de1", "sha256": "53905e5d41dad9a2ca75304d0fc2de7a63c941243b75901107f46ca053b4d64a" }, "downloads": -1, "filename": "stdio_mgr-1.0rc1-py3-none-any.whl", "has_sig": false, "md5_digest": "08f3bc37b58692f7c1150119ab578de1", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3", "size": 5289, "upload_time": "2018-03-26T19:59:45", "url": "https://files.pythonhosted.org/packages/1a/af/8d2bb815d9993747a8946d2977fba86b1a1542c1012181a0d7eea4166a87/stdio_mgr-1.0rc1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "606147327144e6cad9ed4ca7ebff79cf", "sha256": "45cf1c5ff5b912d7ca516419c71797f47ec7b1bf83d9ea53bba41c17c7855d4d" }, "downloads": -1, "filename": "stdio-mgr-1.0rc1.tar.gz", "has_sig": false, "md5_digest": "606147327144e6cad9ed4ca7ebff79cf", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3", "size": 4136, "upload_time": "2018-03-26T19:59:55", "url": "https://files.pythonhosted.org/packages/04/cb/4970e115d29a46e21578193c50d5b69fa6a7dac04fea5ca931bffe0dc869/stdio-mgr-1.0rc1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "81625e1bbacb2b0e4016330f93838f40", "sha256": "c8e6c9568709560dfc3e57fa322fe6d72c505ac7e666b12f22aea1589006da95" }, "downloads": -1, "filename": "stdio_mgr-1.0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "81625e1bbacb2b0e4016330f93838f40", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3", "size": 7386, "upload_time": "2019-02-11T22:01:13", "url": "https://files.pythonhosted.org/packages/f5/49/5a7e6b42fb3b2bca6cc3dedc6e935f224bf0e4ac9fdd1e368d16d540fa7f/stdio_mgr-1.0.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "027b7e83918676ce44698883cab3d37c", "sha256": "781c3b51632a5e098ecad316e1fa64959ae540e1d66d8ea8a9db9c9b7b9f4186" }, "downloads": -1, "filename": "stdio-mgr-1.0.1.tar.gz", "has_sig": false, "md5_digest": "027b7e83918676ce44698883cab3d37c", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3", "size": 7222, "upload_time": "2019-02-11T22:01:24", "url": "https://files.pythonhosted.org/packages/dc/e2/5a1e907d5ecdcadaca69663d50426a19e513dc1cdbaefbfab9e6212009b4/stdio-mgr-1.0.1.tar.gz" } ] }