Mercurial > traipse_dev
comparison orpg/mapper/images.py @ 20:072ffc1d466f traipse_dev
2nd attempt. Still untested.
author | sirebral |
---|---|
date | Sat, 25 Jul 2009 19:23:25 -0500 |
parents | 78407d627cba |
children | 88cea66228d6 |
comparison
equal
deleted
inserted
replaced
19:78407d627cba | 20:072ffc1d466f |
---|---|
49 __queue = Queue.Queue(0) | 49 __queue = Queue.Queue(0) |
50 __lock = Lock() | 50 __lock = Lock() |
51 | 51 |
52 def load(self, path, image_type, imageId): | 52 def load(self, path, image_type, imageId): |
53 # Load an image, with a intermideary fetching image shown while it loads in a background thread | 53 # Load an image, with a intermideary fetching image shown while it loads in a background thread |
54 if self.__cache.has_key(path): | 54 if self.__cache.has_key(path): return wx.ImageFromMime(self.__cache[path][1], |
55 return wx.ImageFromMime(self.__cache[path][1], self.__cache[path][2]).ConvertToBitmap() | 55 self.__cache[path][2]).ConvertToBitmap() |
56 if not self.__fetching.has_key(path): | 56 if not self.__fetching.has_key(path): |
57 self.__fetching[path] = True | 57 self.__fetching[path] = True |
58 #Start Image Loading Thread | 58 #Start Image Loading Thread |
59 thread.start_new_thread(self.__loadThread, (path, image_type, imageId)) | 59 thread.start_new_thread(self.__loadThread, (path, image_type, imageId)) |
60 else: | 60 else: |
61 if self.__fetching[path] is True: | 61 if self.__fetching[path] is True: thread.start_new_thread(self.__loadCacheThread, (path, image_type, imageId)) |
62 thread.start_new_thread(self.__loadCacheThread, (path, image_type, imageId)) | |
63 return wx.Bitmap(open_rpg.get_component("dir_struct")["icon"] + "fetching.png", wx.BITMAP_TYPE_PNG) | 62 return wx.Bitmap(open_rpg.get_component("dir_struct")["icon"] + "fetching.png", wx.BITMAP_TYPE_PNG) |
64 | 63 |
65 def directLoad(self, path): | 64 def directLoad(self, path): |
66 # Directly load an image, no threads | 65 # Directly load an image, no threads |
67 if self.__cache.has_key(path): | 66 if self.__cache.has_key(path): return wx.ImageFromMime(self.__cache[path][1], |
68 return wx.ImageFromMime(self.__cache[path][1], self.__cache[path][2]).ConvertToBitmap() | 67 self.__cache[path][2]).ConvertToBitmap() |
69 uriPath = urllib.unquote(path) | 68 uriPath = urllib.unquote(path) |
70 try: | 69 try: |
71 d = urllib.urlretrieve(uriPath) | 70 d = urllib.urlretrieve(uriPath) |
72 # We have to make sure that not only did we fetch something, but that | 71 # We have to make sure that not only did we fetch something, but that |
73 # it was an image that we got back. | 72 # it was an image that we got back. |
83 ORPG_GENERAL, True) | 82 ORPG_GENERAL, True) |
84 return None | 83 return None |
85 | 84 |
86 def cleanCache(self): | 85 def cleanCache(self): |
87 # Shrinks the Cache down to the proper size | 86 # Shrinks the Cache down to the proper size |
88 try: | 87 try: cacheSize = int(open_rpg.get_component('settings').get_setting("ImageCacheSize")) |
89 cacheSize = int(open_rpg.get_component('settings').get_setting("ImageCacheSize")) | 88 except: cacheSize = 32 |
90 except: | |
91 cacheSize = 32 | |
92 cache = self.__cache.keys() | 89 cache = self.__cache.keys() |
93 cache.sort() | 90 cache.sort() |
94 for key in cache[cacheSize:]: | 91 for key in cache[cacheSize:]: del self.__cache[key] |
95 del self.__cache[key] | |
96 | 92 |
97 def flushCache(self): | 93 def flushCache(self): |
98 # This function will flush all images contained within the image cache. | 94 # This function will flush all images contained within the image cache. |
99 self.__lock.acquire() | 95 self.__lock.acquire() |
100 try: | 96 try: |
101 keyList = self.__cache.keys() | 97 keyList = self.__cache.keys() |
102 for key in keyList: | 98 for key in keyList: del self.__cache[key] |
103 del self.__cache[key] | 99 finally: self.__lock.release() |
104 finally: | |
105 self.__lock.release() | |
106 urllib.urlcleanup() | 100 urllib.urlcleanup() |
107 | 101 |
108 #Private Methods | 102 #Private Methods |
109 def __loadThread(self, path, image_type, imageId): | 103 def __loadThread(self, path, image_type, imageId): |
110 uriPath = urllib.unquote(path) | 104 uriPath = urllib.unquote(path) |
114 # We have to make sure that not only did we fetch something, but that | 108 # We have to make sure that not only did we fetch something, but that |
115 # it was an image that we got back. | 109 # it was an image that we got back. |
116 if d[0] and d[1].getmaintype() == "image": | 110 if d[0] and d[1].getmaintype() == "image": |
117 self.__cache[path] = (path, d[0], d[1].gettype(), imageId) | 111 self.__cache[path] = (path, d[0], d[1].gettype(), imageId) |
118 self.__queue.put((self.__cache[path], image_type, imageId)) | 112 self.__queue.put((self.__cache[path], image_type, imageId)) |
119 if self.__fetching.has_key(path): | 113 if self.__fetching.has_key(path): del self.__fetching[path] |
120 del self.__fetching[path] | |
121 else: | 114 else: |
122 open_rpg.get_component('log').log("Image refused to load or URI did not reference a valid image: " + path, | 115 open_rpg.get_component('log').log("Image refused to load or URI did not reference a valid image: " + path, |
123 ORPG_GENERAL, True) | 116 ORPG_GENERAL, True) |
124 del self.__fetching[path] | 117 del self.__fetching[path] |
125 except IOError: | 118 except IOError: |
126 del self.__fetching[path] | 119 del self.__fetching[path] |
127 open_rpg.get_component('log').log("Unable to resolve/open the specified URI; image was NOT laoded: " + path, | 120 open_rpg.get_component('log').log("Unable to resolve/open the specified URI; image was NOT laoded: " + path, |
128 ORPG_GENERAL, True) | 121 ORPG_GENERAL, True) |
129 finally: | 122 finally: self.__lock.release() |
130 self.__lock.release() | |
131 | 123 |
132 def __loadCacheThread(self, path, image_type, imageId): | 124 def __loadCacheThread(self, path, image_type, imageId): |
133 try: | 125 try: |
134 st = time.time() | 126 st = time.time() |
135 while self.__fetching.has_key(path) and self.__fetching[path] is not False: | 127 while self.__fetching.has_key(path) and self.__fetching[path] is not False: |
144 return | 136 return |
145 self.__lock.acquire() | 137 self.__lock.acquire() |
146 try: | 138 try: |
147 open_rpg.get_component('log').log("Adding Image to Queue from Cache: " + str(self.__cache[path]), ORPG_DEBUG) | 139 open_rpg.get_component('log').log("Adding Image to Queue from Cache: " + str(self.__cache[path]), ORPG_DEBUG) |
148 self.__queue.put((self.__cache[path], image_type, imageId)) | 140 self.__queue.put((self.__cache[path], image_type, imageId)) |
149 finally: | 141 finally: self.__lock.release() |
150 self.__lock.release() | |
151 | 142 |
152 #Property Methods | 143 #Property Methods |
153 def _getCache(self): | 144 def _getCache(self): |
154 return self.__cache | 145 return self.__cache |
155 | 146 |