view orpg/mapper/images.py @ 184:dcae32e219f1 beta

Traipse Beta 'OpenRPG' {100117-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) New Features: Added Bookmarks 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 New TrueDebug Class in orpg_log (See documentation for usage) Portable Mercurial Tip of the Day added, from Core and community New Reference Syntax added for custom PC sheets New Child Reference for gametree New Parent Reference for gametree New Gametree Recursion method, mapping, context sensitivity, and effeciency.. New Features node with bonus nodes and Node Referencing help added Dieroller structure from Core New DieRoller portability for odd Dice Added 7th Sea die roller; ie [7k3] = [7d10.takeHighest(3).open(10)] New 'Mythos' System die roller added Added new vs. die roller method for WoD; ie [3v3] = [3d10.vs(3)]. Included for Mythos roller also New Warhammer FRPG Die Roller (Special thanks to Puu-san for the support) New EZ_Tree Reference system. Push a button, Traipse the tree, get a reference (Beta!) Fixes: Fix to Text based Server Fix to Remote Admin Commands Fix to Pretty Print, from Core Fix to Splitter Nodes not being created Fix to massive amounts of images loading, from Core Fix to Map from gametree not showing to all clients Fix to gametree about menus Fix to Password Manager check on startup Fix to PC Sheets from tool nodes. They now use the tabber_panel Fix to Whiteboard ID to prevent random line or text deleting. Fixes to Server, Remote Server, and Server GUI Fix to Update Manager; cleaner clode for saved repositories Fixes made to Settings Panel and now reactive settings when Ok is pressed Fixes to Alternity roller's attack roll. Uses a simple Tuple instead of a Splice Fix to Use panel of Forms and Tabbers. Now longer enters design mode Fix made Image Fetching. New fetching image and new failed image Modified ID's to prevent non updated clients from ruining the fix. default_manifest.xml renamed to default_upmana.xml
author sirebral
date Sun, 17 Jan 2010 21:37:34 -0600
parents 0d9b746b5751
children
line wrap: on
line source

# Copyright (C) 2000-2001 The OpenRPG Project
#
#    openrpg-dev@lists.sourceforge.net
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
# --
#
# File: mapper/images.py
# Author: OpenRPG
# Maintainer:
# Version:
#   $Id: images.py,v Traipse 'Ornery-Orc' prof.ebral Exp $
#
# Description:
#
from __future__ import with_statement

import urllib, Queue, thread, time
from threading import Lock
from orpg.orpg_wx import *
from orpg.orpgCore import *

from orpg.dirpath import dir_struct
from orpg.tools.orpg_log import logger
from orpg.tools.settings import settings

class ImageHandlerClass(object):
    __cache = {}
    __fetching = {}
    __queue = Queue.Queue(0)
    __lock = Lock()

    def __new__(cls):
        it = cls.__dict__.get("__it__")
        if it is not None:
            return it
        cls.__it__ = it = object.__new__(cls)
        return it

    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])
        if path not in self.__fetching:
            self.__fetching[path] = True
            #Start Image Loading Thread
            thread.start_new_thread(self.__loadThread,
                                    (path, image_type, imageId))
        else:
            if self.__fetching[path]:
                thread.start_new_thread(self.__loadCacheThread,
                                        (path, image_type, imageId))
        return wx.Image(dir_struct["icon"] + "fetching.png", wx.BITMAP_TYPE_PNG)

    def directLoad(self, path):
        # Directly load an image, no threads
        if path in self.__cache:
            return wx.ImageFromMime(self.__cache[path][1],
                                    self.__cache[path][2])
        uriPath = urllib.unquote(path)
        try:
            d = urllib.urlretrieve(uriPath)
            # We have to make sure that not only did we fetch something, but that
            # it was an image that we got back.
            if d[0] and d[1].getmaintype() == "image":
                with self.__lock:
                    self.__cache[path] = (path, d[0], d[1].gettype(), None)
                return wx.ImageFromMime(self.__cache[path][1], self.__cache[path][2])
            else:
                logger.general("Image refused to load or URI did not "
                               "reference a valid image: " + path, True)
                return None
        except IOError:
            logger.general("Unable to resolve/open the specified URI; "
                           "image was NOT loaded: " + path, True)
            return None

    def cleanCache(self):
        # Shrinks the Cache down to the proper size
        try:
            cacheSize = int(settings.get("ImageCacheSize"))
        except:
            cacheSize = 32
        cache = self.__cache.keys()
        cache.sort()
        for key in cache[cacheSize:]:
            del self.__cache[key]

    def flushCache(self):
        # This function will flush all images contained within the image cache.
        with self.__lock:
            self.__cache = {}
            self.__fetching = {}
            urllib.urlcleanup()

    #Private Methods
    def __loadThread(self, path, image_type, imageId):
        uriPath = urllib.unquote(path)
        try:
            d = urllib.urlretrieve(uriPath)
            # We have to make sure that not only did we fetch something, but that
            # it was an image that we got back.
            if d[0] and d[1].getmaintype() == "image":
                with self.__lock:
                    self.__cache[path] = (path, d[0], d[1].gettype(), imageId)
                    self.__queue.put((self.__cache[path], image_type, imageId))
                if path in self.__fetching: del self.__fetching[path]
            else:
                logger.general("Image refused to load or URI did not "
                               "reference a valid image: " + path, True)
                self.__queue.put(('failed', image_type, imageId))
                del self.__fetching[path]
        except IOError:
            del self.__fetching[path]
            logger.general("Unable to resolve/open the specified URI; "
                           "image was NOT laoded: " + path, True)
            self.__queue.put((dir_struct["icon"] + "failed.png", image_type, imageId))

    def __loadCacheThread(self, path, image_type, imageId):
        try:
            st = time.time()
            while path in self.__fetching and self.__fetching[path] is not False:
                time.sleep(0.025)
                if (time.time()-st) > 120:
                    logger.general("Timeout: " + path, True)
                    del self.__fetching[path]
                    break
        except:
            del self.__fetching[path]
            logger.general("Unable to resolve/open the specified URI; "
                           "image was NOT loaded: " + path, True)
            return
        with self.__lock:
            if path in self.__cache:
                logger.debug("Adding Image to Queue from Cache: " + str(self.__cache[path]))
                self.__queue.put((self.__cache[path], image_type, imageId))
            else: self.__loadThread(path, image_type, imageId)

    #Property Methods
    def _getCache(self):
        return self.__cache

    def _getQueue(self):
        return self.__queue

    #Properties
    Cache = property(_getCache)
    Queue = property(_getQueue)

ImageHandler = ImageHandlerClass()