Skip to content

Commit 37c0d73

Browse files
committed
Small code review
Constants in jsonrpclib.utils have been renamed to respect PEP-8 naming conventions: string_types => STRING_TYPES, ...
1 parent 765d7fb commit 37c0d73

File tree

4 files changed

+27
-30
lines changed

4 files changed

+27
-30
lines changed

jsonrpclib/SimpleJSONRPCServer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def validate_request(request, json_config):
121121
params = request.get('params')
122122
param_types = (utils.ListType, utils.DictType, utils.TupleType)
123123

124-
if not method or not isinstance(method, utils.string_types) or \
124+
if not method or not isinstance(method, utils.STRING_TYPES) or \
125125
not isinstance(params, param_types):
126126
# Invalid type of method name or parameters
127127
fault = Fault(-32600, 'Invalid request parameters or method.',

jsonrpclib/jsonclass.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
# ------------------------------------------------------------------------------
4646

4747
# Supported transmitted code
48-
SUPPORTED_TYPES = (utils.DictType,) + utils.iterable_types \
49-
+ utils.primitive_types
48+
SUPPORTED_TYPES = (utils.DictType,) + utils.ITERABLE_TYPES \
49+
+ utils.PRIMITIVE_TYPES
5050

5151
# Regex of invalid module characters
5252
INVALID_MODULE_CHARS = r'[^a-zA-Z0-9\_\.]'
@@ -134,11 +134,11 @@ def dump(obj, serialize_method=None, ignore_attribute=None, ignore=None,
134134
ignore, config)
135135

136136
# Primitive
137-
if isinstance(obj, utils.primitive_types):
137+
if isinstance(obj, utils.PRIMITIVE_TYPES):
138138
return obj
139139

140140
# Iterative
141-
elif isinstance(obj, utils.iterable_types):
141+
elif isinstance(obj, utils.ITERABLE_TYPES):
142142
# List, set or tuple
143143
return [dump(item, serialize_method, ignore_attribute, ignore, config)
144144
for item in obj]
@@ -207,11 +207,11 @@ def load(obj, classes=None):
207207
:return: The loaded object
208208
"""
209209
# Primitive
210-
if isinstance(obj, utils.primitive_types):
210+
if isinstance(obj, utils.PRIMITIVE_TYPES):
211211
return obj
212212

213213
# List, set or tuple
214-
elif isinstance(obj, utils.iterable_types):
214+
elif isinstance(obj, utils.ITERABLE_TYPES):
215215
# This comes from a JSON parser, so it can only be a list...
216216
return [load(entry) for entry in obj]
217217

@@ -234,15 +234,13 @@ def load(obj, classes=None):
234234

235235
# Load the class
236236
json_module_parts = json_module_clean.split('.')
237-
json_class = None
238237
if classes and len(json_module_parts) == 1:
239238
# Local class name -- probably means it won't work
240239
try:
241240
json_class = classes[json_module_parts[0]]
242241
except KeyError:
243242
raise TranslationError('Unknown class or module {0}.'
244243
.format(json_module_parts[0]))
245-
246244
else:
247245
# Module + class
248246
json_class_name = json_module_parts.pop()
@@ -262,21 +260,18 @@ def load(obj, classes=None):
262260
.format(json_module_tree, json_class_name))
263261

264262
# Create the object
265-
new_obj = None
266263
if isinstance(params, utils.ListType):
267264
try:
268265
new_obj = json_class(*params)
269266
except TypeError as ex:
270267
raise TranslationError("Error instantiating {0}: {1}"
271268
.format(json_class.__name__, ex))
272-
273269
elif isinstance(params, utils.DictType):
274270
try:
275271
new_obj = json_class(**params)
276272
except TypeError as ex:
277273
raise TranslationError("Error instantiating {0}: {1}"
278274
.format(json_class.__name__, ex))
279-
280275
else:
281276
raise TranslationError("Constructor args must be a dict or a list, "
282277
"not {0}".format(type(params).__name__))

jsonrpclib/jsonrpc.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,12 @@ def jloads(json_string):
131131
to a Python object, using cjson.
132132
"""
133133
return cjson.decode(json_string)
134-
135134
except ImportError:
136135
# pylint: disable=F0401,E0611
137136
# Use json or simplejson
138137
try:
139138
import json
140139
_logger.debug("Using json as JSON library")
141-
142140
except ImportError:
143141
try:
144142
import simplejson as json
@@ -156,7 +154,6 @@ def jdumps(obj, encoding='utf-8'):
156154
"""
157155
# Python 2 (explicit encoding)
158156
return json.dumps(obj, encoding=encoding)
159-
160157
else:
161158
# Python 3
162159
def jdumps(obj, encoding='utf-8'):
@@ -223,7 +220,8 @@ def feed(self, data):
223220
"""
224221
self.target.feed(data)
225222

226-
def close(self):
223+
@staticmethod
224+
def close():
227225
"""
228226
Does nothing
229227
"""
@@ -428,7 +426,8 @@ def send_content(self, connection, request_body):
428426
if request_body:
429427
connection.send(request_body)
430428

431-
def getparser(self):
429+
@staticmethod
430+
def getparser():
432431
"""
433432
Create an instance of the parser, and attach it to an unmarshalling
434433
object. Return both objects.
@@ -594,7 +593,6 @@ def __call__(self, attr):
594593
"""
595594
if attr == "close":
596595
return self.__close
597-
598596
elif attr == "transport":
599597
return self.__transport
600598

@@ -763,7 +761,8 @@ def __init__(self, results):
763761
"""
764762
self.results = results
765763

766-
def __get_result(self, item):
764+
@staticmethod
765+
def __get_result(item):
767766
"""
768767
Checks for error and returns the "real" result stored in a MultiCall
769768
result.
@@ -958,7 +957,7 @@ def request(self, method, params=None):
958957
:param params: Method parameters
959958
:return: A JSON-RPC request dictionary
960959
"""
961-
if not isinstance(method, utils.string_types):
960+
if not isinstance(method, utils.STRING_TYPES):
962961
raise ValueError('Method name must be a string.')
963962

964963
if not self.id:
@@ -1056,7 +1055,7 @@ def dump(params=None, methodname=None, rpcid=None, version=None,
10561055
if is_response:
10571056
valid_params.append(type(None))
10581057

1059-
if isinstance(methodname, utils.string_types) and \
1058+
if isinstance(methodname, utils.STRING_TYPES) and \
10601059
not isinstance(params, tuple(valid_params)):
10611060
"""
10621061
If a method, and params are not in a listish or a Fault,
@@ -1073,7 +1072,7 @@ def dump(params=None, methodname=None, rpcid=None, version=None,
10731072
# pylint: disable=E1103
10741073
return payload.error(params.faultCode, params.faultString, params.data)
10751074

1076-
if not isinstance(methodname, utils.string_types) and not is_response:
1075+
if not isinstance(methodname, utils.STRING_TYPES) and not is_response:
10771076
# Neither a request nor a response
10781077
raise ValueError('Method name must be a string, or is_response '
10791078
'must be set to True.')

jsonrpclib/utils.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,18 @@
4040

4141
if sys.version_info[0] < 3:
4242
# Python 2
43+
# pylint: disable=E1101
4344
import types
4445
try:
45-
string_types = (
46+
STRING_TYPES = (
4647
types.StringType,
4748
types.UnicodeType
4849
)
4950
except NameError:
5051
# Python built without unicode support
51-
string_types = (types.StringType,)
52+
STRING_TYPES = (types.StringType,)
5253

53-
numeric_types = (
54+
NUMERIC_TYPES = (
5455
types.IntType,
5556
types.LongType,
5657
types.FloatType
@@ -60,6 +61,7 @@ def to_bytes(string):
6061
"""
6162
Converts the given string into bytes
6263
"""
64+
# pylint: disable=E0602
6365
if type(string) is unicode:
6466
return str(string)
6567
return string
@@ -74,12 +76,13 @@ def from_bytes(data):
7476

7577
else:
7678
# Python 3
77-
string_types = (
79+
# pylint: disable=E1101
80+
STRING_TYPES = (
7881
bytes,
7982
str
8083
)
8184

82-
numeric_types = (
85+
NUMERIC_TYPES = (
8386
int,
8487
float
8588
)
@@ -108,15 +111,15 @@ def from_bytes(data):
108111
ListType = list
109112
TupleType = tuple
110113

111-
iterable_types = (
114+
ITERABLE_TYPES = (
112115
list,
113116
set, frozenset,
114117
tuple
115118
)
116119

117-
value_types = (
120+
VALUE_TYPES = (
118121
bool,
119122
type(None)
120123
)
121124

122-
primitive_types = string_types + numeric_types + value_types
125+
PRIMITIVE_TYPES = STRING_TYPES + NUMERIC_TYPES + VALUE_TYPES

0 commit comments

Comments
 (0)