comparison orpg/mapper/images.py @ 135:dcf4fbe09b70 beta

Traipse Beta 'OpenRPG' {091010-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 (Beta) Added Bookmarks Fix to Remote Admin Commands Minor fix to text based Server Fix to Pretty Print, from Core Fix to Splitter Nodes not being created Fix to massive amounts of images loading, from Core Added 'boot' command to remote admin Added confirmation window for sent nodes Minor changes to allow for portability to an OpenSUSE linux OS Miniatures Layer pop up box allows users to turn off Mini labels, from FlexiRPG Zoom Mouse plugin added Images added to Plugin UI Switching to Element Tree Map efficiency, from FlexiRPG Added Status Bar to Update Manager default_manifest.xml renamed to default_upmana.xml Cleaner clode for saved repositories New TrueDebug Class in orpg_log (See documentation for usage) Mercurial's hgweb folder is ported to upmana **Pretty important update that can help remove thousands of dead children from your gametree. **Children, <forms />, <group_atts />, <horizontal />, <cols />, <rows />, <height />, etc... are all tags now. Check your gametree and look for dead children!! **New Gamtree Recusion method, mapping, and context sensitivity. !!Alpha - Watch out for infinite loops!!
author sirebral
date Tue, 10 Nov 2009 14:11:28 -0600
parents 7ed4979cc1cf
children e842a5f1b775
comparison
equal deleted inserted replaced
101:394ebb3b6a0f 135:dcf4fbe09b70
23 # Version: 23 # Version:
24 # $Id: images.py,v 1.21 2007/12/11 04:07:15 digitalxero Exp $ 24 # $Id: images.py,v 1.21 2007/12/11 04:07:15 digitalxero Exp $
25 # 25 #
26 # Description: 26 # Description:
27 # 27 #
28 __version__ = "$Id: images.py,v 1.21 2007/12/11 04:07:15 digitalxero Exp $" 28 from __future__ import with_statement
29 29
30 import urllib 30 import urllib, Queue, thread, time
31 import Queue
32 import thread
33 from threading import Lock 31 from threading import Lock
34 import time 32 from orpg.orpg_wx import *
33 from orpg.orpgCore import *
35 34
36 from orpg.orpg_wx import *
37 from orpg.orpgCore import component
38 from orpg.dirpath import dir_struct 35 from orpg.dirpath import dir_struct
39 from orpg.tools.orpg_log import logger 36 from orpg.tools.orpg_log import logger
40 37 from orpg.tools.settings import settings
41 def singleton(cls):
42 instances = {}
43 def getinstance():
44 if cls not in instances:
45 instances[cls] = cls()
46 return instances[cls]
47 return getinstance()
48 38
49 class ImageHandlerClass(object): 39 class ImageHandlerClass(object):
50 __cache = {} 40 __cache = {}
51 __fetching = {} 41 __fetching = {}
52 __queue = Queue.Queue(0) 42 __queue = Queue.Queue(0)
53 __lock = Lock() 43 __lock = Lock()
54 chat = component.get("chat") 44
45 def __new__(cls):
46 it = cls.__dict__.get("__it__")
47 if it is not None:
48 return it
49 cls.__it__ = it = object.__new__(cls)
50 return it
55 51
56 def load(self, path, image_type, imageId): 52 def load(self, path, image_type, imageId):
57 """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
58 if self.__cache.has_key(path): return wx.ImageFromMime(self.__cache[path][1], 54 if self.__cache.has_key(path):
59 self.__cache[path][2]).ConvertToBitmap() 55 return wx.ImageFromMime(self.__cache[path][1],
60 if not self.__fetching.has_key(path): 56 self.__cache[path][2])
57 if path not in self.__fetching:
61 self.__fetching[path] = True 58 self.__fetching[path] = True
62 """Start Image Loading Thread""" 59 #Start Image Loading Thread
63 thread.start_new_thread(self.__loadThread, (path, image_type, imageId)) 60 thread.start_new_thread(self.__loadThread,
61 (path, image_type, imageId))
64 else: 62 else:
65 if self.__fetching[path] is True: thread.start_new_thread(self.__loadCacheThread, (path, image_type, imageId)) 63 if self.__fetching[path]:
64 thread.start_new_thread(self.__loadCacheThread,
65 (path, image_type, imageId))
66 return wx.Bitmap(dir_struct["icon"] + "fetching.png", wx.BITMAP_TYPE_PNG) 66 return wx.Bitmap(dir_struct["icon"] + "fetching.png", wx.BITMAP_TYPE_PNG)
67 67
68 def directLoad(self, path): 68 def directLoad(self, path):
69 """Directly load an image, no threads""" 69 # Directly load an image, no threads
70 if self.__cache.has_key(path): return wx.ImageFromMime(self.__cache[path][1], 70 if path in self.__cache:
71 self.__cache[path][2]).ConvertToBitmap() 71 return wx.ImageFromMime(self.__cache[path][1],
72 self.__cache[path][2])
72 uriPath = urllib.unquote(path) 73 uriPath = urllib.unquote(path)
73 try: 74 try:
74 d = urllib.urlretrieve(uriPath) 75 d = urllib.urlretrieve(uriPath)
75 """We have to make sure that not only did we fetch something, but that 76 # We have to make sure that not only did we fetch something, but that
76 it was an image that we got back.""" 77 # it was an image that we got back.
77 if d[0] and d[1].getmaintype() == "image": 78 if d[0] and d[1].getmaintype() == "image":
78 self.__cache[path] = (path, d[0], d[1].gettype(), None) 79 with self.__lock:
79 return wx.ImageFromMime(self.__cache[path][1], self.__cache[path][2]).ConvertToBitmap() 80 self.__cache[path] = (path, d[0], d[1].gettype(), None)
81 return wx.ImageFromMime(self.__cache[path][1], self.__cache[path][2])
80 else: 82 else:
81 logger.exception(str("Image refused to load or URI did not reference a valid image: " + path), True) 83 logger.general("Image refused to load or URI did not "
84 "reference a valid image: " + path, True)
82 return None 85 return None
83 except IOError: 86 except IOError:
84 logger.exception(str("Unable to resolve/open the specified URI; image was NOT loaded: " + path), True) 87 logger.general("Unable to resolve/open the specified URI; "
88 "image was NOT loaded: " + path, True)
85 return None 89 return None
86 90
87 def cleanCache(self): 91 def cleanCache(self):
88 """Shrinks the Cache down to the proper size""" 92 # Shrinks the Cache down to the proper size
89 try: cacheSize = int(component.get('settings').get_setting("ImageCacheSize")) 93 try:
90 except: cacheSize = 32 94 cacheSize = int(settings.get("ImageCacheSize"))
95 except:
96 cacheSize = 32
91 cache = self.__cache.keys() 97 cache = self.__cache.keys()
92 cache.sort() 98 cache.sort()
93 for key in cache[cacheSize:]: del self.__cache[key] 99 for key in cache[cacheSize:]:
100 del self.__cache[key]
94 101
95 def flushCache(self): 102 def flushCache(self):
96 """This function will flush all images contained within the image cache.""" 103 # This function will flush all images contained within the image cache.
97 self.__lock.acquire() 104 with self.__lock:
98 try:
99 self.__cache = {} 105 self.__cache = {}
100 self.__fetching = {} 106 self.__fetching = {}
101 finally:
102 self.__lock.release()
103 urllib.urlcleanup() 107 urllib.urlcleanup()
104 108
105 """Private Methods""" 109 #Private Methods
106 def __loadThread(self, path, image_type, imageId): 110 def __loadThread(self, path, image_type, imageId):
107 uriPath = urllib.unquote(path) 111 uriPath = urllib.unquote(path)
108 self.__lock.acquire()
109 try: 112 try:
110 d = urllib.urlretrieve(uriPath) 113 d = urllib.urlretrieve(uriPath)
111 """We have to make sure that not only did we fetch something, but that 114 # We have to make sure that not only did we fetch something, but that
112 it was an image that we got back.""" 115 # it was an image that we got back.
113 if d[0] and d[1].getmaintype() == "image": 116 if d[0] and d[1].getmaintype() == "image":
114 self.__cache[path] = (path, d[0], d[1].gettype(), imageId) 117 with self.__lock:
115 self.__queue.put((self.__cache[path], image_type, imageId)) 118 self.__cache[path] = (path, d[0], d[1].gettype(), imageId)
116 if self.__fetching.has_key(path): del self.__fetching[path] 119 self.__queue.put((self.__cache[path], image_type, imageId))
120 if path in self.__fetching: del self.__fetching[path]
117 else: 121 else:
118 logger.exception(str("Image refused to load or URI did not reference a valid image: " + path), True) 122 logger.general("Image refused to load or URI did not "
123 "reference a valid image: " + path, True)
119 del self.__fetching[path] 124 del self.__fetching[path]
120 except IOError: 125 except IOError:
121 del self.__fetching[path] 126 del self.__fetching[path]
122 logger.exception(str("Unable to resolve/open the specified URI; image was NOT loaded: " + path), True) 127 logger.general("Unable to resolve/open the specified URI; "
123 finally: self.__lock.release() 128 "image was NOT laoded: " + path, True)
124 129
125 def __loadCacheThread(self, path, image_type, imageId): 130 def __loadCacheThread(self, path, image_type, imageId):
126 try: 131 try:
127 st = time.time() 132 st = time.time()
128 while self.__fetching.has_key(path) and self.__fetching[path] is not False: 133 while path in self.__fetching and self.__fetching[path] is not False:
129 time.sleep(0.025) 134 time.sleep(0.025)
130 if (time.time()-st) > 120: 135 if (time.time()-st) > 120:
131 logger.general("Timeout: " + path) 136 logger.general("Timeout: " + path, True)
137 del self.__fetching[path]
132 break 138 break
133 except: 139 except:
134 del self.__fetching[path] 140 del self.__fetching[path]
135 logger.exception(str("Unable to resolve/open the specified URI; image was NOT loaded: " + path), True) 141 logger.general("Unable to resolve/open the specified URI; "
136 return 142 "image was NOT loaded: " + path, True)
137 self.__lock.acquire() 143 return
138 try: 144 with self.__lock:
139 logger.info("Adding Image to Queue from Cache: " + str(self.__cache[path]), True) 145 if path in self.__cache:
140 self.__queue.put((self.__cache[path], image_type, imageId)) 146 logger.debug("Adding Image to Queue from Cache: " + str(self.__cache[path]))
141 finally: self.__lock.release() 147 self.__queue.put((self.__cache[path], image_type, imageId))
148 else: self.__loadThread(path, image_type, imageId)
142 149
143 """Property Methods""" 150 #Property Methods
144 def _getCache(self): 151 def _getCache(self):
145 return self.__cache 152 return self.__cache
146 153
147 def _getQueue(self): 154 def _getQueue(self):
148 return self.__queue 155 return self.__queue
149 156
150 """Properties""" 157 #Properties
151 Cache = property(_getCache) 158 Cache = property(_getCache)
152 Queue = property(_getQueue) 159 Queue = property(_getQueue)
153 160
154 ImageHandler = singleton(ImageHandlerClass) 161 ImageHandler = ImageHandlerClass()
155 component.add('ImageHandler', ImageHandler)