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 ./container/dangerzone.py #167

Merged
merged 14 commits into from
Nov 10, 2022

Conversation

gmarmstrong
Copy link
Contributor

@gmarmstrong gmarmstrong commented Jun 5, 2022

Cleans up the container script by consolidating all calls to subprocess.run to a single function. Shrinks container/dangerzone.py from 541 lines to 397 total.

Everything (except __main__ itself) has been refactored to raise exceptions instead of returning an integer. Intended to cause no noticeable changes to program behavior.

@gmarmstrong gmarmstrong marked this pull request as ready for review June 5, 2022 22:35
Copy link
Contributor

@deeplow deeplow left a comment

Choose a reason for hiding this comment

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

Overall, I think this is a great refactor, simplifying a lot of stuff. Thanks a lot for your contribution!
I have made some suggestions.

@gmarmstrong gmarmstrong mentioned this pull request Aug 16, 2022
3 tasks
@deeplow
Copy link
Contributor

deeplow commented Sep 21, 2022

Hi @gmarmstrong. Would you have the time to address this review feedback I gave a while ago? (It should be pretty minor)

@deeplow
Copy link
Contributor

deeplow commented Sep 21, 2022

Thanks! Now I think you're only missing catching that ValueError.

gmarmstrong added a commit to gmarmstrong/dangerzone that referenced this pull request Sep 21, 2022
@gmarmstrong
Copy link
Contributor Author

Should be good now! Do double-check that it works, of course.

@deeplow
Copy link
Contributor

deeplow commented Sep 21, 2022

Awesome! I am just going to do some final testing tomorrow but all looks great. Thanks!

I may do a rebase prior to merging, OK?

@gmarmstrong
Copy link
Contributor Author

Awesome! I am just going to do some final testing tomorrow but all looks great. Thanks!

I may do a rebase prior to merging, OK?

Go for it!

deeplow pushed a commit to deeplow/dangerzone that referenced this pull request Sep 22, 2022
@deeplow
Copy link
Contributor

deeplow commented Sep 22, 2022

Rebased, tested on a Linux and Windows system and made lint happy.

@apyrgio
Copy link
Contributor

apyrgio commented Oct 27, 2022

Nice, giving it a look as well.

Copy link
Contributor

@apyrgio apyrgio left a comment

Choose a reason for hiding this comment

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

Did a quick check, and I have some minor comments. Other than that, this is a great improvement to the codebase.

Comment on lines +309 to 322
run_command(
[
"gm",
"convert",
"-size",
f"{width}x{height}",
"-depth",
"8",
f"rgb:{rgb_filename}",
f"pdf:{pdf_filename}",
],
error_message=f"Page {page}/{num_pages} conversion to PDF failed",
timeout_message=f"Error converting RGB to PDF, convert timed out after {DEFAULT_TIMEOUT} seconds",
)
Copy link
Contributor

Choose a reason for hiding this comment

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

We define the same gm command twice, regardless of whether we want OCR or not. Shouldn't we factor it out?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Almost the same, but not exactly the same. The first is from RGB to PNG, the second is from RGB to PDF.

Copy link
Contributor

@apyrgio apyrgio Nov 7, 2022

Choose a reason for hiding this comment

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

Oh, you're right, thanks for pointing it out. Still, I think we can do the factoring out (e.g., gm_convert_to(), and add the target file type and filename as arguments to that command).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see your point, although gm convert is also used (very differently) in the document_to_pixels function, so that function should be named gm_convert_pixels_to. Alternatively, it could be a nested function, but that could be counterproductive to readability.

Since the duplicate code wasn't introduced in this PR, I think it would be better to address it in a later PR. This one introduces the run_command function, which shrank the two gm convert steps considerably already.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, makes sense. We're good to go then!

@deeplow do you want to merge this, or should I go ahead?

@gmarmstrong
Copy link
Contributor Author

Thanks @apyrgio, I'll take a look this weekend.

@deeplow deeplow merged commit 2085405 into freedomofpress:main Nov 10, 2022
@deeplow
Copy link
Contributor

deeplow commented Nov 10, 2022

Rebased and merged. Thanks again @gmarmstrong and sorry that this took so long.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants