summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth Hitefield <sdh11@vt.edu>2015-11-13 18:51:04 -0500
committerSebastian Koslowski <koslowski@kit.edu>2015-11-24 17:01:31 +0100
commitba57cd0c5e7352c81e5025760d7aaecde92d083b (patch)
tree7ad66399e7268628c4586ecf513b4802ef6a9c2b
parent461152d46e6db790e37b09bd3b92035b62d7091f (diff)
grc: Added option to use the default editor when opening embedded python blocks
-rw-r--r--grc/gui/Dialogs.py80
-rw-r--r--grc/gui/FlowGraph.py18
-rw-r--r--grc/gui/Param.py2
-rw-r--r--grc/gui/external_editor.py20
4 files changed, 88 insertions, 32 deletions
diff --git a/grc/gui/Dialogs.py b/grc/gui/Dialogs.py
index 631dc0fd98..f2941250a8 100644
--- a/grc/gui/Dialogs.py
+++ b/grc/gui/Dialogs.py
@@ -21,7 +21,11 @@ import pygtk
pygtk.require('2.0')
import gtk
-from . import Utils, Actions, Constants
+import sys
+from distutils.spawn import find_executable
+
+
+from . import Utils, Actions, Constants, Messages
class SimpleTextDisplay(gtk.TextView):
@@ -237,19 +241,63 @@ def MissingXTermDialog(xterm):
def ChooseEditorDialog():
- file_dialog = gtk.FileChooserDialog(
- 'Open a Data File...', None,
- gtk.FILE_CHOOSER_ACTION_OPEN,
- ('gtk-cancel', gtk.RESPONSE_CANCEL, 'gtk-open', gtk.RESPONSE_OK)
+ # Give the option to either choose an editor or use the default
+ # Always return true/false so the caller knows it was successful
+ buttons = (
+ 'Choose Editor', gtk.RESPONSE_YES,
+ 'Use Default', gtk.RESPONSE_NO,
+ gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL
)
- file_dialog.set_select_multiple(False)
- file_dialog.set_local_only(True)
- file_dialog.set_current_folder('/usr/bin')
- response = file_dialog.run()
-
- if response == gtk.RESPONSE_OK:
- file_path = file_dialog.get_filename()
- Constants.prefs.set_string('grc', 'editor', file_path)
- Constants.prefs.save()
- Constants.EDITOR = file_path
- file_dialog.destroy()
+ response = MessageDialogHelper(
+ gtk.MESSAGE_QUESTION, gtk.BUTTONS_NONE, 'Choose Editor',
+ 'Would you like to choose the editor to use?', gtk.RESPONSE_YES, buttons
+ )
+
+ # Handle the inital default/choose/cancel response
+ # User wants to choose the editor to use
+ if response == gtk.RESPONSE_YES:
+ file_dialog = gtk.FileChooserDialog(
+ 'Select an Editor...', None,
+ gtk.FILE_CHOOSER_ACTION_OPEN,
+ ('gtk-cancel', gtk.RESPONSE_CANCEL, 'gtk-open', gtk.RESPONSE_OK)
+ )
+ file_dialog.set_select_multiple(False)
+ file_dialog.set_local_only(True)
+ file_dialog.set_current_folder('/usr/bin')
+ try:
+ if file_dialog.run() == gtk.RESPONSE_OK:
+ file_path = file_dialog.get_filename()
+ Constants.prefs.set_string('grc', 'editor', file_path)
+ Constants.prefs.save()
+ Constants.EDITOR = file_path
+ file_dialog.destroy()
+ return file_path
+ finally:
+ file_dialog.destroy()
+
+ # Go with the default editor
+ elif response == gtk.RESPONSE_NO:
+ # Determine the platform
+ try:
+ process = None
+ if sys.platform.startswith('linux'):
+ process = find_executable('xdg-open')
+ elif sys.platform.startswith('darwin'):
+ process = find_executable('open')
+ if process is None:
+ raise ValueError("Can't find default editor executable")
+ # Save
+ Constants.prefs.set_string('grc', 'editor', process)
+ Constants.prefs.save()
+ Constants.EDITOR = process
+ return process
+ except Exception:
+ Messages.send('>>> Unable to load the default editor. Please choose an editor.\n')
+ # Just reset of the constant and force the user to select an editor the next time
+ Constants.prefs.set_string('grc', 'editor', '')
+ Constants.prefs.save()
+ Constants.EDITOR = ""
+ return
+
+ Messages.send('>>> No editor selected.\n')
+ return
diff --git a/grc/gui/FlowGraph.py b/grc/gui/FlowGraph.py
index b27f0153db..6d712e520a 100644
--- a/grc/gui/FlowGraph.py
+++ b/grc/gui/FlowGraph.py
@@ -21,10 +21,11 @@ import random
import functools
from itertools import chain
from operator import methodcaller
+from distutils.spawn import find_executable
import gobject
-from . import Actions, Colors, Utils, Messages, Bars
+from . import Actions, Colors, Constants, Utils, Messages, Bars, Dialogs
from . Element import Element
from . Constants import SCROLL_PROXIMITY_SENSITIVITY, SCROLL_DISTANCE
from . external_editor import ExternalEditor
@@ -67,14 +68,27 @@ class FlowGraph(Element):
if target in self._external_updaters:
editor = self._external_updaters[target]
else:
+ editor = (find_executable(Constants.EDITOR) or
+ Dialogs.ChooseEditorDialog())
+ if not editor:
+ return
updater = functools.partial(
self.handle_external_editor_change, target=target)
editor = self._external_updaters[target] = ExternalEditor(
+ editor=editor,
name=target[0], value=param.get_value(),
callback=functools.partial(gobject.idle_add, updater)
)
editor.start()
- editor.open_editor()
+ try:
+ editor.open_editor()
+ except Exception as e:
+ # Problem launching the editor. Need to select a new editor.
+ Messages.send('>>> Error opening an external editor. Please select a different editor.\n')
+ # Reset the editor to force the user to select a new one.
+ Constants.prefs.set_string('grc', 'editor', '')
+ Constants.prefs.save()
+ Constants.EDITOR = ""
def handle_external_editor_change(self, new_value, target):
try:
diff --git a/grc/gui/Param.py b/grc/gui/Param.py
index 5f83689023..515c345a73 100644
--- a/grc/gui/Param.py
+++ b/grc/gui/Param.py
@@ -221,8 +221,6 @@ class PythonEditorParam(InputParam):
self.pack_start(button, True)
def open_editor(self, widget=None):
- if not os.path.exists(Constants.EDITOR):
- Dialogs.ChooseEditorDialog()
flowgraph = self.param.get_parent().get_parent()
flowgraph.install_external_editor(self.param)
diff --git a/grc/gui/external_editor.py b/grc/gui/external_editor.py
index 3322556ce7..76f21412b0 100644
--- a/grc/gui/external_editor.py
+++ b/grc/gui/external_editor.py
@@ -24,16 +24,15 @@ import threading
import tempfile
import subprocess
-import Constants
-
class ExternalEditor(threading.Thread):
- def __init__(self, name, value, callback):
+ def __init__(self, editor, name, value, callback):
threading.Thread.__init__(self)
self.daemon = True
self._stop_event = threading.Event()
+ self.editor = editor
self.callback = callback
self.tempfile = self._create_tempfile(name, value)
@@ -49,9 +48,7 @@ class ExternalEditor(threading.Thread):
return self.tempfile.name
def open_editor(self):
- proc = subprocess.Popen(
- args=(Constants.EDITOR, self.filename)
- )
+ proc = subprocess.Popen(args=(self.editor, self.filename))
proc.poll()
return proc
@@ -84,10 +81,9 @@ if __name__ == '__main__':
def p(data):
print data
- Constants.EDITOR = '/usr/bin/gedit'
- editor = ExternalEditor("test", "content", p)
- editor.open_editor()
- editor.start()
+ e = ExternalEditor('/usr/bin/gedit', "test", "content", p)
+ e.open_editor()
+ e.start()
time.sleep(15)
- editor.stop()
- editor.join()
+ e.stop()
+ e.join()