pngtile.image: have BaseApplication.lookup_image() return type as well
authorTero Marttila <terom@qmsk.net>
Fri, 03 Oct 2014 23:56:02 +0300
changeset 164 e1e0c8099c8b
parent 163 7c929ba47ba6
child 165 1dc09e81a4e2
pngtile.image: have BaseApplication.lookup_image() return type as well
pngtile/application.py
pngtile/image.py
--- a/pngtile/application.py	Fri Oct 03 23:18:42 2014 +0300
+++ b/pngtile/application.py	Fri Oct 03 23:56:02 2014 +0300
@@ -31,7 +31,7 @@
         """
             Lookup image by request path.
 
-            Returns name, path.
+            Returns name, path, type. For dirs, type will be None.
         """
 
         if not os.path.isdir(self.image_root):
@@ -49,23 +49,24 @@
         
         if not os.path.exists(path):
             raise exceptions.NotFound(name)
-
-        return name, path
+        
+        # determine time
+        if os.path.isdir(path):
+            return name, path, None
+        else:
+            basename, type = path.rsplit('.', 1)
+ 
+            return name, path, type
 
     def get_image (self, url):
         """
             Return Image object.
         """
 
-        name, path = self.lookup_path(url)
+        name, path, type = self.lookup_path(url)
 
-        if os.path.isdir(path):
-            raise exceptions.BadRequest("Is a directory: {name}".format(name=name))
-
-        basename, file_type = path.rsplit('.', 1)
-
-        if file_type not in self.IMAGE_TYPES:
-            raise exceptions.BadRequest("Not a supported image: {name}: {type}".format(name=name, type=file_type))
+        if type not in self.IMAGE_TYPES:
+            raise exceptions.BadRequest("Not a supported image: {name}: {type}".format(name=name, type=type))
 
         # get Image object
         image = self.image_cache.get(path)
--- a/pngtile/image.py	Fri Oct 03 23:18:42 2014 +0300
+++ b/pngtile/image.py	Fri Oct 03 23:56:02 2014 +0300
@@ -190,26 +190,12 @@
             Handle request for an image
         """
 
-        name, path = self.lookup_path(request.path)
+        name, path, type = self.lookup_path(request.path)
         
-        # determine type
-        if '/' in name:
-            _, name_base = name.rsplit('/', 1)
-        else:
-            name_base = name
-
-        if '.' in name_base:
-            name_base, name_type = name_base.rsplit('.', 1)
-        else:
-            name_type = None
-
         # determine handler
-        if os.path.isdir(path):
+        if not type:
             return self.handle_dir(request, name, path)
         
-        elif name_type and name_type in self.IMAGE_TYPES:
+        else:
             return self.handle_image(request, name, path)
 
-        else:
-            raise exceptions.NotFound(path)
-