Skip to content

Commit 5f1862e

Browse files
committed
Formatting with Black
Option: -l 80
1 parent dbc4e70 commit 5f1862e

File tree

8 files changed

+517
-289
lines changed

8 files changed

+517
-289
lines changed

jsonrpclib/SimpleJSONRPCServer.py

Lines changed: 150 additions & 73 deletions
Large diffs are not rendered by default.

jsonrpclib/__init__.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,18 @@
2626
"""
2727

2828
# Easy access to utility methods and classes
29-
from jsonrpclib.jsonrpc import Server, ServerProxy
30-
from jsonrpclib.jsonrpc import ( MultiCall, Fault,
31-
ProtocolError, AppError, TransportError )
32-
from jsonrpclib.jsonrpc import loads, dumps, load, dump
33-
from jsonrpclib.jsonrpc import jloads, jdumps
34-
import jsonrpclib.history as history
35-
import jsonrpclib.utils as utils
29+
from jsonrpclib.jsonrpc import Server, ServerProxy # noqa: F401
30+
from jsonrpclib.jsonrpc import ( # noqa: F401
31+
MultiCall,
32+
Fault,
33+
ProtocolError,
34+
AppError,
35+
TransportError,
36+
)
37+
from jsonrpclib.jsonrpc import loads, dumps, load, dump # noqa: F401
38+
from jsonrpclib.jsonrpc import jloads, jdumps # noqa: F401
39+
import jsonrpclib.history as history # noqa: F401
40+
import jsonrpclib.utils as utils # noqa: F401
3641

3742
# ------------------------------------------------------------------------------
3843

jsonrpclib/config.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class LocalClasses(dict):
4242
"""
4343
Associates local classes with their names (used in the jsonclass module)
4444
"""
45+
4546
def add(self, cls, name=None):
4647
"""
4748
Stores a local class
@@ -51,6 +52,7 @@ def add(self, cls, name=None):
5152
"""
5253
self[name or cls.__name__] = cls
5354

55+
5456
# ------------------------------------------------------------------------------
5557

5658

@@ -61,11 +63,17 @@ class Config(object):
6163
You can change serialize_method and ignore_attribute, or use
6264
the local_classes.add(class) to include "local" classes.
6365
"""
64-
def __init__(self, version=2.0, content_type="application/json-rpc",
65-
user_agent=None, use_jsonclass=True,
66-
serialize_method='_serialize',
67-
ignore_attribute='_ignore',
68-
serialize_handlers=None):
66+
67+
def __init__(
68+
self,
69+
version=2.0,
70+
content_type="application/json-rpc",
71+
user_agent=None,
72+
use_jsonclass=True,
73+
serialize_method="_serialize",
74+
ignore_attribute="_ignore",
75+
serialize_handlers=None,
76+
):
6977
"""
7078
Sets up a configuration of JSONRPClib
7179
@@ -97,9 +105,9 @@ def __init__(self, version=2.0, content_type="application/json-rpc",
97105

98106
# Default user agent
99107
if user_agent is None:
100-
user_agent = 'jsonrpclib/{0} (Python {1})'.format(
101-
__version__,
102-
'.'.join(str(ver) for ver in sys.version_info[0:3]))
108+
user_agent = "jsonrpclib/{0} (Python {1})".format(
109+
__version__, ".".join(str(ver) for ver in sys.version_info[0:3])
110+
)
103111
self.user_agent = user_agent
104112

105113
# The list of classes to use for jsonclass translation.
@@ -128,12 +136,19 @@ def copy(self):
128136
129137
:return: A shallow copy of this configuration
130138
"""
131-
new_config = Config(self.version, self.content_type, self.user_agent,
132-
self.use_jsonclass, self.serialize_method,
133-
self.ignore_attribute, None)
139+
new_config = Config(
140+
self.version,
141+
self.content_type,
142+
self.user_agent,
143+
self.use_jsonclass,
144+
self.serialize_method,
145+
self.ignore_attribute,
146+
None,
147+
)
134148
new_config.classes = self.classes.copy()
135149
new_config.serialize_handlers = self.serialize_handlers.copy()
136150
return new_config
137151

152+
138153
# Default configuration
139154
DEFAULT = Config()

jsonrpclib/history.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class History(object):
4242
each request cycle in order to keep it from clogging
4343
memory.
4444
"""
45+
4546
def __init__(self):
4647
"""
4748
Sets up members

jsonrpclib/jsonclass.py

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

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

5152
# Regex of invalid module characters
52-
INVALID_MODULE_CHARS = r'[^a-zA-Z0-9\_\.]'
53+
INVALID_MODULE_CHARS = r"[^a-zA-Z0-9\_\.]"
5354

