bear-utils 0.8.13__py3-none-any.whl → 0.8.14__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,33 +3,44 @@
3
3
  import atexit
4
4
  from collections.abc import Generator
5
5
  from contextlib import contextmanager
6
+ import hashlib
6
7
  from pathlib import Path
7
8
  from typing import Any, Self
8
9
 
9
10
  from tinydb import Query, TinyDB
10
11
 
12
+ DEFAULT_PATH: Path = Path.home() / ".config" / "bear_utils"
11
13
 
12
- def get_config_folder() -> Path:
14
+
15
+ def get_config_folder(path: str | Path | None = None) -> Path:
13
16
  """Get the path to the bear configuration directory."""
14
- config_path: Path = Path.home() / ".bear_utils"
17
+ config_path: Path = Path(path) if isinstance(path, str) else path or DEFAULT_PATH
15
18
  config_path.mkdir(parents=True, exist_ok=True)
16
19
  return config_path
17
20
 
18
21
 
22
+ def get_file_hash(file_path: Path) -> str:
23
+ """Return the blake2 hash of the file at the given path."""
24
+ hasher = hashlib.blake2b()
25
+ with file_path.open("rb") as file:
26
+ while chunk := file.read(8192):
27
+ hasher.update(chunk)
28
+ return hasher.hexdigest()
29
+
30
+
19
31
  class SettingsManager:
20
32
  """A class to manage settings using TinyDB and an in-memory cache."""
21
33
 
22
- __slots__ = ("cache", "db", "file_path", "settings_name")
34
+ __slots__ = ("cache", "db", "file_hash", "file_path", "settings_name")
23
35
 
24
36
  def __init__(self, settings_name: str, folder_path: str | Path | None = None) -> None:
25
37
  """Initialize the SettingsManager with a specific settings name."""
26
38
  self.settings_name: str = settings_name
27
39
  self.cache: dict[str, Any] = {}
28
40
  file_name: str = f"{settings_name}.json"
29
- self.file_path: Path = Path(folder_path) / file_name if folder_path else get_config_folder() / file_name
30
-
41
+ self.file_path: Path = get_config_folder(folder_path) / file_name
31
42
  self.db: TinyDB = TinyDB(self.file_path, indent=4, ensure_ascii=False)
32
-
43
+ self.file_hash: str = get_file_hash(self.file_path) if self.file_path.exists() else ""
33
44
  atexit.register(self.close)
34
45
  self._load_cache()
35
46
 
@@ -48,8 +59,19 @@ class SettingsManager:
48
59
  return
49
60
  self.set(key=key, value=value)
50
61
 
62
+ def invalidate_cache(self) -> None:
63
+ """Invalidate the in-memory cache."""
64
+ self.cache.clear()
65
+ self._load_cache()
66
+
51
67
  def get(self, key: str, default: Any = None) -> Any:
52
68
  """Get a setting value."""
69
+ file_hash = get_file_hash(self.file_path)
70
+
71
+ if file_hash != self.file_hash:
72
+ self.invalidate_cache()
73
+ self.file_hash = file_hash
74
+
53
75
  if key in self.cache:
54
76
  return self.cache[key]
55
77
  if result := self.db.search(Query().key == key):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: bear-utils
3
- Version: 0.8.13
3
+ Version: 0.8.14
4
4
  Summary: Various utilities for Bear programmers, including a rich logging utility, a disk cache, and a SQLite database wrapper amongst other things.
5
5
  Author-email: chaz <bright.lid5647@fastmail.com>
6
6
  Requires-Python: >=3.12
@@ -22,7 +22,7 @@ Requires-Dist: toml>=0.10.2
22
22
  Requires-Dist: uvicorn>=0.35.0
23
23
  Description-Content-Type: text/markdown
24
24
 
25
- # Bear Utils v# Bear Utils v0.8.13
25
+ # Bear Utils v# Bear Utils v0.8.14
26
26
 
27
27
  Personal set of tools and utilities for Python projects, focusing on modularity and ease of use. This library includes components for caching, database management, logging, time handling, file operations, CLI prompts, image processing, clipboard interaction, gradient utilities, event systems, and async helpers.
28
28
 
@@ -20,7 +20,7 @@ bear_utils/cli/shell/_common.py,sha256=_KQyL5lvqOfjonFIwlEOyp3K9G3TSOj19RhgVzfNN
20
20
  bear_utils/config/__init__.py,sha256=HC_lWpmLF0kbPr5i1Wa2FLER2b446E_GecgU9EPmc04,353
21
21
  bear_utils/config/config_manager.py,sha256=Xj0xOmY-wo_rwfcWiXyxNZWX9NknX_Jm9W56Gx8yyHQ,8244
22
22
  bear_utils/config/dir_manager.py,sha256=slIy1oRr7VIPdsiwN66-xQiuSvgqm_j6d1IrKhxRsSk,2028
23
- bear_utils/config/settings_manager.py,sha256=6YaCSHYA0k6VPaLSrXJ_yjZ-lOPklMgQC73K20Z8yis,5181
23
+ bear_utils/config/settings_manager.py,sha256=0hvqLzSiA8f9UVN45UY2-SIHMmOsg1eDctbg293qyyU,5978
24
24
  bear_utils/constants/__init__.py,sha256=vmr1_grp658VvYi1Pk_5Q7_Qab0fUIbWxJ5BrsAuEFU,939
25
25
  bear_utils/constants/_exceptions.py,sha256=gnAGTmuD9NYpJakeLrYHAyPrAQPHDNahY_rS42Ct39k,251
26
26
  bear_utils/constants/_lazy_typing.py,sha256=WfuWpRqx9XchvuyPWg3tVjMC5-C4QA-Bhwfskf4YmAE,339
@@ -86,6 +86,6 @@ bear_utils/monitoring/__init__.py,sha256=9DKNIWTp_voLnaWgiP-wJ-o_N0hYixo-MzjUmg8
86
86
  bear_utils/monitoring/_common.py,sha256=LYQFxgTP9fk0cH71IQTuGwBYYPWCqHP_mMRNecoD76M,657
87
87
  bear_utils/monitoring/host_monitor.py,sha256=e0TYRJw9iDj5Ga6y3ck1TBFEeH42Cax5mQYaNU8yams,13241
88
88
  bear_utils/time/__init__.py,sha256=VctjJG17SyEHAFXytI1sZrOrq7zm3hVenIDOJFdaMN0,1424
89
- bear_utils-0.8.13.dist-info/METADATA,sha256=Pi8ZiQ0ovpAROUIa_Lece_5-ggSkfRYlKMq8m2H24Qg,8699
90
- bear_utils-0.8.13.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
91
- bear_utils-0.8.13.dist-info/RECORD,,
89
+ bear_utils-0.8.14.dist-info/METADATA,sha256=YpwGNeJKju3r-1LtWE15HMHG6P_NFcY8_sqiiEiZy3o,8699
90
+ bear_utils-0.8.14.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
91
+ bear_utils-0.8.14.dist-info/RECORD,,