Skip to content

Commit 046ed0b

Browse files
committed
Move CLI parsing to its own python module
Having this parsing done in the `__init__.py` script leads to issues down the line when one wants to import parts of the code without having to parse the CLI arguments. Doing so enables us to remove the `dev_scripts/dangerzone*` scripts, which were forcing the project into the path and enabling dev mode. - The dev mode is now handled by the use of a DANGERZONE_DEV=1 environment variable; and - The path changes were actually unnecessary when using poetry run, which already does that for us.
1 parent f22bdf3 commit 046ed0b

17 files changed

Lines changed: 121 additions & 180 deletions

dangerzone/__init__.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,5 @@
3030
except ImportError:
3131
pass
3232

33-
if "DANGERZONE_MODE" in os.environ:
34-
mode = os.environ["DANGERZONE_MODE"]
35-
else:
36-
cli_names = [
37-
"dangerzone-cli",
38-
"dangerzone-cli.exe",
39-
"dangerzone-image",
40-
"dangerzone-image.exe",
41-
]
42-
if basename in cli_names:
43-
mode = "cli"
44-
else:
45-
mode = "gui"
46-
47-
48-
if mode == "cli":
49-
from .cli import cli_main as main
50-
else:
51-
from .gui import gui_main as main # noqa: F401
33+
if os.environ.get("DANGERZONE_DEV", "0") == "1":
34+
setattr(sys, "dangerzone_dev", True)

