#
# Copyright 2012-2013 Alejandro Autalán
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, 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, see <http://www.gnu.org/licenses/>.
#
# For further info, check  http://pygubu.web.here

__all__ = ['StockImage']

import os
import logging
import tkinter

stock_data = {}

logger = logging.getLogger('stockimage')


#class StockImage(Object):
class StockImage:
    """Maintain references to image name and file.
When image is used, the class maintains it on memory for tkinter"""
    _stock = stock_data
    _cached = {}


    @classmethod
    def register(cls, key, filename):
        if key in cls._stock:
            logger.info('Warning, replacing resource ' + str(key))
        cls._stock[key] = {'type': 'custom', 'filename': filename}
        logger.info('%s registered as %s' % (filename, key))


    @classmethod
    def register_from_dir(cls, dir_path):
        for filename in os.listdir(dir_path):
            filekey = filename.lower()
            if filekey.endswith('.gif'):
                cls.register(filekey[:-4], os.path.join(dir_path, filename))


    @classmethod
    def get(cls, rkey):
        if rkey in cls._cached:
            logger.info('Resource %s is in cache.' % rkey)
            return cls._cached[rkey]
        if rkey in cls._stock:
            v = cls._stock[rkey]
            img = None
            if v['type'] == 'stock':
                img = tkinter.PhotoImage(format=v['format'],data=v['data'])
            else:
                img = tkinter.PhotoImage(file=v['filename'])
            cls._cached[rkey] = img
            logger.info('Loaded resource %s.' % rkey)
            return img
        else:
            raise Exception('StockImage: %s not registered.' % rkey)


@@ -801,21 +801,66 @@
 ger(
-'s
+__name__)%0A%0A%0Aclass S
 tock
-i
+I
 mage
-')
+Exception(Exception):%0A    pass
 %0A%0A%0A#
@@ -1063,16 +1063,41 @@
 ed = %7B%7D%0A
+    _formats = ('.gif',)%0A
 %0A%0A    @c
@@ -1145,16 +1145,63 @@
 ename):%0A
+        %22%22%22Register a image file using key%22%22%22%0A%0A
         
@@ -1467,34 +1467,280 @@
 ir(cls, dir_path
-):
+, prefix=''):%0A        %22%22%22List files from dir_path and register images with%0A            filename as key (without extension)%0A        Additionaly a prefix for the key can be provided,%0A        so the resulting key will be prefix + filename%0A        %22%22%22%0A
 %0A        for fil
@@ -1786,26 +1786,50 @@
     
-filekey = 
+name, file_ext = os.path.splitext(
 filename
 .low
@@ -1828,15 +1828,8 @@
 name
-.lower(
 )%0A  
@@ -1849,29 +1849,82 @@
 file
-key.endswith('.gif'):
+_ext in cls._formats:%0A                fkey = '%7B0%7D%7B1%7D'.format(prefix, name)
 %0A   
@@ -1954,19 +1954,11 @@
 er(f
-ilekey%5B:-4%5D
+key
 , os
@@ -2016,19 +2016,27 @@
     def 
+_load_ima
 ge
-t
 (cls, rk
@@ -2052,110 +2052,42 @@
     
-if rkey in cls._cached:%0A            logger.info('Resource %25s is in cache.' %25 rkey)%0A           
+%22%22%22Load image from file or
  return 
 cls.
@@ -2086,61 +2086,33 @@
 urn 
-cls._
+the 
 cached
-%5Brkey%5D%0A        if rkey in cls._stock:%0A    
+ instance.%22%22%22%0A%0A
     
@@ -2140,28 +2140,24 @@
 ey%5D%0A        
-  
-  
 img = None%0A 
@@ -2155,20 +2155,16 @@
  = None%0A
-    
         
@@ -2188,20 +2188,16 @@
 stock':%0A
-    
         
@@ -2264,28 +2264,24 @@
 '%5D)%0A        
-    
 else:%0A      
@@ -2278,36 +2278,32 @@
 se:%0A            
- 
-   
 img = tkinter.Ph
@@ -2335,28 +2335,24 @@
 '%5D)%0A        
-    
 cls._cached%5B
@@ -2363,36 +2363,32 @@
 %5D = img%0A        
- 
-   
 logger.info('Loa
@@ -2413,16 +2413,400 @@
 %25 rkey)%0A
+        return img%0A%0A%0A    @classmethod%0A    def get(cls, rkey):%0A        %22%22%22Get image previously registered with key rkey.%0A        If key not exist, raise StockImageException%0A        %22%22%22%0A%0A        if rkey in cls._cached:%0A            logger.info('Resource %25s is in cache.' %25 rkey)%0A            return cls._cached%5Brkey%5D%0A        if rkey in cls._stock:%0A            img = cls._load_image(rkey)%0A
         
@@ -2852,16 +2852,26 @@
   raise 
+StockImage
 Exceptio

Add StockImageException. Allow to register a directory with a prefix