diff options
| author | Lex Neva <github.com@lexneva.name> | 2022-07-29 18:17:50 -0400 |
|---|---|---|
| committer | Lex Neva <github.com@lexneva.name> | 2023-02-18 22:34:47 -0500 |
| commit | 98bc2e2ff9c843a64c3db355290ed541e6708312 (patch) | |
| tree | 593edf118780f9902437a306323595c6846acb24 /lib | |
| parent | a0834e2e7c2f2b6734877a8ee922c01e8c9330ba (diff) | |
add preferences UI including cache settings
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/api/preferences.py | 41 | ||||
| -rw-r--r-- | lib/api/server.py | 2 | ||||
| -rw-r--r-- | lib/extensions/__init__.py | 4 | ||||
| -rw-r--r-- | lib/extensions/base.py | 9 | ||||
| -rw-r--r-- | lib/extensions/preferences.py (renamed from lib/extensions/embroider_settings.py) | 18 | ||||
| -rw-r--r-- | lib/utils/cache.py | 6 | ||||
| -rwxr-xr-x | lib/utils/paths.py | 11 | ||||
| -rw-r--r-- | lib/utils/settings.py | 58 |
8 files changed, 142 insertions, 7 deletions
diff --git a/lib/api/preferences.py b/lib/api/preferences.py new file mode 100644 index 00000000..bc8328b8 --- /dev/null +++ b/lib/api/preferences.py @@ -0,0 +1,41 @@ +# Authors: see git history +# +# Copyright (c) 2010 Authors +# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. + +from flask import Blueprint, g, jsonify, request + +from ..utils.cache import get_stitch_plan_cache +from ..utils.settings import global_settings + +preferences = Blueprint('preferences', __name__) + + +@preferences.route('/', methods=["POST"]) +def update_preferences(): + metadata = g.extension.get_inkstitch_metadata() + metadata.update(request.json['this_svg_settings']) + global_settings.update(request.json['global_settings']) + + # cache size may have changed + stitch_plan_cache = get_stitch_plan_cache() + stitch_plan_cache.size_limit = global_settings['cache_size'] * 1024 * 1024 + stitch_plan_cache.cull() + + return jsonify({"status": "success"}) + + +@preferences.route('/', methods=["GET"]) +def get_preferences(): + metadata = g.extension.get_inkstitch_metadata() + return jsonify({"status": "success", + "this_svg_settings": metadata, + "global_settings": global_settings + }) + + +@preferences.route('/clear_cache', methods=["POST"]) +def clear_cache(): + stitch_plan_cache = get_stitch_plan_cache() + stitch_plan_cache.clear(retry=True) + return jsonify({"status": "success"}) diff --git a/lib/api/server.py b/lib/api/server.py index 93b08ff1..626e412e 100644 --- a/lib/api/server.py +++ b/lib/api/server.py @@ -18,6 +18,7 @@ from ..utils.json import InkStitchJSONEncoder from .install import install from .simulator import simulator from .stitch_plan import stitch_plan +from .preferences import preferences class APIServer(Thread): @@ -45,6 +46,7 @@ class APIServer(Thread): self.app.register_blueprint(simulator, url_prefix="/simulator") self.app.register_blueprint(stitch_plan, url_prefix="/stitch_plan") self.app.register_blueprint(install, url_prefix="/install") + self.app.register_blueprint(preferences, url_prefix="/preferences") @self.app.before_request def store_extension(): diff --git a/lib/extensions/__init__.py b/lib/extensions/__init__.py index 6f42a349..f9f6072b 100644 --- a/lib/extensions/__init__.py +++ b/lib/extensions/__init__.py @@ -17,7 +17,7 @@ from .cut_satin import CutSatin from .cutwork_segmentation import CutworkSegmentation from .density_map import DensityMap from .duplicate_params import DuplicateParams -from .embroider_settings import EmbroiderSettings +from .preferences import Preferences from .fill_to_stroke import FillToStroke from .flip import Flip from .generate_palette import GeneratePalette @@ -96,5 +96,5 @@ __all__ = extensions = [StitchPlanPreview, Simulator, Reorder, DuplicateParams, - EmbroiderSettings, + Preferences, CutworkSegmentation] diff --git a/lib/extensions/base.py b/lib/extensions/base.py index c2f76b27..7b3c6f1c 100644 --- a/lib/extensions/base.py +++ b/lib/extensions/base.py @@ -23,6 +23,7 @@ from ..svg import generate_unique_id from ..svg.tags import (CONNECTOR_TYPE, EMBROIDERABLE_TAGS, INKSCAPE_GROUPMODE, NOT_EMBROIDERABLE_TAGS, SVG_CLIPPATH_TAG, SVG_DEFS_TAG, SVG_GROUP_TAG, SVG_MASK_TAG) +from ..utils.settings import DEFAULT_METADATA, global_settings SVG_METADATA_TAG = inkex.addNS("metadata", "svg") @@ -52,9 +53,14 @@ class InkStitchMetadata(MutableMapping): """ def __init__(self, document): + super().__init__() self.document = document self.metadata = document.metadata + for setting in DEFAULT_METADATA: + if self[setting] is None: + self[setting] = global_settings[f'default_{setting}'] + # Because this class inherints from MutableMapping, all we have to do is # implement these five methods and we get a full dict-like interface. @@ -96,6 +102,9 @@ class InkStitchMetadata(MutableMapping): return i + 1 + def __json__(self): + return dict(self) + class InkstitchExtension(inkex.Effect): """Base class for Inkstitch extensions. Not intended for direct use.""" diff --git a/lib/extensions/embroider_settings.py b/lib/extensions/preferences.py index cdf18991..8a06f829 100644 --- a/lib/extensions/embroider_settings.py +++ b/lib/extensions/preferences.py @@ -4,12 +4,15 @@ # Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. from .base import InkstitchExtension +from ..api import APIServer +from ..gui import open_url -class EmbroiderSettings(InkstitchExtension): +class Preferences(InkstitchExtension): ''' This saves embroider settings into the metadata of the file ''' + def __init__(self, *args, **kwargs): InkstitchExtension.__init__(self, *args, **kwargs) self.arg_parser.add_argument("-c", "--collapse_len_mm", @@ -22,6 +25,13 @@ class EmbroiderSettings(InkstitchExtension): help="minimum stitch length (mm)") def effect(self): - self.metadata = self.get_inkstitch_metadata() - self.metadata['collapse_len_mm'] = self.options.collapse_length_mm - self.metadata['min_stitch_len_mm'] = self.options.min_stitch_len_mm + api_server = APIServer(self) + port = api_server.start_server() + electron = open_url("/preferences?port=%d" % port) + electron.wait() + api_server.stop() + api_server.join() + + # self.metadata = self.get_inkstitch_metadata() + # self.metadata['collapse_len_mm'] = self.options.collapse_length_mm + # self.metadata['min_stitch_len_mm'] = self.options.min_stitch_len_mm diff --git a/lib/utils/cache.py b/lib/utils/cache.py index 767978ca..1ede8ce1 100644 --- a/lib/utils/cache.py +++ b/lib/utils/cache.py @@ -10,6 +10,8 @@ import pickle import appdirs import diskcache +from lib.utils.settings import global_settings + try: from functools import lru_cache except ImportError: @@ -29,7 +31,9 @@ def get_stitch_plan_cache(): if __stitch_plan_cache is None: cache_dir = os.path.join(appdirs.user_config_dir('inkstitch'), 'cache', 'stitch_plan') - __stitch_plan_cache = diskcache.Cache(cache_dir, size=1024 * 1024 * 100) + size_limit = global_settings['cache_size'] * 1024 * 1024 + __stitch_plan_cache = diskcache.Cache(cache_dir, size=size_limit) + __stitch_plan_cache.size_limit = size_limit atexit.register(__stitch_plan_cache.close) return __stitch_plan_cache diff --git a/lib/utils/paths.py b/lib/utils/paths.py index 2a95f6e7..10d72de9 100755 --- a/lib/utils/paths.py +++ b/lib/utils/paths.py @@ -7,6 +7,8 @@ import sys import os from os.path import dirname, realpath +import appdirs + def get_bundled_dir(name): if getattr(sys, 'frozen', None) is not None: @@ -26,3 +28,12 @@ def get_resource_dir(name): return realpath(os.path.join(sys._MEIPASS, name)) else: return realpath(os.path.join(dirname(realpath(__file__)), '..', '..', name)) + + +def get_user_dir(name=None): + path = appdirs.user_config_dir("inkstitch") + + if name is not None: + path = os.path.join(path, name) + + return path diff --git a/lib/utils/settings.py b/lib/utils/settings.py new file mode 100644 index 00000000..f2ce276d --- /dev/null +++ b/lib/utils/settings.py @@ -0,0 +1,58 @@ +from collections.abc import MutableMapping +import json +import os + +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 = { + "min_stitch_len_mm": 0, + "collapse_len_mm": 3, +} + +DEFAULT_SETTINGS = { + "cache_size": 100 +} + + +class GlobalSettings(MutableMapping): + def __init__(self): + super().__init__() + self.__settings_file = os.path.join(get_user_dir(), "settings.json") + self.__settings = {} + + for name, value in DEFAULT_METADATA.items(): + self.__settings[f"default_{name}"] = value + + self.__settings.update(DEFAULT_SETTINGS) + + try: + with open(self.__settings_file, 'r') as settings_file: + self.__settings.update(json.load(settings_file)) + except (OSError, json.JSONDecodeError, ValueError): + pass + + def __setitem__(self, item, value): + self.__settings[item] = value + + with open(self.__settings_file, 'w') as settings_file: + json.dump(self.__settings, settings_file) + + def __getitem__(self, item): + return self.__settings[item] + + def __delitem__(self, item): + del self.__settings[item] + + def __iter__(self): + return iter(self.__settings) + + def __len__(self): + return len(self.__settings) + + def __json__(self): + return self.__settings + + +global_settings = GlobalSettings() |
