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$ 
  5  #* $Id$ 
  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  import logging 
 32  from config import Config 
 33  config = Config() 
 34   
 35   
 36  ################################################################################################ 
 37  ###############  Class ImageCache for storing the bitmaps in a Borg ############################ 
 38  ################################################################################################ 
39 -class ImageCache(dict):
40 """ 41 this class is a Borg : always returns the same values regardless to the instance of the object 42 it is used as data storage for images ... with a limit on the number of images to keep in memory. 43 """ 44 __shared_state = {} 45 __data_initialized = False 46 47
48 - def __init__(self, maxSize=100):
49 """ 50 Constructor of ImageCache 51 @param maxSize: number of element to keep in memory 52 """ 53 self.__dict__ = self.__shared_state 54 if ImageCache.__data_initialized is False: 55 ImageCache.__data_initialized = True 56 logging.debug("ImageCache.__init__: initalization of the Borg") 57 self.ordered = [] 58 self.imageDict = {} 59 self.maxSize = maxSize 60 self.size = 0
61 62
63 - def __setitem__(self, key, value):
64 """ 65 x.__setitem__(i, y) <==> x[i]=y 66 """ 67 logging.debug("ImageCache.__setitem__: %s" % key) 68 self.imageDict[ key ] = value 69 if key in self.ordered: 70 index = self.ordered.index(key) 71 self.ordered.pop(index) 72 self.size -= 1 73 self.size += 1 74 if self.size > self.maxSize: 75 firstKey = self.ordered[ 0 ] 76 if config.DEBUG: 77 print("Removing file %s from cache" % firstKey) 78 self.imageDict.pop(firstKey) 79 self.size -= 1 80 self.ordered = self.ordered[1:] 81 self.ordered.append(key)
82 83
84 - def __getitem__(self, key):
85 """ 86 x.__getitem__(y) <==> x[y] 87 """ 88 logging.debug("ImageCache.__setitem__: %s" % key) 89 index = self.ordered.index(key) 90 self.ordered.pop(index) 91 self.ordered.append(key) 92 return self.imageDict[ key ]
93 94
95 - def keys(self):
96 """ 97 Returns the list of keys, ordered 98 """ 99 logging.debug("ImageCache.keys") 100 return self.ordered[:]
101 102
103 - def pop(self, key):
104 """ 105 Remove a key for the dictionary and return it's value 106 """ 107 logging.debug("ImageCache.pop %s" % key) 108 try: 109 index = self.ordered.index(key) 110 except: 111 raise KeyError 112 self.ordered.pop(index) 113 myData = self.imageDict.pop(key) 114 return myData
115
116 - def rename(self, oldKey, newKey):
117 """ 118 Change the name of a key without affecting anything else 119 If the name is not present: do nothing. 120 """ 121 logging.debug("ImageCache.rename %s->%s" % (oldKey, newKey)) 122 if oldKey not in self.ordered: 123 return 124 index = self.ordered.index(oldKey) 125 self.ordered[index] = newKey 126 myData = self.imageDict.pop(oldKey) 127 self.imageDict[newKey] = myData
128