Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions betterpythonconsole.plugin
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[Plugin]
Loader=python
Loader=python3
Module=betterpythonconsole
IAge=3
Name=Better Python Console
Description=A python console in the style of IDLE.
Icon=gnome-mime-text-x-python
Authors=Zeth Green, Jono Finger <jono@foodnotblogs.com>
Authors=Zeth Green, Jono Finger <jono@foodnotblogs.com>, Jacek Pliszka
Copyright=Copyright © 2006-7 Zeth Green, © 2012 Jono Finger
Website=http://www.pywm.eu
Website=https://wiki.gnome.org/Apps/Gedit/Plugins/BetterPythonConsole
40 changes: 35 additions & 5 deletions betterpythonconsole/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,28 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""IDLE like Console for Gedit, hit F5 and it executes the module"""

from gi.repository import Gtk, GObject, Gedit
import consolefunctions
import sys
from gi.repository import GObject, Gedit, Gio
from betterpythonconsole import consolefunctions

class BetterConsolePlugin(GObject.Object, Gedit.WindowActivatable):

class BetterConsoleAppActivatable(GObject.Object, Gedit.AppActivatable):

app = GObject.property(type=Gedit.App)

def do_activate(self):
# action self.on_clear_document_activate)])
self.app.add_accelerator("F5", "win.betterconsole", None)
self.menu_ext = self.extend_menu("tools-section")
item = Gio.MenuItem.new(_("Better Console"), "win.betterconsole")
self.menu_ext.append_menu_item(item)

def do_deactivate(self):
"""Pull our item from the menu."""
self.app.remove_accelerator("win.betterconsole", None)
self.menu_ext = None


class BetterConsoleWindowActivatable(GObject.Object, Gedit.WindowActivatable):
"""This Class creates the Gedit plugin. """

window = GObject.property(type=Gedit.Window)
Expand All @@ -35,7 +52,16 @@ def do_activate(self):
"""This adds the plugin to the running Gedit. This method is used
when the plugin is turned on and then when Gedit starts"""
home_path = __path__[0]
self._instances[self.window] = consolefunctions.BetterConsoleHelper(self, self.window, home_path)
self._instances[self.window] = consolefunctions.BetterConsoleHelper(
self, self.window, home_path
)

action = Gio.SimpleAction(name="betterconsole")
action.connect('activate', self.on_clear_document_activate)
self.window.add_action(action)

for view in self.window.get_views():
self.add_helper(view, self.window)

def do_deactivate(self):
"""This removes the plugin from the running Gedit."""
Expand All @@ -45,3 +71,7 @@ def do_deactivate(self):
def update_ui(self):
"""We do not use this yet."""
self._instances[self.window].update_ui()

def on_clear_document_activate(self, action, data=None):
self._instances[self.window].on_clear_document_activate(action)

85 changes: 20 additions & 65 deletions betterpythonconsole/consolefunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,75 +18,28 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""Core Functions for Gedit to interact with the Python Console. """

import sys
from gi.repository import Gtk
from gi.repository import GObject
import subprocess

# Insert a new item in the Tools menu
UI_STR = """<ui>
<menubar name="MenuBar">
<menu name="ToolsMenu" action="Tools">
<placeholder name="ToolsOps_2">
<menuitem name="BetterConsole" action="BetterConsole"/>
</placeholder>
</menu>
</menubar>
</ui>
"""

class BetterConsoleHelper:
"""Provides interaction with Gedit."""
def __init__(self, plugin, window, consolepath):
self._window = window
self._plugin = plugin
self._consolepath = consolepath
# Insert menu items
self._insert_menu()

def deactivate(self):
"""Remove any installed menu items from the Gedit Menu."""
self._remove_menu()

self._window = None
self._plugin = None
self._action_group = None

def _insert_menu(self):
"""Insert our item into the Gedit menu."""
# Get the GtkUIManager
manager = self._window.get_ui_manager()

# Create a new action group
self._action_group = Gtk.ActionGroup("BetterConsolePluginActions")
self._action_group.add_actions([("BetterConsole", None, _("Run Module"),
'F5', _("Run file in Python Console"),
self.on_clear_document_activate)])

# Insert the action group
manager.insert_action_group(self._action_group, -1)

# Merge the UI
self._ui_id = manager.add_ui_from_string(UI_STR)

def _remove_menu(self):
"""Pull our item from the menu."""
# Get the GtkUIManager
manager = self._window.get_ui_manager()

# Remove the ui
manager.remove_ui(self._ui_id)

# Remove the action group
manager.remove_action_group(self._action_group)

# Make sure the manager updates
manager.ensure_update()

def update_ui(self):
"""Unused in our case at the moment,
but required for the plugins system to be happy."""
self._action_group.set_sensitive(
self._window.get_active_document() != None)
self._window.get_active_document() is not None)

def on_clear_document_activate(self, action):
"""Menu activate handler,
Expand All @@ -98,53 +51,55 @@ def on_clear_document_activate(self, action):
return
our_filename = doc.get_short_name_for_display()

# Check for unsaved changes
unsaved = self._window.get_unsaved_documents()
# Check for unsaved changes
unsaved = self._window.get_unsaved_documents()
unsaved_filenames = []
for i in range(len(unsaved)):
unsaved_filenames.append(unsaved[i].get_uri_for_display())
unsaved_filenames.append(unsaved[i].get_uri_for_display())
if unsaved_filenames.count(our_filename) == 1:
mes_id = "unsaved_changes"
message = "There are unsaved changes."
message = "There are unsaved changes."
self.send_staus_message(message, mes_id)
# Check for an untitled document
elif doc.is_untitled() == True:

# Check for an untitled document
elif doc.is_untitled():
mes_id = "untitled_document"
message = "You must save the document first."
self.send_staus_message(message, mes_id)
# Check for an non-local file

# Check for an non-local file
# elif doc.get_uri_for_display()[:7]!="file://":
# mes_id = "unsupported_location"
# message = """This file location is currently unsupported.
# Please save the file locally."""
# self.send_staus_message(message, mes_id)

# Everything is fine
else:
self.launch_python_console(doc.get_uri_for_display())
mes_id = "upforit"
message = "The module " + doc.get_short_name_for_display() + \
" has been executed."
message = "The module {} has been executed.".format(
doc.get_short_name_for_display()
)
self.send_staus_message(message, mes_id)

def launch_python_console(self, filename):
"""Launch a console."""
interpreter_name = "python2"
fullpath = self._consolepath + "/consoleinterface.py"
run_command = [interpreter_name, fullpath, filename]
p1 = subprocess.Popen(run_command, stdout=subprocess.PIPE)
subprocess.Popen(run_command, stdout=subprocess.PIPE)

def send_staus_message(self, message, mes_id):
"""Put a message on the Status bar."""
our_statusbar = self._window.get_statusbar()
our_newid = our_statusbar.get_context_id(mes_id)
our_statusbar.push(our_newid, message)
GObject.timeout_add(
2000,self.clear_statusbar_from_crap,our_newid,our_statusbar)
2000, self.clear_statusbar_from_crap, our_newid, our_statusbar)

def clear_statusbar_from_crap(self, crap_id, status_bar):
"""Take a message off the Status bar."""
status_bar.pop(crap_id)
return False
return False

Loading