Skip to content

Commit 078e5b2

Browse files
authored
Improvements (#35)
1 parent 19938f8 commit 078e5b2

File tree

6 files changed

+30
-5
lines changed

6 files changed

+30
-5
lines changed

thingsdb/client/client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .buildin import Buildin
1010
from .protocol import Proto, Protocol
1111
from ..exceptions import NodeError, AuthError
12+
from ..util import strip_code
1213

1314

1415
class Client(Buildin):
@@ -324,7 +325,7 @@ def query(
324325
if scope is None:
325326
scope = self._scope
326327

327-
code = code.strip() # strip white space characters
328+
code = strip_code(code)
328329
data = [scope, code]
329330
if kwargs:
330331
data.append(kwargs)
@@ -581,7 +582,7 @@ def _on_event(self, pkg):
581582
if pkg.tp == Proto.ON_WARN:
582583
warn = pkg.data
583584
logging.warn(
584-
f'Warning from ThingsDB: '
585+
f'ThingsDB: '
585586
f'{warn["warn_msg"]} ({warn["warn_code"]})')
586587
else:
587588
logging.warn(f'Unexpected event: tp:{pkg.tp} data:{pkg.data}')

thingsdb/room/room.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,10 @@ def on_delete(self) -> None:
5252
Called when the room is removed from ThingsDB.
5353
"""
5454
pass
55+
56+
def on_emit(self, event, *args):
57+
"""On emit
58+
Called when no event handler is configured for the event.
59+
"""
60+
logging.debug(f"No emit handler found for `{event}`")
61+
pass

thingsdb/room/roombase.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ async def on_join(self) -> None:
151151
def on_leave(self) -> None:
152152
pass
153153

154+
@abc.abstractmethod
155+
def on_emit(self, event: str, *args) -> None:
156+
pass
157+
154158
async def _on_first_join(self):
155159
fut = self._wait_join
156160
self._wait_join = None
@@ -184,8 +188,7 @@ def _emit_handler(self, data):
184188
try:
185189
fun = cls._event_handlers[event]
186190
except KeyError:
187-
logging.debug(
188-
f"No emit handler found for `{event}` on {cls.__name__}")
191+
self.on_emit(event, *data['args'])
189192
else:
190193
fun(self, *data['args'])
191194

thingsdb/util/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
from .cnscope import cnscope
22
from .access import Access
3+
from .cleancode import strip_code

thingsdb/util/cleancode.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import re
2+
3+
4+
rbegin = re.compile(r'^\s*(\/\/.*)?\s*')
5+
6+
7+
def strip_code(code: str):
8+
"""Strip white space and start comment from query code.
9+
10+
This function now removes only white space and the first line comment
11+
when starting with // from code.
12+
"""
13+
return rbegin.sub('', code).rstrip()

thingsdb/version.py

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

0 commit comments

Comments
 (0)