home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / gimp / 2.0 / plug-ins / gtkcons.py < prev    next >
Encoding:
Python Source  |  2006-07-10  |  10.4 KB  |  305 lines

  1. #!/usr/bin/env python
  2.  
  3. #   Interactive Python-GTK Console
  4. #   Copyright (C), 1998 James Henstridge <james@daa.com.au>
  5. #
  6. #   This program is free software; you can redistribute it and/or modify
  7. #   it under the terms of the GNU General Public License as published by
  8. #   the Free Software Foundation; either version 2 of the License, or
  9. #   (at your option) any later version.
  10. #
  11. #   This program is distributed in the hope that it will be useful,
  12. #   but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. #   GNU General Public License for more details.
  15. #
  16. #   You should have received a copy of the GNU General Public License
  17. #   along with this program; if not, write to the Free Software
  18. #   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19.  
  20. # This module implements an interactive python session in a GTK window.  To
  21. # start the session, use the gtk_console command.  Its specification is:
  22. #   gtk_console(namespace, title, copyright)
  23. # where namespace is a dictionary representing the namespace of the session,
  24. #       title     is the title on the window and
  25. #       copyright is any additional copyright info to print.
  26. #
  27. # As well as the starting attributes in namespace, the session will also
  28. # have access to the list __history__, which is the command history.
  29.  
  30. import sys, string, traceback
  31. import pango, gtk, gtk.keysyms
  32.  
  33. stdout = sys.stdout
  34.  
  35. if not hasattr(sys, 'ps1'): sys.ps1 = '>>> '
  36. if not hasattr(sys, 'ps2'): sys.ps2 = '... '
  37.  
  38. # some functions to help recognise breaks between commands
  39. def remQuotStr(s):
  40.     '''Returns s with any quoted strings removed (leaving quote marks)'''
  41.     ret = ''
  42.     in_quote = 0
  43.     quote = ''
  44.     prev = None
  45.     for ch in s:
  46.         if in_quote and (ch != quote or prev == '\\'):
  47.             if ch == '\\' and prev == '\\':
  48.                 prev = None
  49.             else:
  50.                 prev = ch
  51.             continue
  52.             prev = ch
  53.         if ch in '\'"':
  54.             if in_quote:
  55.                 in_quote = 0
  56.             else:
  57.                 in_quote = 1
  58.                 quote = ch
  59.         ret = ret + ch
  60.     return ret
  61.  
  62. def bracketsBalanced(s):
  63.     '''Returns true iff the brackets in s are balanced'''
  64.     stack = []
  65.     brackets = {'(':')', '[':']', '{':'}'}
  66.     for ch in s:
  67.         if ch in '([{':
  68.             stack.append(ch)
  69.         elif ch in '}])':
  70.             if len(stack) != 0 and brackets[stack[-1]] == ch:
  71.                 del stack[-1]
  72.             else:
  73.                 return 0
  74.     return len(stack) == 0
  75.  
  76. class gtkoutfile:
  77.     '''A fake output file object.  It sends output to a TK test widget,
  78.     and if asked for a file number, returns one set on instance creation'''
  79.     def __init__(self, buffer, fileno, tag):
  80.         self.__fileno = fileno
  81.         self.__buffer = buffer
  82.         self.__tag = tag
  83.     def close(self): pass
  84.     flush = close
  85.     def fileno(self):    return self.__fileno
  86.     def isatty(self):    return 0
  87.     def read(self, a):   return ''
  88.     def readline(self):  return ''
  89.     def readlines(self): return []
  90.     def write(self, string):
  91.         #stdout.write(str(self.__w.get_point()) + '\n')
  92.         iter = self.__buffer.get_end_iter()
  93.         self.__buffer.insert_with_tags_by_name(iter, string, self.__tag)
  94.     def writelines(self, lines):
  95.         iter = self.__buffer.get_end_iter()
  96.         for line in lines:
  97.             self.__buffer.insert_with_tags_by_name(iter, lines, self.__tag)
  98.     def seek(self, a):   raise IOError, (29, 'Illegal seek')
  99.     def tell(self):      raise IOError, (29, 'Illegal seek')
  100.     truncate = tell
  101.  
  102. class Console(gtk.VBox):
  103.     def __init__(self, namespace={}, quit_cb=None):
  104.         gtk.VBox.__init__(self, spacing=2)
  105.         self.set_border_width(2)
  106.  
  107.         self.quit_cb = quit_cb
  108.  
  109.         swin = gtk.ScrolledWindow()
  110.         swin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
  111.         self.pack_start(swin)
  112.         swin.show()
  113.  
  114.         self.vadj = swin.get_vadjustment()
  115.  
  116.         self.textview = gtk.TextView()
  117.         self.textview.set_editable(gtk.FALSE)
  118.         self.textview.set_cursor_visible(gtk.FALSE)
  119.         self.textview.set_wrap_mode(gtk.WRAP_WORD)
  120.         self.buffer = self.textview.get_buffer()
  121.         swin.add(self.textview)
  122.         self.textview.show()
  123.  
  124.         # tags to use when inserting text ...
  125.         tag = self.buffer.create_tag('normal')
  126.         tag = self.buffer.create_tag('command')
  127.         tag.set_property('weight', pango.WEIGHT_BOLD)
  128.         tag = self.buffer.create_tag('error')
  129.         tag.set_property('style', pango.STYLE_ITALIC)
  130.  
  131.         self.inputbox = gtk.HBox(spacing=2)
  132.         self.pack_end(self.inputbox, expand=gtk.FALSE)
  133.         self.inputbox.show()
  134.  
  135.         self.prompt = gtk.Label(sys.ps1)
  136.         self.prompt.set_padding(2, 0)
  137.         self.inputbox.pack_start(self.prompt, expand=gtk.FALSE)
  138.         self.prompt.show()
  139.  
  140.         self.closer = gtk.Button("Close")
  141.         self.closer.connect("clicked", self.quit)
  142.         self.inputbox.pack_end(self.closer, expand=gtk.FALSE)
  143.         self.closer.show()
  144.  
  145.         self.line = gtk.Entry()
  146.         self.line.connect("key_press_event", self.key_function)
  147.         self.inputbox.pack_start(self.line, padding=2)
  148.         self.line.show()
  149.  
  150.         self.namespace = namespace
  151.  
  152.         self.cmd = ''
  153.         self.cmd2 = ''
  154.  
  155.         # set up hooks for standard output.
  156.         self.stdout = gtkoutfile(self.buffer, sys.stdout.fileno(), 'normal')
  157.         self.stderr = gtkoutfile(self.buffer, sys.stderr.fileno(), 'error')
  158.  
  159.         # set up command history
  160.         self.history = ['']
  161.         self.histpos = 0
  162.         self.namespace['__history__'] = self.history
  163.  
  164.     def init(self):
  165.         message = 'Python %s\n\n' % (sys.version,)
  166.         iter = self.buffer.get_end_iter()
  167.         self.buffer.insert_with_tags_by_name(iter, message, 'command') 
  168.         self.line.grab_focus()
  169.  
  170.     def quit(self, *args):
  171.         self.hide()
  172.         self.destroy()
  173.         if self.quit_cb: self.quit_cb()
  174.  
  175.     def key_function(self, entry, event):
  176.         if event.keyval == gtk.keysyms.Return:
  177.             self.line.emit_stop_by_name("key_press_event")
  178.             self.eval()
  179.         if event.keyval == gtk.keysyms.Tab:
  180.             self.line.emit_stop_by_name("key_press_event")
  181.             self.line.append_text('\t')
  182.             gtk.idle_add(self.focus_text)
  183.         elif event.keyval in (gtk.keysyms.KP_Up, gtk.keysyms.Up):
  184.             self.line.emit_stop_by_name("key_press_event")
  185.             self.historyUp()
  186.             gtk.idle_add(self.focus_text)
  187.         elif event.keyval in (gtk.keysyms.KP_Down, gtk.keysyms.Down):
  188.             self.line.emit_stop_by_name("key_press_event")
  189.             self.historyDown()
  190.             gtk.idle_add(self.focus_text)
  191.         elif event.keyval in (gtk.keysyms.D, gtk.keysyms.d) and \
  192.                  event.state & gtk.gdk.CONTROL_MASK:
  193.             self.line.emit_stop_by_name("key_press_event")
  194.             self.ctrld()
  195.  
  196.     def focus_text(self):
  197.         self.line.grab_focus()
  198.         return gtk.FALSE  # don't requeue this handler
  199.     def scroll_bottom(self):
  200.         self.vadj.set_value(self.vadj.upper - self.vadj.page_size)
  201.         return gtk.FALSE
  202.  
  203.     def ctrld(self):
  204.         #self.quit()
  205.         pass
  206.  
  207.     def historyUp(self):
  208.         if self.histpos > 0:
  209.             l = self.line.get_text()
  210.             if len(l) > 0 and l[0] == '\n': l = l[1:]
  211.             if len(l) > 0 and l[-1] == '\n': l = l[:-1]
  212.             self.history[self.histpos] = l
  213.             self.histpos = self.histpos - 1
  214.             self.line.set_text(self.history[self.histpos])
  215.  
  216.     def historyDown(self):
  217.         if self.histpos < len(self.history) - 1:
  218.             l = self.line.get_text()
  219.             if len(l) > 0 and l[0] == '\n': l = l[1:]
  220.             if len(l) > 0 and l[-1] == '\n': l = l[:-1]
  221.             self.history[self.histpos] = l
  222.             self.histpos = self.histpos + 1
  223.             self.line.set_text(self.history[self.histpos])
  224.             self.line.set_position(-1)
  225.  
  226.     def eval(self):
  227.         l = self.line.get_text() + '\n'
  228.         if len(l) > 1 and l[0] == '\n': l = l[1:]
  229.         self.histpos = len(self.history) - 1
  230.         if len(l) > 0 and l[-1] == '\n':
  231.             self.history[self.histpos] = l[:-1]
  232.         else:
  233.             self.history[self.histpos] = l
  234.         self.line.set_text('')
  235.         iter = self.buffer.get_end_iter()
  236.         self.buffer.insert_with_tags_by_name(iter, self.prompt.get_text() + l,
  237.                                              'command')
  238.         if l == '\n':
  239.             self.run(self.cmd)
  240.             self.cmd = ''
  241.             self.cmd2 = ''
  242.             return
  243.         self.histpos = self.histpos + 1
  244.         self.history.append('')
  245.         self.cmd = self.cmd + l
  246.         self.cmd2 = self.cmd2 + remQuotStr(l)
  247.         l = string.rstrip(l)
  248.         if not bracketsBalanced(self.cmd2) or l[-1] == ':' or \
  249.                 l[-1] == '\\' or l[0] in ' \11':
  250.             self.prompt.set_text(sys.ps2)
  251.             self.prompt.queue_draw()
  252.             return
  253.         self.run(self.cmd)
  254.         self.cmd = ''
  255.         self.cmd2 = ''
  256.  
  257.     def run(self, cmd):
  258.         sys.stdout, self.stdout = self.stdout, sys.stdout
  259.         sys.stderr, self.stderr = self.stderr, sys.stderr
  260.         try:
  261.             try:
  262.                 r = eval(cmd, self.namespace, self.namespace)
  263.                 self.namespace['_'] = r
  264.                 if r is not None:
  265.                     print `r`
  266.             except SyntaxError:
  267.                 exec cmd in self.namespace
  268.         except:
  269.             if hasattr(sys, 'last_type') and \
  270.                     sys.last_type == SystemExit:
  271.                 self.quit()
  272.             else:
  273.                 traceback.print_exc()
  274.         self.prompt.set_text(sys.ps1)
  275.         self.prompt.queue_draw()
  276.         gtk.idle_add(self.scroll_bottom)
  277.         sys.stdout, self.stdout = self.stdout, sys.stdout
  278.         sys.stderr, self.stderr = self.stderr, sys.stderr
  279.  
  280. def gtk_console(ns, title='Python', menu=None):
  281.     win = gtk.Window()
  282.     win.set_default_size(475, 300)
  283.     win.connect("destroy", gtk.mainquit)
  284.     win.connect("delete_event", gtk.mainquit)
  285.     win.set_title(title)
  286.     cons = Console(namespace=ns, quit_cb=gtk.mainquit)
  287.     if menu:
  288.         box = gtk.VBox()
  289.         win.add(box)
  290.         box.show()
  291.         box.pack_start(menu, expand=gtk.FALSE)
  292.         menu.show()
  293.         box.pack_start(cons)
  294.     else:
  295.         win.add(cons)
  296.     cons.show()
  297.     win.show()
  298.     cons.init()
  299.     gtk.main()
  300.  
  301. if __name__ == '__main__':
  302.     if len(sys.argv) < 2 or sys.argv[1] != '-gimp':
  303.         gtk_console({'__builtins__': __builtins__, '__name__': '__main__',
  304.                      '__doc__': None})
  305.