json-database 0.8.1__tar.gz → 0.9.0__tar.gz
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.
- {json_database-0.8.1 → json_database-0.9.0}/PKG-INFO +1 -1
- json_database-0.9.0/json_database/hpm.py +86 -0
- {json_database-0.8.1 → json_database-0.9.0}/json_database/version.py +2 -2
- {json_database-0.8.1 → json_database-0.9.0}/json_database.egg-info/PKG-INFO +1 -1
- {json_database-0.8.1 → json_database-0.9.0}/json_database.egg-info/SOURCES.txt +1 -0
- {json_database-0.8.1 → json_database-0.9.0}/LICENSE +0 -0
- {json_database-0.8.1 → json_database-0.9.0}/README.md +0 -0
- {json_database-0.8.1 → json_database-0.9.0}/json_database/__init__.py +0 -0
- {json_database-0.8.1 → json_database-0.9.0}/json_database/crypto.py +0 -0
- {json_database-0.8.1 → json_database-0.9.0}/json_database/exceptions.py +0 -0
- {json_database-0.8.1 → json_database-0.9.0}/json_database/search.py +0 -0
- {json_database-0.8.1 → json_database-0.9.0}/json_database/utils.py +0 -0
- {json_database-0.8.1 → json_database-0.9.0}/json_database/xdg_utils.py +0 -0
- {json_database-0.8.1 → json_database-0.9.0}/json_database.egg-info/dependency_links.txt +0 -0
- {json_database-0.8.1 → json_database-0.9.0}/json_database.egg-info/requires.txt +0 -0
- {json_database-0.8.1 → json_database-0.9.0}/json_database.egg-info/top_level.txt +0 -0
- {json_database-0.8.1 → json_database-0.9.0}/setup.cfg +0 -0
- {json_database-0.8.1 → json_database-0.9.0}/setup.py +0 -0
- {json_database-0.8.1 → json_database-0.9.0}/test/test_crypto.py +0 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
from hivemind_plugin_manager.database import Client, AbstractDB, cast2client
|
|
2
|
+
from ovos_utils.log import LOG
|
|
3
|
+
from ovos_utils.xdg_utils import xdg_data_home
|
|
4
|
+
from typing import Union, Iterable, List
|
|
5
|
+
from json_database import JsonStorageXDG
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class JsonDB(AbstractDB):
|
|
9
|
+
"""HiveMind Database implementation using JSON files."""
|
|
10
|
+
|
|
11
|
+
def __init__(self, name="clients", subfolder="hivemind-core"):
|
|
12
|
+
self._db = JsonStorageXDG(name, subfolder=subfolder, xdg_folder=xdg_data_home())
|
|
13
|
+
LOG.debug(f"json database path: {self._db.path}")
|
|
14
|
+
|
|
15
|
+
def sync(self):
|
|
16
|
+
"""update db from disk if needed"""
|
|
17
|
+
self._db.reload()
|
|
18
|
+
|
|
19
|
+
def add_item(self, client: Client) -> bool:
|
|
20
|
+
"""
|
|
21
|
+
Add a client to the JSON database.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
client: The client to be added.
|
|
25
|
+
|
|
26
|
+
Returns:
|
|
27
|
+
True if the addition was successful, False otherwise.
|
|
28
|
+
"""
|
|
29
|
+
self._db[client.client_id] = client.__dict__
|
|
30
|
+
return True
|
|
31
|
+
|
|
32
|
+
def search_by_value(self, key: str, val: Union[str, bool, int, float]) -> List[Client]:
|
|
33
|
+
"""
|
|
34
|
+
Search for clients by a specific key-value pair in the JSON database.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
key: The key to search by.
|
|
38
|
+
val: The value to search for.
|
|
39
|
+
|
|
40
|
+
Returns:
|
|
41
|
+
A list of clients that match the search criteria.
|
|
42
|
+
"""
|
|
43
|
+
res = []
|
|
44
|
+
if key == "client_id":
|
|
45
|
+
v = self._db.get(val)
|
|
46
|
+
if v:
|
|
47
|
+
res.append(cast2client(v))
|
|
48
|
+
else:
|
|
49
|
+
for client in self._db.values():
|
|
50
|
+
v = client.get(key)
|
|
51
|
+
if v == val:
|
|
52
|
+
res.append(cast2client(client))
|
|
53
|
+
return res
|
|
54
|
+
|
|
55
|
+
def __len__(self) -> int:
|
|
56
|
+
"""
|
|
57
|
+
Get the number of clients in the database.
|
|
58
|
+
|
|
59
|
+
Returns:
|
|
60
|
+
The number of clients in the database.
|
|
61
|
+
"""
|
|
62
|
+
return len(self._db)
|
|
63
|
+
|
|
64
|
+
def __iter__(self) -> Iterable['Client']:
|
|
65
|
+
"""
|
|
66
|
+
Iterate over all clients in the JSON database.
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
An iterator over the clients in the database.
|
|
70
|
+
"""
|
|
71
|
+
for item in self._db.values():
|
|
72
|
+
yield Client.deserialize(item)
|
|
73
|
+
|
|
74
|
+
def commit(self) -> bool:
|
|
75
|
+
"""
|
|
76
|
+
Commit changes to the JSON database.
|
|
77
|
+
|
|
78
|
+
Returns:
|
|
79
|
+
True if the commit was successful, False otherwise.
|
|
80
|
+
"""
|
|
81
|
+
try:
|
|
82
|
+
self._db.store()
|
|
83
|
+
return True
|
|
84
|
+
except Exception as e:
|
|
85
|
+
LOG.error(f"Failed to save {self._db.path} - {e}")
|
|
86
|
+
return False
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|