Skip to content

Commit

Permalink
refactor: introduce ProblemReport._write_base64_encoded_line
Browse files Browse the repository at this point in the history
Introduce `ProblemReport._write_base64_encoded_line` which writes one
line of a RFC822 multiline. This will write blocks line by line instead
of writing the leading space after writing a line.

Signed-off-by: Benjamin Drung <benjamin.drung@canonical.com>
  • Loading branch information
bdrung committed Oct 27, 2023
1 parent 5d900fa commit 9f853d3
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions problem_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,16 @@ def _write_key_and_ascii_value_to_file(
file.write(v)
file.write(b"\n")

@staticmethod
def _write_base64_encoded_line(file: typing.BinaryIO, block: bytes) -> None:
"""Write the given binary block as base64-encoded line.
The base64-encoded line is assumed to be part of a RFC822 multiline.
"""
file.write(b" ")
file.write(base64.b64encode(block))
file.write(b"\n")

def _write_key_and_binary_value_compressed_and_encoded_to_file(
self, file: typing.BinaryIO, key: str
) -> None:
Expand All @@ -466,13 +476,11 @@ def _write_key_and_binary_value_compressed_and_encoded_to_file(
size = 0

curr_pos = file.tell()
file.write(key.encode("ASCII"))
file.write(b": base64\n ")
file.write(f"{key}: base64\n".encode("ASCII"))

# CompressedValue
if isinstance(v, CompressedValue):
file.write(base64.b64encode(v.gzipvalue))
file.write(b"\n")
self._write_base64_encoded_line(file, v.gzipvalue)
return

# write gzip header
Expand All @@ -482,8 +490,7 @@ def _write_key_and_binary_value_compressed_and_encoded_to_file(
+ key.encode("UTF-8")
+ b"\000"
)
file.write(base64.b64encode(gzip_header))
file.write(b"\n ")
self._write_base64_encoded_line(file, gzip_header)
crc = zlib.crc32(b"")
write_aborted = False

Expand All @@ -494,8 +501,7 @@ def _write_key_and_binary_value_compressed_and_encoded_to_file(
crc = zlib.crc32(v, crc)
outblock = bc.compress(v)
if outblock:
file.write(base64.b64encode(outblock))
file.write(b"\n ")
self._write_base64_encoded_line(file, outblock)
# file reference
else:
if len(v) >= 3 and v[2] is not None:
Expand All @@ -521,8 +527,7 @@ def _write_key_and_binary_value_compressed_and_encoded_to_file(
if block:
outblock = bc.compress(block)
if outblock:
file.write(base64.b64encode(outblock))
file.write(b"\n ")
self._write_base64_encoded_line(file, outblock)
else:
break
if not hasattr(v[0], "read"):
Expand All @@ -542,8 +547,7 @@ def _write_key_and_binary_value_compressed_and_encoded_to_file(
block += struct.pack("<L", crc & 0xFFFFFFFF)
block += struct.pack("<L", size & 0xFFFFFFFF)

file.write(base64.b64encode(block))
file.write(b"\n")
self._write_base64_encoded_line(file, block)

def add_to_existing(self, reportfile, keep_times=False):
"""Add this report's data to an already existing report file.
Expand Down

0 comments on commit 9f853d3

Please sign in to comment.