Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: introduce ProblemReport._write_base64_encoded_line #238

Closed
wants to merge 1 commit into from

Conversation

bdrung
Copy link
Collaborator

@bdrung bdrung commented Oct 27, 2023

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.

@codecov
Copy link

codecov bot commented Oct 27, 2023

Codecov Report

Merging #238 (9f853d3) into main (5d900fa) will increase coverage by 10.52%.
The diff coverage is 100.00%.

@@             Coverage Diff             @@
##             main     #238       +/-   ##
===========================================
+ Coverage   73.16%   83.69%   +10.52%     
===========================================
  Files          91       93        +2     
  Lines       18031    18756      +725     
===========================================
+ Hits        13193    15698     +2505     
+ Misses       4838     3058     -1780     
Files Coverage Δ
problem_report.py 97.27% <100.00%> (-0.01%) ⬇️

... and 28 files with indirect coverage changes

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>
Copy link
Contributor

@schopin-pro schopin-pro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you put the cursor far enough in the refactoring. The new method is still a footgun, as it requires the caller to actually prepare the terrain first by writing out the RFC822 key and base64 header, which means the base64-specific knowledge is split in two places.

Instead, I suggest having something like this:

def _write_base64_paragraph(file, typing.BinaryIO, key: str, data: list[bytes]):
   """Write the data out in a RFC 822 base64 multiline paragraph, using one line per item in the data list.
   """

This would even make the control flow a bit easier in _write_key_and_binary_value_compressed_and_encoded_to_file (wow that name is a handful, typing it out takes forever). You don't have to rewind the file in case of aborted write, you just never call the helper method :)

@bdrung
Copy link
Collaborator Author

bdrung commented Oct 31, 2023

issue: _write_base64_paragraph requires to construct a complete data list containing the whole data. This data structure can become quite large, because it can contain the coredump.

@schopin-pro
Copy link
Contributor

Meh, that's barely relevant 😉.

Maybe have

def _binary_chunks_from_value(v: Any) -> Generator[bytes, None, None]:
    pass

def _write_base64_paragraph(file, typing.BinaryIO, key: str, data: Iterable[bytes]):
    pass

def _write_key_and_binary_value_compressed_and_encoded_to_file(
    self, file: typing.BinaryIO, key: str
) -> None:
    _write_base64_paragraph(key, file, _binary_chunks_from_value(self.data[key]))

With binary_chunks_from_value being essentially the same as your current _write_key_and_binary_value_compressed_and_encoded_to_file with calls to yield instead of calls to write_base64_encoded_line and the base64 header + recovery code moved into an except block in _write_base64_paragraph.

WDYT?

@bdrung
Copy link
Collaborator Author

bdrung commented Oct 31, 2023

That idea sounds promising. Do you want to implement it or should I give it a try?

@schopin-pro
Copy link
Contributor

I'll give it a shot.

@bdrung
Copy link
Collaborator Author

bdrung commented Oct 31, 2023

Closing this PR in favor of #242.

@bdrung bdrung closed this Oct 31, 2023
@bdrung bdrung deleted the write-base64-encoded branch October 31, 2023 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants