-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Christian Barra
committed
Jul 19, 2017
1 parent
efe04db
commit 0be85e6
Showing
6 changed files
with
104 additions
and
52 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,55 @@ | ||
import sys | ||
import asyncio | ||
import json | ||
import logging | ||
import aiohttp | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class JsonStreamResult: | ||
class _JsonStreamResult: | ||
def __init__(self, response, transform=None): | ||
self.response = response | ||
self.transform = transform or (lambda x: x) | ||
self._response = response | ||
self._transform = transform or (lambda x: x) | ||
|
||
def __aiter__(self): | ||
return self | ||
|
||
if sys.version_info <= (3, 5, 2): | ||
__aiter__ = asyncio.coroutine(__aiter__) | ||
|
||
async def __anext__(self): | ||
response = await self.fetch() | ||
if response: | ||
return response | ||
else: | ||
raise StopAsyncIteration | ||
|
||
async def fetch(self): | ||
while True: | ||
try: | ||
data = await self.response.content.readline() | ||
data = await self._response.content.readline() | ||
if not data: | ||
break | ||
except (aiohttp.ClientConnectionError, | ||
aiohttp.ServerDisconnectedError): | ||
break | ||
yield self.transform(json.loads(data.decode('utf8'))) | ||
return self._transform(json.loads(data.decode('utf8'))) | ||
|
||
async def close(self): | ||
# response.release() indefinitely hangs because the server is sending | ||
# an infinite stream of messages. | ||
# (see https://github.com/KeepSafe/aiohttp/issues/739) | ||
|
||
# response error , it has been closed | ||
self.response.close() | ||
await self._response.close() | ||
|
||
|
||
async def json_stream_result(response, transform=None, stream=True): | ||
json_stream = JsonStreamResult(response, transform) | ||
json_stream = _JsonStreamResult(response, transform) | ||
if stream: | ||
return json_stream | ||
data = [] | ||
async for obj in json_stream.fetch(): | ||
async for obj in json_stream: | ||
data.append(obj) | ||
return data |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,77 @@ | ||
import sys | ||
import asyncio | ||
import struct | ||
import aiohttp | ||
|
||
from . import constants | ||
from aiodocker.utils import decoded | ||
from aiodocker.utils import _DecodeHelper | ||
|
||
|
||
class MultiplexedResult: | ||
def __init__(self, response): | ||
self.response = response | ||
def __init__(self, response, raw): | ||
self._response = response | ||
|
||
if raw: | ||
self._iter = self.fetch_raw | ||
else: | ||
self._iter = self.fetch | ||
|
||
def __aiter__(self): | ||
return self | ||
|
||
if sys.version_info <= (3, 5, 2): | ||
__aiter__ = asyncio.coroutine(__aiter__) | ||
|
||
async def __anext__(self): | ||
response = await self._iter() | ||
if not response: | ||
raise StopAsyncIteration | ||
return response | ||
|
||
async def fetch(self): | ||
try: | ||
while True: | ||
try: | ||
hdrlen = constants.STREAM_HEADER_SIZE_BYTES | ||
header = await self.response.content.readexactly(hdrlen) | ||
header = await self._response.content.readexactly(hdrlen) | ||
_, length = struct.unpack('>BxxxL', header) | ||
if not length: | ||
continue | ||
data = await self.response.content.readexactly(length) | ||
data = await self._response.content.readexactly(length) | ||
except (aiohttp.ClientConnectionError, | ||
aiohttp.ServerDisconnectedError): | ||
break | ||
except asyncio.IncompleteReadError: | ||
break | ||
yield data | ||
return data | ||
finally: | ||
await self.close() | ||
|
||
async def fetch_raw(self): | ||
try: | ||
async for data in self.response.content.iter_chunked(1024): | ||
yield data | ||
async for data in self._response.content.iter_chunked(1024): | ||
return data | ||
except aiohttp.ClientConnectionError: | ||
pass | ||
finally: | ||
await self.close() | ||
|
||
async def close(self): | ||
await self.response.release() | ||
await self._response.release() | ||
|
||
|
||
async def multiplexed_result(response, follow=False, is_tty=False, | ||
encoding='utf-8'): | ||
log_stream = MultiplexedResult(response) | ||
|
||
log_stream = MultiplexedResult(response, raw=False) | ||
if is_tty: | ||
if follow: | ||
return decoded(log_stream.fetch_raw(), encoding=encoding) | ||
else: | ||
d = [] | ||
async for piece in decoded(log_stream.fetch_raw(), | ||
encoding=encoding): | ||
d.append(piece) | ||
return ''.join(d) | ||
log_stream = MultiplexedResult(response, raw=True) | ||
|
||
if follow: | ||
return _DecodeHelper(log_stream, encoding=encoding) | ||
else: | ||
if follow: | ||
return decoded(log_stream.fetch(), encoding=encoding) | ||
else: | ||
d = [] | ||
async for piece in decoded(log_stream.fetch(), | ||
encoding=encoding): | ||
d = [] | ||
async for piece in _DecodeHelper(log_stream, encoding=encoding): | ||
if isinstance(piece, str): | ||
d.append(piece) | ||
return ''.join(d) | ||
return ''.join(d) |
This file contains 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
This file contains 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
This file contains 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