From 4452d86498efd75fc0e4567070bdff9897159cba Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Mon, 25 Jun 2012 21:29:10 +0200 Subject: [PATCH 1/3] Extract method DirListingHandler._appendToListing (so we can re-use it) --- servefile | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/servefile b/servefile index 93124d9..e1b44a0 100755 --- a/servefile +++ b/servefile @@ -334,6 +334,27 @@ class DirListingHandler(FileBaseHandler): self.send_response(404) self.end_headers() + def _appendToListing(self, content, item, itemPath, stat, is_dir): + # Strings to display on directory listing + lastModifiedDate = datetime.datetime.fromtimestamp(stat.st_mtime) + lastModified = lastModifiedDate.strftime("%Y-%m-%d %H:%M") + fileSize = "%.1f%s" % self.convertSize(stat.st_size) + (fileType, _) = mimetypes.guess_type(itemPath) + if not fileType: + fileType = "-" + + if is_dir: + item += "/" + fileType = "Directory" + content.append(""" + + %s + %s + %s + %s + + """ % (urllib.quote(item), item, lastModified, fileSize, fileType)) + def sendDirectoryListing(self, path, head): """ Generate a directorylisting for path and send it """ header = """ @@ -385,25 +406,7 @@ class DirListingHandler(FileBaseHandler): except IOError: continue - # Strings to display on directory listing - lastModifiedDate = datetime.datetime.fromtimestamp(stat.st_mtime) - lastModified = lastModifiedDate.strftime("%Y-%m-%d %H:%M") - fileSize = "%.1f%s" % self.convertSize(stat.st_size) - (fileType, _) = mimetypes.guess_type(itemPath) - if not fileType: - fileType = "-" - - if os.path.isdir(itemPath): - item += "/" - fileType = "Directory" - content.append(""" - - %s - %s - %s - %s - - """ % (urllib.quote(item), item, lastModified, fileSize, fileType)) + self._appendToListing(content, item, itemPath, stat, os.path.isdir(itemPath)) listing = header + "\n".join(content) + footer From 0df9c5621470804e40f999f6f611afe08ca62a96 Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Mon, 25 Jun 2012 21:37:19 +0200 Subject: [PATCH 2/3] Make all directories go on top in listings --- servefile | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/servefile b/servefile index e1b44a0..8e0b5f9 100755 --- a/servefile +++ b/servefile @@ -395,6 +395,10 @@ class DirListingHandler(FileBaseHandler): """ % {'version': __version__} content = [] + + dir_items = list() + file_items = list() + for item in [".."] + sorted(os.listdir(path)): # create path to item itemPath = os.path.join(path, item) @@ -406,7 +410,19 @@ class DirListingHandler(FileBaseHandler): except IOError: continue - self._appendToListing(content, item, itemPath, stat, os.path.isdir(itemPath)) + if os.path.isdir(itemPath): + target_items = dir_items + else: + target_items = file_items + target_items.append((item, itemPath, stat)) + + # Directories first, then files + for (tuple_list, is_dir) in ( + (dir_items, True), + (file_items, False), + ): + for (item, itemPath, stat) in tuple_list: + self._appendToListing(content, item, itemPath, stat, is_dir=is_dir) listing = header + "\n".join(content) + footer From dece73be04a97474e0cfa2675f6a297c472da293 Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Mon, 25 Jun 2012 21:39:37 +0200 Subject: [PATCH 3/3] Make sorting case-insensitive so that "aaa" and "AAA" end up next to each other --- servefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/servefile b/servefile index 8e0b5f9..883bcb8 100755 --- a/servefile +++ b/servefile @@ -399,7 +399,7 @@ class DirListingHandler(FileBaseHandler): dir_items = list() file_items = list() - for item in [".."] + sorted(os.listdir(path)): + for item in [".."] + sorted(os.listdir(path), key=lambda x:x.lower()): # create path to item itemPath = os.path.join(path, item)