Skip to content

Commit

Permalink
support room name
Browse files Browse the repository at this point in the history
  • Loading branch information
joente committed Dec 3, 2024
1 parent 9d54a7e commit fb135ab
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 28 deletions.
8 changes: 5 additions & 3 deletions thingsdb/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,11 +484,12 @@ def _emit(
scope = self._scope
return self._write_pkg(Proto.REQ_EMIT, [scope, room_id, event, *args])

def _join(self, *ids: int, scope: Optional[str] = None) -> asyncio.Future:
def _join(self, *ids: Union[int, str],
scope: Optional[str] = None) -> asyncio.Future:
"""Join one or more rooms.
Args:
*ids (int):
*ids (int/str):
Room Ids to join. No error is returned in case one of
the given room Ids are not found within the collection.
Instead, the return value will contain `None` instead of the
Expand All @@ -512,7 +513,8 @@ def _join(self, *ids: int, scope: Optional[str] = None) -> asyncio.Future:

return self._write_pkg(Proto.REQ_JOIN, [scope, *ids])

def _leave(self, *ids: int, scope: Optional[str] = None) -> asyncio.Future:
def _leave(self, *ids: Union[int, str],
scope: Optional[str] = None) -> asyncio.Future:
"""Leave one or more rooms.
Stop receiving events for the rooms given by one or more ids. It is
Expand Down
5 changes: 0 additions & 5 deletions thingsdb/room/room.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import abc
import asyncio
import logging
from typing import Union
from ..client import Client
from ..client.protocol import Proto
from .roombase import RoomBase


Expand Down
42 changes: 27 additions & 15 deletions thingsdb/room/roombase.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import abc
import asyncio
import logging
import functools
from typing import Union, Optional
from ..client import Client
from ..client.protocol import Proto
from ..util.is_name import is_name


class RoomBase(abc.ABC):
Expand Down Expand Up @@ -61,13 +61,19 @@ async def no_join(self, client: Client):
self._client = client

if isinstance(self._id, str):
code = self._id
id = await client.query(code, scope=self._scope)
if not isinstance(id, int):
raise TypeError(
f'expecting ThingsDB code `{code}` to return with a '
f'room Id (integer value), '
f'but got type `{type(id).__name__}`')
if is_name(self._id):
id = await client.query(
"room(name).id();",
name=self._id,
scope=self._scope)
else:
code = self._id
id = await client.query(code, scope=self._scope)
if not isinstance(id, int):
raise TypeError(
f'expecting ThingsDB code `{code}` to return with '
f'a room Id (integer value), '
f'but got type `{type(id).__name__}`')
else:
id = self._id
is_room = \
Expand Down Expand Up @@ -99,13 +105,19 @@ async def join(self, client: Client, wait: Optional[float] = 60.0):
self._client = client

if isinstance(self._id, str):
code = self._id
id = await client.query(code, scope=self._scope)
if not isinstance(id, int):
raise TypeError(
f'expecting ThingsDB code `{code}` to return with a '
f'room Id (integer value), '
f'but got type `{type(id).__name__}`')
if is_name(self._id):
id = await client.query(
"room(name).id();",
name=self._id,
scope=self._scope)
else:
code = self._id
id = await client.query(code, scope=self._scope)
if not isinstance(id, int):
raise TypeError(
f'expecting ThingsDB code `{code}` to return with '
f'a room Id (integer value), '
f'but got type `{type(id).__name__}`')
res = await client._join(id, scope=self._scope)
if res[0] is None:
raise LookupError(
Expand Down
6 changes: 2 additions & 4 deletions thingsdb/util/cnscope.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import re

_VALID_NAME = re.compile(r'^[A-Za-z_][0-9A-Za-z_]{0,254}')
from .is_name import is_name


def cnscope(scope):
Expand All @@ -18,7 +16,7 @@ def cnscope(scope):
else:
name = ''

if _VALID_NAME.match(name):
if is_name(name):
return name

raise ValueError(f'invalid (collection) scope name: {scope}')
Expand Down
7 changes: 7 additions & 0 deletions thingsdb/util/is_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import re

_VALID_NAME = re.compile(r'^[A-Za-z_][0-9A-Za-z_]{0,254}$')


def is_name(s: str) -> bool:
bool(_VALID_NAME.match(s))
2 changes: 1 addition & 1 deletion thingsdb/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.1.2'
__version__ = '1.1.3'

0 comments on commit fb135ab

Please sign in to comment.