all: fix all the arguments

This commit is contained in:
2018-12-12 15:15:26 +01:00
parent cf16476a0f
commit 23f054d3b8
2 changed files with 49 additions and 9 deletions

View File

@@ -32,13 +32,46 @@ def mkname(method, name, params):
return name
def mkparam(param):
param = inflection.underscore(param)
if param in reserved:
return "{}_".format(param)
def mkparam(param, data):
if '$ref' in param:
keys = param['$ref'][2:].split("/")
param = data
for key in keys:
param = param[key]
name = param["name"]
name = inflection.underscore(name)
if name in reserved:
name = "{}_".format(name)
param["name"] = name
return param
def mkkwargs(params):
for param in params:
name = param['name']
if param['required']:
yield name
for param in params:
name = param['name']
if not param['required']:
yield "{}=None".format(name)
def getparams(params, where):
ps = []
for param in params:
if param['in'] == where:
ps.append(param['name'])
return ps
def mkparams(params, where):
ps = getparams(params, where)
if not ps:
return None
return '{{\n{}\n }}'.format(',\n'.join(' "{}": {}'.format(p, p) for p in ps))
def pretty_print(name, data):
info = data['info']
r = re.compile('\\/\\{([^\\}]*)\\}')
@@ -50,13 +83,19 @@ def pretty_print(name, data):
for method, body in fns.items():
method = method.lower()
fmt = r2.sub('{}', path)
params = [mkparam(param) for param in r.findall(path)]
paramdef = [mkparam(param, data) for param in body.get('parameters', [])]
name = r.sub('', path).replace("/", "_").replace('-', '_')[1:]
params = [p["name"] for p in paramdef]
name = mkname(method, name, params)
params = ", {}".format(", ".join(params)) if params else ''
params = ", {}".format(", ".join(mkkwargs(paramdef))) if params else ''
print(' def {}(self{}):'.format(name, params))
print(' """{}"""'.format(', '.join(body['tags'])))
print(' return self.{}("{}"{})\n'.format(method, fmt, params))
print(' """{}"""'.format(body.get("summary", "")))
print(' data = {}'.format(mkparams(paramdef, 'body')))
print(' params = {}'.format(mkparams(paramdef, 'query')))
tpl = ' return self.{}("{}"{}, data=data, params=params)\n'
args = getparams(paramdef, 'path')
args = '.format({})'.format(', '.join(args)) if args else ''
print(tpl.format(method, fmt, args))
def generate(name, url):