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

rename "common" to "document" & simplify "new window" window logic #208

Merged
merged 12 commits into from
Oct 27, 2022

Conversation

deeplow
Copy link
Contributor

@deeplow deeplow commented Sep 19, 2022

Simplify code logic for creating and handling documents through the renaming of Common class to DocumentHolder and associated simplifications.

When we support bulk document conversion we'll need to keep track of each document's state. This refactor opens way for that. A document holder does not represent a document in itself (e.g. can be instantiated without a document) but stores the input name, output name, and in the future possibly the conversion progress.

Clarification renames: some of the names were changed to better reflect the classes' or files' purpose.

  • renames: common.py::Common -> document.py::DocumentHandler
  • renames: global_common.py::GlobalCommon ->logic.py::DangerzoneCore
  • renames: gui/common.py::GuiCommon -> gui/common.py::DangerzoneGui

Associated logic simplication:

  • deduplicates input / output filename validation now done in DocumentHandler, when a new filename is added, throwing an DocumentFilenameException if that validation fails. (code was previously duplicated in gui_main and cli_main).
  • simplifies "new window" creation logic (and associated DocumentHandler instantiation)
    now when the users click new window, it simply opens a new window without extra validation to see if any window is already open and doesn't have a document associated to it.

And lastly, it adds unit tests for document.py (100% coverage)

@deeplow
Copy link
Contributor Author

deeplow commented Sep 19, 2022

I was getting errors with the last commit when we ran the cli without a full path. This is now fixed.

$ poetry run dev_scripts/dangerzone-cli tests/test_docs/sample.pdf 
╭──────────────────────────╮
│           ▄██▄           │
│          ██████          │
│         ███▀▀▀██         │
│        ███   ████        │
│       ███   ██████       │
│      ███   ▀▀▀▀████      │
│     ███████  ▄██████     │
│    ███████ ▄█████████    │
│   ████████████████████   │
│    ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀    │
│                          │
│    Dangerzone v0.3.2     │
│ https://dangerzone.rocks │
╰──────────────────────────╯

Converting document to safe PDF
> /usr/bin/podman run --network none -u dangerzone --security-opt no-new-privileges --userns keep-id --cap-drop all --rm -v tests/test_docs/sample.pdf:/tmp/input_file -v /home/user/.config/dangerzone/tmp/tmpzkabfaa4/pixels:/dangerzone dangerzone.rocks/dangerzone /usr/bin/python3 /usr/local/bin/dangerzone.py document-to-pixels
Invalid JSON returned from container: Error: error creating named volume "tests/test_docs/sample.pdf": error running volume create option: names must match [a-zA-Z0-9][a-zA-Z0-9_.-]*: invalid argument

documents-to-pixels failed

Failed to convert document

@deeplow deeplow force-pushed the simplify-common branch 4 times, most recently from a9afa8d to 38d65c6 Compare September 21, 2022 10:50
tests/test_document.py Outdated Show resolved Hide resolved
@apyrgio apyrgio self-requested a review September 30, 2022 13:03
@apyrgio
Copy link
Contributor

apyrgio commented Oct 4, 2022

I'm testing out this PR in order to see if #204 is resolved, and I have some quick questions:

  1. What is the proper way to test this on Windows environments? In 0.3.2, when I click on New Window from the systray, I don't see any window being opened, nor an exception somewhere (I checked the Event Log Viewer as well). I suppose that the exception is logged somewhere, but I haven't found where yet. Any pointers would be helpful.
  2. In Linux, I have reproduced the error, and I'm seeing that with your fix, two windows get opened. However, when you close either of them, both of them close, which is unexpected. Am I missing something here?

@deeplow
Copy link
Contributor Author

deeplow commented Oct 4, 2022

  1. What is the proper way to test this on Windows environments? In 0.3.2, when I click on New Window from the systray, I don't see any window being opened, nor an exception somewhere (I checked the Event Log Viewer as well). I suppose that the exception is logged somewhere, but I haven't found where yet. Any pointers would be helpful.

I think it would be logged on the terminal from which you launch dangerzone. But I've also had issues where exceptions are consumed somewhere along the codepath. It could be that.

2. In Linux, I have reproduced the error, and I'm seeing that with your fix, two windows get opened.

So two windows get open when only one should open? Or that part is working?

However, when you close either of them, both of them close, which is unexpected.

