Mercurial > traipse_dev
comparison orpg/mapper/images.py @ 112:61fc775862f7 alpha
Traipse Alpha 'OpenRPG' {091009-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:
{091006}
00:
Adds Bookmarks (Alpha) with cool Smiley Star and Plus Symbol images!
03:
Changes made to the map for increased portability. SnowDog has changes planned in
Core, though.
Added an initial push to the BCG. Not much to see, just shows off how it is
re-writing Main code.
{091008}
00:
Fix to remote admin commands
01:
Minor fix to texted based server, works in /System/ folder
Some Core changes to gametree to correctly disply Pretty Print, thanks David!
{091009}
00:
Fix to Splitter Nodes not being created.
Plugin Control panel works with images now
Fix to massive amounts of images loading; from Core
author | sirebral |
---|---|
date | Fri, 09 Oct 2009 23:26:21 -0500 |
parents | a647e0e8f520 |
children | 217fb049bd00 |
comparison
equal
deleted
inserted
replaced
111:0c936d98f9eb | 112:61fc775862f7 |
---|---|
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 |
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 from orpg.orpg_wx import * | |
36 from orpg.orpgCore import * | |
35 | 37 |
36 from orpg.orpg_wx import * | |
37 from orpg.orpgCore import component | |
38 from orpg.dirpath import dir_struct | 38 from orpg.dirpath import dir_struct |
39 from orpg.tools.orpg_log import logger | 39 from orpg.tools.orpg_log import logger |
40 | 40 from orpg.tools.orpg_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 | 41 |
49 class ImageHandlerClass(object): | 42 class ImageHandlerClass(object): |
50 __cache = {} | 43 __cache = {} |
51 __fetching = {} | 44 __fetching = {} |
52 __queue = Queue.Queue(0) | 45 __queue = Queue.Queue(0) |
53 __lock = Lock() | 46 __lock = Lock() |
54 chat = component.get("chat") | 47 |
48 def __new__(cls): | |
49 it = cls.__dict__.get("__it__") | |
50 if it is not None: | |
51 return it | |
52 cls.__it__ = it = object.__new__(cls) | |
53 return it | |
55 | 54 |
56 def load(self, path, image_type, imageId): | 55 def load(self, path, image_type, imageId): |
57 """Load an image, with a intermideary fetching image shown while it loads in a background thread""" | 56 # 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], | 57 if self.__cache.has_key(path): |
59 self.__cache[path][2]).ConvertToBitmap() | 58 return wx.ImageFromMime(self.__cache[path][1], |
60 if not self.__fetching.has_key(path): | 59 self.__cache[path][2]).ConvertToBitmap() |
60 if path not in self.__fetching: | |
61 self.__fetching[path] = True | 61 self.__fetching[path] = True |
62 """Start Image Loading Thread""" | 62 #Start Image Loading Thread |
63 thread.start_new_thread(self.__loadThread, (path, image_type, imageId)) | 63 thread.start_new_thread(self.__loadThread, |
64 (path, image_type, imageId)) | |
64 else: | 65 else: |
65 if self.__fetching[path] is True: thread.start_new_thread(self.__loadCacheThread, (path, image_type, imageId)) | 66 if self.__fetching[path]: |
66 return wx.Bitmap(dir_struct["icon"] + "fetching.png", wx.BITMAP_TYPE_PNG) | 67 thread.start_new_thread(self.__loadCacheThread, |
68 (path, image_type, imageId)) | |
69 return wx.Bitmap(dir_struct["icon"] + "fetching.png", | |
70 wx.BITMAP_TYPE_PNG) | |
67 | 71 |
68 def directLoad(self, path): | 72 def directLoad(self, path): |
69 """Directly load an image, no threads""" | 73 # Directly load an image, no threads |
70 if self.__cache.has_key(path): return wx.ImageFromMime(self.__cache[path][1], | 74 if path in self.__cache: |
71 self.__cache[path][2]).ConvertToBitmap() | 75 return wx.ImageFromMime(self.__cache[path][1], |
76 self.__cache[path][2]).ConvertToBitmap() | |
77 | |
72 uriPath = urllib.unquote(path) | 78 uriPath = urllib.unquote(path) |
73 try: | 79 try: |
74 d = urllib.urlretrieve(uriPath) | 80 d = urllib.urlretrieve(uriPath) |
75 """We have to make sure that not only did we fetch something, but that | 81 # We have to make sure that not only did we fetch something, but that |
76 it was an image that we got back.""" | 82 # it was an image that we got back. |
77 if d[0] and d[1].getmaintype() == "image": | 83 if d[0] and d[1].getmaintype() == "image": |
78 self.__cache[path] = (path, d[0], d[1].gettype(), None) | 84 with self.__lock: |
79 return wx.ImageFromMime(self.__cache[path][1], self.__cache[path][2]).ConvertToBitmap() | 85 self.__cache[path] = (path, d[0], d[1].gettype(), None) |
86 return wx.ImageFromMime(self.__cache[path][1], | |
87 self.__cache[path][2]).ConvertToBitmap() | |
80 else: | 88 else: |
81 logger.exception(str("Image refused to load or URI did not reference a valid image: " + path), True) | 89 logger.general("Image refused to load or URI did not " |
90 "reference a valid image: " + path, True) | |
82 return None | 91 return None |
83 except IOError: | 92 except IOError: |
84 logger.exception(str("Unable to resolve/open the specified URI; image was NOT loaded: " + path), True) | 93 logger.general("Unable to resolve/open the specified URI; " |
94 "image was NOT loaded: " + path, True) | |
85 return None | 95 return None |
86 | 96 |
87 def cleanCache(self): | 97 def cleanCache(self): |
88 """Shrinks the Cache down to the proper size""" | 98 # Shrinks the Cache down to the proper size |
89 try: cacheSize = int(component.get('settings').get_setting("ImageCacheSize")) | 99 try: |
90 except: cacheSize = 32 | 100 cacheSize = int(settings.get("ImageCacheSize")) |
101 except: | |
102 cacheSize = 32 | |
91 cache = self.__cache.keys() | 103 cache = self.__cache.keys() |
92 cache.sort() | 104 cache.sort() |
93 for key in cache[cacheSize:]: del self.__cache[key] | 105 for key in cache[cacheSize:]: |
106 del self.__cache[key] | |
94 | 107 |
95 def flushCache(self): | 108 def flushCache(self): |
96 """This function will flush all images contained within the image cache.""" | 109 # This function will flush all images contained within the image cache. |
97 self.__lock.acquire() | 110 with self.__lock: |
98 try: | |
99 self.__cache = {} | 111 self.__cache = {} |
100 self.__fetching = {} | 112 self.__fetching = {} |
101 finally: | |
102 self.__lock.release() | |
103 urllib.urlcleanup() | 113 urllib.urlcleanup() |
104 | 114 |
105 """Private Methods""" | 115 #Private Methods |
106 def __loadThread(self, path, image_type, imageId): | 116 def __loadThread(self, path, image_type, imageId): |
107 uriPath = urllib.unquote(path) | 117 uriPath = urllib.unquote(path) |
108 self.__lock.acquire() | 118 |
109 try: | 119 try: |
110 d = urllib.urlretrieve(uriPath) | 120 d = urllib.urlretrieve(uriPath) |
111 """We have to make sure that not only did we fetch something, but that | 121 # We have to make sure that not only did we fetch something, but that |
112 it was an image that we got back.""" | 122 # it was an image that we got back. |
113 if d[0] and d[1].getmaintype() == "image": | 123 if d[0] and d[1].getmaintype() == "image": |
114 self.__cache[path] = (path, d[0], d[1].gettype(), imageId) | 124 with self.__lock: |
115 self.__queue.put((self.__cache[path], image_type, imageId)) | 125 self.__cache[path] = (path, d[0], d[1].gettype(), imageId) |
116 if self.__fetching.has_key(path): del self.__fetching[path] | 126 self.__queue.put((self.__cache[path], image_type, imageId)) |
127 | |
128 if path in self.__fetching: | |
129 del self.__fetching[path] | |
117 else: | 130 else: |
118 logger.exception(str("Image refused to load or URI did not reference a valid image: " + path), True) | 131 logger.general("Image refused to load or URI did not " |
132 "reference a valid image: " + path, True) | |
119 del self.__fetching[path] | 133 del self.__fetching[path] |
120 except IOError: | 134 except IOError: |
121 del self.__fetching[path] | 135 del self.__fetching[path] |
122 logger.exception(str("Unable to resolve/open the specified URI; image was NOT loaded: " + path), True) | 136 logger.general("Unable to resolve/open the specified URI; " |
123 finally: self.__lock.release() | 137 "image was NOT laoded: " + path, True) |
124 | 138 |
125 def __loadCacheThread(self, path, image_type, imageId): | 139 def __loadCacheThread(self, path, image_type, imageId): |
126 try: | 140 try: |
127 st = time.time() | 141 st = time.time() |
128 while self.__fetching.has_key(path) and self.__fetching[path] is not False: | 142 while path in self.__fetching and self.__fetching[path] is not False: |
129 time.sleep(0.025) | 143 time.sleep(0.025) |
130 if (time.time()-st) > 120: | 144 if (time.time()-st) > 120: |
131 logger.general("Timeout: " + path) | 145 logger.general("Timeout: " + path, True) |
146 del self.__fetching[path] | |
132 break | 147 break |
133 except: | 148 except: |
134 del self.__fetching[path] | 149 del self.__fetching[path] |
135 logger.exception(str("Unable to resolve/open the specified URI; image was NOT loaded: " + path), True) | 150 logger.general("Unable to resolve/open the specified URI; " |
136 return | 151 "image was NOT loaded: " + path, True) |
137 self.__lock.acquire() | 152 return |
138 try: | |
139 logger.info("Adding Image to Queue from Cache: " + str(self.__cache[path]), True) | |
140 self.__queue.put((self.__cache[path], image_type, imageId)) | |
141 finally: self.__lock.release() | |
142 | 153 |
143 """Property Methods""" | 154 with self.__lock: |
155 if path in self.__cache: | |
156 logger.debug("Adding Image to Queue from Cache: " + str(self.__cache[path])) | |
157 self.__queue.put((self.__cache[path], image_type, imageId)) | |
158 else: | |
159 self.__loadThread(path, image_type, imageId) | |
160 | |
161 #Property Methods | |
144 def _getCache(self): | 162 def _getCache(self): |
145 return self.__cache | 163 return self.__cache |
146 | 164 |
147 def _getQueue(self): | 165 def _getQueue(self): |
148 return self.__queue | 166 return self.__queue |
149 | 167 |
150 """Properties""" | 168 #Properties |
151 Cache = property(_getCache) | 169 Cache = property(_getCache) |
152 Queue = property(_getQueue) | 170 Queue = property(_getQueue) |
153 | 171 |
154 ImageHandler = singleton(ImageHandlerClass) | 172 ImageHandler = ImageHandlerClass() |
155 component.add('ImageHandler', ImageHandler) |