Skip to content

Commit a417b72

Browse files
authored
support room name (#44)
1 parent 9d54a7e commit a417b72

6 files changed

Lines changed: 42 additions & 28 deletions

File tree

thingsdb/client/client.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -484,11 +484,12 @@ def _emit(
484484
scope = self._scope
485485
return self._write_pkg(Proto.REQ_EMIT, [scope, room_id, event, *args])
486486

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

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

515-
def _leave(self, *ids: int, scope: Optional[str] = None) -> asyncio.Future:
516+
def _leave(self, *ids: Union[int, str],
517+
scope: Optional[str] = None) -> asyncio.Future:
516518
"""Leave one or more rooms.
517519
518520
Stop receiving events for the rooms given by one or more ids. It is

thingsdb/room/room.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import abc
2-
import asyncio
31
import logging
4-
from typing import Union
5-
from ..client import Client
6-
from ..client.protocol import Proto
72
from .roombase import RoomBase
83

94

thingsdb/room/roombase.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import abc
22
import asyncio
33
import logging
4-
import functools
54
from typing import Union, Optional
65
from ..client import Client
76
from ..client.protocol import Proto
7+
from ..util.is_name import is_name
88

99

1010
class RoomBase(abc.ABC):
@@ -61,13 +61,19 @@ async def no_join(self, client: Client):
6161
self._client = client
6262

6363
if isinstance(self._id, str):
64-
code = self._id
65-
id = await client.query(code, scope=self._scope)
66-
if not isinstance(id, int):
67-
raise TypeError(
68-
f'expecting ThingsDB code `{code}` to return with a '
69-
f'room Id (integer value), '
70-
f'but got type `{type(id).__name__}`')
64+
if is_name(self._id):
65+
id = await client.query(
66+
"room(name).id();",
67+
name=self._id,
68+
scope=self._scope)
69+
else:
70+
code = self._id
71+
id = await client.query(code, scope=self._scope)
72+
if not isinstance(id, int):
73+
raise TypeError(
74+
f'expecting ThingsDB code `{code}` to return with '
75+
f'a room Id (integer value), '
76+
f'but got type `{type(id).__name__}`')
7177
else:
7278
id = self._id
7379
is_room = \
@@ -99,13 +105,19 @@ async def join(self, client: Client, wait: Optional[float] = 60.0):
99105
self._client = client
100106

101107
if isinstance(self._id, str):
102-
code = self._id
103-
id = await client.query(code, scope=self._scope)
104-
if not isinstance(id, int):
105-
raise TypeError(
106-
f'expecting ThingsDB code `{code}` to return with a '
107-
f'room Id (integer value), '
108-
f'but got type `{type(id).__name__}`')
108+
if is_name(self._id):
109+
id = await client.query(
110+
"room(name).id();",
111+
name=self._id,
112+
scope=self._scope)
113+
else:
114+
code = self._id
115+
id = await client.query(code, scope=self._scope)
116+
if not isinstance(id, int):
117+
raise TypeError(
118+
f'expecting ThingsDB code `{code}` to return with '
119+
f'a room Id (integer value), '
120+
f'but got type `{type(id).__name__}`')
109121
res = await client._join(id, scope=self._scope)
110122
if res[0] is None:
111123
raise LookupError(

thingsdb/util/cnscope.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import re
2-
3-
_VALID_NAME = re.compile(r'^[A-Za-z_][0-9A-Za-z_]{0,254}')
1+
from .is_name import is_name
42

53

64
def cnscope(scope):
@@ -18,7 +16,7 @@ def cnscope(scope):
1816
else:
1917
name = ''
2018

21-
if _VALID_NAME.match(name):
19+
if is_name(name):
2220
return name
2321

2422
raise ValueError(f'invalid (collection) scope name: {scope}')

thingsdb/util/is_name.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import re
2+
3+
_VALID_NAME = re.compile(r'^[A-Za-z_][0-9A-Za-z_]{0,254}$')
4+
5+
6+
def is_name(s: str) -> bool:
7+
bool(_VALID_NAME.match(s))

thingsdb/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.1.2'
1+
__version__ = '1.1.3'

0 commit comments

Comments
 (0)