{ "info": { "author": "", "author_email": "", "bugtrack_url": null, "classifiers": [], "description": "|Build Status| |codecov.io|\n\nMethod Capture\n==============\n\n*Wraps callables to capture stdout, stderr, exceptions and the return.*\n\nWhy?\n----\n\nThis library was created to assist in the testing of Python code, where\nreturn values and exceptions are important, in addition to what is\nwritten to stdout and stderr. It allows for a callable to be ran without\nthe caller being concerned with how important outputs are captured.\n\nAn example use is when writing common code to test both a Python\ninterface and a corresponding CLI (tested by invoking ``main``). A\ncommon test procedure - which may involve a complex setup and tear down\n- can be ran using the different interfaces and return results that\nencapsulate the outputs, without concern over the form they take (e.g.\nthe Python interface may have returned ``True`` on success but the\n``CLI`` may have called ``exit(0)``, with raises a ``SystemExit``\nexception).\n\nOther `alternatives <#alternatives>`__ exist for capturing stdout and\nstderr but none also handle exceptions.\n\nHow?\n----\n\nGeneral\n~~~~~~~\n\n.. code:: python\n\n from capturewrap import CaptureWrapBuilder\n\n builder = CaptureWrapBuilder(capture_stdout=True, capture_stderr=True, capture_exceptions=True)\n wrapped = builder.build(my_method)\n\n result = wrapped(*args, **kwargs)\n print(f\"stdout: {result.stdout}\")\n print(f\"stderr: {result.stderr}\")\n print(f\"return_value: {result.return_value}\")\n print(f\"exception: {result.exception}\")\n\nNote: if an exception is captured, ``return_value`` will be ``None``.\n\nCustom Exception Capture\n~~~~~~~~~~~~~~~~~~~~~~~~\n\nIt may be desirable to capture only some exceptions and leave others to\nget raised as normal. To do this with ``CaptureWrapBuilder``, set\n``capture_exceptions`` as a function that takes the exception as the\nfirst argument and returns back a boolean value to indicate if the\nexception should be captured. e.g.\n\n.. code:: python\n\n from capturewrap import CaptureWrapBuilder\n\n builder = CaptureWrapBuilder(capture_exceptions=lambda e: isinstance(e, SystemExit) and e.code == 0)\n wrapped = builder.build(exit)\n\n print(wrapped(0)) # {\"exception\": [\"SystemExit: 0\\n\"]}\n print(wrapped(1)) # Raises exception\n\nRequirements\n------------\n\n- Python >= 3.6\n\nInstallation\n------------\n\nStable releases can be installed via\n`PyPI `__:\n\n.. code:: bash\n\n $ pip install capturewrap\n\nBleeding edge versions can be installed directly from GitHub:\n\n.. code:: bash\n\n $ pip install git+https://github.com/wtsi-hgi/python-capturewrap.git@$commitOrBranch#egg=capturewrap\n\nImplementation\n--------------\n\nThis implementation captures stdout and stdin using\n```redirect_stdout`` `__\nand\n```redirect_stderr`` `__,\nwhich were added to the standard library in Python 3.5.\n\nAlternatives\n------------\n\n- `iocapture `__: older solution,\n replaces ``sys.stdout`` and ``sys.stderr`` with capturing methods.\n- `capturer `__: complex\n solution that works at lower level to intercept subprocess ``stdout``\n and ``stderr`` using a pseudo terminal.\n\n.. |Build Status| image:: https://travis-ci.org/wtsi-hgi/python-capturewrap.svg\n :target: https://travis-ci.org/wtsi-hgi/python-capturewrap\n.. |codecov.io| image:: https://codecov.io/gh/wtsi-hgi/python-capturewrap/graph/badge.svg\n :target: https://codecov.io/github/wtsi-hgi/python-capturewrap\n", "description_content_type": null, "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/wtsi-hgi/python-capturewrap", "keywords": "", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "capturewrap", "package_url": "https://pypi.org/project/capturewrap/", "platform": "", "project_url": "https://pypi.org/project/capturewrap/", "project_urls": { "Homepage": "https://github.com/wtsi-hgi/python-capturewrap" }, "release_url": "https://pypi.org/project/capturewrap/1.1.0/", "requires_dist": null, "requires_python": "", "summary": "Wraps callables to capture stdout, stderr, exceptions and the return", "version": "1.1.0" }, "last_serial": 3494795, "releases": { "1.0.0": [ { "comment_text": "", "digests": { "md5": "7e959b08b7e2593bd8e6f8d0d379d249", "sha256": "606f02a0e055aab0c6abd8615defb4d90c0a635f6436ea2d6d437c2e6fe98693" }, "downloads": -1, "filename": "capturewrap-1.0.0.tar.gz", "has_sig": false, "md5_digest": "7e959b08b7e2593bd8e6f8d0d379d249", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5794, "upload_time": "2017-11-22T11:07:14", "url": "https://files.pythonhosted.org/packages/19/4a/760c8b623441fcafe4dc7c0bedfd14fb91e2963cea9d23b22ce46fedea2d/capturewrap-1.0.0.tar.gz" } ], "1.0.1": [ { "comment_text": "", "digests": { "md5": "80280f9b91317efea8bb311bb0f9fd69", "sha256": "edd7434f21c71b5449e636dc33ed7356b3e8ad7cdaf90ec2ed3b18c4b09c5963" }, "downloads": -1, "filename": "capturewrap-1.0.1.tar.gz", "has_sig": false, "md5_digest": "80280f9b91317efea8bb311bb0f9fd69", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5817, "upload_time": "2017-11-22T11:23:23", "url": "https://files.pythonhosted.org/packages/f7/5a/a7cf841ebc7c7d13d59e8db8064174e81a3cc61695314e54247fe2140695/capturewrap-1.0.1.tar.gz" } ], "1.1.0": [ { "comment_text": "", "digests": { "md5": "4bb957bfebfaf3e20154720ad9404b06", "sha256": "e86faa7743b2ca2400f0b77921e2f6d5e67eb92d51395a57537bf86be82b74dc" }, "downloads": -1, "filename": "capturewrap-1.1.0.tar.gz", "has_sig": false, "md5_digest": "4bb957bfebfaf3e20154720ad9404b06", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6945, "upload_time": "2018-01-16T17:09:49", "url": "https://files.pythonhosted.org/packages/33/29/fc4c189e8f1f76aee7d2b7674b9eed957531317180e31ff456574c72b4ac/capturewrap-1.1.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "4bb957bfebfaf3e20154720ad9404b06", "sha256": "e86faa7743b2ca2400f0b77921e2f6d5e67eb92d51395a57537bf86be82b74dc" }, "downloads": -1, "filename": "capturewrap-1.1.0.tar.gz", "has_sig": false, "md5_digest": "4bb957bfebfaf3e20154720ad9404b06", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6945, "upload_time": "2018-01-16T17:09:49", "url": "https://files.pythonhosted.org/packages/33/29/fc4c189e8f1f76aee7d2b7674b9eed957531317180e31ff456574c72b4ac/capturewrap-1.1.0.tar.gz" } ] }