home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 May / maximum-cd-2009-05.iso / DiscContents / gimp-2.6.5-i686-setup.exe / {app} / lib / gimp / 2.0 / python / gimpshelf,2.py < prev    next >
Encoding:
Python Source  |  2009-02-15  |  2.8 KB  |  93 lines

  1. #   Gimp-Python - allows the writing of Gimp plugins in Python.
  2. #   Copyright (C) 1997  James Henstridge <james@daa.com.au>
  3. #
  4. #   This program is free software; you can redistribute it and/or modify
  5. #   it under the terms of the GNU General Public License as published by
  6. #   the Free Software Foundation; either version 2 of the License, or
  7. #   (at your option) any later version.
  8. #
  9. #   This program is distributed in the hope that it will be useful,
  10. #   but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. #   GNU General Public License for more details.
  13. #
  14. #   You should have received a copy of the GNU General Public License
  15. #   along with this program; if not, write to the Free Software
  16. #   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17.  
  18. # gimpshelf.py -- a simple module to help gimp modules written in Python
  19. #                 store persistent data.
  20. #
  21. # Copyright (C) 1997, James Henstridge
  22. #
  23. # The gimp module provides a basic method for storing information that persists
  24. # for a whole gimp session, but only allows for the storage of strings.  This
  25. # is because other Python types usually have pointers to other Python objects,
  26. # making it dificult to work out what to save.  This module gives an interface
  27. # to the gimp module's primitive interface, which resembles the shelve module.
  28.  
  29. # use cPickle and cStringIO if available
  30.  
  31. try:
  32.     import cPickle as pickle
  33. except ImportError:
  34.     import pickle
  35.  
  36. try:
  37.     import cStringIO as StringIO
  38. except ImportError:
  39.     import StringIO
  40.  
  41. import gimp
  42.  
  43. import copy_reg
  44.  
  45. def _image_id(obj):
  46.     return gimp._id2image, (obj.ID,)
  47.  
  48. def _drawable_id(obj):
  49.     return gimp._id2drawable, (obj.ID,)
  50.  
  51. def _display_id(obj):
  52.     return gimp._id2display, (obj.ID,)
  53.  
  54. def _vectors_id(obj):
  55.     return gimp._id2vectors, (int(obj.ID),)
  56.  
  57. copy_reg.pickle(gimp.Image,   _image_id,    gimp._id2image)
  58. copy_reg.pickle(gimp.Layer,   _drawable_id, gimp._id2drawable)
  59. copy_reg.pickle(gimp.Channel, _drawable_id, gimp._id2drawable)
  60. copy_reg.pickle(gimp.Display, _display_id,  gimp._id2display)
  61. copy_reg.pickle(gimp.Vectors, _vectors_id,  gimp._id2vectors)
  62.  
  63. del copy_reg, _image_id, _drawable_id, _display_id, _vectors_id
  64.  
  65. class Gimpshelf:
  66.     def has_key(self, key):
  67.         try:
  68.             s = gimp.get_data(key)
  69.             return 1
  70.         except gimp.error:
  71.             return 0
  72.  
  73.     def __getitem__(self, key):
  74.         try:
  75.             s = gimp.get_data(key)
  76.         except gimp.error:
  77.             raise KeyError, key
  78.  
  79.         f = StringIO.StringIO(s)
  80.         return pickle.Unpickler(f).load()
  81.  
  82.     def __setitem__(self, key, value):
  83.         f = StringIO.StringIO()
  84.         p = pickle.Pickler(f)
  85.         p.dump(value)
  86.         gimp.set_data(key, f.getvalue())
  87.  
  88.     def __delitem__(self, key):
  89.         gimp.set_data(key, '')
  90.  
  91. shelf = Gimpshelf()
  92. del Gimpshelf
  93.