Skip to content

Commit 4548443

Browse files
committed
Fix various issues with initial PR
Reorg of these changes into a set of commits that actually makes sense is still coming; don't worry.
1 parent 65d3160 commit 4548443

4 files changed

Lines changed: 39 additions & 9 deletions

File tree

matrix_client/client.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -491,11 +491,11 @@ def _upload(self, response):
491491
"The upload was successful, but content_uri wasn't found."
492492
)
493493

494-
def _mkroom(self, response=None, room_id_or_alias=None):
494+
def _mkroom(self, room_id_or_alias=None, response=None):
495495
if response and "room_id" in response:
496496
room_id_or_alias = response["room_id"]
497-
self.rooms[room_id_or_alias] = Room(self, room_id)
498-
return self.rooms[room_id]
497+
self.rooms[room_id_or_alias] = Room(self, room_id_or_alias)
498+
return self.rooms[room_id_or_alias]
499499

500500
def _process_state_event(self, state_event, current_room):
501501
if "type" not in state_event:
@@ -593,14 +593,32 @@ def remove_room_alias(self, room_alias):
593593
return False
594594

595595
def _async_call(self, first_callback, final_callback):
596-
"""Call `final_callback` on result of `first_callback` asynchronously"""
596+
"""Call `final_callback` on result of `first_callback` asynchronously
597+
598+
Args:
599+
first_callback(callable): Callable with 0 args to be called first
600+
final_callback(callable): Callable with 1 arg whose result will be
601+
returned. Called with output from first_callback.
602+
603+
Returns:
604+
AsyncResult: Promise for the result of final_callback.
605+
"""
597606
first_result = AsyncResult()
598-
self.queue.put((first_callback, first_result))
607+
self.queue.matrix_put((first_callback, first_result))
599608
final_result = AsyncResult()
600609
# lambda function will wait for first_result to be fulfilled
601-
self.queue.put(lambda: final_callback(first_result.get()), final_result)
610+
self.queue.matrix_put((lambda: final_callback(first_result.get()), final_result))
602611
return final_result
603612

604613
def _sync_call(self, first_callback, final_callback):
605-
"""Call `final_callback` on result of `first_callback` synchronously"""
614+
"""Call `final_callback` on result of `first_callback` synchronously
615+
616+
Args:
617+
first_callback(callable): Callable with 0 args to be called first
618+
final_callback(callable): Callable with 1 arg whose result will be
619+
returned. Called with output from first_callback.
620+
621+
Returns:
622+
Object: Result of final_callback
623+
"""
606624
return final_callback(first_callback())

matrix_client/queue.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# limitations under the License.
1515

1616
import gevent.queue
17+
from gevent.event import AsyncResult
1718
import time
1819
import json
1920
from .errors import MatrixRequestError
@@ -68,3 +69,12 @@ def call_forever(self):
6869
"""Calls self.call forever."""
6970
while True:
7071
self.call()
72+
73+
def matrix_put(self, item, *args, **kwargs):
74+
"""Calls `self.put` after validating type of item."""
75+
if (type(item) == tuple and len(item) == 2 and
76+
callable(item[0]) and type(item[1]) == AsyncResult):
77+
78+
return self.put(item, *args, **kwargs)
79+
else:
80+
raise TypeError("Received %s when expecting (callable, AsyncResult)" % str(item))

matrix_client/room.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
from functools import partial
1718
import re
1819
from uuid import uuid4
1920

@@ -534,7 +535,7 @@ def _set_room_topic(topic):
534535
return self._call(
535536
partial(self._handle_api_errors,
536537
partial(self.client.api.set_room_topic, self.room_id, topic)),
537-
lambda _: set_room_topic(topic)
538+
lambda _: _set_room_topic(topic)
538539
)
539540

540541
def update_aliases(self):

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ def exec_file(names):
4444
],
4545
keywords='chat sdk matrix matrix.org',
4646
install_requires=[
47-
'requests'
47+
'requests',
48+
'gevent',
4849
],
4950
extras_require={
5051
'test': ['tox', 'pytest', 'flake8', 'responses'],

0 commit comments

Comments
 (0)