PersistentObjects 0.1.2__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.
@@ -0,0 +1,3 @@
1
+ from .core import PersistentObject
2
+
3
+ __all__ = ["PersistentObject"]
@@ -0,0 +1,88 @@
1
+ import json, os, threading
2
+
3
+ class _PersistentState:
4
+ _instances = {}
5
+
6
+ def __new__(cls, path):
7
+ path = os.path.abspath(path)
8
+
9
+ if path in cls._instances:
10
+ return cls._instances[path]
11
+
12
+ instance = super().__new__(cls)
13
+ cls._instances[path] = instance
14
+ return instance
15
+
16
+ def __init__(self, path):
17
+ # Prevent re-initialization when __new__ returns an existing instance
18
+ if hasattr(self, "_initialized"):
19
+ return
20
+
21
+ self._initialized = True
22
+ self._path = os.path.abspath(path)
23
+ self._lock = threading.Lock()
24
+ self._data = {}
25
+ self._load()
26
+
27
+ def _load(self):
28
+ if os.path.exists(self._path):
29
+ try:
30
+ with open(self._path, "r", encoding="utf-8") as f:
31
+ self._data = json.load(f)
32
+ except Exception:
33
+ self._data = {}
34
+
35
+ def _save(self):
36
+ tmp = self._path + ".tmp"
37
+ with open(tmp, "w", encoding="utf-8") as f:
38
+ json.dump(self._data, f, indent=2)
39
+ os.replace(tmp, self._path)
40
+
41
+ def __getattr__(self, name):
42
+ if name.startswith("_"):
43
+ raise AttributeError(name)
44
+ try:
45
+ return self._data[name]
46
+ except KeyError:
47
+ raise AttributeError(name)
48
+
49
+ def __setattr__(self, name, value):
50
+ if name.startswith("_"):
51
+ super().__setattr__(name, value)
52
+ return
53
+
54
+ with self._lock:
55
+ self._data[name] = value
56
+ self._save()
57
+
58
+
59
+ class _Namespace:
60
+ def __init__(self, root, key):
61
+ self._root = root
62
+ self._key = key
63
+
64
+ def __getattr__(self, name):
65
+ try:
66
+ return self._root._data[self._key][name]
67
+ except KeyError:
68
+ raise AttributeError(name)
69
+
70
+ def __setattr__(self, name, value):
71
+ if name.startswith("_"):
72
+ super().__setattr__(name, value)
73
+ return
74
+
75
+ with self._root._lock:
76
+ bucket = self._root._data.setdefault(self._key, {})
77
+ bucket[name] = value
78
+ self._root._save()
79
+
80
+
81
+ class PersistentObject(_PersistentState):
82
+ def namespace(self, name) -> _Namespace:
83
+ return _Namespace(self, name)
84
+
85
+
86
+
87
+
88
+ __all__ = ["PersistentObject"]
@@ -0,0 +1,36 @@
1
+ Metadata-Version: 2.4
2
+ Name: PersistentObjects
3
+ Version: 0.1.2
4
+ Summary: JSON-backed attribute-persistent object with namespace support
5
+ Author: Tornado300
6
+ License-Expression: MIT
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: Operating System :: OS Independent
9
+ Requires-Python: >=3.10
10
+ Description-Content-Type: text/markdown
11
+ License-File: LICENSE
12
+ Dynamic: license-file
13
+
14
+ This is a very simple lib that provides the PersistantObject class, which saves all its attributes to the given json file.
15
+ Attributes are saved as soon as they are set.
16
+
17
+ [!IMPORTANT]
18
+ > morphing actions like appending, inserting, extending, etc. will not automaticly save!
19
+ > so its recomended to create a temp var, then append to it and at the end set the PersistantObject attr to the temp var.
20
+
21
+ with namespace(NAME) seperate sub namespaces can be created inside the object.
22
+
23
+
24
+ ## Usage example:
25
+ ```python
26
+
27
+ from PersistantObject import PersistantObject
28
+
29
+ pobject = PersistantObject("save.json")
30
+
31
+ pobject.first_value = 10
32
+ pobject.second_value = "foo"
33
+
34
+ namespace = pobject.namespace("test_namespace")
35
+ namespace.third_value = [10, 5]
36
+ ```
@@ -0,0 +1,7 @@
1
+ PersistentObjects/__init__.py,sha256=sEfMxXx8Nlt-a0Hf31vMLiijXHJKo8JHMd_Y940GW1c,67
2
+ PersistentObjects/__main__.py,sha256=xVf9ZsWxFMurLiP6SGgu6HvOcIit_qEq9UybEb22kF0,2267
3
+ persistentobjects-0.1.2.dist-info/licenses/LICENSE,sha256=7EI8xVBu6h_7_JlVw-yPhhOZlpY9hP8wal7kHtqKT_E,1074
4
+ persistentobjects-0.1.2.dist-info/METADATA,sha256=Ud_t9p6hoy5yvVbU__-Vi2KjOPqeSPK3yTV0TIbnVtQ,1113
5
+ persistentobjects-0.1.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
6
+ persistentobjects-0.1.2.dist-info/top_level.txt,sha256=m_IeuK6UA0oDyLLbqyJqZdYfQc2ZTcb4IP9BlUaVymI,18
7
+ persistentobjects-0.1.2.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.9.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2018 The Python Packaging Authority
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ PersistentObjects