Permalink
Browse files

Clear PEP-8 errors

  • Loading branch information...
1 parent 9f3f06c commit d623bef52d3e500269b8e557e8ed426f956f0005 Tadeas Moravec committed Dec 29, 2012
Showing with 49 additions and 35 deletions.
  1. +1 −0 examples/client.py
  2. +5 −4 fastjsonrpc/client.py
  3. +16 −7 fastjsonrpc/jsonrpc.py
  4. +5 −5 fastjsonrpc/server.py
  5. +1 −0 tests/dummyserver.py
  6. +0 −1 tests/test_client.py
  7. +9 −7 tests/test_jsonrpc.py
  8. +12 −11 tests/test_server.py
View
1 examples/client.py
@@ -7,6 +7,7 @@
from fastjsonrpc.client import Proxy
+
def printValue(value):
print "Result: %s" % str(value)
View
9 fastjsonrpc/client.py
@@ -34,6 +34,7 @@
import jsonrpc
+
class ReceiverProtocol(Protocol):
"""
Protocol for receiving the server response. It's only purpose is to get the
@@ -101,8 +102,8 @@ def stopProducing(self):
class Proxy(object):
"""
A proxy to one specific JSON-RPC server. Pass the server URL to the
- constructor and call proxy.callRemote('method', *args) to call 'method' with
- *args.
+ constructor and call proxy.callRemote('method', *args) to call 'method'
+ with *args.
"""
def __init__(self, url, version=jsonrpc.VERSION_1):
@@ -126,8 +127,8 @@ def bodyFromResponse(self, response):
@param response: Response object from the call
@rtype: t.i.d.Deferred
- @return: Deferred, that will fire callback with body of the response (as
- string)
+ @return: Deferred, that will fire callback with body of the response
+ (as string)
"""
finished = Deferred()
View
23 fastjsonrpc/jsonrpc.py
@@ -49,7 +49,8 @@
INTERNAL_ERROR = -32603
ID_MIN = 1
-ID_MAX = 2**31 - 1 # 32-bit maxint
+ID_MAX = 2 ** 31 - 1 # 32-bit maxint
+
def jdumps(obj):
"""
@@ -66,6 +67,7 @@ def jdumps(obj):
else:
return json.dumps(obj)
+
def jloads(json_string):
"""
Decode JSON no matter what library did we import
@@ -81,20 +83,21 @@ def jloads(json_string):
else:
return json.loads(json_string)
+
def encodeRequest(method, args=None, id_=0, version=VERSION_1):
"""
Return a JSON object representation of the request.
@type id_: int or None
@param id_: request ID. If None, a notification will be sent. If 0 (the
- default), we'll coin some random.
+ default), we'll coin some random.
@type method: str
@param method: Method name
@type args: list
@param args: List of arguments for the method. Note that packing of the
- arguments is up to the caller!
+ arguments is up to the caller!
@type version: float
@param version: Which JSON-RPC version to use? Defaults to version 1.
@@ -120,6 +123,7 @@ def encodeRequest(method, args=None, id_=0, version=VERSION_1):
return jdumps(request)
+
def decodeResponse(json_response):
"""
Parse the response JSON and return what the called function returned. Raise
@@ -162,6 +166,7 @@ def decodeResponse(json_response):
raise ValueError('Not a valid JSON-RPC response')
+
def decodeRequest(request):
"""
Decodes the JSON encoded request.
@@ -171,7 +176,7 @@ def decodeRequest(request):
@rtype: mixed
@return: Whatever the client sent, most probably a list (in the case of
- a batch request) or dict (in the case of a single method call).
+ a batch request) or dict (in the case of a single method call).
@raise JSONRPCError: If there's error in parsing.
"""
@@ -183,10 +188,12 @@ def decodeRequest(request):
return decoded
+
def verifyMethodCall(request):
"""
Verifies a single method call. We call this for every method in case of a
- batch request. If there are any params missing and we don't need it, add it.
+ batch request. If there are any params missing and we don't need it, add
+ it.
@type request: dict
@param request: Decoded request from user
@@ -195,8 +202,8 @@ def verifyMethodCall(request):
@return: What we got, possibly with some keys more.
@raise JSONRPCError: If there's anything wrong with the request, raise
- JSONRPCError with description of what's wrong. Attempts to add to it as much
- information as possible (id, version).
+ JSONRPCError with description of what's wrong. Attempts to add to it
+ as much information as possible (id, version).
"""
try:
# 'jsonrpc' is only contained in V2 requests
@@ -237,6 +244,7 @@ def verifyMethodCall(request):
else:
raise JSONRPCError(e.strerror, e.errno)
+
def _getErrorResponse(exception):
"""
Parses Exception into a dict, that can be encoded to JSON.
@@ -270,6 +278,7 @@ def _getErrorResponse(exception):
return error_result
+
def prepareMethodResponse(result, id_=None, version=VERSION_1):
"""
Add all info we have to the response, prepare it to be serialized.
View
10 fastjsonrpc/server.py
@@ -29,6 +29,7 @@
import jsonrpc
+
class JSONRPCServer(resource.Resource):
"""
JSON-RPC server. Subclass this, implement your own methods and publish
@@ -107,16 +108,16 @@ def _callMethod(self, request_dict):
def render(self, request):
"""
- This is the 'main' RPC method. This will always be called when a request
- arrives and it's up to this method to parse the request and
+ This is the 'main' RPC method. This will always be called when
+ a request arrives and it's up to this method to parse the request and
dispatch it further.
@type request: t.w.s.Request
@param request: Request from client
@rtype: some constant :-)
- @return: NOT_DONE_YET signalizing, that there's Deferred, that will take
- care about sending the response.
+ @return: NOT_DONE_YET signalizing, that there's Deferred, that will
+ take care about sending the response.
"""
try:
@@ -187,4 +188,3 @@ def _sendResponse(self, response, request):
request.write(response)
request.finish()
-
View
1 tests/dummyserver.py
@@ -5,6 +5,7 @@
from twisted.enterprise import adbapi
from fastjsonrpc.server import JSONRPCServer
+
class DummyServer(JSONRPCServer):
def jsonrpc_echo(self, data):
View
1 tests/test_client.py
@@ -241,4 +241,3 @@ def finished(result):
e.addCallback(finished)
return d
-
View
16 tests/test_jsonrpc.py
@@ -8,6 +8,7 @@
from fastjsonrpc.jsonrpc import JSONRPCError
from twisted.trial.unittest import TestCase
+
class TestEncodeRequest(TestCase):
def test_noArgs(self):
@@ -72,14 +73,15 @@ def test_methodArgsId(self):
def test_methodArgsVersion2(self):
result = jsonrpc.encodeRequest('method', 'abcdef', version=2)
- pattern = '\{"jsonrpc": "2.0", "params": "abcdef", "method": "method", '
- pattern += '"id": \d+\}'
+ pattern = '\{"jsonrpc": "2.0", "params": "abcdef", "method": '
+ pattern += '"method", "id": \d+\}'
self.assertTrue(re.match(pattern, result))
def test_all(self):
- result = jsonrpc.encodeRequest('method', 'abcdef', id_=123, version=2.0)
- expected = '{"jsonrpc": "2.0", "params": "abcdef", "method": "method", '
- expected += '"id": 123}'
+ result = jsonrpc.encodeRequest('method', 'abcdef', id_=123,
+ version=2.0)
+ expected = '{"jsonrpc": "2.0", "params": "abcdef", "method": '
+ expected += '"method", "id": 123}'
self.assertEquals(result, expected)
@@ -241,7 +243,8 @@ def test_invalidParams(self):
self.assertEquals(result, expected)
def test_methodNotFount(self):
- response = JSONRPCError('Method aa not found', jsonrpc.METHOD_NOT_FOUND)
+ response = JSONRPCError('Method aa not found',
+ jsonrpc.METHOD_NOT_FOUND)
result = jsonrpc.prepareMethodResponse(response, 123)
expected = {"result": None, "id": 123,
"error": {"message": "Method aa not found",
@@ -303,7 +306,6 @@ def test_errorAndResultNullExceptionDetails(self):
self.assertEquals(e.errno, 123)
self.assertEquals(e.version, jsonrpc.VERSION_1)
-
def test_errorAndResult(self):
response = '{"error": {"message": "some error", "code": 123}, '
response += '"result": "abcd"}'
View
23 tests/test_server.py
@@ -10,6 +10,7 @@
from dummyserver import DummyServer
+
def _render(resource, request):
result = resource.render(request)
if isinstance(result, str):
@@ -130,8 +131,8 @@ def test_caseSensitiveMethodV1(self):
d = _render(self.srv, request)
def rendered(_):
- expected = '{"result": null, "id": "ABCD", "error": {"message": ' +\
- '"Method ECHO not found", "code": -32601}}'
+ expected = '{"result": null, "id": "ABCD", "error": {' + \
+ '"message": "Method ECHO not found", "code": -32601}}'
self.assertEquals(request.written[0], expected)
d.addCallback(rendered)
@@ -353,8 +354,8 @@ def test_keywordsUnexpected(self):
def rendered(_):
expected = '{"result": null, "id": 1, "error": {"message": ' + \
- '"jsonrpc_echo() got an unexpected keyword argument ' + \
- '\'wrongname\'", "code": -32602}}'
+ '"jsonrpc_echo() got an unexpected keyword argument' + \
+ ' \'wrongname\'", "code": -32602}}'
self.assertEquals(request.written[0], expected)
@@ -363,7 +364,7 @@ def rendered(_):
def test_batch(self):
json = '[{"method": "echo", "id": 1, "params": {"data": "arg"}}, ' + \
- '{"method": "echo", "id": 2, "params": {"data": "arg"}}]'
+ '{"method": "echo", "id": 2, "params": {"data": "arg"}}]'
request = DummyRequest([''])
request.content = StringIO(json)
d = _render(self.srv, request)
@@ -378,7 +379,7 @@ def rendered(_):
def test_batchNotificationOnly(self):
json = '[{"method": "echo", "params": {"data": "arg"}}, ' + \
- '{"method": "echo", "params": {"data": "arg"}}]'
+ '{"method": "echo", "params": {"data": "arg"}}]'
request = DummyRequest([''])
request.content = StringIO(json)
d = _render(self.srv, request)
@@ -392,7 +393,7 @@ def rendered(_):
def test_batchNotificationMixed(self):
json = '[{"method": "echo", "id": 1, "params": {"data": "arg"}}, ' + \
'{"method": "echo", "id": 2, "params": {"data": "arg"}}, ' + \
- '{"method": "echo", "params": {"data": "arg"}}]'
+ '{"method": "echo", "params": {"data": "arg"}}]'
request = DummyRequest([''])
request.content = StringIO(json)
d = _render(self.srv, request)
@@ -407,15 +408,15 @@ def rendered(_):
def test_batchV1V2(self):
json = '[{"method": "echo", "id": 1, "params": ["arg"]}, ' + \
- '{"method": "echo", "id": "abc", "params": ["arg"], ' + \
- '"jsonrpc": "2.0"}]'
+ '{"method": "echo", "id": "abc", "params": ["arg"], ' + \
+ '"jsonrpc": "2.0"}]'
request = DummyRequest([''])
request.content = StringIO(json)
d = _render(self.srv, request)
def rendered(_):
expected = '[{"error": null, "id": 1, "result": "arg"}, ' + \
- '{"jsonrpc": "2.0", "id": "abc", "result": "arg"}]'
+ '{"jsonrpc": "2.0", "id": "abc", "result": "arg"}]'
self.assertEquals(request.written[0], expected)
d.addCallback(rendered)
@@ -436,7 +437,7 @@ def rendered(_):
def test_batchNotificationAndSingle(self):
json = '[{"method": "echo", "id": 1, "params": ["arg"]}, ' + \
- '{"method": "echo", "params": ["arg"]}]'
+ '{"method": "echo", "params": ["arg"]}]'
request = DummyRequest([''])
request.content = StringIO(json)
d = _render(self.srv, request)

0 comments on commit d623bef

Please sign in to comment.