Mercurial > traipse
comparison orpg/mapper/images.py @ 18:97265586402b ornery-orc
Traipse 'OpenRPG' {090827-00}
Traipse is a distribution of OpenRPG that is designed to be easy to setup and go. Traipse also makes it easy for developers to work on code without fear of sacrifice. 'Ornery-Orc' continues the trend of 'Grumpy' and adds fixes to the code. 'Ornery-Orc''s main goal is to offer more advanced features and enhance the productivity of the user.
Update Summary:
Update Manager is now in version 0.8. While not every button works, users can now browse the different revisions and their different changesets. The code has been refined some with feature from Core added to it. A Crash report is now created if the users software crashes. Update Manager has been moved to the Traipse Suite menu item, and a Debug Console as been added as well.
author | sirebral |
---|---|
date | Thu, 27 Aug 2009 01:04:43 -0500 |
parents | 211ac836b6a0 |
children | 51428d30c59e |
comparison
equal
deleted
inserted
replaced
17:265b987cce4f | 18:97265586402b |
---|---|
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 * | 37 from orpg.orpgCore import component |
38 from orpg.dirpath import dir_struct | |
39 from orpg.tools.orpg_log import logger | |
37 | 40 |
38 def singleton(cls): | 41 def singleton(cls): |
39 instances = {} | 42 instances = {} |
40 def getinstance(): | 43 def getinstance(): |
41 if cls not in instances: | 44 if cls not in instances: |
46 class ImageHandlerClass(object): | 49 class ImageHandlerClass(object): |
47 __cache = {} | 50 __cache = {} |
48 __fetching = {} | 51 __fetching = {} |
49 __queue = Queue.Queue(0) | 52 __queue = Queue.Queue(0) |
50 __lock = Lock() | 53 __lock = Lock() |
54 chat = component.get("chat") | |
51 | 55 |
52 def load(self, path, image_type, imageId): | 56 def load(self, path, image_type, imageId): |
53 # 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""" |
54 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], |
55 self.__cache[path][2]).ConvertToBitmap() | 59 self.__cache[path][2]).ConvertToBitmap() |
56 if not self.__fetching.has_key(path): | 60 if not self.__fetching.has_key(path): |
57 self.__fetching[path] = True | 61 self.__fetching[path] = True |
58 #Start Image Loading Thread | 62 """Start Image Loading Thread""" |
59 thread.start_new_thread(self.__loadThread, (path, image_type, imageId)) | 63 thread.start_new_thread(self.__loadThread, (path, image_type, imageId)) |
60 else: | 64 else: |
61 if self.__fetching[path] is True: thread.start_new_thread(self.__loadCacheThread, (path, image_type, imageId)) | 65 if self.__fetching[path] is True: thread.start_new_thread(self.__loadCacheThread, (path, image_type, imageId)) |
62 return wx.Bitmap(open_rpg.get_component("dir_struct")["icon"] + "fetching.png", wx.BITMAP_TYPE_PNG) | 66 return wx.Bitmap(dir_struct["icon"] + "fetching.png", wx.BITMAP_TYPE_PNG) |
63 | 67 |
64 def directLoad(self, path): | 68 def directLoad(self, path): |
65 # Directly load an image, no threads | 69 """Directly load an image, no threads""" |
66 if self.__cache.has_key(path): return wx.ImageFromMime(self.__cache[path][1], | 70 if self.__cache.has_key(path): return wx.ImageFromMime(self.__cache[path][1], |
67 self.__cache[path][2]).ConvertToBitmap() | 71 self.__cache[path][2]).ConvertToBitmap() |
68 uriPath = urllib.unquote(path) | 72 uriPath = urllib.unquote(path) |
69 try: | 73 try: |
70 d = urllib.urlretrieve(uriPath) | 74 d = urllib.urlretrieve(uriPath) |
71 # We have to make sure that not only did we fetch something, but that | 75 """We have to make sure that not only did we fetch something, but that |
72 # it was an image that we got back. | 76 it was an image that we got back.""" |
73 if d[0] and d[1].getmaintype() == "image": | 77 if d[0] and d[1].getmaintype() == "image": |
74 self.__cache[path] = (path, d[0], d[1].gettype(), None) | 78 self.__cache[path] = (path, d[0], d[1].gettype(), None) |
75 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() |
76 else: | 80 else: |
77 open_rpg.get_component('log').log("Image refused to load or URI did not reference a valid image: " + path, | 81 logger.general("Image refused to load or URI did not reference a valid image: " + path) |
78 ORPG_GENERAL, True) | 82 component.get('chat').InfoPost("<font color='#FF0000'>Image refused to load or URI did not reference a valid image: " + path + "</font>") |
79 return None | 83 return None |
80 except IOError: | 84 except IOError: |
81 open_rpg.get_component('log').log("Unable to resolve/open the specified URI; image was NOT loaded: " + path, | 85 logger.general("Unable to resolve/open the specified URI; image was NOT loaded: " + path) |
82 ORPG_GENERAL, True) | 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(open_rpg.get_component('settings').get_setting("ImageCacheSize")) | 91 try: cacheSize = int(component.get('settings').get_setting("ImageCacheSize")) |
88 except: cacheSize = 32 | 92 except: cacheSize = 32 |
89 cache = self.__cache.keys() | 93 cache = self.__cache.keys() |
90 cache.sort() | 94 cache.sort() |
91 for key in cache[cacheSize:]: del self.__cache[key] | 95 for key in cache[cacheSize:]: del self.__cache[key] |
92 | 96 |
93 def flushCache(self): | 97 def flushCache(self): |
94 # This function will flush all images contained within the image cache. | 98 """This function will flush all images contained within the image cache.""" |
95 self.__lock.acquire() | 99 self.__lock.acquire() |
96 try: | 100 try: |
97 keyList = self.__cache.keys() | 101 keyList = self.__cache.keys() |
98 for key in keyList: del self.__cache[key] | 102 for key in keyList: del self.__cache[key] |
99 finally: self.__lock.release() | 103 finally: self.__lock.release() |
100 urllib.urlcleanup() | 104 urllib.urlcleanup() |
101 | 105 |
102 #Private Methods | 106 """Private Methods""" |
103 def __loadThread(self, path, image_type, imageId): | 107 def __loadThread(self, path, image_type, imageId): |
104 uriPath = urllib.unquote(path) | 108 uriPath = urllib.unquote(path) |
105 self.__lock.acquire() | 109 self.__lock.acquire() |
106 try: | 110 try: |
107 d = urllib.urlretrieve(uriPath) | 111 d = urllib.urlretrieve(uriPath) |
108 # We have to make sure that not only did we fetch something, but that | 112 """We have to make sure that not only did we fetch something, but that |
109 # it was an image that we got back. | 113 it was an image that we got back.""" |
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): del self.__fetching[path] | 117 if self.__fetching.has_key(path): self.__fetching[path] = False #Fix for failed multi-load? |
114 else: | 118 else: |
115 open_rpg.get_component('log').log("Image refused to load or URI did not reference a valid image: " + path, | 119 logger.general("Image refused to load or URI did not reference a valid image: " + path) |
116 ORPG_GENERAL, True) | 120 component.get('chat').InfoPost("<font color='#FF0000'>Image refused to load or URI did not reference a valid image: " + path +"</font>") |
117 del self.__fetching[path] | 121 del self.__fetching[path] |
118 except IOError: | 122 except IOError: |
119 del self.__fetching[path] | 123 del self.__fetching[path] |
120 open_rpg.get_component('log').log("Unable to resolve/open the specified URI; image was NOT laoded: " + path, | 124 logger.general("Unable to resolve/open the specified URI; image was NOT loaded: " + path) |
121 ORPG_GENERAL, True) | 125 component.get('chat').InfoPost("<font color='#FF0000'> Unable to resolve/open the specified URI; image was NOT loaded: " + path + "</font>") |
122 finally: self.__lock.release() | 126 finally: self.__lock.release() |
123 | 127 |
124 def __loadCacheThread(self, path, image_type, imageId): | 128 def __loadCacheThread(self, path, image_type, imageId): |
125 if self.__cache.has_key(path): | 129 if self.__cache.has_key(path): |
126 try: | 130 try: |
127 st = time.time() | 131 st = time.time() |
128 while self.__fetching.has_key(path) and self.__fetching[path] is not False: | 132 while self.__fetching.has_key(path) and self.__fetching[path] is not False: |
129 time.sleep(0.025) | 133 time.sleep(0.025) |
130 if (time.time()-st) > 120: | 134 if (time.time()-st) > 120: |
131 open_rpg.get_component('log').log("Timeout: " + path, ORPG_GENERAL, True) | 135 logger.general("Timeout: " + path) |
132 break | 136 break |
133 except: | 137 except: |
134 del self.__fetching[path] | 138 del self.__fetching[path] |
135 open_rpg.get_component('log').log("Unable to resolve/open the specified URI; image was NOT loaded: " + path, ORPG_GENERAL, True) | 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>") | |
136 return | 141 return |
137 self.__lock.acquire() | 142 self.__lock.acquire() |
138 try: | 143 try: |
139 open_rpg.get_component('log').log("Adding Image to Queue from Cache: " + str(self.__cache[path]), ORPG_DEBUG) | 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>") | |
140 self.__queue.put((self.__cache[path], image_type, imageId)) | 146 self.__queue.put((self.__cache[path], image_type, imageId)) |
141 finally: self.__lock.release() | 147 finally: self.__lock.release() |
142 | 148 |
143 #Property Methods | 149 """Property Methods""" |
144 def _getCache(self): | 150 def _getCache(self): |
145 return self.__cache | 151 return self.__cache |
146 | 152 |
147 def _getQueue(self): | 153 def _getQueue(self): |
148 return self.__queue | 154 return self.__queue |
149 | 155 |
150 #Properties | 156 """Properties""" |
151 Cache = property(_getCache) | 157 Cache = property(_getCache) |
152 Queue = property(_getQueue) | 158 Queue = property(_getQueue) |
153 | 159 |
154 ImageHandler = singleton(ImageHandlerClass) | 160 ImageHandler = singleton(ImageHandlerClass) |