Skip to content

Commit

Permalink
style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
spirali committed Apr 19, 2019
1 parent d4fe0fc commit 49cddab
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 27 deletions.
5 changes: 2 additions & 3 deletions abrpc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@

from .connection import Connection, RemoteException
from .utils import on_connection, expose
from .connection import Connection, RemoteException # noqa
from .utils import on_connection, expose # noqa
23 changes: 14 additions & 9 deletions abrpc/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def set_nr_error_handle(self, callback):
async def close(self):
self.writer.close()
# TODO: in 3.7
#await self.writer.wait_closed()
# await self.writer.wait_closed()

async def call(self, method_name, *args):
future = asyncio.Future()
Expand All @@ -41,7 +41,8 @@ async def call(self, method_name, *args):
async def call_no_response(self, method_name, *args):
if self.nr_error_handle is None:
raise Exception(
"No error handler set for no_response calls. Use set_on_error_resposen_call()")
"No error handler set for no_response calls. "
"Use set_on_error_resposen_call()")
await self._send_call(method_name, args, True)

async def serve(self, service=None):
Expand All @@ -57,11 +58,13 @@ async def serve(self, service=None):
# CALL
if message[0] == self.MESSAGE_CALL:
asyncio.ensure_future(
self._run_method(service, message[1], message[2], message[3], False))
self._run_method(
service, message[1], message[2], message[3], False))
# CALL_NO_RESPONSE
elif message[0] == self.MESSAGE_CALL_NO_RESPONSE:
asyncio.ensure_future(
self._run_method(service, message[1], message[2], message[3], True))
self._run_method(
service, message[1], message[2], message[3], True))
# RESPOSE
elif message[0] == self.MESSAGE_RESPONSE:
future = self.running_calls.pop(message[1], None)
Expand All @@ -75,7 +78,9 @@ async def serve(self, service=None):
future = None
else:
raise Exception("Invalid message (Invalid message type)")
message = None # Do not hold reference to message while waiting for new message

# Do not hold reference to message while waiting for new message
message = None

def _send_call(self, method_name, args, no_response):
assert isinstance(method_name, str)
Expand Down Expand Up @@ -115,8 +120,8 @@ async def _run_method(self, service, call_id, method_name, args, no_response):
await self._send_error(call_id, "No service registered")
else:
await self._send_error(
call_id, "Method '{}' does not exist or is not exposed on '{}'"
.format(method_name, type(service).__name__))
call_id, "Method '{}' does not exist or is not exposed on '{}'".format(
method_name, type(service).__name__))
return
try:
result = await method(*args)
Expand All @@ -128,11 +133,11 @@ async def _run_method(self, service, call_id, method_name, args, no_response):
True,
result
])
except:
except Exception:
await self._send_error(call_id, traceback.format_exc())

def _send_message(self, message):
data = msgpack.packb(message, use_bin_type=True)
self.writer.write(len(data).to_bytes(4, "big"))
self.writer.write(data)
return self.writer.drain()
return self.writer.drain()
11 changes: 5 additions & 6 deletions abrpc/utils.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
from .connection import Connection


def on_connection(callback):
async def helper(reader, writer):
#try:
connection = Connection((reader, writer))
await callback(connection)
#finally:
# await connection.close()
connection = Connection((reader, writer))
await callback(connection)
return helper


def _expose_helper(fn):
fn._abrpc_exposed = True
return fn


def expose():
return _expose_helper
return _expose_helper
1 change: 1 addition & 0 deletions examples/bidirectional/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import uvloop # Optional; for better performance
uvloop.install()


class ServerService():

@expose()
Expand Down
1 change: 1 addition & 0 deletions examples/simple_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import uvloop # Optional; for better performance
uvloop.install()


class ServerService():

@expose()
Expand Down
8 changes: 4 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import sys
import os
import pytest
import time
import asyncio

TEST_DIR = os.path.dirname(__file__)
ROOT_DIR = os.path.dirname(TEST_DIR)

sys.path.insert(0, ROOT_DIR)

from abrpc import on_connection
import asyncio
from abrpc import on_connection # noqa


@pytest.fixture()
def port():
Expand Down Expand Up @@ -39,4 +39,4 @@ def run(self, coro):
env.server.close()
loop = asyncio.get_event_loop()
loop.run_until_complete(env.server.wait_closed())
env.server = None
env.server = None
3 changes: 0 additions & 3 deletions tests/test_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,12 @@ class ServerService:
async def server_method(self):
return "data"


class ClientService:

@expose()
async def client_method(self, x, y):
return x + y


oneshot = asyncio.Future()

async def handle(conn):
Expand All @@ -107,7 +105,6 @@ async def main():
test_env.run(main())



def test_call_no_response(test_env, port):

service = MyCounterService()
Expand Down
5 changes: 3 additions & 2 deletions tests/test_connect.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import asyncio

from abrpc import on_connection, Connection
from abrpc import on_connection


def test_multiple_connect_close(port):

connections = [0]

async def handle_conn(conn):
connections[0] += 1
await conn.close()
Expand All @@ -22,4 +23,4 @@ async def main():
loop = asyncio.get_event_loop()
loop.run_until_complete(main())

assert connections[0] == 3
assert connections[0] == 3

0 comments on commit 49cddab

Please sign in to comment.