# webkitcorepy
Provides a number of utilities intended to support intermediate to advanced Python programming.
## Requirements
The mock, requests and six libraries.
## Usage
Version representation
```
from webkitcorepy import Version
version = Version(1, 2, 3)
```
Unicode stream management across Python 2 and 3
```
from webkitcorepy import BytesIO, StringIO, UnicodeIO, unicode
```
Encoding and decoding byte strings and unicode strings
```
from webkitcorepy import string_utils
string_utils.encode(...)
string_utils.decode(...)
```
Automatically install libraries on import.
```
from webkitcorepy import AutoInstall
AutoInstall.register(Package('requests', Version(2, 24)))
import requests
```
Mocking basic time and sleep calls
```
import time
from webkitcorepy import mocks
with mocks.Time:
stamp = time.time()
time.sleep(5)
```
Capturing stdout, stderr and logging output for testing
```
capturer = OutputCapture()
with capturer:
print('data\n')
assert capturer.stdout.getvalue() == 'data\n'
```
Capturing stdout, stderr and logging output for testing
```
capturer = OutputCapture()
with capturer:
print('data\n')
assert capturer.stdout.getvalue() == 'data\n'
```
Timeout context:
```
import time
from webkitcorepy import Timeout
with Timeout(5, handler=RuntimeError('Exceeded 5 second timeout')):
time.sleep(4)
```
subprocess.run replacement:
```
import sys
from webkitcorepy import run
result = run([sys.executable, '-c', 'print("message")'], capture_output=True, encoding='utf-8')
```
Mocking of subprocess commands:
```
from webkitcorepy import mocks, run
with mocks.Subprocess(
'ls', completion=mocks.ProcessCompletion(returncode=0, stdout='file1.txt\nfile2.txt\n'),
):
result = run(['ls'], capture_output=True, encoding='utf-8')
assert result.returncode == 0
assert result.stdout == 'file1.txt\nfile2.txt\n'
```
The mocking system for subprocess also supports other subprocess APIs based on Popen:
```
with mocks.Subprocess(
'ls', completion=mocks.ProcessCompletion(returncode=0, stdout='file1.txt\nfile2.txt\n'),
):
assert subprocess.check_output(['ls']) == b'file1.txt\nfile2.txt\n'
assert subprocess.check_call(['ls']) == 0
```
For writing integration tests, the mocking system for subprocess supports mocking multiple process calls at the same time:
```
with mocks.Subprocess(
mocks.Subprocess.CommandRoute('command-a', 'argument', completion=mocks.ProcessCompletion(returncode=0)),
mocks.Subprocess.CommandRoute('command-b', completion=mocks.ProcessCompletion(returncode=-1)),
):
result = run(['command-a', 'argument'])
assert result.returncode == 0
result = run(['command-b'])
assert result.returncode == -1
```
Raw data
{
"_id": null,
"home_page": "https://github.com/WebKit/WebKit/tree/main/Tools/Scripts/libraries/webkitcorepy",
"name": "webkitcorepy",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "python unicode",
"author": "Jonathan Bedard",
"author_email": "jbedard@apple.com",
"download_url": "https://files.pythonhosted.org/packages/40/a0/e8356cc961cc2b8afd084eb639cd6bb28990c7a9282ab4056703b2f91ffe/webkitcorepy-1.0.1.tar.gz",
"platform": null,
"description": "# webkitcorepy\n\nProvides a number of utilities intended to support intermediate to advanced Python programming.\n\n## Requirements\n\nThe mock, requests and six libraries.\n \n## Usage\n\nVersion representation\n```\nfrom webkitcorepy import Version\nversion = Version(1, 2, 3)\n```\n\nUnicode stream management across Python 2 and 3\n```\nfrom webkitcorepy import BytesIO, StringIO, UnicodeIO, unicode\n```\n\nEncoding and decoding byte strings and unicode strings\n```\nfrom webkitcorepy import string_utils\n\nstring_utils.encode(...)\nstring_utils.decode(...)\n```\n\nAutomatically install libraries on import.\n```\nfrom webkitcorepy import AutoInstall\nAutoInstall.register(Package('requests', Version(2, 24)))\nimport requests\n```\n\nMocking basic time and sleep calls\n```\nimport time\nfrom webkitcorepy import mocks\n\nwith mocks.Time:\n stamp = time.time()\n time.sleep(5)\n```\nCapturing stdout, stderr and logging output for testing\n```\ncapturer = OutputCapture()\nwith capturer:\n print('data\\n')\nassert capturer.stdout.getvalue() == 'data\\n'\n```\nCapturing stdout, stderr and logging output for testing\n```\ncapturer = OutputCapture()\nwith capturer:\n print('data\\n')\nassert capturer.stdout.getvalue() == 'data\\n'\n```\n\nTimeout context:\n```\nimport time\n\nfrom webkitcorepy import Timeout\n\nwith Timeout(5, handler=RuntimeError('Exceeded 5 second timeout')):\n time.sleep(4)\n```\n\nsubprocess.run replacement:\n```\nimport sys\n\nfrom webkitcorepy import run\n\nresult = run([sys.executable, '-c', 'print(\"message\")'], capture_output=True, encoding='utf-8')\n```\n\nMocking of subprocess commands:\n```\nfrom webkitcorepy import mocks, run\n\nwith mocks.Subprocess(\n 'ls', completion=mocks.ProcessCompletion(returncode=0, stdout='file1.txt\\nfile2.txt\\n'),\n):\n result = run(['ls'], capture_output=True, encoding='utf-8')\n assert result.returncode == 0\n assert result.stdout == 'file1.txt\\nfile2.txt\\n'\n```\nThe mocking system for subprocess also supports other subprocess APIs based on Popen:\n```\nwith mocks.Subprocess(\n 'ls', completion=mocks.ProcessCompletion(returncode=0, stdout='file1.txt\\nfile2.txt\\n'),\n):\n assert subprocess.check_output(['ls']) == b'file1.txt\\nfile2.txt\\n'\n assert subprocess.check_call(['ls']) == 0\n```\nFor writing integration tests, the mocking system for subprocess supports mocking multiple process calls at the same time:\n```\nwith mocks.Subprocess(\n mocks.Subprocess.CommandRoute('command-a', 'argument', completion=mocks.ProcessCompletion(returncode=0)),\n mocks.Subprocess.CommandRoute('command-b', completion=mocks.ProcessCompletion(returncode=-1)),\n):\n result = run(['command-a', 'argument'])\n assert result.returncode == 0\n\n result = run(['command-b'])\n assert result.returncode == -1\n```\n",
"bugtrack_url": null,
"license": "Modified BSD",
"summary": "Library containing various Python support classes and functions.",
"version": "1.0.1",
"project_urls": {
"Homepage": "https://github.com/WebKit/WebKit/tree/main/Tools/Scripts/libraries/webkitcorepy"
},
"split_keywords": [
"python",
"unicode"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "918c28bc3e0211baff057d93765c3e2ab8fdc08f2b9ee327cc72ed99ed2c9eba",
"md5": "9c2493f6e618c610fa2ac54223102d01",
"sha256": "b5a94a9bc0b3d519a89a4e4bc628da495fba00a9a8abd1cf5ae868da5469bd25"
},
"downloads": -1,
"filename": "webkitcorepy-1.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "9c2493f6e618c610fa2ac54223102d01",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 109963,
"upload_time": "2024-10-18T16:54:52",
"upload_time_iso_8601": "2024-10-18T16:54:52.160369Z",
"url": "https://files.pythonhosted.org/packages/91/8c/28bc3e0211baff057d93765c3e2ab8fdc08f2b9ee327cc72ed99ed2c9eba/webkitcorepy-1.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "40a0e8356cc961cc2b8afd084eb639cd6bb28990c7a9282ab4056703b2f91ffe",
"md5": "14779a2ceb7e1ced271da459f4d1ec1f",
"sha256": "625116398050c6c6114f3a67114dc28f9764aa8e64637da1e2404678fa865a15"
},
"downloads": -1,
"filename": "webkitcorepy-1.0.1.tar.gz",
"has_sig": false,
"md5_digest": "14779a2ceb7e1ced271da459f4d1ec1f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 58374,
"upload_time": "2024-10-18T16:54:53",
"upload_time_iso_8601": "2024-10-18T16:54:53.588240Z",
"url": "https://files.pythonhosted.org/packages/40/a0/e8356cc961cc2b8afd084eb639cd6bb28990c7a9282ab4056703b2f91ffe/webkitcorepy-1.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-18 16:54:53",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "WebKit",
"github_project": "WebKit",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "webkitcorepy"
}