author | Tero Marttila <terom@qmsk.net> |
Sat, 04 Oct 2014 01:01:43 +0300 | |
changeset 166 | 986052d7d0ce |
child 176 | ad32cf3a25fa |
permissions | -rw-r--r-- |
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:
diff
changeset
|
1 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
2 |
import pypngtile |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
3 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
4 |
import os.path |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
5 |
import threading |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
6 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
7 |
class Error (Exception): |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
8 |
pass |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
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:
diff
changeset
|
10 |
class NotFound (KeyError): |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
11 |
pass |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
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:
diff
changeset
|
13 |
class InvalidImage (Exception): |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
14 |
pass |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
15 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
16 |
class UncachedImage (Exception): |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
17 |
pass |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
18 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
19 |
class PNGTileStore (object): |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
20 |
""" |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
21 |
Access pypngtile.Image's on a filesystem. |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
22 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
23 |
Intended to be threadsafe for open() |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
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:
diff
changeset
|
25 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
26 |
IMAGE_TYPES = ( |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
27 |
'png', |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
28 |
) |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
29 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
30 |
def __init__ (self, image_root): |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
31 |
if not os.path.isdir(image_root) : |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
32 |
raise Error("Given image_root does not exist: {image_root}".format(image_root=image_root)) |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
33 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
34 |
self.image_root = os.path.abspath(image_root) |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
35 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
36 |
# cache opened Images |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
37 |
self.images = { } |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
38 |
self.images_lock = threading.Lock() |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
39 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
40 |
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:
diff
changeset
|
41 |
""" |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
42 |
Lookup image by request path. |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
43 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
44 |
Returns name, path, type. For dirs, type will be None. |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
45 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
46 |
Raises Error, NotFound, InvalidImage |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
47 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
48 |
Threadless. |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
49 |
""" |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
50 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
51 |
if not os.path.isdir(self.image_root): |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
52 |
raise Error("Server image_root has gone missing") |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
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:
diff
changeset
|
54 |
# path to image |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
55 |
name = url.lstrip('/') |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
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:
diff
changeset
|
57 |
# build absolute path |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
58 |
path = os.path.abspath(os.path.join(self.image_root, name)) |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
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:
diff
changeset
|
60 |
# ensure the path points inside the data root |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
61 |
if not path.startswith(self.image_root): |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
62 |
raise NotFound(name) |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
63 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
64 |
if not os.path.exists(path): |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
65 |
raise NotFound(name) |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
66 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
67 |
# determine time |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
68 |
if os.path.isdir(path): |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
69 |
return name, path, None |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
70 |
else: |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
71 |
basename, type = path.rsplit('.', 1) |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
72 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
73 |
if type not in self.IMAGE_TYPES: |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
74 |
raise InvalidImage(name) |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
75 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
76 |
return name, path, type |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
77 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
78 |
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:
diff
changeset
|
79 |
""" |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
80 |
Yield a series of valid sub-names for the given directory: |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
81 |
foo.type |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
82 |
foo/ |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
83 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
84 |
The yielded items should be sorted before use. |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
85 |
""" |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
86 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
87 |
name, root, type = 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:
diff
changeset
|
88 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
89 |
if type: |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
90 |
raise InvalidImage(name) |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
91 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
92 |
for name in os.listdir(root): |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
93 |
path = os.path.join(root, name) |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
94 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
95 |
# skip dotfiles |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
96 |
if name.startswith('.'): |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
97 |
continue |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
98 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
99 |
# show dirs |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
100 |
if os.path.isdir(path): |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
101 |
if not os.access(path, os.R_OK): |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
102 |
# skip inaccessible dirs |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
103 |
continue |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
104 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
105 |
yield name + '/' |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
106 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
107 |
# examine ext |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
108 |
if '.' in name: |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
109 |
name_base, name_type = name.rsplit('.', 1) |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
110 |
else: |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
111 |
name_base = name |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
112 |
name_type = None |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
113 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
114 |
# show .png files with a .cache file |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
115 |
if name_type in self.IMAGE_TYPES and os.path.exists(os.path.join(root, name_base + '.cache')): |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
116 |
yield name |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
117 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
118 |
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:
diff
changeset
|
119 |
""" |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
120 |
Return Image object for given 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:
diff
changeset
|
121 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
122 |
Raises UncachedImage, pypngtile.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:
diff
changeset
|
123 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
124 |
Threadsafe. |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
125 |
""" |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
126 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
127 |
name, path, type = 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:
diff
changeset
|
128 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
129 |
if not type: |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
130 |
raise InvalidImage(name) |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
131 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
132 |
# get Image object |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
133 |
with self.images_lock: |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
134 |
image = self.images.get(path) |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
135 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
136 |
if not image: |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
137 |
# open |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
138 |
image = pypngtile.Image(path) |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
139 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
140 |
# check |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
141 |
if image.status() not in (pypngtile.CACHE_FRESH, pypngtile.CACHE_STALE): |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
142 |
raise UncachedImage(name) |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
143 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
144 |
# load |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
145 |
image.open() |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
146 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
147 |
# cache |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
148 |
with self.images_lock: |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
149 |
# we don't really care if some other thread raced us on the same Image and opened it up concurrently... |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
150 |
# this is just a cache |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
151 |
self.images[path] = image |
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
152 |
|
986052d7d0ce
pngtile.store: split PNGTileApplication and PNGTileStore with lookup/list/open Image handling logic and a threadsafe cache
Tero Marttila <terom@qmsk.net>
parents:
diff
changeset
|
153 |
return image, name |