summaryrefslogtreecommitdiff
path: root/lib/utils
diff options
context:
space:
mode:
authorLex Neva <lexelby@users.noreply.github.com>2023-02-20 15:27:15 -0500
committerGitHub <noreply@github.com>2023-02-20 15:27:15 -0500
commit8b98083ac723e4145a7c41483f7dda10f722566f (patch)
tree9a058b6aa3c907d3da12d3efdfbc115ef1b4eff4 /lib/utils
parent43ec2db4516545744051d5762728f287cc19acf6 (diff)
parentaa65a2bf3fb747dc89e2d905f1fc45b269b5cab4 (diff)
Merge pull request #1732 from inkstitch/lexelby/cache-stitch-plan
stitch plan caching
Diffstat (limited to 'lib/utils')
-rw-r--r--lib/utils/cache.py63
-rwxr-xr-xlib/utils/paths.py11
-rw-r--r--lib/utils/settings.py58
3 files changed, 130 insertions, 2 deletions
diff --git a/lib/utils/cache.py b/lib/utils/cache.py
index c0313ebe..1ede8ce1 100644
--- a/lib/utils/cache.py
+++ b/lib/utils/cache.py
@@ -2,14 +2,73 @@
#
# Copyright (c) 2010 Authors
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
+import os
+import atexit
+import hashlib
+import pickle
+
+import appdirs
+import diskcache
+
+from lib.utils.settings import global_settings
try:
from functools import lru_cache
except ImportError:
from backports.functools_lru_cache import lru_cache
-# simplify use of lru_cache decorator
-
+# simplify use of lru_cache decorator
def cache(*args, **kwargs):
return lru_cache(maxsize=None)(*args, **kwargs)
+
+
+__stitch_plan_cache = None
+
+
+def get_stitch_plan_cache():
+ global __stitch_plan_cache
+
+ if __stitch_plan_cache is None:
+ cache_dir = os.path.join(appdirs.user_config_dir('inkstitch'), 'cache', 'stitch_plan')
+ 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
+
+
+class CacheKeyGenerator(object):
+ """Generate cache keys given arbitrary data.
+
+ Given arbitrary data, generate short cache key that is extremely likely
+ to be unique.
+
+ Use example:
+
+ >>> generator = CacheKeyGenerator()
+ >>> generator.update(b'12345')
+ >>> generator.update([1, 2, 3, {4, 5, 6}])
+ >>> generator.get_cache_key()
+ """
+
+ def __init__(self):
+ # SHA1 is chosen for speed. We don't need cryptography-grade hashing
+ # for this use case.
+ self._hasher = hashlib.sha1()
+
+ def update(self, data):
+ """Provide data to be hashed into a cache key.
+
+ Arguments:
+ data -- a bytes object or any object that can be pickled
+ """
+
+ if not isinstance(data, bytes):
+ data = pickle.dumps(data)
+
+ self._hasher.update(data)
+
+ def get_cache_key(self):
+ return self._hasher.hexdigest()
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()