home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / totem / plugins / pythonconsole / pythonconsole.py < prev    next >
Encoding:
Python Source  |  2009-04-14  |  4.9 KB  |  148 lines

  1. # -*- coding: utf-8 -*-
  2.  
  3. # pythonconsole.py -- plugin object
  4. #
  5. # Copyright (C) 2006 - Steve Fr√©cinaux
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2, or (at your option)
  10. # any later version.
  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. # Parts from "Interactive Python-GTK Console" (stolen from epiphany's console.py)
  21. #     Copyright (C), 1998 James Henstridge <james@daa.com.au>
  22. #     Copyright (C), 2005 Adam Hooper <adamh@densi.com>
  23. # Bits from gedit Python Console Plugin
  24. #     Copyrignt (C), 2005 Rapha√´l Slinckx
  25. #
  26. # The Totem project hereby grant permission for non-gpl compatible GStreamer
  27. # plugins to be used and distributed together with GStreamer and Totem. This
  28. # permission are above and beyond the permissions granted by the GPL license
  29. # Totem is covered by.
  30. #
  31. # Monday 7th February 2005: Christian Schaller: Add exception clause.
  32. # See license_change file for details.
  33.  
  34. from console import PythonConsole
  35.  
  36. __all__ = ('PythonConsole', 'OutFile')
  37.  
  38. import gtk
  39. import totem
  40. import gconf
  41. import gobject
  42. try:
  43.     import rpdb2
  44.     have_rpdb2 = True
  45. except:
  46.     have_rpdb2 = False
  47.  
  48. ui_str = """
  49. <ui>
  50.   <menubar name="tmw-menubar">
  51.     <menu name="Python" action="Python">
  52.       <placeholder name="ToolsOps_5">
  53.         <menuitem name="PythonConsole" action="PythonConsole"/>
  54.         <menuitem name="PythonDebugger" action="PythonDebugger"/>
  55.       </placeholder>
  56.     </menu>
  57.   </menubar>
  58. </ui>
  59. """
  60.  
  61. class PythonConsolePlugin(totem.Plugin):
  62.     def __init__(self):
  63.         totem.Plugin.__init__(self)
  64.         self.window = None
  65.     
  66.     def activate(self, totem_object):
  67.  
  68.         data = dict()
  69.         manager = totem_object.get_ui_manager()
  70.  
  71.         data['action_group'] = gtk.ActionGroup('Python')
  72.         
  73.         action = gtk.Action('Python', 'Python', _('Python Console Menu'), None)
  74.         data['action_group'].add_action(action)
  75.  
  76.         action = gtk.Action('PythonConsole', _('_Python Console'),
  77.                             _("Show Totem's Python console"),
  78.                             'gnome-mime-text-x-python')
  79.         action.connect('activate', self.show_console, totem_object)
  80.         data['action_group'].add_action(action)
  81.  
  82.         action = gtk.Action('PythonDebugger', _('Python Debugger'),
  83.                     _("Enable remote Python debugging with rpdb2"),
  84.                     None)
  85.         if have_rpdb2:
  86.             action.connect('activate', self.enable_debugging, totem_object)
  87.         else:
  88.             action.set_visible(False)
  89.         data['action_group'].add_action(action)
  90.                 
  91.         manager.insert_action_group(data['action_group'], 0)
  92.         data['ui_id'] = manager.add_ui_from_string(ui_str)
  93.         manager.ensure_update()
  94.         
  95.         totem_object.set_data('PythonConsolePluginInfo', data)
  96.     
  97.     def show_console(self, action, totem_object):
  98.         if not self.window:
  99.             console = PythonConsole(namespace = {'__builtins__' : __builtins__,
  100.                                                  'totem' : totem,
  101.                                                             'totem_object' : totem_object},
  102.                                      destroy_cb = self.destroy_console)
  103.  
  104.             console.set_size_request(600, 400)
  105.             console.eval('print "%s" %% totem_object' % _("You can access the totem object through " \
  106.                      "\'totem_object\' :\\n%s"), False)
  107.  
  108.     
  109.             self.window = gtk.Window()
  110.             self.window.set_title(_('Totem Python Console'))
  111.             self.window.add(console)
  112.             self.window.connect('destroy', self.destroy_console)
  113.             self.window.show_all()
  114.         else:
  115.             self.window.show_all()
  116.             self.window.grab_focus()
  117.  
  118.     def enable_debugging(self, action, totem_object):
  119.         msg = _("After you press OK, Totem will wait until you connect to it with winpdb or rpdb2. If you have not set a debugger password in GConf, it will use the default password ('totem').")
  120.         dialog = gtk.MessageDialog(None, 0, gtk.MESSAGE_INFO, gtk.BUTTONS_OK_CANCEL, msg)
  121.         if dialog.run() == gtk.RESPONSE_OK:
  122.             gconfclient = gconf.client_get_default()
  123.             password = gconfclient.get_string('/apps/totem/plugins/pythonconsole/rpdb2_password') or "totem"
  124.             def start_debugger(password):
  125.                 rpdb2.start_embedded_debugger(password)
  126.                 return False
  127.  
  128.             gobject.idle_add(start_debugger, password)
  129.         dialog.destroy()
  130.  
  131.     def destroy_console(self, *args):
  132.         self.window.destroy()
  133.         self.window = None
  134.  
  135.     def deactivate(self, totem_object):
  136.         data = totem_object.get_data('PythonConsolePluginInfo')
  137.  
  138.         manager = totem_object.get_ui_manager()
  139.         manager.remove_ui(data['ui_id'])
  140.         manager.remove_action_group(data['action_group'])
  141.         manager.ensure_update()
  142.  
  143.         totem_object.set_data('PythonConsolePluginInfo', None)
  144.         
  145.         if self.window is not None:
  146.             self.window.destroy()
  147.