Skip to content

Commit

Permalink
Fix various issues with initial PR
Browse files Browse the repository at this point in the history
Reorg of these changes into a set of commits that actually makes
sense is still coming; don't worry.
  • Loading branch information
non-Jedi committed Sep 17, 2017
1 parent 65d3160 commit 3944a4f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
30 changes: 24 additions & 6 deletions matrix_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,8 @@ def _upload(self, response):
def _mkroom(self, response=None, room_id_or_alias=None):
if response and "room_id" in response:
room_id_or_alias = response["room_id"]
self.rooms[room_id_or_alias] = Room(self, room_id)
return self.rooms[room_id]
self.rooms[room_id_or_alias] = Room(self, room_id_or_alias)
return self.rooms[room_id_or_alias]

def _process_state_event(self, state_event, current_room):
if "type" not in state_event:
Expand Down Expand Up @@ -593,14 +593,32 @@ def remove_room_alias(self, room_alias):
return False

def _async_call(self, first_callback, final_callback):
"""Call `final_callback` on result of `first_callback` asynchronously"""
"""Call `final_callback` on result of `first_callback` asynchronously
Args:
first_callback(callable): Callable with 0 args to be called first
final_callback(callable): Callable with 1 arg whose result will be
returned. Called with output from first_callback.
Returns:
AsyncResult: Promise for the result of final_callback.
"""
first_result = AsyncResult()
self.queue.put((first_callback, first_result))
self.queue.matrix_put((first_callback, first_result))
final_result = AsyncResult()
# lambda function will wait for first_result to be fulfilled
self.queue.put(lambda: final_callback(first_result.get()), final_result)
self.queue.matrix_put((lambda: final_callback(first_result.get()), final_result))
return final_result

def _sync_call(self, first_callback, final_callback):
"""Call `final_callback` on result of `first_callback` synchronously"""
"""Call `final_callback` on result of `first_callback` synchronously
Args:
first_callback(callable): Callable with 0 args to be called first
final_callback(callable): Callable with 1 arg whose result will be
returned. Called with output from first_callback.
Returns:
Object: Result of final_callback
"""
return final_callback(first_callback())
10 changes: 10 additions & 0 deletions matrix_client/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.

import gevent.queue
from gevent.event import AsyncResult
import time
import json
from .errors import MatrixRequestError
Expand Down Expand Up @@ -68,3 +69,12 @@ def call_forever(self):
"""Calls self.call forever."""
while True:
self.call()

def matrix_put(self, item, *args, **kwargs):
"""Calls `self.put` after validating type of item."""
if (type(item) == tuple and len(item) == 2 and
callable(item[0]) and type(item[1]) == AsyncResult):

return self.put(item, *args, **kwargs)
else:
raise TypeError("Received %s when expecting (callable, AsyncResult)" % str(item))
3 changes: 2 additions & 1 deletion matrix_client/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from functools import partial
import re
from uuid import uuid4

Expand Down Expand Up @@ -534,7 +535,7 @@ def _set_room_topic(topic):
return self._call(
partial(self._handle_api_errors,
partial(self.client.api.set_room_topic, self.room_id, topic)),
lambda _: set_room_topic(topic)
lambda _: _set_room_topic(topic)
)

def update_aliases(self):
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def exec_file(names):
],
keywords='chat sdk matrix matrix.org',
install_requires=[
'requests'
'requests',
'gevent',
],
extras_require={
'test': ['tox', 'pytest', 'flake8', 'responses'],
Expand Down

0 comments on commit 3944a4f

Please sign in to comment.