-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
69 lines (56 loc) · 1.77 KB
/
api.py
File metadata and controls
69 lines (56 loc) · 1.77 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
# -*- encoding: utf-8 -*-
import yaml, json
class Route():
def __init__(self, path, params, re, fid):
self.path = path
self.params = params
self.re = re
self.fid = fid
@property
def args(self):
return ''.join(", %d" % i for i, _ in enumerate(self.params, 1))
@property
def signature(self):
return ', '.join("std::string %s" % p for p in self.params)
class API:
from path import Path
from entity import Entity
@property
def description(self):
return self.api.get('info', {}).get('description', '')
@property
def __json__(self):
return json.dumps(self.api).replace('"', r'\"').replace(r'\n', r'\\n');
@property
def __yaml__(self):
return yaml.dump(self.api).replace('"', r'\"').replace('\n', r'\n');
def __init__(self, filename):
with open(filename) as yamlfile:
self.api = yaml.load(yamlfile)
self.paths = tuple(
sorted(
(self.Path(self, uri, yaml)
for uri, yaml in self.api.get('paths', {}).iteritems()),
key = lambda path: path.uri # UGLY! curly braces '{' '}' are heavier than letters in ascii
)
)
self.definitions = {
"#/definitions/%s" % name: self.Entity(name, yaml)
for name, yaml in self.api.get('definitions', {}).iteritems()
}
self.routes = []
for path in self.paths:
uriRe = path.uri
params = set()
for parameter in path.parameters:
if not parameter.iN('path'):
continue
uriParam = '{%s}' % parameter.name
pattern = '([^/]+)' # param['type'] == 'string' or unkonwn
if parameter.yaml.get('type') == 'integer':
pattern = r'(\\d+)'
if uriParam in path.uri:
uriRe = uriRe.replace(uriParam, pattern)
params.add(parameter.name)
fid = path.uri.replace('/', '_').replace('{', '').replace('}', '')
self.routes.append(Route(path, params, uriRe, fid))