diff orpg/mapper/images.py @ 13:211ac836b6a0 grumpy-goblin

{090731-00} Fixes problem with Name & Settings window, cleans code.
author sirebral
date Fri, 31 Jul 2009 15:22:11 -0500
parents 5df1340bda13
children 97265586402b
line wrap: on
line diff
--- a/orpg/mapper/images.py	Fri Jul 24 10:03:22 2009 -0500
+++ b/orpg/mapper/images.py	Fri Jul 31 15:22:11 2009 -0500
@@ -51,21 +51,20 @@
 
     def load(self, path, image_type, imageId):
         # Load an image, with a intermideary fetching image shown while it loads in a background thread
-        if self.__cache.has_key(path):
-            return wx.ImageFromMime(self.__cache[path][1], self.__cache[path][2]).ConvertToBitmap()
+        if self.__cache.has_key(path): return wx.ImageFromMime(self.__cache[path][1], 
+                                                self.__cache[path][2]).ConvertToBitmap()
         if not self.__fetching.has_key(path):
             self.__fetching[path] = True
             #Start Image Loading Thread
             thread.start_new_thread(self.__loadThread, (path, image_type, imageId))
         else:
-            if self.__fetching[path] is True:
-                thread.start_new_thread(self.__loadCacheThread, (path, image_type, imageId))
+            if self.__fetching[path] is True: thread.start_new_thread(self.__loadCacheThread, (path, image_type, imageId))
         return wx.Bitmap(open_rpg.get_component("dir_struct")["icon"] + "fetching.png", wx.BITMAP_TYPE_PNG)
 
     def directLoad(self, path):
         # Directly load an image, no threads
-        if self.__cache.has_key(path):
-            return wx.ImageFromMime(self.__cache[path][1], self.__cache[path][2]).ConvertToBitmap()
+        if self.__cache.has_key(path): return wx.ImageFromMime(self.__cache[path][1], 
+                                                self.__cache[path][2]).ConvertToBitmap()
         uriPath = urllib.unquote(path)
         try:
             d = urllib.urlretrieve(uriPath)
@@ -75,32 +74,29 @@
                 self.__cache[path] = (path, d[0], d[1].gettype(), None)
                 return wx.ImageFromMime(self.__cache[path][1], self.__cache[path][2]).ConvertToBitmap()
             else:
-                open_rpg.get_component('log').log("Image refused to load or URI did not reference a valid image: " + path, ORPG_GENERAL, True)
+                open_rpg.get_component('log').log("Image refused to load or URI did not reference a valid image: " + path, 
+                    ORPG_GENERAL, True)
                 return None
         except IOError:
-            open_rpg.get_component('log').log("Unable to resolve/open the specified URI; image was NOT loaded: " + path, ORPG_GENERAL, True)
+            open_rpg.get_component('log').log("Unable to resolve/open the specified URI; image was NOT loaded: " + path, 
+                ORPG_GENERAL, True)
             return None
 
     def cleanCache(self):
         # Shrinks the Cache down to the proper size
-        try:
-            cacheSize = int(open_rpg.get_component('settings').get_setting("ImageCacheSize"))
-        except:
-            cacheSize = 32
+        try: cacheSize = int(open_rpg.get_component('settings').get_setting("ImageCacheSize"))
+        except: cacheSize = 32
         cache = self.__cache.keys()
         cache.sort()
-        for key in cache[cacheSize:]:
-            del self.__cache[key]
+        for key in cache[cacheSize:]: del self.__cache[key]
 
     def flushCache(self):
         #    This function will flush all images contained within the image cache.
         self.__lock.acquire()
         try:
             keyList = self.__cache.keys()
-            for key in keyList:
-                del self.__cache[key]
-        finally:
-            self.__lock.release()
+            for key in keyList: del self.__cache[key]
+        finally: self.__lock.release()
         urllib.urlcleanup()
 
 #Private Methods
@@ -114,35 +110,35 @@
             if d[0] and d[1].getmaintype() == "image":
                 self.__cache[path] = (path, d[0], d[1].gettype(), imageId)
                 self.__queue.put((self.__cache[path], image_type, imageId))
-                if self.__fetching.has_key(path):
-                    del self.__fetching[path]
+                if self.__fetching.has_key(path): del self.__fetching[path]
             else:
-                open_rpg.get_component('log').log("Image refused to load or URI did not reference a valid image: " + path, ORPG_GENERAL, True)
+                open_rpg.get_component('log').log("Image refused to load or URI did not reference a valid image: " + path, 
+                    ORPG_GENERAL, True)
                 del self.__fetching[path]
         except IOError:
             del self.__fetching[path]
-            open_rpg.get_component('log').log("Unable to resolve/open the specified URI; image was NOT laoded: " + path, ORPG_GENERAL, True)
-        finally:
-            self.__lock.release()
+            open_rpg.get_component('log').log("Unable to resolve/open the specified URI; image was NOT laoded: " + path, 
+                ORPG_GENERAL, True)
+        finally: self.__lock.release()
 
     def __loadCacheThread(self, path, image_type, imageId):
-        try:
-            st = time.time()
-            while self.__fetching.has_key(path) and self.__fetching[path] is not False:
-                time.sleep(0.025)
-                if (time.time()-st) > 120:
-                    open_rpg.get_component('log').log("Timeout: " + path, ORPG_GENERAL, True)
-                    break
-        except:
-            self.__fetching[path] = False
-            open_rpg.get_component('log').log("Unable to resolve/open the specified URI; image was NOT loaded: " + path, ORPG_GENERAL, True)
-            return 
-        self.__lock.acquire()
-        try:
-            open_rpg.get_component('log').log("Adding Image to Queue from Cache: " + str(self.__cache[path]), ORPG_DEBUG)
-            self.__queue.put((self.__cache[path], image_type, imageId))
-        finally:
-            self.__lock.release()
+        if self.__cache.has_key(path):
+            try:
+                st = time.time()
+                while self.__fetching.has_key(path) and self.__fetching[path] is not False:
+                    time.sleep(0.025)
+                    if (time.time()-st) > 120:
+                        open_rpg.get_component('log').log("Timeout: " + path, ORPG_GENERAL, True)
+                        break
+            except:
+                del self.__fetching[path]
+                open_rpg.get_component('log').log("Unable to resolve/open the specified URI; image was NOT loaded: " + path, ORPG_GENERAL, True)
+                return 
+            self.__lock.acquire()
+            try:
+                open_rpg.get_component('log').log("Adding Image to Queue from Cache: " + str(self.__cache[path]), ORPG_DEBUG)
+                self.__queue.put((self.__cache[path], image_type, imageId))
+            finally: self.__lock.release()
 
 #Property Methods
     def _getCache(self):