equal
deleted
inserted
replaced
4 import pypngtile |
4 import pypngtile |
5 |
5 |
6 import os.path |
6 import os.path |
7 |
7 |
8 class BaseApplication (object): |
8 class BaseApplication (object): |
|
9 IMAGE_TYPES = ( |
|
10 'png', |
|
11 ) |
|
12 |
9 def __init__ (self, image_root): |
13 def __init__ (self, image_root): |
10 if not os.path.isdir(image_root) : |
14 if not os.path.isdir(image_root) : |
11 raise Exception("Given image_root does not exist: {image_root}".format(image_root=image_root)) |
15 raise Exception("Given image_root does not exist: {image_root}".format(image_root=image_root)) |
12 |
16 |
13 self.image_root = os.path.abspath(image_root) |
17 self.image_root = os.path.abspath(image_root) |
14 |
18 |
15 self.image_cache = { } |
19 self.image_cache = { } |
16 |
20 |
17 def lookup_image (self, url): |
21 def lookup_path (self, url): |
18 """ |
22 """ |
19 Lookup image by request path. |
23 Lookup image by request path. |
20 |
24 |
21 Returns image_name, image_path. |
25 Returns name, path. |
22 """ |
26 """ |
23 |
27 |
24 if not os.path.isdir(self.image_root): |
28 if not os.path.isdir(self.image_root): |
25 raise exceptions.InternalServerError("Server image_root has gone missing") |
29 raise exceptions.InternalServerError("Server image_root has gone missing") |
26 |
30 |
35 raise exceptions.NotFound(name) |
39 raise exceptions.NotFound(name) |
36 |
40 |
37 if not os.path.exists(path): |
41 if not os.path.exists(path): |
38 raise exceptions.NotFound(name) |
42 raise exceptions.NotFound(name) |
39 |
43 |
40 if os.path.isdir(path): |
|
41 raise exceptions.BadRequest("Is a directory: {name}".format(name=name)) |
|
42 |
|
43 if not path.endswith('.png'): |
|
44 raise exceptions.BadRequest("Not a PNG image: {name}".format(name=name)) |
|
45 |
|
46 return name, path |
44 return name, path |
47 |
45 |
48 def get_image (self, url): |
46 def get_image (self, url): |
49 """ |
47 """ |
50 Return Image object. |
48 Return Image object. |
51 """ |
49 """ |
52 |
50 |
53 name, path = self.lookup_image(url) |
51 name, path = self.lookup_path(url) |
|
52 |
|
53 if os.path.isdir(path): |
|
54 raise exceptions.BadRequest("Is a directory: {name}".format(name=name)) |
|
55 |
|
56 basename, file_type = path.rsplit('.', 1) |
|
57 |
|
58 if file_type not in self.IMAGE_TYPES: |
|
59 raise exceptions.BadRequest("Not a supported image: {name}: {type}".format(name=name, type=file_type)) |
54 |
60 |
55 # get Image object |
61 # get Image object |
56 image = self.image_cache.get(path) |
62 image = self.image_cache.get(path) |
57 |
63 |
58 if not image: |
64 if not image: |