Merge branch 'spingpatches'

Conflicts:
	servefile
This commit is contained in:
Sebastian Lohff 2012-10-25 03:06:19 +02:00
commit 6a38212242
1 changed files with 38 additions and 18 deletions

View File

@ -349,6 +349,27 @@ class DirListingHandler(FileBaseHandler):
htmlstr = htmlstr.replace(src, dst) htmlstr = htmlstr.replace(src, dst)
return htmlstr return htmlstr
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("""
<tr>
<td class="name"><a href="%s">%s</a></td>
<td class="last-modified">%s</td>
<td class="size">%s</td>
<td class="type">%s</td>
</tr>
""" % (urllib.quote(item), item, lastModified, fileSize, fileType))
def sendDirectoryListing(self, path, head): def sendDirectoryListing(self, path, head):
""" Generate a directorylisting for path and send it """ """ Generate a directorylisting for path and send it """
header = """<!DOCTYPE html> header = """<!DOCTYPE html>
@ -389,7 +410,11 @@ class DirListingHandler(FileBaseHandler):
</body> </body>
</html>""" % {'version': __version__} </html>""" % {'version': __version__}
content = [] content = []
for item in [".."] + sorted(os.listdir(path)):
dir_items = list()
file_items = list()
for item in [".."] + sorted(os.listdir(path), key=lambda x:x.lower()):
# create path to item # create path to item
itemPath = os.path.join(path, item) itemPath = os.path.join(path, item)
@ -400,24 +425,19 @@ class DirListingHandler(FileBaseHandler):
except IOError: except IOError:
continue 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): if os.path.isdir(itemPath):
item += "/" target_items = dir_items
fileType = "Directory" else:
content.append(""" target_items = file_items
<tr> target_items.append((item, itemPath, stat))
<td class="name"><a href="%s">%s</a></td>
<td class="last-modified">%s</td> # Directories first, then files
<td class="size">%s</td> for (tuple_list, is_dir) in (
<td class="type">%s</td> (dir_items, True),
</tr>""" % (urllib.quote(item), item, lastModified, fileSize, fileType)) (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 listing = header + "\n".join(content) + footer