-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
2 changed files
with
70 additions
and
51 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import struct | ||
|
||
|
||
def eof_aware_read(stream, length, fail_on_no_data=False): | ||
if length == 0: | ||
return b"" | ||
|
||
ret = stream.read(length) | ||
if not ret: | ||
if fail_on_no_data: | ||
raise EOFError() | ||
|
||
return None | ||
|
||
if len(ret) < length: | ||
raise EOFError() | ||
|
||
return ret | ||
|
||
|
||
def unpack_from_stream(fmt, stream): | ||
data = eof_aware_read(stream, struct.calcsize(fmt)) | ||
if not data: | ||
return None | ||
|
||
return struct.unpack(fmt, data) | ||
|
||
|
||
def dump_dict(d): | ||
def _dump_value(v): | ||
if isinstance(v, dict): | ||
return "{{{}}}".format(dump_dict(v)) | ||
elif isinstance(v, (bytes, list)): | ||
return "<Len: {}>".format(len(v)) | ||
|
||
return str(v) | ||
|
||
return ", ".join(["=".join([str(k), _dump_value(v)]) | ||
for k, v in d.items()]) | ||
|
||
|
||
def log_dict(log, d, prefix=""): | ||
log.debug(prefix + dump_dict(d)) |