Package imagizer :: Module imagecache |
[hide private]
[frames] | no frames]

# Source Code for Module imagizer.imagecache

     1  #!/usr/bin/env python
     2  # -*- coding: UTF8 -*-
     3  #******************************************************************************\
     4  #* $Source: /srv/cvs/imagizer/doc/html/imagizer.imagecache-pysrc.html,v $
     5  #* $Id: imagizer.imagecache-pysrc.html,v 1.1 2010-04-28 19:44:10 jerome Exp $
     6  #*
     7  #* Copyright (C) 2006-2009,  Jérome Kieffer <kieffer@terre-adelie.org>
     8  #* Conception : Jérôme KIEFFER, Mickael Profeta & Isabelle Letard
     9  #* Licence GPL v2
    10  #*
    11  #* This program is free software; you can redistribute it and/or modify
    12  #* it under the terms of the GNU General Public License as published by
    13  #* the Free Software Foundation; either version 2 of the License, or
    14  #* (at your option) any later version.
    15  #*
    16  #* This program is distributed in the hope that it will be useful,
    17  #* but WITHOUT ANY WARRANTY; without even the implied warranty of
    18  #* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    19  #* GNU General Public License for more details.
    20  #*
    21  #* You should have received a copy of the GNU General Public License
    22  #* along with this program; if not, write to the Free Software
    23  #* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    24  #*
    25  #*****************************************************************************/
    26
    27  """
    28  ImageCache is a class containing a copy of the bitmap of images .
    29  Technically it is a Borg (design Pattern) so every instance of ImageCache has exactly the same contents.
    30  """
    31  from config import Config
    32  config = Config()
    33  ################################################################################################
    34  ###############  Class ImageCache for storing the bitmaps in a Borg ############################
    35  ################################################################################################
    36 -class ImageCache(dict):
    37      """this class is a Borg : always returns the same values regardless to the instance of the object
    38      it is used as data storage for bitmaps ... with a limit of max_len
    39      """
    40      __shared_state = {}
    41      __data_initialized = False
    42 -    def __init__(self, maxSize=1000000):
    43          self.__dict__ = self.__shared_state
    44          if  not ImageCache.__data_initialized :
    45              ImageCache.__data_initialized = True
    46              self.ordered = []
    47              self.imageDict = {}
    48              self.maxSize = maxSize
    49              self.size = 0
    50 -    def __setitem__(self, key, value):
    51          """x.__setitem__(i, y) <==> x[i]=y"""
    52          self.imageDict[ key ] = value
    53          if key in self.ordered:
    54              index = self.ordered.index(key)
    55              self.ordered.pop(index)
    56              pixBuf = self.imageDict[ key ]
    57              self.size -= 3 * pixBuf.get_width() * pixBuf.get_height()
    58          self.size += 3 * value.get_width() * value.get_height()
    59          if self.size > self.maxSize:
    60              firstKey = self.ordered[ 0 ]
    61              if config.DEBUG: print("Removing file %s from cache" % firstKey)
    62              firstPixBuf = self.imageDict.pop(firstKey)
    63              self.size -= 3 * firstPixBuf.get_width() * firstPixBuf.get_height()
    64              self.ordered = self.ordered[1:]
    65          self.ordered.append(key)
    66
    67 -    def __getitem__(self, key):
    68          """x.__getitem__(y) <==> x[y]"""
    69          index = self.ordered.index(key)
    70          self.ordered.pop(index)
    71          self.ordered.append(key)
    72          return self.imageDict[ key ]
    73