comparison orpg/mapper/images.py @ 76:37a11fea3304 ornery-dev

More clean up. Images now posts a Chat Info post if image doesn't load.
author sirebral
date Sat, 22 Aug 2009 04:02:14 -0500
parents 449a8900f9ac
children 6226a5f9a4b3
comparison
equal deleted inserted replaced
75:f518bc709fd1 76:37a11fea3304
30 import urllib 30 import urllib
31 import Queue 31 import Queue
32 import thread 32 import thread
33 from threading import Lock 33 from threading import Lock
34 import time 34 import time
35
35 from orpg.orpg_wx import * 36 from orpg.orpg_wx import *
36 from orpg.orpgCore import component 37 from orpg.orpgCore import component
37 from orpg.dirpath import dir_struct 38 from orpg.dirpath import dir_struct
38 from orpg.tools.orpg_log import logger 39 from orpg.tools.orpg_log import logger
39 40
48 class ImageHandlerClass(object): 49 class ImageHandlerClass(object):
49 __cache = {} 50 __cache = {}
50 __fetching = {} 51 __fetching = {}
51 __queue = Queue.Queue(0) 52 __queue = Queue.Queue(0)
52 __lock = Lock() 53 __lock = Lock()
54 chat = component.get("chat")
53 55
54 def load(self, path, image_type, imageId): 56 def load(self, path, image_type, imageId):
55 """Load an image, with a intermideary fetching image shown while it loads in a background thread""" 57 """Load an image, with a intermideary fetching image shown while it loads in a background thread"""
56 if self.__cache.has_key(path): return wx.ImageFromMime(self.__cache[path][1], 58 if self.__cache.has_key(path): return wx.ImageFromMime(self.__cache[path][1],
57 self.__cache[path][2]).ConvertToBitmap() 59 self.__cache[path][2]).ConvertToBitmap()
74 it was an image that we got back.""" 76 it was an image that we got back."""
75 if d[0] and d[1].getmaintype() == "image": 77 if d[0] and d[1].getmaintype() == "image":
76 self.__cache[path] = (path, d[0], d[1].gettype(), None) 78 self.__cache[path] = (path, d[0], d[1].gettype(), None)
77 return wx.ImageFromMime(self.__cache[path][1], self.__cache[path][2]).ConvertToBitmap() 79 return wx.ImageFromMime(self.__cache[path][1], self.__cache[path][2]).ConvertToBitmap()
78 else: 80 else:
79 logger.general("Image refused to load or URI did not reference a valid image: " + path) ##logger.general 81 logger.general("Image refused to load or URI did not reference a valid image: " + path)
82 component.get('chat').InfoPost("<font color='#FF0000'>Image refused to load or URI did not reference a valid image: " + path + "</font>")
80 return None 83 return None
81 except IOError: 84 except IOError:
82 logger.general("Unable to resolve/open the specified URI; image was NOT loaded: " + path) ##logger.general 85 logger.general("Unable to resolve/open the specified URI; image was NOT loaded: " + path)
86 component.get('chat').InfoPost("<font color='#FF0000'>Unable to resolve/open the specified URI; image was NOT loaded: " + path + "</font>")
83 return None 87 return None
84 88
85 def cleanCache(self): 89 def cleanCache(self):
86 """Shrinks the Cache down to the proper size""" 90 """Shrinks the Cache down to the proper size"""
87 try: cacheSize = int(component.get('settings').get_setting("ImageCacheSize")) 91 try: cacheSize = int(component.get('settings').get_setting("ImageCacheSize"))
110 if d[0] and d[1].getmaintype() == "image": 114 if d[0] and d[1].getmaintype() == "image":
111 self.__cache[path] = (path, d[0], d[1].gettype(), imageId) 115 self.__cache[path] = (path, d[0], d[1].gettype(), imageId)
112 self.__queue.put((self.__cache[path], image_type, imageId)) 116 self.__queue.put((self.__cache[path], image_type, imageId))
113 if self.__fetching.has_key(path): self.__fetching[path] = False #Fix for failed multi-load? 117 if self.__fetching.has_key(path): self.__fetching[path] = False #Fix for failed multi-load?
114 else: 118 else:
115 logger.general("Image refused to load or URI did not reference a valid image: " + path) ##logger.general 119 logger.general("Image refused to load or URI did not reference a valid image: " + path)
120 component.get('chat').InfoPost("<font color='#FF0000'>Image refused to load or URI did not reference a valid image: " + path +"</font>")
116 del self.__fetching[path] 121 del self.__fetching[path]
117 except IOError: 122 except IOError:
118 del self.__fetching[path] 123 del self.__fetching[path]
119 logger.general("Unable to resolve/open the specified URI; image was NOT laoded: " + path) ##logger.general 124 logger.general("Unable to resolve/open the specified URI; image was NOT loaded: " + path)
125 component.get('chat').InfoPost("<font color='#FF0000'> Unable to resolve/open the specified URI; image was NOT loaded: " + path + "</font>")
120 finally: self.__lock.release() 126 finally: self.__lock.release()
121 127
122 def __loadCacheThread(self, path, image_type, imageId): 128 def __loadCacheThread(self, path, image_type, imageId):
123 if self.__cache.has_key(path): 129 if self.__cache.has_key(path):
124 try: 130 try:
128 if (time.time()-st) > 120: 134 if (time.time()-st) > 120:
129 logger.general("Timeout: " + path) 135 logger.general("Timeout: " + path)
130 break 136 break
131 except: 137 except:
132 del self.__fetching[path] 138 del self.__fetching[path]
133 logger.general("Unable to resolve/open the specified URI; image was NOT loaded: " + path) ##logger.general 139 logger.general("Unable to resolve/open the specified URI; image was NOT loaded: " + path)
140 component.get('chat').InfoPost("<font color='#FF0000'>Unable to resolve/open the specified URI; image was NOT loaded: " + path + "</font>")
134 return 141 return
135 self.__lock.acquire() 142 self.__lock.acquire()
136 try: 143 try:
137 logger.debug("Adding Image to Queue from Cache: " + str(self.__cache[path])) 144 logger.debug("Adding Image to Queue from Cache: " + str(self.__cache[path]))
145 component.debug('chat').InfoPost("<font color='#FF0000'>Adding Image to Queue from Cache: " + str(self.__cache[path]) + "</font>)
138 self.__queue.put((self.__cache[path], image_type, imageId)) 146 self.__queue.put((self.__cache[path], image_type, imageId))
139 finally: self.__lock.release() 147 finally: self.__lock.release()
140 148
141 """Property Methods""" 149 """Property Methods"""
142 def _getCache(self): 150 def _getCache(self):