2022-07-29 22:17:50 +00:00
|
|
|
import json
|
|
|
|
import os
|
2024-11-18 10:12:58 +00:00
|
|
|
from collections.abc import MutableMapping
|
2022-07-29 22:17:50 +00:00
|
|
|
|
|
|
|
from .paths import get_user_dir
|
|
|
|
|
|
|
|
# These settings are the defaults for SVG metadata settings of the same name in
|
|
|
|
# lib.extensions.base.InkstitchMetadata
|
|
|
|
DEFAULT_METADATA = {
|
2024-07-24 14:28:23 +00:00
|
|
|
"min_stitch_len_mm": 0.1,
|
2022-07-29 22:17:50 +00:00
|
|
|
"collapse_len_mm": 3,
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFAULT_SETTINGS = {
|
2024-12-07 14:13:22 +00:00
|
|
|
# Ink/Stitch preferences
|
2023-10-21 16:16:34 +00:00
|
|
|
"cache_size": 100,
|
2024-04-27 18:16:18 +00:00
|
|
|
"pop_out_simulator": False,
|
2024-12-07 14:13:22 +00:00
|
|
|
# simulator
|
2025-01-09 16:06:07 +00:00
|
|
|
"simulator_adaptive_speed": True,
|
|
|
|
"simulator_speed": 16,
|
2024-06-14 07:49:57 +00:00
|
|
|
"simulator_line_width": 0.1,
|
2024-11-18 10:12:58 +00:00
|
|
|
"simulator_npp_size": 0.5,
|
2024-12-07 14:13:22 +00:00
|
|
|
"npp_button_status": False,
|
|
|
|
"jump_button_status": False,
|
|
|
|
"trim_button_status": False,
|
|
|
|
"stop_button_status": False,
|
|
|
|
"color_change_button_status": False,
|
|
|
|
"toggle_page_button_status": True,
|
2025-03-29 05:23:49 +00:00
|
|
|
"display_crosshair": True,
|
2024-12-07 14:13:22 +00:00
|
|
|
# apply palette
|
2025-01-29 17:04:07 +00:00
|
|
|
"last_applied_palette": "",
|
|
|
|
# sew stack editor
|
|
|
|
"stitch_layer_editor_sash_position": -200,
|
2025-02-11 17:52:01 +00:00
|
|
|
# lettering (all lettering applications)
|
|
|
|
"last_font": "Ink/Stitch Small Font",
|
2025-02-13 17:06:12 +00:00
|
|
|
# lettering
|
|
|
|
"lettering_align_text": 0,
|
|
|
|
"lettering_trim_option": 0,
|
|
|
|
"lettering_use_command_symbols": False,
|
2025-02-11 17:52:01 +00:00
|
|
|
# font sampling
|
|
|
|
"font_sampling_max_line_width": 180
|
2022-07-29 22:17:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class GlobalSettings(MutableMapping):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
fix: generate-inx-files failing with Nix
Fixes the following error when attempting to build the Nix package:
python bin/generate-inx-files;
Traceback (most recent call last):
File "/build/inkstitch/bin/generate-inx-files", line 59, in <module>
from lib.inx.generate import generate_inx_files
File "/build/inkstitch/lib/inx/generate.py", line 6, in <module>
from .extensions import generate_extension_inx_files
File "/build/inkstitch/lib/inx/extensions.py", line 10, in <module>
from ..commands import (COMMANDS, GLOBAL_COMMANDS, LAYER_COMMANDS,
File "/build/inkstitch/lib/commands.py", line 16, in <module>
from .i18n import N_, _
File "/build/inkstitch/lib/i18n.py", line 10, in <module>
from .utils import cache, get_resource_dir
File "/build/inkstitch/lib/utils/__init__.py", line 6, in <module>
from . import cache as cache_module # Slight hack to allow cache to be imported for monkeypatching
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/utils/cache.py", line 13, in <module>
from lib.utils.settings import global_settings
File "/build/inkstitch/lib/utils/settings.py", line 84, in <module>
global_settings = GlobalSettings()
^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/utils/settings.py", line 48, in __init__
self.__settings_file = os.path.join(get_user_dir(), "settings.json")
^^^^^^^^^^^^^^
File "/build/inkstitch/lib/utils/paths.py", line 50, in get_user_dir
os.makedirs(path)
File "<frozen os>", line 215, in makedirs
File "<frozen os>", line 215, in makedirs
File "<frozen os>", line 225, in makedirs
PermissionError: [Errno 13] Permission denied: '/homeless-shelter'
2025-06-22 16:59:29 +00:00
|
|
|
# We set create=False here because this code is executed on module load
|
|
|
|
# and via imports also runs on generate-inx-files, which with the Nix
|
|
|
|
# package manager is executed with a non-writable home directory.
|
|
|
|
user_dir = get_user_dir(create=False)
|
fix: tests during Nix build
Fixes the following error when executing the tests during a Nix build:
Traceback (most recent call last):
File "/build/inkstitch/lib/elements/element.py", line 714, in handle_unexpected_exceptions
yield
File "/build/inkstitch/lib/elements/element.py", line 650, in embroider
stitch_groups = self._load_cached_stitch_groups(previous_stitch, next_element)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/debug/debug.py", line 172, in decorated
result = func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/elements/element.py", line 541, in _load_cached_stitch_groups
stitch_groups = get_stitch_plan_cache().get(cache_key)
^^^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/utils/cache.py", line 31, in get_stitch_plan_cache
cache_dir = get_user_dir('cache')
^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/utils/paths.py", line 50, in get_user_dir
os.makedirs(path)
File "<frozen os>", line 215, in makedirs
File "<frozen os>", line 215, in makedirs
File "<frozen os>", line 225, in makedirs
PermissionError: [Errno 13] Permission denied: '/homeless-shelter'
2025-06-22 16:31:46 +00:00
|
|
|
self._settings_file = os.path.join(user_dir, "settings.json")
|
|
|
|
self._settings = {}
|
2022-07-29 22:17:50 +00:00
|
|
|
|
|
|
|
for name, value in DEFAULT_METADATA.items():
|
fix: tests during Nix build
Fixes the following error when executing the tests during a Nix build:
Traceback (most recent call last):
File "/build/inkstitch/lib/elements/element.py", line 714, in handle_unexpected_exceptions
yield
File "/build/inkstitch/lib/elements/element.py", line 650, in embroider
stitch_groups = self._load_cached_stitch_groups(previous_stitch, next_element)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/debug/debug.py", line 172, in decorated
result = func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/elements/element.py", line 541, in _load_cached_stitch_groups
stitch_groups = get_stitch_plan_cache().get(cache_key)
^^^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/utils/cache.py", line 31, in get_stitch_plan_cache
cache_dir = get_user_dir('cache')
^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/utils/paths.py", line 50, in get_user_dir
os.makedirs(path)
File "<frozen os>", line 215, in makedirs
File "<frozen os>", line 215, in makedirs
File "<frozen os>", line 225, in makedirs
PermissionError: [Errno 13] Permission denied: '/homeless-shelter'
2025-06-22 16:31:46 +00:00
|
|
|
self._settings[f"default_{name}"] = value
|
2022-07-29 22:17:50 +00:00
|
|
|
|
fix: tests during Nix build
Fixes the following error when executing the tests during a Nix build:
Traceback (most recent call last):
File "/build/inkstitch/lib/elements/element.py", line 714, in handle_unexpected_exceptions
yield
File "/build/inkstitch/lib/elements/element.py", line 650, in embroider
stitch_groups = self._load_cached_stitch_groups(previous_stitch, next_element)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/debug/debug.py", line 172, in decorated
result = func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/elements/element.py", line 541, in _load_cached_stitch_groups
stitch_groups = get_stitch_plan_cache().get(cache_key)
^^^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/utils/cache.py", line 31, in get_stitch_plan_cache
cache_dir = get_user_dir('cache')
^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/utils/paths.py", line 50, in get_user_dir
os.makedirs(path)
File "<frozen os>", line 215, in makedirs
File "<frozen os>", line 215, in makedirs
File "<frozen os>", line 225, in makedirs
PermissionError: [Errno 13] Permission denied: '/homeless-shelter'
2025-06-22 16:31:46 +00:00
|
|
|
self._settings.update(DEFAULT_SETTINGS)
|
2022-07-29 22:17:50 +00:00
|
|
|
|
|
|
|
try:
|
fix: tests during Nix build
Fixes the following error when executing the tests during a Nix build:
Traceback (most recent call last):
File "/build/inkstitch/lib/elements/element.py", line 714, in handle_unexpected_exceptions
yield
File "/build/inkstitch/lib/elements/element.py", line 650, in embroider
stitch_groups = self._load_cached_stitch_groups(previous_stitch, next_element)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/debug/debug.py", line 172, in decorated
result = func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/elements/element.py", line 541, in _load_cached_stitch_groups
stitch_groups = get_stitch_plan_cache().get(cache_key)
^^^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/utils/cache.py", line 31, in get_stitch_plan_cache
cache_dir = get_user_dir('cache')
^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/utils/paths.py", line 50, in get_user_dir
os.makedirs(path)
File "<frozen os>", line 215, in makedirs
File "<frozen os>", line 215, in makedirs
File "<frozen os>", line 225, in makedirs
PermissionError: [Errno 13] Permission denied: '/homeless-shelter'
2025-06-22 16:31:46 +00:00
|
|
|
with open(self._settings_file, 'r') as settings_file:
|
|
|
|
self._settings.update(json.load(settings_file))
|
2022-07-29 22:17:50 +00:00
|
|
|
except (OSError, json.JSONDecodeError, ValueError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __setitem__(self, item, value):
|
fix: tests during Nix build
Fixes the following error when executing the tests during a Nix build:
Traceback (most recent call last):
File "/build/inkstitch/lib/elements/element.py", line 714, in handle_unexpected_exceptions
yield
File "/build/inkstitch/lib/elements/element.py", line 650, in embroider
stitch_groups = self._load_cached_stitch_groups(previous_stitch, next_element)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/debug/debug.py", line 172, in decorated
result = func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/elements/element.py", line 541, in _load_cached_stitch_groups
stitch_groups = get_stitch_plan_cache().get(cache_key)
^^^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/utils/cache.py", line 31, in get_stitch_plan_cache
cache_dir = get_user_dir('cache')
^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/utils/paths.py", line 50, in get_user_dir
os.makedirs(path)
File "<frozen os>", line 215, in makedirs
File "<frozen os>", line 215, in makedirs
File "<frozen os>", line 225, in makedirs
PermissionError: [Errno 13] Permission denied: '/homeless-shelter'
2025-06-22 16:31:46 +00:00
|
|
|
self._settings[item] = value
|
2022-07-29 22:17:50 +00:00
|
|
|
|
fix: tests during Nix build
Fixes the following error when executing the tests during a Nix build:
Traceback (most recent call last):
File "/build/inkstitch/lib/elements/element.py", line 714, in handle_unexpected_exceptions
yield
File "/build/inkstitch/lib/elements/element.py", line 650, in embroider
stitch_groups = self._load_cached_stitch_groups(previous_stitch, next_element)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/debug/debug.py", line 172, in decorated
result = func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/elements/element.py", line 541, in _load_cached_stitch_groups
stitch_groups = get_stitch_plan_cache().get(cache_key)
^^^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/utils/cache.py", line 31, in get_stitch_plan_cache
cache_dir = get_user_dir('cache')
^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/utils/paths.py", line 50, in get_user_dir
os.makedirs(path)
File "<frozen os>", line 215, in makedirs
File "<frozen os>", line 215, in makedirs
File "<frozen os>", line 225, in makedirs
PermissionError: [Errno 13] Permission denied: '/homeless-shelter'
2025-06-22 16:31:46 +00:00
|
|
|
with open(self._settings_file, 'w') as settings_file:
|
|
|
|
json.dump(self._settings, settings_file)
|
2022-07-29 22:17:50 +00:00
|
|
|
|
|
|
|
def __getitem__(self, item):
|
fix: tests during Nix build
Fixes the following error when executing the tests during a Nix build:
Traceback (most recent call last):
File "/build/inkstitch/lib/elements/element.py", line 714, in handle_unexpected_exceptions
yield
File "/build/inkstitch/lib/elements/element.py", line 650, in embroider
stitch_groups = self._load_cached_stitch_groups(previous_stitch, next_element)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/debug/debug.py", line 172, in decorated
result = func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/elements/element.py", line 541, in _load_cached_stitch_groups
stitch_groups = get_stitch_plan_cache().get(cache_key)
^^^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/utils/cache.py", line 31, in get_stitch_plan_cache
cache_dir = get_user_dir('cache')
^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/utils/paths.py", line 50, in get_user_dir
os.makedirs(path)
File "<frozen os>", line 215, in makedirs
File "<frozen os>", line 215, in makedirs
File "<frozen os>", line 225, in makedirs
PermissionError: [Errno 13] Permission denied: '/homeless-shelter'
2025-06-22 16:31:46 +00:00
|
|
|
return self._settings[item]
|
2022-07-29 22:17:50 +00:00
|
|
|
|
|
|
|
def __delitem__(self, item):
|
fix: tests during Nix build
Fixes the following error when executing the tests during a Nix build:
Traceback (most recent call last):
File "/build/inkstitch/lib/elements/element.py", line 714, in handle_unexpected_exceptions
yield
File "/build/inkstitch/lib/elements/element.py", line 650, in embroider
stitch_groups = self._load_cached_stitch_groups(previous_stitch, next_element)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/debug/debug.py", line 172, in decorated
result = func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/elements/element.py", line 541, in _load_cached_stitch_groups
stitch_groups = get_stitch_plan_cache().get(cache_key)
^^^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/utils/cache.py", line 31, in get_stitch_plan_cache
cache_dir = get_user_dir('cache')
^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/utils/paths.py", line 50, in get_user_dir
os.makedirs(path)
File "<frozen os>", line 215, in makedirs
File "<frozen os>", line 215, in makedirs
File "<frozen os>", line 225, in makedirs
PermissionError: [Errno 13] Permission denied: '/homeless-shelter'
2025-06-22 16:31:46 +00:00
|
|
|
del self._settings[item]
|
2022-07-29 22:17:50 +00:00
|
|
|
|
|
|
|
def __iter__(self):
|
fix: tests during Nix build
Fixes the following error when executing the tests during a Nix build:
Traceback (most recent call last):
File "/build/inkstitch/lib/elements/element.py", line 714, in handle_unexpected_exceptions
yield
File "/build/inkstitch/lib/elements/element.py", line 650, in embroider
stitch_groups = self._load_cached_stitch_groups(previous_stitch, next_element)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/debug/debug.py", line 172, in decorated
result = func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/elements/element.py", line 541, in _load_cached_stitch_groups
stitch_groups = get_stitch_plan_cache().get(cache_key)
^^^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/utils/cache.py", line 31, in get_stitch_plan_cache
cache_dir = get_user_dir('cache')
^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/utils/paths.py", line 50, in get_user_dir
os.makedirs(path)
File "<frozen os>", line 215, in makedirs
File "<frozen os>", line 215, in makedirs
File "<frozen os>", line 225, in makedirs
PermissionError: [Errno 13] Permission denied: '/homeless-shelter'
2025-06-22 16:31:46 +00:00
|
|
|
return iter(self._settings)
|
2022-07-29 22:17:50 +00:00
|
|
|
|
|
|
|
def __len__(self):
|
fix: tests during Nix build
Fixes the following error when executing the tests during a Nix build:
Traceback (most recent call last):
File "/build/inkstitch/lib/elements/element.py", line 714, in handle_unexpected_exceptions
yield
File "/build/inkstitch/lib/elements/element.py", line 650, in embroider
stitch_groups = self._load_cached_stitch_groups(previous_stitch, next_element)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/debug/debug.py", line 172, in decorated
result = func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/elements/element.py", line 541, in _load_cached_stitch_groups
stitch_groups = get_stitch_plan_cache().get(cache_key)
^^^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/utils/cache.py", line 31, in get_stitch_plan_cache
cache_dir = get_user_dir('cache')
^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/utils/paths.py", line 50, in get_user_dir
os.makedirs(path)
File "<frozen os>", line 215, in makedirs
File "<frozen os>", line 215, in makedirs
File "<frozen os>", line 225, in makedirs
PermissionError: [Errno 13] Permission denied: '/homeless-shelter'
2025-06-22 16:31:46 +00:00
|
|
|
return len(self._settings)
|
2022-07-29 22:17:50 +00:00
|
|
|
|
|
|
|
def __json__(self):
|
fix: tests during Nix build
Fixes the following error when executing the tests during a Nix build:
Traceback (most recent call last):
File "/build/inkstitch/lib/elements/element.py", line 714, in handle_unexpected_exceptions
yield
File "/build/inkstitch/lib/elements/element.py", line 650, in embroider
stitch_groups = self._load_cached_stitch_groups(previous_stitch, next_element)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/debug/debug.py", line 172, in decorated
result = func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/elements/element.py", line 541, in _load_cached_stitch_groups
stitch_groups = get_stitch_plan_cache().get(cache_key)
^^^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/utils/cache.py", line 31, in get_stitch_plan_cache
cache_dir = get_user_dir('cache')
^^^^^^^^^^^^^^^^^^^^^
File "/build/inkstitch/lib/utils/paths.py", line 50, in get_user_dir
os.makedirs(path)
File "<frozen os>", line 215, in makedirs
File "<frozen os>", line 215, in makedirs
File "<frozen os>", line 225, in makedirs
PermissionError: [Errno 13] Permission denied: '/homeless-shelter'
2025-06-22 16:31:46 +00:00
|
|
|
return self._settings
|
2022-07-29 22:17:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
global_settings = GlobalSettings()
|