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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: json_database
3
- Version: 0.8.1
3
+ Version: 0.9.0
4
4
  Summary: searchable json database with persistence
5
5
  Home-page: https://github.com/TigreGotico/json_database
6
6
  Author: jarbasAI
@@ -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
@@ -1,6 +1,6 @@
1
1
  # START_VERSION_BLOCK
2
2
  VERSION_MAJOR = 0
3
- VERSION_MINOR = 8
4
- VERSION_BUILD = 1
3
+ VERSION_MINOR = 9
4
+ VERSION_BUILD = 0
5
5
  VERSION_ALPHA = 0
6
6
  # END_VERSION_BLOCK
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: json-database
3
- Version: 0.8.1
3
+ Version: 0.9.0
4
4
  Summary: searchable json database with persistence
5
5
  Home-page: https://github.com/TigreGotico/json_database
6
6
  Author: jarbasAI
@@ -4,6 +4,7 @@ setup.py
4
4
  json_database/__init__.py
5
5
  json_database/crypto.py
6
6
  json_database/exceptions.py
7
+ json_database/hpm.py
7
8
  json_database/search.py
8
9
  json_database/utils.py
9
10
  json_database/version.py
File without changes
File without changes
File without changes
File without changes