author | Tero Marttila <terom@qmsk.net> |
Sat, 04 Oct 2014 03:03:17 +0300 | |
changeset 174 | 58c9e2de0dd4 |
parent 166 | 986052d7d0ce |
permissions | -rw-r--r-- |
138
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
1 |
from werkzeug import Request, Response, exceptions |
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
2 |
|
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
3 |
import pypngtile |
166
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
4 |
import pngtile.store |
152
7bd8d6062c9e
pngtile.application: url(): helper to construct urls
Tero Marttila <terom@qmsk.net>
parents:
146
diff
changeset
|
5 |
|
166
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
6 |
class PNGTileApplication (pngtile.store.PNGTileStore): |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
7 |
""" |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
8 |
Web application with a PNGTileStore. |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
9 |
""" |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
10 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
11 |
def list (self, url): |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
12 |
""" |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
13 |
Yield names by request.url. |
138
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
14 |
|
166
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
15 |
Raises HTTPException |
138
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
16 |
""" |
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
17 |
|
166
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
18 |
try: |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
19 |
return super(PNGTileApplication, self).list(url) |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
20 |
except pngtile.store.NotFound as error: |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
21 |
raise exceptions.NotFound(str(error)) |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
22 |
except pngtile.store.InvalidImage as error: |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
23 |
raise exceptions.BadRequest(str(error)) |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
24 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
25 |
def lookup (self, url): |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
26 |
""" |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
27 |
Lookup neme, path, type by request.url. |
138
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
28 |
|
166
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
29 |
Raises HTTPException |
138
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
30 |
""" |
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
31 |
|
166
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
32 |
try: |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
33 |
return super(PNGTileApplication, self).lookup(url) |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
34 |
except pngtile.store.Error as error: |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
35 |
raise exceptions.InternalServerError(str(error)) |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
36 |
except pngtile.store.NotFound as error: |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
37 |
raise exceptions.NotFound(str(error)) |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
38 |
except pngtile.store.InvalidImage as error: |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
39 |
raise exceptions.BadRequest(str(error)) |
138
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
40 |
|
166
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
41 |
def open (self, url): |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
42 |
""" |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
43 |
Return Image, name by request.url |
138
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
44 |
|
166
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
45 |
Raises HTTPException. |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
46 |
""" |
138
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
47 |
|
166
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
48 |
try: |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
49 |
return super(PNGTileApplication, self).open(url) |
138
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
50 |
|
166
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
51 |
except pypngtile.Error as error: |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
52 |
raise exceptions.InternalServerError(str(error)) |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
53 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
54 |
except pngtile.store.Error as error: |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
55 |
raise exceptions.InternalServerError(str(error)) |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
56 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
57 |
except pngtile.store.NotFound as error: |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
58 |
raise exceptions.NotFound(str(error)) |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
59 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
60 |
except pngtile.store.InvalidImage as error: |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
61 |
raise exceptions.BadRequest(str(error)) |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
62 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
63 |
except pngtile.store.UncachedImage as error: |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
164
diff
changeset
|
64 |
raise exceptions.InternalServerError("Requested image has not yet been cached: {image}".format(image=error)) |
138
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
65 |
|
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
66 |
def handle (self, request): |
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
67 |
""" |
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
68 |
Handle request for an image |
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
69 |
""" |
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
70 |
|
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
71 |
raise NotImplementedError() |
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
72 |
|
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
73 |
@Request.application |
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
74 |
def __call__ (self, request): |
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
75 |
""" |
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
76 |
WSGI entry point. |
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
77 |
""" |
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
78 |
|
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
79 |
try: |
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
80 |
return self.handle(request) |
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
81 |
|
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
82 |
except exceptions.HTTPException as error: |
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
83 |
return error |
59d61da2b64f
pngtile.tile: split off BaseAppliation
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
84 |