maybe fix json permission error

This commit is contained in:
pdxlocations committed 2026-09-18 15:48:24 -07:00
1 parent 642c4289f8
commit 6c4bb0da11
2 files changed
+38 -13

No files matched your search

+4 -12
View File
@@ -1,5 +1,4 @@
import os import os
import json
import curses import curses
from typing import Any, List, Dict, Optional from typing import Any, List, Dict, Optional
@@ -429,22 +428,15 @@ def json_editor(stdscr: curses.window, menu_state: Any) -> bool:
menu_state.selected_index = 0 # Track the selected option menu_state.selected_index = 0 # Track the selected option
made_changes = False # Track if any changes were made made_changes = False # Track if any changes were made
script_dir = os.path.dirname(os.path.abspath(__file__)) file_path = config.json_file_path
parent_dir = os.path.abspath(os.path.join(script_dir, os.pardir))
file_path = os.path.join(parent_dir, "config.json")
menu_state.show_save_option = True # Always show the Save button menu_state.show_save_option = True # Always show the Save button
menu_state.help_win = None menu_state.help_win = None
menu_state.help_text = {} menu_state.help_text = {}
# Ensure the file exists # Use the same path and defaults as startup, including the writable
if not os.path.exists(file_path): # fallback for shared installations and CONTACT_CONFIG_ROOT overrides.
with open(file_path, "w") as f: original_data = config.initialize_config()
json.dump({}, f)
# Load JSON data
with open(file_path, "r", encoding="utf-8") as f:
original_data = json.load(f)
data = original_data # Reference to the original data data = original_data # Reference to the original data
menu_state.current_menu = data # Track the current level of the menu menu_state.current_menu = data # Track the current level of the menu
+34 -1
View File
@@ -1,5 +1,9 @@
import unittest import unittest
from tempfile import NamedTemporaryFile import json
from contextlib import ExitStack
from pathlib import Path
from tempfile import NamedTemporaryFile, TemporaryDirectory
from types import SimpleNamespace
from unittest import mock from unittest import mock
import contact.ui.default_config # Initialize config before the color helpers. import contact.ui.default_config # Initialize config before the color helpers.
@@ -7,6 +11,35 @@ from contact.ui import user_config
class UserConfigTests(unittest.TestCase): class UserConfigTests(unittest.TestCase):
def test_editor_reads_and_saves_active_config_outside_installation(self):
for existing in (False, True):
with self.subTest(existing=existing), TemporaryDirectory() as directory, ExitStack() as stack:
root = Path(directory)
config_path = root / "config.json"
package = root / "site-packages" / "contact"
(package / "ui").mkdir(parents=True)
if existing:
config_path.write_text(json.dumps({"message_prefix": "Custom"}), encoding="utf-8")
stack.enter_context(mock.patch.object(user_config, "__file__", str(package / "ui" / "user_config.py")))
stack.enter_context(mock.patch.object(user_config.config, "json_file_path", str(config_path)))
reload_config = stack.enter_context(mock.patch.object(user_config.config, "reload_config"))
stack.enter_context(mock.patch.object(user_config, "reload_translations"))
stack.enter_context(mock.patch.object(user_config, "update_app_settings_help"))
window = mock.Mock()
state = SimpleNamespace()
stack.enter_context(mock.patch.object(user_config, "display_menu", return_value=(window, mock.Mock(), [])))
def save_from_editor():
self.assertEqual(state.current_menu["message_prefix"], "Custom" if existing else ">>")
state.current_menu["message_prefix"] = "Edited"
return 10 # Save button.
window.getch.side_effect = save_from_editor
self.assertFalse(user_config.json_editor(mock.Mock(), state))
self.assertEqual(json.loads(config_path.read_text())["message_prefix"], "Edited")
self.assertFalse((package / "config.json").exists())
reload_config.assert_called_once_with()
def test_ping_bot_enabled_uses_boolean_picker(self) -> None: def test_ping_bot_enabled_uses_boolean_picker(self) -> None:
with mock.patch.object(user_config, "get_list_input", return_value="True") as picker: with mock.patch.object(user_config, "get_list_input", return_value="True") as picker:
result = user_config.edit_value("enabled", "Enabled", "False") result = user_config.edit_value("enabled", "Enabled", "False")