2016-08-01 89 views
0

从表单URL:/foo/(.*)/bar/(.*),我想提供文件,其中从2个捕获的组计算实际路径。我的问题是,StaticFileHandler的get()只需要1个路径参数。有没有办法让这个工作,而不必重新实现StaticFileHandler的大部分方法?多个正则表达式捕获组的Tornado StaticFileHandler路径

我目前的解决方法是捕获一切:(/foo/.*/bar/.*),但是我必须在重写get_absolute_path()内重新分析类似的正则表达式。

回答

1

没有延伸StaticFileHandler没有办法做到这一点。这将是一个微小的变化:

from tornado import gen, web 

class CustomStaticFileHandler(web.StaticFileHandler): 

    def get(self, part1, part2, include_body=True): 
     # mangle path 
     path = "dome_{}_combined_path_{}".format(part1, part2) 
     # back to staticfilehandler 
     return super().get(path, include_body) 

    # if you need to use coroutines on mangle use 
    # 
    # @gen.coroutine 
    # def get(self, part1, part2, include_body=True): 
    #  path = yield some_db.get_path(part1, part2) 
    #  yield super().get(path, include_body) 

app = web.Application([ 
    (r"/foo/(.*)/bar/(.*)", CustomStaticFileHandler, {"path": "/tmp"}), 
]) 
+0

谢谢,这个工程。应该注意的是,您还需要重写validate_absolute_path,或者确保在处理程序声明中给出的“路径”是您将生成的每个路径的父目录。 – Gnurfos