[meld/pathlabel: 1/10] melddoc: Pull external file opening helper out to a helper function
- From: Kai Willadsen <kaiw src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [meld/pathlabel: 1/10] melddoc: Pull external file opening helper out to a helper function
- Date: Sun, 10 Jan 2021 02:57:35 +0000 (UTC)
commit 6b9b54b39d32c1e25037455e3a5dab06285ee651
Author: Kai Willadsen <kai willadsen gmail com>
Date: Sun Mar 24 06:51:55 2019 +1000
melddoc: Pull external file opening helper out to a helper function
meld/dirdiff.py | 4 +-
meld/filediff.py | 6 +--
meld/melddoc.py | 112 ++++++++++++++++++++++++++++---------------------------
meld/vcview.py | 4 +-
4 files changed, 64 insertions(+), 62 deletions(-)
---
diff --git a/meld/dirdiff.py b/meld/dirdiff.py
index cf107488..c8982c22 100644
--- a/meld/dirdiff.py
+++ b/meld/dirdiff.py
@@ -33,7 +33,7 @@ from meld import misc, tree
from meld.conf import _
from meld.const import FILE_FILTER_ACTION_FORMAT, MISSING_TIMESTAMP
from meld.iohelpers import trash_or_confirm
-from meld.melddoc import MeldDoc
+from meld.melddoc import MeldDoc, open_files_external
from meld.misc import all_same, apply_text_filters, with_focused_pane
from meld.recent import RecentType
from meld.settings import bind_settings, get_meld_settings, settings
@@ -1366,7 +1366,7 @@ class DirDiff(Gtk.VBox, tree.TreeviewCommon, MeldDoc):
]
files = [f for f in files if f]
if files:
- self._open_files(files)
+ open_files_external(files)
def action_copy_file_paths(self, *args):
pane = self._get_focused_pane()
diff --git a/meld/filediff.py b/meld/filediff.py
index 03224f0e..5b83b388 100644
--- a/meld/filediff.py
+++ b/meld/filediff.py
@@ -42,7 +42,7 @@ from meld.meldbuffer import (
BufferInsertionAction,
BufferLines,
)
-from meld.melddoc import ComparisonState, MeldDoc
+from meld.melddoc import ComparisonState, MeldDoc, open_files_external
from meld.misc import user_critical, with_focused_pane
from meld.patchdialog import PatchDialog
from meld.recent import RecentType
@@ -1295,7 +1295,7 @@ class FileDiff(Gtk.VBox, MeldDoc):
line = cursor_it.get_line() + 1
# TODO: Support URI-based opens
path = self.textbuffer[pane].data.gfile.get_path()
- self._open_files([path], line)
+ open_files_external([path], line=line)
def update_text_actions_sensitivity(self, *args):
widget = self.focus_pane
@@ -1556,7 +1556,7 @@ class FileDiff(Gtk.VBox, MeldDoc):
"Do you want to open the file using the default application?")
self.msgarea_mgr[pane].add_action_msg(
'dialog-warning-symbolic', primary, secondary, _("Open"),
- functools.partial(self._open_files, [gfile.get_path()]))
+ functools.partial(open_files_external, [gfile.get_path()]))
self.update_buffer_writable(buf)
diff --git a/meld/melddoc.py b/meld/melddoc.py
index 8a3d0b20..a7aa6914 100644
--- a/meld/melddoc.py
+++ b/meld/melddoc.py
@@ -49,6 +49,63 @@ def make_custom_editor_command(path: str, line: int = 0) -> Sequence[str]:
return shlex.split(cmd)
+def open_files_external(
+ self, selected: Iterable[str], *, line: int = 0) -> None:
+ query_attrs = ",".join((Gio.FILE_ATTRIBUTE_STANDARD_TYPE,
+ Gio.FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE))
+
+ def os_open(path: str, uri: str):
+ if not path:
+ return
+ if sys.platform == "win32":
+ os.startfile(path)
+ elif sys.platform == "darwin":
+ subprocess.Popen(["open", path])
+ else:
+ Gtk.show_uri(Gdk.Screen.get_default(), uri,
+ Gtk.get_current_event_time())
+
+ def open_cb(source, result, *data):
+ info = source.query_info_finish(result)
+ file_type = info.get_file_type()
+ path, uri = source.get_path(), source.get_uri()
+ if file_type == Gio.FileType.DIRECTORY:
+ os_open(path, uri)
+ elif file_type == Gio.FileType.REGULAR:
+ content_type = info.get_content_type()
+ # FIXME: Content types are broken on Windows with current gio
+ # If we can't access a content type, assume it's text.
+ if not content_type or Gio.content_type_is_a(
+ content_type, "text/plain"):
+ if settings.get_boolean('use-system-editor'):
+ gfile = Gio.File.new_for_path(path)
+ if sys.platform == "win32":
+ handler = gfile.query_default_handler(None)
+ result = handler.launch([gfile], None)
+ else:
+ uri = gfile.get_uri()
+ Gio.AppInfo.launch_default_for_uri(
+ uri, None)
+ else:
+ editor = make_custom_editor_command(path, line)
+ if editor:
+ # TODO: If the editor is badly set up, this fails
+ # silently
+ subprocess.Popen(editor)
+ else:
+ os_open(path, uri)
+ else:
+ os_open(path, uri)
+ else:
+ # TODO: Add some kind of 'failed to open' notification
+ pass
+
+ for f in [Gio.File.new_for_path(s) for s in selected]:
+ f.query_info_async(
+ query_attrs, Gio.FileQueryInfoFlags.NONE, GLib.PRIORITY_LOW,
+ None, open_cb, None)
+
+
class ComparisonState(enum.IntEnum):
# TODO: Consider use-cases for states in gedit-enum-types.c
Normal = 0
@@ -120,61 +177,6 @@ class MeldDoc(LabeledObjectMixin, GObject.GObject):
if self.scheduler.tasks_pending():
self.scheduler.remove_task(self.scheduler.get_current_task())
- def _open_files(self, selected: Iterable[str], line: int = 0) -> None:
- query_attrs = ",".join((Gio.FILE_ATTRIBUTE_STANDARD_TYPE,
- Gio.FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE))
-
- def os_open(path: str, uri: str):
- if not path:
- return
- if sys.platform == "win32":
- os.startfile(path)
- elif sys.platform == "darwin":
- subprocess.Popen(["open", path])
- else:
- Gtk.show_uri(Gdk.Screen.get_default(), uri,
- Gtk.get_current_event_time())
-
- def open_cb(source, result, *data):
- info = source.query_info_finish(result)
- file_type = info.get_file_type()
- path, uri = source.get_path(), source.get_uri()
- if file_type == Gio.FileType.DIRECTORY:
- os_open(path, uri)
- elif file_type == Gio.FileType.REGULAR:
- content_type = info.get_content_type()
- # FIXME: Content types are broken on Windows with current gio
- # If we can't access a content type, assume it's text.
- if not content_type or Gio.content_type_is_a(
- content_type, "text/plain"):
- if settings.get_boolean('use-system-editor'):
- gfile = Gio.File.new_for_path(path)
- if sys.platform == "win32":
- handler = gfile.query_default_handler(None)
- result = handler.launch([gfile], None)
- else:
- uri = gfile.get_uri()
- Gio.AppInfo.launch_default_for_uri(
- uri, None)
- else:
- editor = make_custom_editor_command(path, line)
- if editor:
- # TODO: If the editor is badly set up, this fails
- # silently
- subprocess.Popen(editor)
- else:
- os_open(path, uri)
- else:
- os_open(path, uri)
- else:
- # TODO: Add some kind of 'failed to open' notification
- pass
-
- for f in [Gio.File.new_for_path(s) for s in selected]:
- f.query_info_async(
- query_attrs, Gio.FileQueryInfoFlags.NONE, GLib.PRIORITY_LOW,
- None, open_cb, None)
-
def on_file_changed(self, filename: str):
pass
diff --git a/meld/vcview.py b/meld/vcview.py
index cee2e725..da8bea45 100644
--- a/meld/vcview.py
+++ b/meld/vcview.py
@@ -28,7 +28,7 @@ from gi.repository import Gdk, Gio, GLib, GObject, Gtk, Pango
from meld import tree
from meld.conf import _
from meld.iohelpers import trash_or_confirm
-from meld.melddoc import MeldDoc
+from meld.melddoc import MeldDoc, open_files_external
from meld.misc import error_dialog, read_pipe_iter
from meld.recent import RecentType
from meld.settings import bind_settings, settings
@@ -774,7 +774,7 @@ class VcView(Gtk.VBox, tree.TreeviewCommon, MeldDoc):
self.run_diff(f)
def action_open_external(self, *args):
- self._open_files(self._get_selected_files())
+ open_files_external(self._get_selected_files())
def refresh(self):
root = self.model.get_iter_first()
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]