Yes, that is #205 which was present in linux in 0.3.1. This is fixed in the other PR, I think.

@apyrgio
Copy link
Contributor

apyrgio commented Oct 4, 2022

I think it would be logged on the terminal from which you launch dangerzone. But I've also had issues where exceptions are consumed somewhere along the codepath. It could be that.

It might be that I'm testing it out by running the .exe, not following the instructions on BUILD.md. I'll check that out.

So two windows get open when only one should open? Or that part is working?

I meant that this part is working correctly, yes.

Yes, that is #205 which was present in linux in 0.3.1. This is fixed in the other PR, I think.

Oh, I thought that #205 was affecting windows that closed due to conversion completion, not due to a user action. Ok, then we'll see this later on.

@deeplow
Copy link
Contributor Author

deeplow commented Oct 4, 2022

Yes, that is #205 which was present in linux in 0.3.1. This is fixed in the other PR, I think.

Oh, I thought that #205 was affecting windows that closed due to conversion completion, not due to a user action. Ok, then we'll see this later on.

That issue happened because it would close the application as soon as the first document was converted, regardless of whether any other was in progress.

It might be that I'm testing it out by running the .exe, not following the instructions on BUILD.md. I'll check that out.

Well, I've been testing this mostly by running from the dev_scripts/dangerzone, not the .exe.

dangerzone/document.py Outdated Show resolved Hide resolved
dangerzone/gui/__init__.py Show resolved Hide resolved
dangerzone/document.py Outdated Show resolved Hide resolved
dangerzone/cli.py Outdated Show resolved Hide resolved
dangerzone/document.py Outdated Show resolved Hide resolved
dangerzone/document.py Outdated Show resolved Hide resolved
tests/test_document.py Outdated Show resolved Hide resolved
dangerzone/logic.py Show resolved Hide resolved
@apyrgio
Copy link
Contributor

apyrgio commented Oct 5, 2022

Also, I see the following commits:

Common -> Document                                                                             
Document -> DocumentHolder                                                                     
rename (Global)Common & related to "dangerzone"                                                
rename global_common.py -> logic.py

Might if I make their names a bit more consistent, such as "Rename X module/class to Y"?

@deeplow
Copy link
Contributor Author

deeplow commented Oct 6, 2022

@apyrgio I have addressed all comments in the parallel branch simplify-common-2

@apyrgio
Copy link
Contributor

apyrgio commented Oct 6, 2022

@deeplow Thanks a lot for the changes. I'll take a look and report back.

@apyrgio
Copy link
Contributor

apyrgio commented Oct 12, 2022

I have done some changes of my own, based on the above discussion, which you can find on the simplify-common-3 branch. I have tested it on Linux and it works. I need to iron out one small detail, and then it will be good to merge.

@deeplow deeplow closed this Oct 13, 2022
@deeplow deeplow deleted the simplify-common branch October 13, 2022 10:52
@deeplow deeplow restored the simplify-common branch October 13, 2022 10:52
@deeplow
Copy link
Contributor Author

deeplow commented Oct 13, 2022

Whoops. I have accidentally deleted this while trying to force push and I see no way to reopen this via the UI.

@deeplow deeplow reopened this Oct 13, 2022
@deeplow deeplow force-pushed the simplify-common branch 2 times, most recently from 34b6031 to 7f583c6 Compare October 26, 2022 10:23
@deeplow
Copy link
Contributor Author

deeplow commented Oct 26, 2022

  • Rebased w/ newly included windows tests
  • added type hints to test_documents.py (because tests now have type hinting requirements)
  • fixed args.py and errors.py type hints

@apyrgio I think the last thing missing is you changing your author email, if you so desire.

@deeplow
Copy link
Contributor Author

deeplow commented Oct 26, 2022

Interesting. Mypy is throwing some errors but it doesn't throw them on my system.

