durusmail: quixote-users: StaticDirectory serving index files again
StaticDirectory serving index files again
StaticDirectory serving index files again
Shalabh Chaturvedi
2004-09-28
Hello,

The patch attached adds a feature to util.StaticDirectory enabling it to
return an index page (such as index.html) instead of directory listing.
A parameter in __init__() specifies a tuple of filenames that are looked
up in the directory, and the first one found is returned. If the tuple
is empty or specified files dont exist then it is ignored. The index
file takes precedence over directory listing. The patch is against 1.0.

This would be very useful for me (and I hope for others too) as I
wouldn't have to maintain separate version for my use. Any chance of it
getting into Quixote?

Thanks,
Shalabh Chaturvedi

--- util.py     Fri Oct 10 07:02:46 2003
+++ /usr/local/lib/python2.3/site-packages/quixote/util.py      Tue Sep 28
13:19:15 2004
@@ -156,8 +156,8 @@

     def __init__(self, path, use_cache=0, list_directory=0, follow_symlinks=0,
-                 cache_time=None, file_class=None):
+                 cache_time=None, file_class=None, index_filenames=()):
         """StaticDirectory(path:string, use_cache:bool, list_directory:bool,
                            follow_symlinks:bool, cache_time:int,
-                           file_class=None)
+                           file_class=None, index_filenames=())

         Initialize instance with the absolute path to the file.
@@ -166,4 +166,8 @@
         If 'follow_symlinks' is true, symbolic links will be followed.

+        Optional parameter 'index_filenames' specifies a tuple of
+        filenames to be used as index files in the directory. First
+        file found searching left to right is returned.
+
         Optional parameter cache_time allows setting of Expires header in
         response object (see note for StaticFile for more detail).
@@ -184,7 +188,10 @@
         else:
             self.file_class = self.FILE_CLASS
+        self.index_filenames = index_filenames

     def _q_index(self, request):
         """
+        If any specified index file exists in the directory return it.
+
         If directory listings are allowed, generate a simple HTML
         listing of the directory's contents with each item hyperlinked;
@@ -192,4 +199,13 @@
         return a page to that effect.
         """
+        for name in self.index_filenames:
+            try:
+                obj = self._q_lookup(request, name)
+            except errors.TraversalError:
+                continue
+
+            if not isinstance(obj, self.__class__) and callable(obj):
+                return obj(request)
+
         out = StringIO()
         if self.list_directory:
@@ -237,5 +253,6 @@
                                       self.list_directory,
                                       self.follow_symlinks, self.cache_time,
-                                      self.file_class)
+                                      self.file_class,
+                                      self.index_filenames)
             elif os.path.isfile(item_filepath):
                 item = self.file_class(item_filepath, self.follow_symlinks,
reply