comparison orpg/mapper/images.py @ 66:c54768cffbd4 ornery-dev

Traipse Dev 'OpenRPG' {090818-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: *Unstable* This is the first wave of Code Refinement updates. Includes new material from Core Beta; new debugger material (partially implemented), beginnings of switch to etree, TerminalWriter, and a little more. open_rpg has been renamed to component; functioning now as component.get(), component.add(), component.delete(). This version has known bugs, specifically with the gametree and nodes. I think the XML files where not removed during testing of Core and switching back.
author sirebral
date Tue, 18 Aug 2009 06:33:37 -0500
parents 88cea66228d6
children 449a8900f9ac
comparison
equal deleted inserted replaced
65:4840657c23c5 66:c54768cffbd4
32 import thread 32 import thread
33 from threading import Lock 33 from threading import Lock
34 import time 34 import time
35 from orpg.orpg_wx import * 35 from orpg.orpg_wx import *
36 from orpg.orpgCore import * 36 from orpg.orpgCore import *
37 from orpg.dirpath import dir_struct
37 38
38 def singleton(cls): 39 def singleton(cls):
39 instances = {} 40 instances = {}
40 def getinstance(): 41 def getinstance():
41 if cls not in instances: 42 if cls not in instances:
57 self.__fetching[path] = True 58 self.__fetching[path] = True
58 #Start Image Loading Thread 59 #Start Image Loading Thread
59 thread.start_new_thread(self.__loadThread, (path, image_type, imageId)) 60 thread.start_new_thread(self.__loadThread, (path, image_type, imageId))
60 else: 61 else:
61 if self.__fetching[path] is True: thread.start_new_thread(self.__loadCacheThread, (path, image_type, imageId)) 62 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) 63 return wx.Bitmap(dir_struct["icon"] + "fetching.png", wx.BITMAP_TYPE_PNG)
63 64
64 def directLoad(self, path): 65 def directLoad(self, path):
65 # Directly load an image, no threads 66 # Directly load an image, no threads
66 if self.__cache.has_key(path): return wx.ImageFromMime(self.__cache[path][1], 67 if self.__cache.has_key(path): return wx.ImageFromMime(self.__cache[path][1],
67 self.__cache[path][2]).ConvertToBitmap() 68 self.__cache[path][2]).ConvertToBitmap()
72 # it was an image that we got back. 73 # it was an image that we got back.
73 if d[0] and d[1].getmaintype() == "image": 74 if d[0] and d[1].getmaintype() == "image":
74 self.__cache[path] = (path, d[0], d[1].gettype(), None) 75 self.__cache[path] = (path, d[0], d[1].gettype(), None)
75 return wx.ImageFromMime(self.__cache[path][1], self.__cache[path][2]).ConvertToBitmap() 76 return wx.ImageFromMime(self.__cache[path][1], self.__cache[path][2]).ConvertToBitmap()
76 else: 77 else:
77 open_rpg.get_component('log').log("Image refused to load or URI did not reference a valid image: " + path, 78 component.get('log').log("Image refused to load or URI did not reference a valid image: " + path,
78 ORPG_GENERAL, True) 79 ORPG_GENERAL, True)
79 return None 80 return None
80 except IOError: 81 except IOError:
81 open_rpg.get_component('log').log("Unable to resolve/open the specified URI; image was NOT loaded: " + path, 82 component.get('log').log("Unable to resolve/open the specified URI; image was NOT loaded: " + path,
82 ORPG_GENERAL, True) 83 ORPG_GENERAL, True)
83 return None 84 return None
84 85
85 def cleanCache(self): 86 def cleanCache(self):
86 # Shrinks the Cache down to the proper size 87 # Shrinks the Cache down to the proper size
87 try: cacheSize = int(open_rpg.get_component('settings').get_setting("ImageCacheSize")) 88 try: cacheSize = int(component.get('settings').get_setting("ImageCacheSize"))
88 except: cacheSize = 32 89 except: cacheSize = 32
89 cache = self.__cache.keys() 90 cache = self.__cache.keys()
90 cache.sort() 91 cache.sort()
91 for key in cache[cacheSize:]: del self.__cache[key] 92 for key in cache[cacheSize:]: del self.__cache[key]
92 93
97 keyList = self.__cache.keys() 98 keyList = self.__cache.keys()
98 for key in keyList: del self.__cache[key] 99 for key in keyList: del self.__cache[key]
99 finally: self.__lock.release() 100 finally: self.__lock.release()
100 urllib.urlcleanup() 101 urllib.urlcleanup()
101 102
102 #Private Methods 103 #Private Methods
103 def __loadThread(self, path, image_type, imageId): 104 def __loadThread(self, path, image_type, imageId):
104 uriPath = urllib.unquote(path) 105 uriPath = urllib.unquote(path)
105 self.__lock.acquire() 106 self.__lock.acquire()
106 try: 107 try:
107 d = urllib.urlretrieve(uriPath) 108 d = urllib.urlretrieve(uriPath)
110 if d[0] and d[1].getmaintype() == "image": 111 if d[0] and d[1].getmaintype() == "image":
111 self.__cache[path] = (path, d[0], d[1].gettype(), imageId) 112 self.__cache[path] = (path, d[0], d[1].gettype(), imageId)
112 self.__queue.put((self.__cache[path], image_type, imageId)) 113 self.__queue.put((self.__cache[path], image_type, imageId))
113 if self.__fetching.has_key(path): del self.__fetching[path] 114 if self.__fetching.has_key(path): del self.__fetching[path]
114 else: 115 else:
115 open_rpg.get_component('log').log("Image refused to load or URI did not reference a valid image: " + path, 116 component.get('log').log("Image refused to load or URI did not reference a valid image: " + path,
116 ORPG_GENERAL, True) 117 ORPG_GENERAL, True)
117 del self.__fetching[path] 118 del self.__fetching[path]
118 except IOError: 119 except IOError:
119 del self.__fetching[path] 120 del self.__fetching[path]
120 open_rpg.get_component('log').log("Unable to resolve/open the specified URI; image was NOT laoded: " + path, 121 component.get('log').log("Unable to resolve/open the specified URI; image was NOT laoded: " + path,
121 ORPG_GENERAL, True) 122 ORPG_GENERAL, True)
122 finally: self.__lock.release() 123 finally: self.__lock.release()
123 124
124 def __loadCacheThread(self, path, image_type, imageId): 125 def __loadCacheThread(self, path, image_type, imageId):
125 if self.__cache.has_key(path): 126 if self.__cache.has_key(path):
126 try: 127 try:
127 st = time.time() 128 st = time.time()
128 while self.__fetching.has_key(path) and self.__fetching[path] is not False: 129 while self.__fetching.has_key(path) and self.__fetching[path] is not False:
129 time.sleep(0.025) 130 time.sleep(0.025)
130 if (time.time()-st) > 120: 131 if (time.time()-st) > 120:
131 open_rpg.get_component('log').log("Timeout: " + path, ORPG_GENERAL, True) 132 component.get('log').log("Timeout: " + path, ORPG_GENERAL, True)
132 break 133 break
133 except: 134 except:
134 del self.__fetching[path] 135 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) 136 component.get('log').log("Unable to resolve/open the specified URI; image was NOT loaded: " + path, ORPG_GENERAL, True)
136 return 137 return
137 self.__lock.acquire() 138 self.__lock.acquire()
138 try: 139 try:
139 open_rpg.get_component('log').log("Adding Image to Queue from Cache: " + str(self.__cache[path]), ORPG_DEBUG) 140 component.get('log').log("Adding Image to Queue from Cache: " + str(self.__cache[path]), ORPG_DEBUG)
140 self.__queue.put((self.__cache[path], image_type, imageId)) 141 self.__queue.put((self.__cache[path], image_type, imageId))
141 finally: self.__lock.release() 142 finally: self.__lock.release()
142 143
143 #Property Methods 144 #Property Methods
144 def _getCache(self): 145 def _getCache(self):
145 return self.__cache 146 return self.__cache
146 147
147 def _getQueue(self): 148 def _getQueue(self):
148 return self.__queue 149 return self.__queue
149 150
150 #Properties 151 #Properties
151 Cache = property(_getCache) 152 Cache = property(_getCache)
152 Queue = property(_getQueue) 153 Queue = property(_getQueue)
153 154
154 ImageHandler = singleton(ImageHandlerClass) 155 ImageHandler = singleton(ImageHandlerClass)