Skip to content

Commit

Permalink
Add support to remote bus address and port (#79)
Browse files Browse the repository at this point in the history
* Fixing some linting

* Fixing some linting part 2

* Add support for remote bus address and port

* Changes requested from review
  • Loading branch information
goldyfruit committed Jan 2, 2024
1 parent 5b174a8 commit 7558c69
Show file tree
Hide file tree
Showing 16 changed files with 497 additions and 308 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,4 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
#.idea/
12 changes: 5 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

**Implemented enhancements:**

- add site\_id [\#74](https://github.com/JarbasHiveMind/HiveMind-core/pull/74) ([JarbasAl](https://github.com/JarbasAl))
- add site_id [\#74](https://github.com/JarbasHiveMind/HiveMind-core/pull/74) ([JarbasAl](https://github.com/JarbasAl))

## [V0.13.0a0](https://github.com/JarbasHiveMind/HiveMind-core/tree/V0.13.0a0) (2023-09-08)

Expand Down Expand Up @@ -100,7 +100,7 @@

**Merged pull requests:**

- filter allowed\_types [\#71](https://github.com/JarbasHiveMind/HiveMind-core/pull/71) ([emphasize](https://github.com/emphasize))
- filter allowed_types [\#71](https://github.com/JarbasHiveMind/HiveMind-core/pull/71) ([emphasize](https://github.com/emphasize))
- add typehints [\#70](https://github.com/JarbasHiveMind/HiveMind-core/pull/70) ([emphasize](https://github.com/emphasize))
- \[requirements\] Add missing pyOpenSSL [\#69](https://github.com/JarbasHiveMind/HiveMind-core/pull/69) ([goldyfruit](https://github.com/goldyfruit))

Expand Down Expand Up @@ -130,7 +130,7 @@
- Xdg [\#51](https://github.com/JarbasHiveMind/HiveMind-core/pull/51) ([JarbasAl](https://github.com/JarbasAl))
- V2 [\#50](https://github.com/JarbasHiveMind/HiveMind-core/pull/50) ([JarbasAl](https://github.com/JarbasAl))
- Refactor/hivemind presence [\#49](https://github.com/JarbasHiveMind/HiveMind-core/pull/49) ([JarbasAl](https://github.com/JarbasAl))
- refactor/deprecate\_sql [\#48](https://github.com/JarbasHiveMind/HiveMind-core/pull/48) ([JarbasAl](https://github.com/JarbasAl))
- refactor/deprecate_sql [\#48](https://github.com/JarbasHiveMind/HiveMind-core/pull/48) ([JarbasAl](https://github.com/JarbasAl))
- launcher scripts / deprecate mail param / increase RSA key size [\#46](https://github.com/JarbasHiveMind/HiveMind-core/pull/46) ([Joanguitar](https://github.com/Joanguitar))
- move to asyncio [\#41](https://github.com/JarbasHiveMind/HiveMind-core/pull/41) ([JarbasAl](https://github.com/JarbasAl))

Expand Down Expand Up @@ -162,7 +162,7 @@

- migrate to pycryptodomex [\#30](https://github.com/JarbasHiveMind/HiveMind-core/pull/30) ([JarbasAl](https://github.com/JarbasAl))
- Add instructions to README [\#27](https://github.com/JarbasHiveMind/HiveMind-core/pull/27) ([ChanceNCounter](https://github.com/ChanceNCounter))
- migrate from jarbas\_utils to ovos\_utils [\#24](https://github.com/JarbasHiveMind/HiveMind-core/pull/24) ([JarbasAl](https://github.com/JarbasAl))
- migrate from jarbas_utils to ovos_utils [\#24](https://github.com/JarbasHiveMind/HiveMind-core/pull/24) ([JarbasAl](https://github.com/JarbasAl))
- revert cryptodomex requirement change [\#21](https://github.com/JarbasHiveMind/HiveMind-core/pull/21) ([JarbasAl](https://github.com/JarbasAl))
- Switch from pycryptodome to pycryptodomex [\#12](https://github.com/JarbasHiveMind/HiveMind-core/pull/12) ([j1nx](https://github.com/j1nx))
- escalate [\#8](https://github.com/JarbasHiveMind/HiveMind-core/pull/8) ([JarbasAl](https://github.com/JarbasAl))
Expand All @@ -171,6 +171,4 @@
- Feat/emulation [\#5](https://github.com/JarbasHiveMind/HiveMind-core/pull/5) ([JarbasAl](https://github.com/JarbasAl))
- refactor + http support [\#4](https://github.com/JarbasHiveMind/HiveMind-core/pull/4) ([JarbasAl](https://github.com/JarbasAl))



\* *This Changelog was automatically generated by [github_changelog_generator](https://github.com/github-changelog-generator/github-changelog-generator)*
\* _This Changelog was automatically generated by [github_changelog_generator](https://github.com/github-changelog-generator/github-changelog-generator)_
39 changes: 23 additions & 16 deletions examples/fakecroft.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
from ovos_utils.log import LOG
from ovos_utils.messagebus import FakeBus
from hivemind_listener.service import MessageBusEventHandler
from hivemind_listener.protocol import HiveMindListenerProtocol, \
HiveMindListenerInternalProtocol, HiveMindClientConnection
from hivemind_listener.protocol import (
HiveMindListenerProtocol,
HiveMindListenerInternalProtocol,
HiveMindClientConnection,
)


class HiveMindFakeCroftProtocol(HiveMindListenerProtocol):
""" Fake ovos-core instance, not actually connected to a messagebus"""
"""Fake ovos-core instance, not actually connected to a messagebus"""

peer: str = "fakecroft:0.0.0.0"

def bind(self, websocket, bus=None):
Expand All @@ -17,46 +21,49 @@ def bind(self, websocket, bus=None):
bus = FakeBus()
self.internal_protocol = HiveMindListenerInternalProtocol(bus)

def handle_incoming_mycroft(self, message: Message, client: HiveMindClientConnection):
def handle_incoming_mycroft(
self, message: Message, client: HiveMindClientConnection
):
"""
message (Message): mycroft bus message object
"""
super().handle_inject_mycroft_msg(message, client)
answer = "mycroft is dead! long live mycroft!"

payload = HiveMessage(HiveMessageType.BUS,
message.reply("speak", {"utterance": answer}))
payload = HiveMessage(
HiveMessageType.BUS, message.reply("speak", {"utterance": answer})
)
client.send(payload)


def on_ready():
LOG.info('FakeCroft started!')
LOG.info("FakeCroft started!")


def on_error(e='Unknown'):
LOG.info('FakeCroft failed to start ({})'.format(repr(e)))
def on_error(e="Unknown"):
LOG.info("FakeCroft failed to start ({})".format(repr(e)))


def on_stopping():
LOG.info('FakeCroft is shutting down...')
LOG.info("FakeCroft is shutting down...")


def main(ready_hook=on_ready, error_hook=on_error, stopping_hook=on_stopping):
from ovos_utils import create_daemon, wait_for_exit_signal
from tornado import web, ioloop
from ovos_config import Configuration

LOG.info('Starting FakeCroft...')
LOG.info("Starting FakeCroft...")

try:
websocket_configs = Configuration()['websocket']
websocket_configs = Configuration()["websocket"]
except KeyError as ke:
LOG.error('No websocket configs found ({})'.format(repr(ke)))
LOG.error("No websocket configs found ({})".format(repr(ke)))
raise

host = websocket_configs.get('host')
port = websocket_configs.get('port')
route = websocket_configs.get('route')
host = websocket_configs.get("host")
port = websocket_configs.get("port")
route = websocket_configs.get("route")
port = 5678
route = "/"

Expand Down
106 changes: 58 additions & 48 deletions hivemind_core/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,36 @@


def cast_to_client_obj():
valid_kwargs: Iterable[str] = ("client_id", "api_key", "name",
"description", "is_admin", "last_seen",
"blacklist", "allowed_types", "crypto_key",
"password", "can_broadcast", "can_escalate",
"can_propagate")
valid_kwargs: Iterable[str] = (
"client_id",
"api_key",
"name",
"description",
"is_admin",
"last_seen",
"blacklist",
"allowed_types",
"crypto_key",
"password",
"can_broadcast",
"can_escalate",
"can_propagate",
)

def _handler(func):

def _cast(ret):
if ret is None or isinstance(ret, Client):
return ret
if isinstance(ret, list):
return [_cast(r) for r in ret]
if isinstance(ret, dict):
if not all((k in valid_kwargs
for k in ret.keys())):
if not all((k in valid_kwargs for k in ret.keys())):
raise RuntimeError(f"{func} returned a dict with unknown keys")
return Client(**ret)

raise TypeError(
"cast_to_client_obj decorator can only be used in functions that return None, dict, Client or a list of those types")
"cast_to_client_obj decorator can only be used in functions that return None, dict, Client or a list of those types"
)

@wraps(func)
def call_function(*args, **kwargs):
Expand All @@ -40,21 +49,22 @@ def call_function(*args, **kwargs):


class Client:
def __init__(self,
client_id: int,
api_key: str,
name: str = "",
description: str = "",
is_admin: bool = False,
last_seen: float = -1,
blacklist: Optional[Dict[str, List[str]]] = None,
allowed_types: Optional[List[str]] = None,
crypto_key: Optional[str] = None,
password: Optional[str] = None,
can_broadcast: bool = True,
can_escalate: bool = True,
can_propagate: bool = True):

def __init__(
self,
client_id: int,
api_key: str,
name: str = "",
description: str = "",
is_admin: bool = False,
last_seen: float = -1,
blacklist: Optional[Dict[str, List[str]]] = None,
allowed_types: Optional[List[str]] = None,
crypto_key: Optional[str] = None,
password: Optional[str] = None,
can_broadcast: bool = True,
can_escalate: bool = True,
can_propagate: bool = True,
):
self.client_id = client_id
self.description = description
self.api_key = api_key
Expand All @@ -63,11 +73,7 @@ def __init__(self,
self.is_admin = is_admin
self.crypto_key = crypto_key
self.password = password
self.blacklist = blacklist or {
"messages": [],
"skills": [],
"intents": []
}
self.blacklist = blacklist or {"messages": [], "skills": [], "intents": []}
self.allowed_types = allowed_types or ["recognizer_loop:utterance"]
if "recognizer_loop:utterance" not in self.allowed_types:
self.allowed_types.append("recognizer_loop:utterance")
Expand Down Expand Up @@ -155,9 +161,7 @@ def change_name(self, new_name: str, key: str) -> bool:
self.update_item(item_id, user)
return True

def change_blacklist(self,
blacklist: Union[str, Dict[str, Any]],
key: str) -> bool:
def change_blacklist(self, blacklist: Union[str, Dict[str, Any]], key: str) -> bool:
if isinstance(blacklist, dict):
blacklist = json.dumps(blacklist)
user = self.get_client_by_api_key(key)
Expand Down Expand Up @@ -186,15 +190,16 @@ def get_clients_by_name(self, name: str) -> List[Client]:
return self.search_by_value("name", name)

@cast_to_client_obj()
def add_client(self,
name: str,
key: str = "",
admin: bool = False,
blacklist: Optional[Dict[str, Any]] = None,
allowed_types: Optional[List[str]] = None,
crypto_key: Optional[str] = None,
password: Optional[str] = None) -> Client:

def add_client(
self,
name: str,
key: str = "",
admin: bool = False,
blacklist: Optional[Dict[str, Any]] = None,
allowed_types: Optional[List[str]] = None,
crypto_key: Optional[str] = None,
password: Optional[str] = None,
) -> Client:
user = self.get_client_by_api_key(key)
item_id = self.get_item_id(user)
if crypto_key is not None:
Expand All @@ -214,23 +219,28 @@ def add_client(self,
user["password"] = password
self.update_item(item_id, user)
else:
user = Client(api_key=key, name=name,
blacklist=blacklist, crypto_key=crypto_key,
client_id=self.total_clients() + 1,
is_admin=admin, password=password,
allowed_types=allowed_types)
user = Client(
api_key=key,
name=name,
blacklist=blacklist,
crypto_key=crypto_key,
client_id=self.total_clients() + 1,
is_admin=admin,
password=password,
allowed_types=allowed_types,
)
self.add_item(user)
return user

def total_clients(self) -> int:
return len(self)

def __enter__(self):
""" Context handler """
"""Context handler"""
return self

def __exit__(self, _type, value, traceback):
""" Commits changes and Closes the session """
"""Commits changes and Closes the session"""
try:
self.commit()
except Exception as e:
Expand Down
Loading

0 comments on commit 7558c69

Please sign in to comment.