pngtile/wsgi.py
changeset 31 7eec7486a0af
parent 30 53e99e552122
child 34 a387bc77ad52
equal deleted inserted replaced
30:53e99e552122 31:7eec7486a0af
    12 
    12 
    13 IMAGE_CACHE = {}
    13 IMAGE_CACHE = {}
    14 
    14 
    15 TILE_WIDTH = 256
    15 TILE_WIDTH = 256
    16 TILE_HEIGHT = 256
    16 TILE_HEIGHT = 256
       
    17 
       
    18 def dir_view (req, name, path) :
       
    19     prefix = os.path.dirname(req.script_root).rstrip('/')
       
    20     name = name.rstrip('/')
       
    21 
       
    22 
       
    23     return """\
       
    24 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
       
    25     <head>
       
    26         <title>Index of %(dir)s</title>
       
    27         <link rel="Stylesheet" type="text/css" href="%(prefix)s/static/style.css">
       
    28     </head>
       
    29     <body>
       
    30         <h1>Index of %(dir)s</h1>
       
    31 
       
    32         <ul>
       
    33 %(listing)s
       
    34         </ul>
       
    35     </body>
       
    36 </html>""" % dict(
       
    37         prefix          = prefix,
       
    38         dir             = name,
       
    39         
       
    40         listing         = "\n".join(
       
    41             """<li><a href="%(url)s">%(name)s</a></li>""" % dict(
       
    42                 url         = '/'.join((prefix, name, item)),
       
    43                 name        = item,
       
    44             ) for item in ['..'] + os.listdir(path)
       
    45         ),
       
    46     )
    17 
    47 
    18 def image_view (req, image_path, image) :
    48 def image_view (req, image_path, image) :
    19     image_name = os.path.basename(image_path)
    49     image_name = os.path.basename(image_path)
    20 
    50 
    21     return """\
    51     return """\
    52 def render_tile (image, x, y) :
    82 def render_tile (image, x, y) :
    53     return image.tile_mem(TILE_WIDTH, TILE_HEIGHT, x, y)
    83     return image.tile_mem(TILE_WIDTH, TILE_HEIGHT, x, y)
    54 
    84 
    55 def handle_main (req) :
    85 def handle_main (req) :
    56     # path to image
    86     # path to image
    57     image = req.path.lstrip('/')
    87     image_name = req.path.lstrip('/')
    58     
       
    59     # check a .png filename was given
       
    60     if not image or not image.endswith('.png') :
       
    61         raise exceptions.BadRequest("no .png path given")
       
    62     
    88     
    63     # build absolute path
    89     # build absolute path
    64     image_path = os.path.abspath(os.path.join(DATA_ROOT, image))
    90     image_path = os.path.abspath(os.path.join(DATA_ROOT, image_name))
       
    91 
       
    92     print image_name, image_path
    65     
    93     
    66     # ensure the path points inside the data root
    94     # ensure the path points inside the data root
    67     if not image_path.startswith(DATA_ROOT) :
    95     if not image_path.startswith(DATA_ROOT) :
    68         raise exceptions.NotFound(image)
    96         raise exceptions.NotFound(image_name)
       
    97 
       
    98 
       
    99     if os.path.isdir(image_path) :
       
   100         return Response(dir_view(req, image_name, image_path), content_type="text/html")
       
   101 
       
   102     elif not image_name or not image_name.endswith('.png') :
       
   103         raise exceptions.BadRequest("no .png path given")
    69     
   104     
       
   105 
    70     # get Image object
   106     # get Image object
    71     if image_path in IMAGE_CACHE :
   107     if image_path in IMAGE_CACHE :
    72         # get from cache
   108         # get from cache
    73         image = IMAGE_CACHE[image_path]
   109         image = IMAGE_CACHE[image_path]
    74 
   110 
    75     else :
   111     else :
    76         # ensure exists
   112         # ensure exists
    77         if not os.path.exists(image_path) :
   113         if not os.path.exists(image_path) :
    78             raise exceptions.NotFound(image)
   114             raise exceptions.NotFound(image_name)
    79 
   115 
    80         # cache
   116         # cache
    81         image = IMAGE_CACHE[image_path] = pt.Image(image_path)
   117         image = IMAGE_CACHE[image_path] = pt.Image(image_path)
       
   118     
       
   119     if image.status() == pt.CACHE_NONE :
       
   120         raise exceptions.InternalServerError("Image not cached: " + image_name)
    82 
   121 
    83     # what view?
   122     # what view?
    84     if not req.args :
   123     if not req.args :
    85         # viewport
   124         # viewport
    86         return Response(image_view(req, image_path, image), content_type="text/html")
   125         return Response(image_view(req, image_path, image), content_type="text/html")