dangerzone/errors.py:4: error: Module "typing" has no attribute "ParamSpec"; maybe "_ParamSpec"?  [attr-defined]
dangerzone/errors.py:18: error: The first argument to Callable must be a list of types, parameter specification, or "..."  [misc]
dangerzone/errors.py:18: note: See https://mypy.readthedocs.io/en/stable/kinds_of_types.html#callable-types-and-lambdas
dangerzone/errors.py:22: error: Name "P.args" is not defined  [name-defined]
dangerzone/errors.py:22: error: Name "P.kwargs" is not defined  [name-defined]
```

If we take a look at the docs, it is well documented there: https://mypy.readthedocs.io/en/stable/generics.html?highlight=ParamSpec#declaring-decorators

@deeplow
Copy link
Contributor Author

deeplow commented Oct 26, 2022

It looks like this happening because some of CI doesn't have python3.10 version running. We might have moved too soon with #161.

@deeplow
Copy link
Contributor Author

deeplow commented Oct 26, 2022

Rebased and ready to be merged. Can I merge it @apyrgio?

@apyrgio
Copy link
Contributor

apyrgio commented Oct 27, 2022

Gimme a bit to take a last look, and running the unit tests on Windows. Also, we need to squash "FIXUP" commits, since they pollute the history.

@deeplow
Copy link
Contributor Author

deeplow commented Oct 27, 2022

Also, we need to squash "FIXUP" commits, since they pollute the history.

Done

Rename the `common` module and `common.Common` class to `document` and
`document.Document` respectively. Also, rename the variables that hold
instances of this class.

This change reflects the fact that the class is responsible for tracking
the state of the document. When we add bulk document conversion,
allowing us to keep track of a document's state will be key. This name
change is a step towards that.
@apyrgio
Copy link
Contributor

apyrgio commented Oct 27, 2022

I'm doing some final changes (proper author attribution to some of the commits), and I'm testing on a Windows machine. I'll ping you when you can rebase, sign, and merge.

deeplow and others added 4 commits October 27, 2022 14:46
Avoid setting document's filename via document.filename and instead
do it via object instantiation where possible.

Incidentally this has to change some window logic. When
select_document() is called it no longer checks if there is already an
open window with no document selected yet. The user can open as many
windows with unselected documents as they want.
Rename select_document() to new_window() to better encapsulate the fact
that this function is opening a new Dangerzone window.
Factor out the filename validation logic and move it into the Document
class. Previously, the filename validation logic was scattered across
the CLI and GUI code.

Also, introduce a new errors.py module whose purpose is to handle
document-related errors, by providing:

* A special exception for them (DocumentFilenameExcpetion)
* A decorator that handles DocumentFilenameException, logs it and the
  underlying cause, and exits the program gracefully.
Implement Click's callback interface and create validators for the
input/output filenames, using the logic from the Document class. This
way, we can catch user errors as early as possible.
@apyrgio apyrgio force-pushed the simplify-common branch 2 times, most recently from 0fbb274 to e2c9a9c Compare October 27, 2022 12:15
tests/test_document.py Outdated Show resolved Hide resolved
deeplow and others added 7 commits October 27, 2022 15:20
Make the Document class always resolve relative input/output file paths,
which are usually passed as arguments by users.

Previously, resolving relative filepaths was a job left to the
instantiators of the Document class. This was error-prone since this
conversion must happen in all the places where we instantiated the
Document class.
Let the Document class suggest the default filename for the safe PDF,
based on the provided input filename, appended with the extension
`-safe.pdf`.

Previously, this logic was copy-pasted throughout the code, which made
it difficult to maintain.
Rename the `global_common` module and `global_common.GlobalCommon` class
to `logic` and `logic.Dangerzone` respectively. Also rename variables
that hold instances of this class.

This change is part of the initial refactor to make the Dangerzone class
handle the core logic of the Dangerzone project.
Rename the `gui.common` module and `gui.common.GuiCommon` class
to `gui.logic` and `gui.logic.DangerzoneGui` respectively. We keep as is
the original names of the variables that hold instances of this class,
since they will change in subsequent commits.

This change is part of the initial refactor to make the DangerzoneGui
class handle the GUI logic of the Dangerzone project.
Simplify state sharing by having all dangerzone core logic in one
single class instead of two.
@apyrgio
Copy link
Contributor

apyrgio commented Oct 27, 2022

I've updated the branch with the following changes:

  1. I properly attributed some commits to you. I had split some commits while reviewing and the author became myself, but I changed that back.
  2. I fixed a Windows unit test error that I had actually fixed before on simplify-common-4. We probably didn't get the latest changes before force-pushing to simplify-common. That's ok, but in general it's good to have this in mind.
  3. I checked that the Windows unit tests do pass. The only (minor) issue that we have is that test_lang_eng fails, because Tesseract times out. Since this test works on Linux, and Tesseract runs on a container, I'll attribute it to our timeouts configuration and my slow Windows VM.

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.

2 participants