forked from morepath/morepath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_path_tool.py
More file actions
386 lines (285 loc) · 9.18 KB
/
test_path_tool.py
File metadata and controls
386 lines (285 loc) · 9.18 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
from __future__ import print_function
import argparse
import inspect
from dectate import Query
from dectate.tool import parse_app_class # XXX implementation detail
from morepath.directive import ViewAction, PathAction, MountAction
import morepath
def path_tool(app_class):
"""Command-line query tool for Morepath path information.
Displays information about all paths generated by a Morepath
app, including points of definition.
usage: morepath_paths [-h] [--app APP]
param app_class: the root :class:`App` subclass to query by default.
"""
parser = argparse.ArgumentParser(description="Query Morepath paths")
parser.add_argument('--app', help="Dotted name for App subclass.",
type=parse_app_class)
args, filters = parser.parse_known_args()
if args.app:
app_class = args.app
for line in path_tool_output(app_class):
print(line)
def max_length(infos, name):
return max([len(d[name]) for d in infos])
def path_tool_output(app_class):
infos = get_path_and_view_info(app_class)
for info in infos:
if 'predicates' not in info:
predicates_s = ''
else:
predicates_s = ','.join(
['%s=%s' % (name, value)
for name, value in sorted(info['predicates'].items())])
info['predicates_s'] = predicates_s
max_path_length = max_length(infos, 'path')
max_predicates_s_length = max_length(infos, 'predicates_s')
max_directive_length = max_length(infos, 'directive')
max_path_length = max([max_path_length, max_predicates_s_length])
t_path = ("{path:<{max_path_length}} "
"{directive:<{max_directive_length}} "
"{filelineno}")
t_view = ("{predicates:<{max_path_length}} "
"{directive:<{max_directive_length}} "
"{filelineno}")
for info in infos:
if 'predicates' in info:
info['predicates'] = ','.join(
['%s=%s' % (name, value)
for name, value in sorted(info['predicates'].items())])
yield t_view.format(
max_path_length=max_path_length,
max_directive_length=max_directive_length,
**info)
else:
yield t_path.format(
max_path_length=max_path_length,
max_directive_length=max_directive_length,
**info)
def get_path_and_view_info(app_class):
result = []
for action, path in get_path_and_view_actions(app_class):
directive = action.directive
d = {'directive': directive.directive_name,
'filelineno': directive.code_info.filelineno(),
'path': path}
if isinstance(action, ViewAction):
d['predicates'] = action.predicates
result.append(d)
result.sort(key=lambda d: (
d['path'], d['directive'] not in ['path', 'mount']))
return result
def get_path_and_view_actions(app_class, base_path=''):
model_to_view = {}
q = Query(ViewAction)
for action, f in q(app_class):
model_to_view.setdefault(action.model, []).append(action)
for action, path in get_path_actions(app_class, base_path):
yield action, path
if isinstance(action, MountAction):
for sub_action, sub_path in get_path_and_view_actions(
action.app, path):
yield sub_action, sub_path
continue
for view_action, view_path in get_view_actions(app_class, path,
model_to_view,
action.model):
yield view_action, view_path
def get_path_actions(app_class, base_path):
q = Query(PathAction)
for action, f in q(app_class):
path = '/'.join([base_path, normalize_path(action.path)])
yield action, path
def get_view_actions(app_class, base_path, model_to_view, model):
view_actions = []
for class_ in inspect.getmro(model):
view_actions.extend(model_to_view.get(class_, []))
for view_action in view_actions:
name = view_action.predicates.get('name', '')
path = base_path
if name:
path = path + '/+' + name
yield view_action, path
def restrict(infos, names):
result = []
for info in infos:
d = {}
for name in names:
try:
d[name] = info[name]
except KeyError:
pass
result.append(d)
return result
def normalize_path(path):
if path.startswith('/'):
path = path[1:]
if path.endswith('/'):
path = path[:-1]
return path
def test_one_app():
class App(morepath.App):
pass
class A(object):
pass
@App.path(path='/foo', model=A)
def get_a():
return A()
App.commit()
infos = get_path_and_view_info(App)
infos = restrict(infos, ['path', 'directive'])
assert infos == [{'path': '/foo', 'directive': 'path'}]
def test_app_variables():
class App(morepath.App):
pass
class A(object):
pass
@App.path(path='/users/{id}', model=A)
def get_a(id):
return A()
App.commit()
infos = get_path_and_view_info(App)
infos = restrict(infos, ['path', 'directive'])
assert infos == [{'path': '/users/{id}', 'directive': 'path'}]
def test_mounted_app_paths_only():
class App(morepath.App):
pass
class A(object):
pass
@App.path(path='foo', model=A)
def get_a():
return A()
class Sub(morepath.App):
pass
class B(object):
pass
@Sub.path(path='bar', model=B)
def get_b():
return B()
@App.mount(path='sub', app=Sub)
def get_sub():
return Sub()
App.commit()
infos = get_path_and_view_info(App)
infos = restrict(infos, ['path', 'directive'])
assert infos == [
{'path': '/foo', 'directive': 'path'},
{'path': '/sub', 'directive': 'mount'},
{'path': '/sub/bar', 'directive': 'path'}
]
def test_one_app_view_actions():
class App(morepath.App):
pass
class A(object):
pass
@App.path(path='/foo', model=A)
def get_a():
return A()
@App.view(model=A)
def a_default(self, request):
return ""
App.commit()
infos = get_path_and_view_info(App)
infos = restrict(infos, ['path', 'directive'])
assert infos == [
{'path': '/foo', 'directive': 'path'},
{'path': '/foo', 'directive': 'view'},
]
def test_one_app_named_view():
class App(morepath.App):
pass
class A(object):
pass
@App.path(path='/foo', model=A)
def get_a():
return A()
@App.view(model=A)
def a_default(self, request):
return ""
@App.view(model=A, name='edit')
def a_edit(self, request):
return ""
App.commit()
infos = get_path_and_view_info(App)
infos = restrict(infos, ['path', 'directive'])
assert infos == [
{'path': '/foo', 'directive': 'path'},
{'path': '/foo', 'directive': 'view'},
{'path': '/foo/+edit', 'directive': 'view'},
]
def test_one_app_view_predicates():
class App(morepath.App):
pass
class A(object):
pass
@App.path(path='/foo', model=A)
def get_a():
return A()
@App.view(model=A)
def a_default(self, request):
return ""
@App.view(model=A, name='edit')
def a_edit(self, request):
return ""
App.commit()
infos = get_path_and_view_info(App)
predicates = [d.get('predicates') for d in infos if 'predicates' in d]
assert predicates == [{}, {'name': 'edit'}]
def test_one_app_view_actions_base_class():
class App(morepath.App):
pass
class Base(object):
pass
class A(Base):
pass
@App.path(path='/foo', model=A)
def get_a():
return A()
@App.view(model=Base)
def base_default(self, request):
return ""
App.commit()
infos = get_path_and_view_info(App)
infos = restrict(infos, ['path', 'directive'])
assert infos == [
{'path': '/foo', 'directive': 'path'},
{'path': '/foo', 'directive': 'view'},
]
def test_mounted_app_paths_and_views():
class App(morepath.App):
pass
class A(object):
pass
@App.path(path='foo', model=A)
def get_a():
return A()
@App.json(model=A)
def a_default(self, request):
pass
class Sub(morepath.App):
pass
class B(object):
pass
@Sub.path(path='bar', model=B)
def get_b():
return B()
@Sub.view(model=B)
def b_default(self, request):
return ''
# shouldn't be picked up as it's in Sub
@Sub.view(model=A)
def a_sub_view(self, request):
return ''
@App.mount(path='sub', app=Sub)
def get_sub():
return Sub()
App.commit()
infos = get_path_and_view_info(App)
infos = restrict(infos, ['path', 'directive'])
assert infos == [
{'path': '/foo', 'directive': 'path'},
{'path': '/foo', 'directive': 'json'},
{'path': '/sub', 'directive': 'mount'},
{'path': '/sub/bar', 'directive': 'path'},
{'path': '/sub/bar', 'directive': 'view'}
]