boa-constructor /

# Name:
# Purpose:     Cache images used in a central place.
# Author:      Riaan Booysen
# Created:     2000/03/15
# RCS-ID:      $Id$
# Copyright:   (c) 1999, 2000 Riaan Booysen
# Licence:     GPL

from os import path
import sys, os, string

from wxPython import wx

class ImageStore:
    def __init__(self, rootpath, images = None, cache = 1):
        if not images: images = {}
        self.rootpath = rootpath
        self.images = images
        self.useCache = cache

    def createImage(self, filename, ext):
        if ext == '.bmp':
            return wx.wxImage(filename, wx.wxBITMAP_TYPE_BMP).ConvertToBitmap()
        elif ext == '.jpg':
            return wx.wxImage(filename, wx.wxBITMAP_TYPE_JPEG).ConvertToBitmap()
        elif ext == '.gif':
            return wx.wxImage(filename, wx.wxBITMAP_TYPE_GIF).ConvertToBitmap()
        elif ext == '.ico':
            return wx.wxIcon(filename, wx.wxBITMAP_TYPE_ICO)
            raise 'Extension not handled '+ext

    def load(self, name):
        name = path.normpath(name)
        if self.useCache:
            if not self.images.has_key(name):
                self.images[name] = self.createImage(path.join(self.rootpath,
                    name), path.splitext(name)[1])
            return self.images[name]
            return self.createImage(path.join(self.rootpath, name),

class ZippedImageStore(ImageStore):
    def __init__(self, rootpath, defaultArchive, images = None):
        ImageStore.__init__(self, rootpath, images)
        self.archives = {}

    def addArchive(self, defaultArchive):
        import zipfile, tempfile
        zf = zipfile.ZipFile(path.join(self.rootpath, defaultArchive))
        self.archives[defaultArchive] = map(lambda fl: fl.filename, zf.filelist)

        for img in self.archives[defaultArchive]:
            if img[-1] == '/':

            imgExt = path.splitext(img)[1]
            bmpPath = path.join(path.splitext(defaultArchive)[0],

            tmpname = tempfile.mktemp()
            open(tmpname, 'w').write(
                if not self.images.has_key(bmpPath):
                        self.images[bmpPath] = self.createImage(tmpname, imgExt)
                    except Exception, error:
                        print 'Ext not handled', bmpPath, str(error)


    def load(self, name):
        name = path.normpath(name)
            return self.images[name]
        except KeyError:
            print name, 'not found by zipped image store'
            return wx.wxNullBitmap
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.