From 147b21564186760106146e74db16358db387a7b1 Mon Sep 17 00:00:00 2001 From: James Graham Date: Wed, 15 Mar 2017 10:27:04 +0000 Subject: [PATCH] Add support for fixing directory listings to WrapperHandler. WrapperHandler instances add virtual files to the directory listing. To allow these to appear in direcory listings, provide a listing_fixup function which adds the generated files to the list of fies in a directory. --- serve/serve.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/serve/serve.py b/serve/serve.py index 1e78498..ad9a911 100644 --- a/serve/serve.py +++ b/serve/serve.py @@ -91,6 +91,16 @@ def _get_meta(self, request): if replacement: yield replacement + def listing_fixup(self, base, names): + extras = set() + for name in names: + for item in self.path_replace: + dest, src = item[:2] + if name.endswith(src): + extras.add(name.replace(src, dest)) + + return names | extras + @abc.abstractproperty def path_replace(self): # A list containing a mix of 2 item tuples with (input suffix, output suffix) @@ -243,10 +253,16 @@ def add_mount_point(self, url_base, path): ] for (method, suffix, handler_cls) in routes: + handler = handler_cls(base_path=path, url_base=url_base) + if hasattr(handler, "listing_fixup"): + listing_fixup = handler.listing_fixup + else: + listing_fixup = None self.mountpoint_routes[url_base].append( (method, b"%s%s" % (str(url_base) if url_base != "/" else "", str(suffix)), - handler_cls(base_path=path, url_base=url_base))) + handler, + listing_fixup)) def add_file_mount_point(self, file_url, base_path): assert file_url.startswith("/")