+def default_file_not_found_handler(request):
+ return Response('', status=404)
+
+def route_on_subpath(**kwargs):
+ routes = kwargs.pop('routes')
+ file_not_found_handler = kwargs.pop(
+ 'file_not_found_hanlder',
+ default_file_not_found_handler,
+ )
+
+ if routes is None:
+ raise Exception('Keyword argument "routes" is required')
+
+ if len(kwargs) > 0:
+ raise Exception('Unexpected keyword argument')
+
+ def wrapped(request):
+ split_subpath = request.subpath.split('/', 1)
+ subpath = split_subpath[0]
+
+ if len(split_subpath) == 2:
+ request.subpath = split_subpath[1]
+ else:
+ request.subpath = ''
+
+ return routes.get(subpath, file_not_found_handler)(request)
+
+ return wrapped
+