Add Accessor.openText() as context manager for openAddress()#76
Merged
bernhardkaindl merged 6 commits intoxenserver:masterfrom Jun 13, 2023
Merged
Conversation
Codecov Report
@@ Coverage Diff @@
## master #76 +/- ##
==========================================
+ Coverage 77.59% 77.62% +0.03%
==========================================
Files 21 21
Lines 3468 3473 +5
==========================================
+ Hits 2691 2696 +5
Misses 777 777
Flags with carried forward coverage won't be shown. Click here to find out more.
|
Add `Accessor.openText()` as context manager to wrap openAddress() with `io.TextIOWrapper` (if needed). It yields IO[str] or False, closing the created TextIOWrapper and the underlying file on leaving the context. It is intented for use in xcp.repository and any other user requiring text as string without having to call .decode() on the returned file object. It also closes the underlying file object on leaving the context. Signed-off-by: Bernhard Kaindl <bernhard.kaindl@cloud.com>
Use Accessor.openText() in _getVersion() for ConfigParser().read_file(). It simplifies _getVersion() dramatically and causes Accessor.openText() to be tested with xcp.accessor.FileAccessor and xcp.accessor.HTTPAccessor. Signed-off-by: Bernhard Kaindl <bernhard.kaindl@cloud.com>
Test openText() with subclasses of xcp.accessor.MountingAccessor Signed-off-by: Bernhard Kaindl <bernhard.kaindl@cloud.com>
New test: Download a text file containing UTF-8 using FTPAccessor.openText() and compare the returned string. Signed-off-by: Bernhard Kaindl <bernhard.kaindl@cloud.com>
Add test to get text containing UTF-8 using HTTPAccessor.openText() and compare the returned decoded string contents. Signed-off-by: Bernhard Kaindl <bernhard.kaindl@cloud.com>
Split `assert_http_get_request_data()` using contextlib.contextmanager Signed-off-by: Bernhard Kaindl <bernhard.kaindl@cloud.com>
448de5f to
eb14ba0
Compare
psafont
approved these changes
Jun 12, 2023
rosslagerwall
approved these changes
Jun 13, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The commits on this PR depend on the 1st commit:
It adds
Accessor.openText()as a new API to make uses likeConfigParser().read_file(TextIO)which needTextIO(IO[str]). It replaces calls like these (change commit: a97be27?diff=split)with:
It makes it easy to use
TextIO(IO[str]) withxcp.accessoron Python3 by using a context manager which takes care of closing all buffers/readers after leaving the context:xcp/accessor.py:openText(): Context manager to read text from addrAdd
Accessor.openText()as context manager to wrapopenAddress()withio.TextIOWrapper(if needed which is what Yann Dirson did as well in PR Testsuite driven py3 #17/Initial Python3 support with Test-Driven-Development enforced by CI (with git diff coverage and diff lint checking) + mypy, pyre and pytype #27):Accessor.openText():TextIOWrapper(or instance ofIO[str]) orFalseTextIOWrapperand the underlyingfileIt is intented for use in
xcp.repositoryand in any other user requiring text as string without having to call.decode()after reading the returned file object.xcp/repository.py: UseAccessor.openText()forConfigParser()Use
Accessor.openText()in_getVersion()forConfigParser().read_file().It simplifies
_getVersion()dramatically and causesAccessor.openText()to be tested withxcp.accessor.FileAccessorandxcp.accessor.HTTPAccessor.tests/test_mountingaccessor.py: Test MountingAccessor.openText()
Test openText() with subclasses of xcp.accessor.MountingAccessor
tests/test_ftpaccessor.py: Test FTPAccessor.openText()
New test: Download a text file containing UTF-8 using
FTPAccessor.openText()and compare the returned string.tests/test_httpaccessor.py: Add test for HTTPAccessor.openText()
Add test to get text containing UTF-8 using HTTPAccessor.openText() and compare the returned decoded string contents.
tests/test_httpaccessor.py: Split assert_http_get_request_data()
Split
assert_http_get_request_data()usingcontextlib.contextmanager