5455
# ------------------------------------------------------------------------------
5556

@@ -58,7 +59,6 @@ class TranslationError(Exception):
5859
"""
5960
Unmarshalling exception
6061
"""
61-
pass
6262

6363

6464
def _slots_finder(clazz, fields_set):
@@ -100,8 +100,13 @@ def _find_fields(obj):
100100
return fields
101101

102102

103-
def dump(obj, serialize_method=None, ignore_attribute=None, ignore=None,
104-
config=jsonrpclib.config.DEFAULT):
103+
def dump(
104+
obj,
105+
serialize_method=None,
106+
ignore_attribute=None,
107+
ignore=None,
108+
config=jsonrpclib.config.DEFAULT,
109+
):
105110
"""
106111
Transforms the given object into a JSON-RPC compliant form.
107112
Converts beans into dictionaries with a __jsonclass__ entry.
@@ -130,8 +135,9 @@ def dump(obj, serialize_method=None, ignore_attribute=None, ignore=None,
130135
pass
131136
else:
132137
if serializer is not None:
133-
return serializer(obj, serialize_method, ignore_attribute,
134-
ignore, config)
138+
return serializer(
139+
obj, serialize_method, ignore_attribute, ignore, config
140+
)
135141

136142
# Primitive
137143
if isinstance(obj, utils.PRIMITIVE_TYPES):
@@ -140,21 +146,24 @@ def dump(obj, serialize_method=None, ignore_attribute=None, ignore=None,
140146
# Iterative
141147
elif isinstance(obj, utils.ITERABLE_TYPES):
142148
# List, set or tuple
143-
return [dump(item, serialize_method, ignore_attribute, ignore, config)
144-
for item in obj]
149+
return [
150+
dump(item, serialize_method, ignore_attribute, ignore, config)
151+
for item in obj
152+
]
145153

146154
elif isinstance(obj, utils.DictType):
147155
# Dictionary
148-
return {key: dump(value, serialize_method, ignore_attribute,
149-
ignore, config)
150-
for key, value in obj.items()}
156+
return {
157+
key: dump(value, serialize_method, ignore_attribute, ignore, config)
158+
for key, value in obj.items()
159+
}
151160

152161
# It's not a standard type, so it needs __jsonclass__
153162
module_name = inspect.getmodule(type(obj)).__name__
154163
json_class = obj.__class__.__name__
155164

156-
if module_name not in ('', '__main__'):
157-
json_class = '{0}.{1}'.format(module_name, json_class)
165+
if module_name not in ("", "__main__"):
166+
json_class = "{0}.{1}".format(module_name, json_class)
158167

159168
# Keep the class name in the returned object
160169
return_obj = {"__jsonclass__": [json_class]}
@@ -165,16 +174,16 @@ def dump(obj, serialize_method=None, ignore_attribute=None, ignore=None,
165174
# Attrs MUST be a dict.
166175
serialize = getattr(obj, serialize_method)
167176
params, attrs = serialize()
168-
return_obj['__jsonclass__'].append(params)
177+
return_obj["__jsonclass__"].append(params)
169178
return_obj.update(attrs)
170179
elif utils.is_enum(obj):
171180
# Add parameters for enumerations
172-
return_obj['__jsonclass__'].append([obj.value])
181+
return_obj["__jsonclass__"].append([obj.value])
173182
else:
174183
# Otherwise, try to figure it out
175184
# Obviously, we can't assume to know anything about the
176185
# parameters passed to __init__
177-
return_obj['__jsonclass__'].append([])
186+
return_obj["__jsonclass__"].append([])
178187

179188
# Prepare filtering lists
180189
known_types = SUPPORTED_TYPES + tuple(config.serialize_handlers)
@@ -188,14 +197,22 @@ def dump(obj, serialize_method=None, ignore_attribute=None, ignore=None,
188197
attrs = {}
189198
for attr_name in fields:
190199
attr_value = getattr(obj, attr_name)
191-
if isinstance(attr_value, known_types) and \
192-
attr_value not in ignore_list:
193-
attrs[attr_name] = dump(attr_value, serialize_method,
194-
ignore_attribute, ignore, config)
200+
if (
201+
isinstance(attr_value, known_types)
202+
and attr_value not in ignore_list
203+
):
204+
attrs[attr_name] = dump(
205+
attr_value,
206+
serialize_method,
207+
ignore_attribute,
208+
ignore,
209+
config,
210+
)
195211
return_obj.update(attrs)
196212

197213
return return_obj
198214

215+
199216
# ------------------------------------------------------------------------------
200217

201218

@@ -218,75 +235,88 @@ def load(obj, classes=None):
218235
return [load(entry) for entry in obj]
219236

220237
# Otherwise, it's a dict type
221-
elif '__jsonclass__' not in obj:
238+
elif "__jsonclass__" not in obj:
222239
return {key: load(value) for key, value in obj.items()}
223240

224241
# It's a dictionary, and it has a __jsonclass__
225-
orig_module_name = obj['__jsonclass__'][0]
226-
params = obj['__jsonclass__'][1]
242+
orig_module_name = obj["__jsonclass__"][0]
243+
params = obj["__jsonclass__"][1]
227244

228245
# Validate the module name
229246
if not orig_module_name:
230-
raise TranslationError('Module name empty.')
247+
raise TranslationError("Module name empty.")
231248

232-
json_module_clean = re.sub(INVALID_MODULE_CHARS, '', orig_module_name)
249+
json_module_clean = re.sub(INVALID_MODULE_CHARS, "", orig_module_name)
233250
if json_module_clean != orig_module_name:
234-
raise TranslationError('Module name {0} has invalid characters.'
235-
.format(orig_module_name))
251+
raise TranslationError(
252+
"Module name {0} has invalid characters.".format(orig_module_name)
253+
)
236254

237255
# Load the class
238-
json_module_parts = json_module_clean.split('.')
256+
json_module_parts = json_module_clean.split(".")
239257
if classes and len(json_module_parts) == 1:
240258
# Local class name -- probably means it won't work
241259
try:
242260
json_class = classes[json_module_parts[0]]
243261
except KeyError:
244-
raise TranslationError('Unknown class or module {0}.'
245-
.format(json_module_parts[0]))
262+
raise TranslationError(
263+
"Unknown class or module {0}.".format(json_module_parts[0])
264+
)
246265
else:
247266
# Module + class
248267
json_class_name = json_module_parts.pop()
249-
json_module_tree = '.'.join(json_module_parts)
268+
json_module_tree = ".".join(json_module_parts)
250269
try:
251270
# Use fromlist to load the module itself, not the package
252-
temp_module = __import__(json_module_tree,
253-
fromlist=[json_class_name])
271+
temp_module = __import__(
272+
json_module_tree, fromlist=[json_class_name]
273+
)
254274
except ImportError:
255-
raise TranslationError('Could not import {0} from module {1}.'
256-
.format(json_class_name, json_module_tree))
275+
raise TranslationError(
276+
"Could not import {0} from module {1}.".format(
277+
json_class_name, json_module_tree
278+
)
279+
)
257280

258281
try:
259282
json_class = getattr(temp_module, json_class_name)
260283
except AttributeError:
261-
raise TranslationError("Unknown class {0}.{1}."
262-
.format(json_module_tree, json_class_name))
284+
raise TranslationError(
285+
"Unknown class {0}.{1}.".format(
286+
json_module_tree, json_class_name
287+
)
288+
)
263289

264290
# Create the object
265291
if isinstance(params, utils.ListType):
266292
try:
267293
new_obj = json_class(*params)
268294
except TypeError as ex:
269-
raise TranslationError("Error instantiating {0}: {1}"
270-
.format(json_class.__name__, ex))
295+
raise TranslationError(
296+
"Error instantiating {0}: {1}".format(json_class.__name__, ex)
297+
)
271298
elif isinstance(params, utils.DictType):
272299
try:
273300
new_obj = json_class(**params)
274301
except TypeError as ex:
275-
raise TranslationError("Error instantiating {0}: {1}"
276-
.format(json_class.__name__, ex))
302+
raise TranslationError(
303+
"Error instantiating {0}: {1}".format(json_class.__name__, ex)
304+
)
277305
else:
278-
raise TranslationError("Constructor args must be a dict or a list, "
279-
"not {0}".format(type(params).__name__))
306+
raise TranslationError(
307+
"Constructor args must be a dict or a list, "
308+
"not {0}".format(type(params).__name__)
309+
)
280310

281311
# Remove the class information, as it must be ignored during the
282312
# reconstruction of the object
283-
raw_jsonclass = obj.pop('__jsonclass__')
313+
raw_jsonclass = obj.pop("__jsonclass__")
284314

285315
for key, value in obj.items():
286316
# Recursive loading
287317
setattr(new_obj, key, load(value, classes))
288318

289319
# Restore the class information for further usage
290-
obj['__jsonclass__'] = raw_jsonclass
320+
obj["__jsonclass__"] = raw_jsonclass
291321

292322
return new_obj

0 commit comments

Comments
 (0)