Skip to content

Commit 885ca1b

Browse files
committed
[app] Add ability to save settings as user default #202
1 parent ec607c8 commit 885ca1b

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

dvr_scan/app/application.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import webbrowser
1919
from logging import getLogger
2020
from pathlib import Path
21+
from tempfile import NamedTemporaryFile
2122

2223
from scenedetect import AVAILABLE_BACKENDS, FrameTimecode, open_video
2324

@@ -26,7 +27,13 @@
2627
from dvr_scan.app.region_editor import RegionEditor
2728
from dvr_scan.app.scan_window import ScanWindow
2829
from dvr_scan.app.widgets import ColorPicker, Spinbox, TimecodeEntry
29-
from dvr_scan.config import CHOICE_MAP, CONFIG_MAP, ConfigLoadFailure, ConfigRegistry
30+
from dvr_scan.config import (
31+
CHOICE_MAP,
32+
CONFIG_MAP,
33+
USER_CONFIG_FILE_PATH,
34+
ConfigLoadFailure,
35+
ConfigRegistry,
36+
)
3037
from dvr_scan.scanner import OutputMode, Point
3138
from dvr_scan.shared import ScanSettings
3239
from dvr_scan.subtractor import SubtractorCudaMOG2
@@ -1182,6 +1189,7 @@ def save(self, settings: ScanSettings) -> ScanSettings:
11821189
settings.set("output-mode", self._output_mode)
11831190
if self._output_dir:
11841191
settings.set("output-dir", self._output_dir)
1192+
# TODO: Should we save all these settings instead of being dependent on output mode?
11851193
if self._output_mode == OutputMode.FFMPEG:
11861194
settings.set("ffmpeg-input-args", self._ffmpeg_input_args.get())
11871195
settings.set("ffmpeg-output-args", self._ffmpeg_output_args.get())
@@ -1226,6 +1234,7 @@ def __init__(self, root: tk.Tk, frame: tk.Widget):
12261234
pady=(0, PADDING),
12271235
)
12281236
self._scan_only = tk.BooleanVar(frame, value=False)
1237+
# TODO: This should be merged into output-mode to match the config file option.
12291238
self._scan_only_button = ttk.Checkbutton(
12301239
frame,
12311240
text="Scan Only",
@@ -1346,7 +1355,9 @@ def _create_menubar(self):
13461355
)
13471356
# TODO: Add functionality to save settings to a config file.
13481357
settings_menu.add_command(label="Save...", underline=0, command=self._on_save_config)
1349-
settings_menu.add_command(label="Save As User Default", underline=2, state=tk.DISABLED)
1358+
settings_menu.add_command(
1359+
label="Save As User Default", underline=2, command=self._on_save_config_as_user_default
1360+
)
13501361
settings_menu.add_separator()
13511362
settings_menu.add_command(
13521363
label="Reset To User Default", underline=12, command=self._reset_config
@@ -1503,9 +1514,25 @@ def _on_save_config(self):
15031514
if not save_path:
15041515
return
15051516
settings = self._get_config_settings()
1517+
logger.debug(f"saving config to {save_path}")
15061518
with open(save_path, "w") as file:
15071519
settings.write_to_file(file)
15081520

1521+
def _on_save_config_as_user_default(self):
1522+
if not tkinter.messagebox.askyesno(
1523+
title="Save As User Default",
1524+
message="Existing user config will be overwritten. Do you want to continue?",
1525+
icon=tkinter.messagebox.WARNING,
1526+
):
1527+
return
1528+
1529+
settings = self._get_config_settings()
1530+
logger.debug(f"saving config to {USER_CONFIG_FILE_PATH}")
1531+
with NamedTemporaryFile(mode="w", delete_on_close=False) as file:
1532+
settings.write_to_file(file)
1533+
file.close()
1534+
Path(file.name).replace(USER_CONFIG_FILE_PATH)
1535+
15091536
def _get_config_settings(self) -> ScanSettings:
15101537
"""Get current UI state for writing a config file."""
15111538
settings = ScanSettings(args=None, config=ConfigRegistry())

dvr_scan/shared/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import argparse
1313
import typing as ty
1414

15+
import dvr_scan
1516
from dvr_scan.config import ConfigRegistry
1617

1718

@@ -59,6 +60,8 @@ def set(self, option: str, value: ty.Union[str, int, float, bool]):
5960

6061
def write_to_file(self, file: ty.TextIO):
6162
"""Get application settings as a config file. Only works for the UI, not CLI."""
63+
file.write("# DVR-Scan Config File\n")
64+
file.write(f"# Created by: DVR-Scan {dvr_scan.__version__}\n")
6265
keys = sorted(self._app_settings.keys())
6366
if "mask-output" in keys:
6467
keys.remove("mask-output")

0 commit comments

Comments
 (0)