[Twisted-commits] added a neat feature that allows requests for /foo be turned into /foo.bar. Off by default, of course.

carmstro CVS twisted-python@twistedmatrix.com
Wed, 17 Apr 2002 03:00:46 -0500


Modified files:
Twisted/twisted/tap/web.py 1.19 1.20
Twisted/twisted/web/static.py 1.28 1.29

Log message:
added a neat feature that allows requests for /foo be turned into /foo.bar. Off by default, of course.


ViewCVS links:
http://twistedmatrix.com/users/jh.twistd/viewcvs/cgi/viewcvs.cgi/twisted/tap/web.py.diff?r1=text&tr1=1.19&r2=text&tr2=1.20&cvsroot=Twisted
http://twistedmatrix.com/users/jh.twistd/viewcvs/cgi/viewcvs.cgi/twisted/web/static.py.diff?r1=text&tr1=1.28&r2=text&tr2=1.29&cvsroot=Twisted

Index: Twisted/twisted/tap/web.py
diff -u Twisted/twisted/tap/web.py:1.19 Twisted/twisted/tap/web.py:1.20
--- Twisted/twisted/tap/web.py:1.19	Thu Apr  4 08:01:19 2002
+++ Twisted/twisted/tap/web.py	Wed Apr 17 01:00:41 2002
@@ -87,13 +87,20 @@
     opt_s = opt_static
 
     def opt_mime_type(self, defaultType):
+        """Specify the default mime-type for static files."""
         if not isinstance(self.opts['root'], static.File):
-            print "You can only use --static_mime after --static."
+            print "You can only use --mime_type after --static."
             sys.exit(2)
         self.opts['root'].defaultType = defaultType
-
     opt_m = opt_mime_type
 
+
+    def opt_allow_ignore_ext(self):
+        """Specify wether or not a request for 'foo' should return 'foo.ext'"""
+        if not isinstance(self.opts['root'], static.File):
+            print "You can only use --allow_ignore_ext after --static."
+            sys.exit(2)
+        self.opts['root'].allowExt = 1
 
 
 def updateApplication(app, config):

Index: Twisted/twisted/web/static.py
diff -u Twisted/twisted/web/static.py:1.28 Twisted/twisted/web/static.py:1.29
--- Twisted/twisted/web/static.py:1.28	Sun Apr 14 06:02:18 2002
+++ Twisted/twisted/web/static.py	Wed Apr 17 01:00:42 2002
@@ -117,7 +117,11 @@
 
     ### Versioning
 
-    persistenceVersion = 2
+    persistenceVersion = 3
+
+    def upgradeToVersion3(self):
+        if not hasattr(self, 'allowExt'):
+            self.allowExt = 0
 
     def upgradeToVersion2(self):
         self.defaultType = "text/html"
@@ -127,7 +131,7 @@
             self.indexNames = [self.indexName]
             del self.indexName
 
-    def __init__(self, path, defaultType="text/html"):
+    def __init__(self, path, defaultType="text/html", allowExt=0):
         """Create a file with the given path.
         """
         resource.Resource.__init__(self)
@@ -140,6 +144,7 @@
         if self.encoding is not None:
             p, ext = os.path.splitext(p)
         self.defaultType = defaultType
+        self.allowExt = allowExt
         self.type = self.contentTypes.get(string.lower(ext), defaultType)
 
 
@@ -150,11 +155,13 @@
             return error.NoResource("Invalid request URL.")
         if path == '':
             for path in self.indexNames:
+                ##
                 # This next step is so urls like
                 #     /foo/bar/baz/
                 # will be represented (internally) as
                 #     ['foo','bar','baz','index.qux']
                 # So that request.childLink() will work correctly.
+                ##
                 if os.path.exists(os.path.join(self.path, path)):
                     request.prepath[-1] = path
                     break
@@ -163,13 +170,27 @@
                     return widgets.WidgetPage(DirectoryListing(self.path))
                 else:
                     return error.NoResource("File not found.")
+
+        ##
+        # If we're told to, allow requests for 'foo' to return
+        # 'foo.bar'.
+        ##
+        if self.allowExt:
+            for fn in os.listdir(self.path):
+                if os.path.splitext(fn)[0]==path:
+                    log.msg('    Returning %s' % fn)
+                    return File(os.path.join(self.path, fn), self.defaultType, self.allowExt)
+
         newpath = os.path.join(self.path, path)
+        if not os.path.exists(newpath):
+            return error.NoResource("File not found.")
+
         # forgive me, oh lord, for I know not what I do
         p, ext = os.path.splitext(newpath)
         processor = self.processors.get(ext)
         if processor:
             return processor(newpath)
-        f = File(newpath, self.defaultType)
+        f = File(newpath, self.defaultType, self.allowExt)
         f.processors = self.processors
         f.indexNames = self.indexNames[:]
         return f