Skip to content

Commit

Permalink
Merge pull request #8 from TigreGotico/release-0.9.0a1
Browse files Browse the repository at this point in the history
Release 0.9.0a1
  • Loading branch information
JarbasAl authored Dec 28, 2024
2 parents 5880034 + 8eb1c1f commit 53c2fc4
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 8 deletions.
8 changes: 3 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# Changelog

## [0.8.0a1](https://github.com/TigreGotico/json_database/tree/0.8.0a1) (2024-12-24)
## [0.9.0a1](https://github.com/TigreGotico/json_database/tree/0.9.0a1) (2024-12-28)

[Full Changelog](https://github.com/TigreGotico/json_database/compare/0.7.0...0.8.0a1)
[Full Changelog](https://github.com/TigreGotico/json_database/compare/0.8.1...0.9.0a1)

**Merged pull requests:**

- feat: encrypted database [\#5](https://github.com/TigreGotico/json_database/pull/5) ([JarbasAl](https://github.com/JarbasAl))
- feat:semver [\#4](https://github.com/TigreGotico/json_database/pull/4) ([JarbasAl](https://github.com/JarbasAl))
- Update requirements.txt [\#3](https://github.com/TigreGotico/json_database/pull/3) ([JarbasAl](https://github.com/JarbasAl))
- feat: hivemind-plugin-manager [\#7](https://github.com/TigreGotico/json_database/pull/7) ([JarbasAl](https://github.com/JarbasAl))



Expand Down
86 changes: 86 additions & 0 deletions json_database/hpm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from hivemind_plugin_manager.database import Client, AbstractDB, cast2client
from ovos_utils.log import LOG
from ovos_utils.xdg_utils import xdg_data_home
from typing import Union, Iterable, List
from json_database import JsonStorageXDG


class JsonDB(AbstractDB):
"""HiveMind Database implementation using JSON files."""

def __init__(self, name="clients", subfolder="hivemind-core"):
self._db = JsonStorageXDG(name, subfolder=subfolder, xdg_folder=xdg_data_home())
LOG.debug(f"json database path: {self._db.path}")

def sync(self):
"""update db from disk if needed"""
self._db.reload()

def add_item(self, client: Client) -> bool:
"""
Add a client to the JSON database.
Args:
client: The client to be added.
Returns:
True if the addition was successful, False otherwise.
"""
self._db[client.client_id] = client.__dict__
return True

def search_by_value(self, key: str, val: Union[str, bool, int, float]) -> List[Client]:
"""
Search for clients by a specific key-value pair in the JSON database.
Args:
key: The key to search by.
val: The value to search for.
Returns:
A list of clients that match the search criteria.
"""
res = []
if key == "client_id":
v = self._db.get(val)
if v:
res.append(cast2client(v))
else:
for client in self._db.values():
v = client.get(key)
if v == val:
res.append(cast2client(client))
return res

def __len__(self) -> int:
"""
Get the number of clients in the database.
Returns:
The number of clients in the database.
"""
return len(self._db)

def __iter__(self) -> Iterable['Client']:
"""
Iterate over all clients in the JSON database.
Returns:
An iterator over the clients in the database.
"""
for item in self._db.values():
yield Client.deserialize(item)

def commit(self) -> bool:
"""
Commit changes to the JSON database.
Returns:
True if the commit was successful, False otherwise.
"""
try:
self._db.store()
return True
except Exception as e:
LOG.error(f"Failed to save {self._db.path} - {e}")
return False
6 changes: 3 additions & 3 deletions json_database/version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# START_VERSION_BLOCK
VERSION_MAJOR = 0
VERSION_MINOR = 8
VERSION_BUILD = 1
VERSION_ALPHA = 0
VERSION_MINOR = 9
VERSION_BUILD = 0
VERSION_ALPHA = 1
# END_VERSION_BLOCK

0 comments on commit 53c2fc4

Please sign in to comment.