forked from oneyoung/python-ipernity-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
75 lines (61 loc) · 2.09 KB
/
utils.py
File metadata and controls
75 lines (61 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from ipernity_api import rest
def dict2code(d, indent=''):
''' convert json decoded dict into python literal code
Parameters:
d: dict to be converted
indent: optinal parameter for base indent of code segment
'''
tab = ' '
prefix = indent # tab indent mark
code = '' # result
for c in str(d):
if c == '{':
code += '{\n' + prefix
prefix += tab
elif c == '}':
code += '\n' + prefix + '}\n' + prefix
prefix = prefix[:-len(tab)]
else:
code += c
return code
def get_methods_list():
''' return all the methods' name as a list '''
resp = rest.call_api('api.methods.getList')
methods = [m['name'] for m in resp['methods']['method']]
methods.sort()
return methods
def get_methods_info():
''' get detail about each method, and return a dict with method name as key '''
info = {}
for method in get_methods_list():
resp = rest.call_api('api.methods.get', http_post=False, method=method)
m = resp['method']
m = convert_info(m)
info[m['name']] = m
return info
def convert_info(m):
''' some preprocess of method info '''
# 'error' section contains duplicate much information, and once api call
# failure, ipernity would response with error message, so not need to keep
# such info.
try:
m.pop('errors')
m.pop('changelog') # changelog also unnecessary
except KeyError:
pass
# some value are string, need to convert into int
m['authentication'] = {k: int(v) if v.isdigit() else v
for k, v in m['authentication'].items()}
for param in m['parameters']:
param['required'] = int(param.get('required', 0))
return m
def create_methods_file(filename='methods.py'):
''' save methods info to file '''
methods_info = get_methods_info()
code = dict2code(methods_info)
buf = "__methods__ = %s" % code
f = open(filename, 'w')
f.write(buf)
f.close()
if __name__ == '__main__':
create_methods_file('ipernity_api/methods.py')