Skip to content

Commit

Permalink
Allow specifying config/cache directories
Browse files Browse the repository at this point in the history
Allow specifying the config/cache directories that Dangerzone will use,
both for the CLI and the GUI, via CLI arguments. This way, the user has
better control of where Dangerzone will save its state and, most
importantly, we can use this feature to make each test run in a
different directory.

Moreover, we fix an issue where the temporary conversion artifacts were
stored in the configuration directory, instead of the cache directory.

Note that while most OSes (Windows and Linux) offer a way to specify
these directories through environment variables, the same does not apply
to MacOS [1], so having a CLI flag is required.

[1]: tox-dev/platformdirs#4
  • Loading branch information
apyrgio committed Feb 8, 2023
1 parent 23ee60d commit b9287a0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 18 deletions.
20 changes: 17 additions & 3 deletions dangerzone/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import click
from colorama import Back, Fore, Style

from . import args, errors
from . import args, errors, util
from .document import ARCHIVE_SUBDIR, SAFE_EXTENSION
from .isolation_provider.container import Container
from .isolation_provider.dummy import Dummy
Expand Down Expand Up @@ -33,6 +33,18 @@ def print_header(s: str) -> None:
flag_value=True,
help=f"Archives the unsafe version in a subdirectory named '{ARCHIVE_SUBDIR}'",
)
@click.option(
"--config-dir",
default=util.get_default_config_dir(),
show_default=True,
help="The user directory where Dangerzone will store configuration files",
)
@click.option(
"--cache-dir",
default=util.get_default_cache_dir(),
show_default=True,
help="The user directory where Dangerzone will store temporary files",
)
@click.option(
"--unsafe-dummy-conversion", "dummy_conversion", flag_value=True, hidden=True
)
Expand All @@ -50,14 +62,16 @@ def cli_main(
ocr_lang: Optional[str],
filenames: List[str],
archive: bool,
config_dir: str,
cache_dir: str,
dummy_conversion: bool,
) -> None:
setup_logging()

if getattr(sys, "dangerzone_dev", False) and dummy_conversion:
dangerzone = DangerzoneCore(Dummy())
dangerzone = DangerzoneCore(Dummy(), config_dir)
else:
dangerzone = DangerzoneCore(Container())
dangerzone = DangerzoneCore(Container(cache_dir), config_dir)

display_banner()
if len(filenames) == 1 and output_filename:
Expand Down
31 changes: 26 additions & 5 deletions dangerzone/gui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
except ImportError:
from PySide2 import QtCore, QtGui, QtWidgets

from .. import args, errors
from .. import args, errors, util
from ..document import Document
from ..isolation_provider.container import Container
from ..isolation_provider.dummy import Dummy
Expand Down Expand Up @@ -59,6 +59,18 @@ def monkeypatch_event(arg__1: QtCore.QEvent) -> bool:


@click.command()
@click.option(
"--config-dir",
default=util.get_default_config_dir(),
show_default=True,
help="The user directory where Dangerzone will store configuration files",
)
@click.option(
"--cache-dir",
default=util.get_default_cache_dir(),
show_default=True,
help="The user directory where Dangerzone will store temporary files",
)
@click.option(
"--unsafe-dummy-conversion", "dummy_conversion", flag_value=True, hidden=True
)
Expand All @@ -71,7 +83,12 @@ def monkeypatch_event(arg__1: QtCore.QEvent) -> bool:
)
@click.version_option(version=get_version(), message="%(version)s")
@errors.handle_document_errors
def gui_main(dummy_conversion: bool, filenames: Optional[List[str]]) -> bool:
def gui_main(
dummy_conversion: bool,
config_dir: str,
cache_dir: str,
filenames: Optional[List[str]],
) -> bool:
setup_logging()

if platform.system() == "Darwin":
Expand All @@ -91,10 +108,14 @@ def gui_main(dummy_conversion: bool, filenames: Optional[List[str]]) -> bool:
# Common objects
if getattr(sys, "dangerzone_dev", False) and dummy_conversion:
dummy = Dummy()
dangerzone = DangerzoneGui(app, isolation_provider=dummy)
dangerzone = DangerzoneGui(
app, isolation_provider=dummy, appdata_path=config_dir
)
else:
container = Container()
dangerzone = DangerzoneGui(app, isolation_provider=container)
container = Container(cache_dir)
dangerzone = DangerzoneGui(
app, isolation_provider=container, appdata_path=config_dir
)

# Allow Ctrl-C to smoothly quit the program instead of throwing an exception
signal.signal(signal.SIGINT, signal.SIG_DFL)
Expand Down
7 changes: 5 additions & 2 deletions dangerzone/gui/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ class DangerzoneGui(DangerzoneCore):
"""

def __init__(
self, app: QtWidgets.QApplication, isolation_provider: IsolationProvider
self,
app: QtWidgets.QApplication,
isolation_provider: IsolationProvider,
appdata_path: str,
) -> None:
super().__init__(isolation_provider)
super().__init__(isolation_provider, appdata_path)

# Qt app
self.app = app
Expand Down
8 changes: 3 additions & 5 deletions dangerzone/isolation_provider/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import tempfile
from typing import Callable, List, Optional, Tuple

import appdirs

from ..document import Document
from ..util import get_resource_path, get_subprocess_startupinfo
from .base import IsolationProvider
Expand All @@ -36,8 +34,8 @@ class Container(IsolationProvider):
# Name of the dangerzone container
CONTAINER_NAME = "dangerzone.rocks/dangerzone"

def __init__(self) -> None:
pass
def __init__(self, cache_dir: str) -> None:
self.cache_dir = cache_dir

@staticmethod
def get_runtime_name() -> str:
Expand Down Expand Up @@ -227,7 +225,7 @@ def _convert(
else:
ocr = "0"

dz_tmp = os.path.join(appdirs.user_config_dir("dangerzone"), "tmp")
dz_tmp = os.path.join(self.cache_dir, "tmp")
os.makedirs(dz_tmp, exist_ok=True)

tmpdir = tempfile.TemporaryDirectory(dir=dz_tmp)
Expand Down
7 changes: 4 additions & 3 deletions dangerzone/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import sys
from typing import Callable, List, Optional

import appdirs
import colorama

from . import errors
Expand All @@ -26,12 +25,14 @@ class DangerzoneCore(object):
Singleton of shared state / functionality throughout the app
"""

def __init__(self, isolation_provider: IsolationProvider) -> None:
def __init__(
self, isolation_provider: IsolationProvider, appdata_path: str
) -> None:
# Initialize terminal colors
colorama.init(autoreset=True)

# App data folder
self.appdata_path = appdirs.user_config_dir("dangerzone")
self.appdata_path = appdata_path

# Languages supported by tesseract
with open(get_resource_path("ocr-languages.json"), "r") as f:
Expand Down
10 changes: 10 additions & 0 deletions dangerzone/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
import subprocess
import sys

import appdirs


def get_default_config_dir() -> str:
return appdirs.user_config_dir("dangerzone")


def get_default_cache_dir() -> str:
return appdirs.user_cache_dir("dangerzone")


def get_resource_path(filename: str) -> str:
if getattr(sys, "dangerzone_dev", False):
Expand Down

0 comments on commit b9287a0

Please sign in to comment.