Skip to content

Commit 35ea1dc

Browse files
committed
Generic Refactoring to utilise str.format()
Refactored the codebase to exchange string concatenation with str.format() expressions. Also changed old formatting expressions using "%s" with the same. Have not done the same with special cases including hex numbers or in the case of hydrus/hydraspec/legacy files.
1 parent a066cc4 commit 35ea1dc

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

openapi_parser.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def get_class_details(global_: Dict[str,
235235
write=True))
236236
else:
237237
global_[class_name]["prop_definition"].append(
238-
HydraClassProp("vocab:" + prop, prop, required=flag,
238+
HydraClassProp("vocab:".format(prop), prop, required=flag,
239239
read=True, write=True))
240240
else:
241241
global_[class_name]["prop_definition"].append(HydraClassProp(
@@ -342,8 +342,7 @@ def check_for_ref(global_: Dict[str, Any],
342342
pass
343343
# cannot parse because no external ref
344344

345-
print("Cannot parse path" + path +
346-
" because no ref to local class provided")
345+
print("Cannot parse path {} because no ref to local class provided".format(path))
347346
return ""
348347

349348

@@ -383,8 +382,7 @@ def get_parameters(global_: Dict[str, Any],
383382
# check if class has been pared
384383
if parameter["schema"]["$ref"].split(
385384
'/')[2] in global_["class_names"]:
386-
param = "vocab:" + \
387-
parameter["schema"]["$ref"].split('/')[2]
385+
param = "vocab:{}".format(parameter["schema"]["$ref"].split('/')[2])
388386

389387
else:
390388
# if not go to that location and parse and add
@@ -394,8 +392,7 @@ def get_parameters(global_: Dict[str, Any],
394392
parameter["schema"]["$ref"]),
395393
parameter["schema"]["$ref"].split('/')[2],
396394
path=path)
397-
param = "vocab:" + \
398-
parameter["schema"]["$ref"].split('/')[2]
395+
param = "vocab:{}".format(parameter["schema"]["$ref"].split('/')[2])
399396
except KeyError:
400397
param = ""
401398

@@ -430,15 +427,14 @@ def get_ops(global_: Dict[str, Any], path: str,
430427
response),
431428
"description": responses[response]["description"]})
432429
try:
433-
op_returns = "vocab:" + \
434-
responses[response]["schema"]["$ref"].split('/')[2]
430+
op_returns = "vocab:{}".format(
431+
responses[response]["schema"]["$ref"].split('/')[2])
435432
except KeyError:
436433
pass
437434
if op_returns is None:
438435
try:
439-
op_returns = "vocab:" + \
440-
responses[response]["schema"]["items"]["$ref"].\
441-
split('/')[2]
436+
op_returns = "vocab:{}".format(
437+
responses[response]["schema"]["items"]["$ref"].split('/')[2])
442438
except KeyError:
443439
op_returns = try_catch_replacement(
444440
responses[response]["schema"], "type", None)
@@ -451,7 +447,7 @@ def get_ops(global_: Dict[str, Any], path: str,
451447
global_[class_name]["op_definition"].append(HydraClassOp(
452448
op_name, op_method.upper(), op_expects, op_returns, op_status))
453449
else:
454-
print("Method on path" + path + " already present !")
450+
print("Method on path {} already present !".format(path))
455451

456452

457453
def get_paths(global_: Dict[str, Any]) -> None:
@@ -491,7 +487,7 @@ def parse(doc: Dict[str, Any]) -> Dict[str, Any]:
491487
baseURL = try_catch_replacement(doc, "host", "localhost")
492488
name = try_catch_replacement(doc, "basePath", "api")
493489
schemes = try_catch_replacement(doc, "schemes", "http")
494-
api_doc = HydraDoc(name, title, desc, name, schemes[0] + "://" + baseURL)
490+
api_doc = HydraDoc(name, title, desc, name, "{}://{}".format(schemes[0],baseURL))
495491
get_paths(global_)
496492
for name in global_["class_names"]:
497493
for prop in global_[name]["prop_definition"]:
@@ -521,8 +517,8 @@ def dump_documentation(hydra_doc: Dict[str, Any]) -> str:
521517
"""
522518
dump = json.dumps(hydra_doc, indent=4, sort_keys=True)
523519
hydra_doc = '''"""\nGenerated API Documentation for Server API using
524-
server_doc_gen.py."""\n\ndoc = %s''' % dump
525-
hydra_doc = hydra_doc + '\n'
520+
server_doc_gen.py."""\n\ndoc = {}'''.format(dump)
521+
hydra_doc = '{}\n'.format(hydra_doc)
526522
hydra_doc = hydra_doc.replace('true', '"true"')
527523
hydra_doc = hydra_doc.replace('false', '"false"')
528524
hydra_doc = hydra_doc.replace('null', '"null"')

0 commit comments

Comments
 (0)