dangerzone/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def print_header(s: str) -> None:
7575
)
7676
@click.version_option(version=get_version(), message="%(version)s")
7777
@errors.handle_document_errors
78-
def cli_main(
78+
def run(
7979
output_filename: Optional[str],
8080
ocr_lang: Optional[str],
8181
filenames: Optional[List[str]],
@@ -175,7 +175,7 @@ def cli_main(
175175
sys.exit(0)
176176

177177

178-
args.override_parser_and_check_suspicious_options(cli_main)
178+
args.override_parser_and_check_suspicious_options(run)
179179

180180

181181
def setup_logging() -> None:

dangerzone/gui/__init__.py

Lines changed: 1 addition & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
import enum
22
import logging
3-
import os
4-
import platform
5-
import signal
63
import sys
74
import typing
8-
from typing import List, Optional
9-
10-
import click
11-
import colorama
125

136
# FIXME: See https://github.com/freedomofpress/dangerzone/issues/320 for more details.
147
if typing.TYPE_CHECKING:
@@ -19,17 +12,7 @@
1912
except ImportError:
2013
from PySide2 import QtCore, QtGui, QtWidgets
2114

22-
from .. import args, errors
23-
from ..document import Document
24-
from ..isolation_provider.container import Container
25-
from ..isolation_provider.dummy import Dummy
26-
from ..isolation_provider.qubes import Qubes, is_qubes_native_conversion
27-
from ..settings import Settings
28-
from ..updater import errors as updater_errors
29-
from ..updater import releases
30-
from ..util import get_resource_path, get_version
31-
from .logic import DangerzoneGui
32-
from .main_window import MainWindow
15+
from ..util import get_resource_path
3316

3417
log = logging.getLogger(__name__)
3518

@@ -119,82 +102,5 @@ def _handle_palette_change(self) -> None:
119102
self.color_scheme_changed.emit()
120103

121104

122-
@click.command()
123-
@click.option(
124-
"--unsafe-dummy-conversion", "dummy_conversion", flag_value=True, hidden=True
125-
)
126-
@click.argument(
127-
"filenames",
128-
required=False,
129-
nargs=-1,
130-
type=click.UNPROCESSED,
131-
callback=args.validate_input_filenames,
132-
)
133-
@click.version_option(version=get_version(), message="%(version)s")
134-
@errors.handle_document_errors
135-
def gui_main(dummy_conversion: bool, filenames: Optional[List[str]]) -> bool:
136-
setup_logging()
137-
138-
if platform.system() == "Darwin":
139-
# Required for macOS Big Sur: https://stackoverflow.com/a/64878899
140-
os.environ["QT_MAC_WANTS_LAYER"] = "1"
141-
142-
# Make sure /usr/local/bin is in the path
143-
os.environ["PATH"] = "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin"
144-
145-
# Don't show ANSI colors from stdout output, to prevent terminal
146-
# colors from breaking the macOS GUI app
147-
colorama.deinit()
148-
149-
# Create the Qt app
150-
app = Application()
151-
152-
# Common objects
153-
if getattr(sys, "dangerzone_dev", False) and dummy_conversion:
154-
dummy = Dummy()
155-
dangerzone = DangerzoneGui(app, isolation_provider=dummy)
156-
elif is_qubes_native_conversion():
157-
qubes = Qubes()
158-
dangerzone = DangerzoneGui(app, isolation_provider=qubes)
159-
else:
160-
container = Container()
161-
dangerzone = DangerzoneGui(app, isolation_provider=container)
162-
163-
# Allow Ctrl-C to smoothly quit the program instead of throwing an exception
164-
signal.signal(signal.SIGINT, signal.SIG_DFL)
165-
166-
def open_files(filenames: List[str] = []) -> None:
167-
documents = [Document(filename) for filename in filenames]
168-
window.conversion_widget.doc_selection_widget.documents_selected.emit(documents)
169-
170-
window = MainWindow(dangerzone)
171-
settings = Settings()
172-
updates_enabled = bool(settings.get("updater_check_all"))
173-
window.toggle_updates_action.setChecked(updates_enabled)
174-
window.startup_thread.start()
175-
176-
if filenames:
177-
open_files(filenames)
178-
179-
# MacOS: Open a new window, if all windows are closed
180-
def application_activated() -> None:
181-
window.show()
182-
window.adjustSize()
183-
184-
# If we get a file open event, open it
185-
app.document_selected.connect(open_files)
186-
187-
# If the application is activated and all windows are closed, open a new one
188-
app.application_activated.connect(application_activated)
189-
190-
# Launch the GUI
191-
ret = app.exec_()
192-
193-
sys.exit(ret)
194-
195-
196105
def setup_logging() -> None:
197106
logging.basicConfig(level=logging.DEBUG, format="[%(levelname)s] %(message)s")
198-
199-
200-
args.override_parser_and_check_suspicious_options(gui_main)

dangerzone/gui/run.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import os
2+
import platform
3+
import signal
4+
import sys
5+
from typing import List, Optional
6+
7+
import click
8+
import colorama
9+
10+
from .. import args, errors
11+
from ..document import Document
12+
from ..isolation_provider.container import Container
13+
from ..isolation_provider.dummy import Dummy
14+
from ..isolation_provider.qubes import Qubes, is_qubes_native_conversion
15+
from ..settings import Settings
16+
from ..util import get_version
17+
from . import Application, setup_logging
18+
from .logic import DangerzoneGui
19+
from .main_window import MainWindow
20+
21+
22+
@click.command()
23+
@click.option(
24+
"--unsafe-dummy-conversion", "dummy_conversion", flag_value=True, hidden=True
25+
)
26+
@click.argument(
27+
"filenames",
28+
required=False,
29+
nargs=-1,
30+
type=click.UNPROCESSED,
31+
callback=args.validate_input_filenames,
32+
)
33+
@click.version_option(version=get_version(), message="%(version)s")
34+
@errors.handle_document_errors
35+
def run(dummy_conversion: bool, filenames: Optional[List[str]]) -> Optional[bool]:
36+
setup_logging()
37+
38+
if platform.system() == "Darwin":
39+
# Required for macOS Big Sur: https://stackoverflow.com/a/64878899
40+
os.environ["QT_MAC_WANTS_LAYER"] = "1"
41+
42+
# Make sure /usr/local/bin is in the path
43+
os.environ["PATH"] = "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin"
44+
45+
# Don't show ANSI colors from stdout output, to prevent terminal
46+
# colors from breaking the macOS GUI app
47+
colorama.deinit()
48+
49+
# Create the Qt app
50+
app = Application()
51+
52+
# Common objects
53+
if getattr(sys, "dangerzone_dev", False) and dummy_conversion:
54+
dummy = Dummy()
55+
dangerzone = DangerzoneGui(app, isolation_provider=dummy)
56+
elif is_qubes_native_conversion():
57+
qubes = Qubes()
58+
dangerzone = DangerzoneGui(app, isolation_provider=qubes)
59+
else:
60+
container = Container()
61+
dangerzone = DangerzoneGui(app, isolation_provider=container)
62+
63+
# Allow Ctrl-C to smoothly quit the program instead of throwing an exception
64+
signal.signal(signal.SIGINT, signal.SIG_DFL)
65+
66+
def open_files(filenames: List[str] = []) -> None:
67+
documents = [Document(filename) for filename in filenames]
68+
window.conversion_widget.doc_selection_widget.documents_selected.emit(documents)
69+
70+
window = MainWindow(dangerzone)
71+
settings = Settings()
72+
updates_enabled = bool(settings.get("updater_check_all"))
73+
window.toggle_updates_action.setChecked(updates_enabled)
74+
window.startup_thread.start()
75+
76+
if filenames:
77+
open_files(filenames)
78+
79+
# MacOS: Open a new window, if all windows are closed
80+
def application_activated() -> None:
81+
window.show()
82+
window.adjustSize()
83+
84+
# If we get a file open event, open it
85+
app.document_selected.connect(open_files)
86+
87+
# If the application is activated and all windows are closed, open a new one
88+
app.application_activated.connect(application_activated)
89+
90+
# Launch the GUI
91+
ret = app.exec_()
92+
93+
sys.exit(ret)
94+
95+
96+
args.override_parser_and_check_suspicious_options(run)

dangerzone/updater/cli.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:
4343

4444
@click.group(context_settings={"show_default": True})
4545
@click.option("--debug", is_flag=True)
46-
def main(debug: bool) -> None:
46+
def run(debug: bool) -> None:
4747
if debug:
4848
click.echo("Debug mode enabled")
4949
level = logging.DEBUG
@@ -52,7 +52,7 @@ def main(debug: bool) -> None:
5252
logging.basicConfig(level=level)
5353

5454

55-
@main.command()
55+
@run.command()
5656
@requires_container_runtime
5757
def upgrade() -> None:
5858
"""Upgrade the sandbox to the latest version available.
@@ -77,7 +77,7 @@ def upgrade() -> None:
7777
raise click.Abort()
7878

7979

80-
@main.command()
80+
@run.command()
8181
@click.option(
8282
"--image",
8383
default=DEFAULT_IMAGE_NAME,
@@ -91,7 +91,7 @@ def store_signatures(image: str) -> None:
9191
click.echo(f"✅ Signatures have been verified and stored locally")
9292

9393

94-
@main.command()
94+
@run.command()
9595
@click.argument("archive_filename", type=click.Path(exists=True))
9696
@click.option(
9797
"--force",
@@ -119,7 +119,7 @@ def load_archive(archive_filename: Path, force: bool) -> None:
119119
raise click.Abort()
120120

121121

122-
@main.command()
122+
@run.command()
123123
@click.option(
124124
"--image",
125125
default=DEFAULT_IMAGE_NAME,
@@ -149,7 +149,7 @@ def prepare_archive(image: str, output: str, arch: str) -> None:
149149
raise click.Abort()
150150

151151

152-
@main.command()
152+
@run.command()
153153
@click.option(
154154
"--image",
155155
default=DEFAULT_IMAGE_NAME,
@@ -171,7 +171,7 @@ def verify_local(image: str) -> None:
171171
)
172172

173173

174-
@main.command()
174+
@run.command()
175175
@click.argument("image_name")
176176
@click.option(
177177
"--repository",
@@ -206,4 +206,4 @@ def attest_provenance(
206206

207207

208208
if __name__ == "__main__":
209-
main()
209+
run()

dev_scripts/dangerzone

Lines changed: 0 additions & 13 deletions
This file was deleted.

dev_scripts/dangerzone-cli

Lines changed: 0 additions & 1 deletion
This file was deleted.

dev_scripts/dangerzone-cli.bat

Lines changed: 0 additions & 2 deletions
This file was deleted.

dev_scripts/dangerzone-image

Lines changed: 0 additions & 13 deletions
This file was deleted.

dev_scripts/dangerzone-image.bat

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)