#!/usr/bin/env python import gimpfu, gimp, string, sys 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[1:]) + ["query"] for i in a: items.append (name + "." + i) elif obj and (not callable (obj) or next != ""): 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__) dot = string.rfind (text, ".") if dot >= 0: candidates = candidates + self.get_expanditems (text[:dot], text[dot:]) 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 (have_gimp=1): # Set up a sane environment environ = globals().copy() del environ["gimp_shell"] del environ["MC"] del environ["string"] del environ["sys"] import os, sys, code, readline mc = MC(environ) # setup readline if have_gimp: 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 if have_gimp: banner = "Gimp-Python interactive Console\n (C) 2002 Simon Budig " else: banner = "Gimp-Python interactive Console\n (C) 2002 Simon Budig \n\nWARNING: No connection to The GIMP! - gsh may crash unexpectedly." sys.ps1 = "!>>> " sys.ps2 = "!... " ip = code.InteractiveConsole (environ) ip.interact (banner) # write back history if have_gimp: 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 ", "2002", "/Xtns/Python-Fu/Gimp-Shell", "RGB*, GRAY*, INDEXED*", [], [], gimp_shell) # do different stuff depending on the commandline if len(sys.argv) == 1: # no extra parameters. Started via the commandline (useful # for debugging only, since it not possible to find a connection # to a running gimp gimp_shell (0) elif "-query" in sys.argv: # we get queried by the gimp. Nothing special required gimpfu.main() elif sys.argv[-1] == "-has-xterm": # we have been restarted by ourselves. No need to open an xterm. # For gimp-compatibility remove our added parameter. sys.argv = sys.argv[:-1] gimpfu.main() else: # We have been started by the gimp. Since we cannot be sure that # we have a controlling terminal (gimp may have been started from # a button) restart ourselves via an xterm. We add an extra parameter # to be able to detect, if we already started an xterm. import os os.execvp ("xterm", ["xterm", "-title", "Gimp Shell", "-e"] + sys.argv + ["-has-xterm"])