Hi all.

I have written a small Gimp Shell to explore Gimp Python (I currently
use the debian-testing packages for this).

Simply drop it in ~/.gimp-1.2/plug-ins .

To be able to use it (<Toolbox>/Xtns/Python-Fu/Gimp-Shell)
you have to start the Gimp from an Terminal.

The thing has completion and history. You can start exploring
Gimp-Python by entering "gimp.<TAB><TAB>"  .

Have fun!
        Simon
-- 
      [EMAIL PROTECTED]       http://www.home.unix-ag.org/simon/
#!/usr/bin/env python

import gimpfu, gimp, string

class MC:
   def __init__(self, env):
      self.completions = []
      self.environ = env

   def filter (self, text):
      return string.find (text, self.pattern) == 0
      
   def get_expanditems (self, name, next=""):
      items = []
      try:
         obj = eval (name, self.environ)
      except:
         obj = None

      # Eeek - the PDB implementation of Gimp Python is evil.
      if type (obj) == type (gimp.pdb):
         a = obj.query (next)
         for i in a:
            items.append (name + "." + i)
      elif obj and not callable (obj):
         a = dir (obj)
         for i in a:
            items.append (name + "." + i)
      return items

   def complete (self, text, state):
      self.pattern = text
      if state == 0:
         candidates = self.environ.keys () + dir (__builtins__)
         if string.rfind (text, "."):
            candidates = candidates + self.get_expanditems (text[:string.rfind (text, 
".")], text[string.rfind (text, ".")+1:])

         self.completions = filter (self.filter, candidates)

         while len (self.completions) == 1:
            name = self.completions[0]

            items = self.get_expanditems (name)
            if items:
               del (self.completions[0])
               self.completions = self.completions + items
            else:
               break

      if state < len (self.completions):
         return self.completions[state]
      else:
         return None

def gimp_shell ():
   # Set up a sane environment
   environ = globals().copy()
   del environ["gimp_shell"]
   del environ["MC"]
   del environ["string"]

   import os, sys, code, readline

   mc = MC(environ)

   # setup readline
   historyfile = "%s/.gimp-%s/gsh_history" % (os.getenv("HOME"), gimp.pdb.gimp_version 
()[:3])
   f = open (historyfile, "a") ; f.close ()
   readline.read_history_file(historyfile)
   readline.set_completer (mc.complete)
   readline.parse_and_bind("tab: complete")

   # enter interactive console
   ip = code.InteractiveConsole (environ)
   ip.interact ("Gimp-Python interactive Console\n (C) 2002  Simon Budig  
<[EMAIL PROTECTED]>")

   # write back history
   readline.set_history_length (1000)
   readline.write_history_file(historyfile)
   print

gimpfu.register(
   "python_fu_gimp_shell",
   "Simple Interface to Gimp Python",
   "Simple Shell-style Interface for Gimp Python",
   "Simon Budig",
   "Simon Budig  <[EMAIL PROTECTED]>",
   "2002",
   "<Toolbox>/Xtns/Python-Fu/Gimp-Shell",
   "RGB*, GRAY*, INDEXED*",
   [],
   [],
   gimp_shell)

gimpfu.main()

Reply via email to