From 4b53d445376e9c6ffe7a3a099d28d97410fad0f6 Mon Sep 17 00:00:00 2001 From: CreamyCookie Date: Sat, 30 Jun 2018 17:24:42 +0200 Subject: [PATCH 1/6] Added an extension for opening a symlink's parent directory License: CC0 (https://creativecommons.org/publicdomain/zero/1.0/) --- examples/open-symlinks-parent-dirs.py | 42 +++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 examples/open-symlinks-parent-dirs.py diff --git a/examples/open-symlinks-parent-dirs.py b/examples/open-symlinks-parent-dirs.py new file mode 100644 index 0000000..1da2e78 --- /dev/null +++ b/examples/open-symlinks-parent-dirs.py @@ -0,0 +1,42 @@ +import os +import os.path +import urllib + +from gi.repository import Caja, GObject, Gio + + +class OpenSymLinksParentDirsExtension(Caja.MenuProvider, GObject.GObject): + def __init__(self): + pass + + def _open_parent_dir(self, files): + for f in files: + # find the real location of the file (resolves all symlinks) + path = os.path.realpath(f.get_path()) + parent = os.path.abspath(os.path.join(path, os.pardir)) + os.system('caja "%s" &' % parent) + + def menu_activate_cb(self, menu, files): + self._open_parent_dir(files) + + def get_file_items(self, window, files): + if any(f.is_directory() or not self.is_symbolic_link(f) for f in files): + return + + if len(files) == 1: + lbl = "Open Link's Parent Directory" + else: + lbl = "Open Links' Parent Directories" + + item = Caja.MenuItem(name='CajaPython::open_symlink_parent_dirs_item', + label=lbl , + tip=lbl) + item.connect('activate', self.menu_activate_cb, [f.get_location() for f in files]) + return item, + + def is_symbolic_link(self, f): + f_type = f.get_location().query_file_type(Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS) + return f_type is Gio.FileType.SYMBOLIC_LINK + + def get_background_items(self, window, file): + return tuple() From d5c4ddceda0eb6e8846e8bbf90d9c75635a9b27f Mon Sep 17 00:00:00 2001 From: CreamyCookie Date: Wed, 4 Jul 2018 19:14:14 +0200 Subject: [PATCH 2/6] performance improvement: only use get_location when menu item clicked --- examples/open-symlinks-parent-dirs.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/open-symlinks-parent-dirs.py b/examples/open-symlinks-parent-dirs.py index 1da2e78..a0be89b 100644 --- a/examples/open-symlinks-parent-dirs.py +++ b/examples/open-symlinks-parent-dirs.py @@ -12,7 +12,7 @@ def __init__(self): def _open_parent_dir(self, files): for f in files: # find the real location of the file (resolves all symlinks) - path = os.path.realpath(f.get_path()) + path = os.path.realpath(f.get_location().get_path()) parent = os.path.abspath(os.path.join(path, os.pardir)) os.system('caja "%s" &' % parent) @@ -31,7 +31,7 @@ def get_file_items(self, window, files): item = Caja.MenuItem(name='CajaPython::open_symlink_parent_dirs_item', label=lbl , tip=lbl) - item.connect('activate', self.menu_activate_cb, [f.get_location() for f in files]) + item.connect('activate', self.menu_activate_cb, files) return item, def is_symbolic_link(self, f): From 42d61a1738cce86819b5cf63d9ad6b4be4e16e16 Mon Sep 17 00:00:00 2001 From: CreamyCookie Date: Wed, 4 Jul 2018 19:17:21 +0200 Subject: [PATCH 3/6] formatting fix --- examples/open-symlinks-parent-dirs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/open-symlinks-parent-dirs.py b/examples/open-symlinks-parent-dirs.py index a0be89b..6d906d6 100644 --- a/examples/open-symlinks-parent-dirs.py +++ b/examples/open-symlinks-parent-dirs.py @@ -29,7 +29,7 @@ def get_file_items(self, window, files): lbl = "Open Links' Parent Directories" item = Caja.MenuItem(name='CajaPython::open_symlink_parent_dirs_item', - label=lbl , + label=lbl, tip=lbl) item.connect('activate', self.menu_activate_cb, files) return item, From a39bb48f1b58a4cb94b01bc7689a543ee61ae5f2 Mon Sep 17 00:00:00 2001 From: CreamyCookie Date: Tue, 17 Jul 2018 20:50:30 +0200 Subject: [PATCH 4/6] Now also works for directory symlinks --- examples/open-symlinks-parent-dirs.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/examples/open-symlinks-parent-dirs.py b/examples/open-symlinks-parent-dirs.py index 6d906d6..c030e3a 100644 --- a/examples/open-symlinks-parent-dirs.py +++ b/examples/open-symlinks-parent-dirs.py @@ -9,8 +9,8 @@ class OpenSymLinksParentDirsExtension(Caja.MenuProvider, GObject.GObject): def __init__(self): pass - def _open_parent_dir(self, files): - for f in files: + def _open_parent_dir(self, files_or_directories): + for f in files_or_directories: # find the real location of the file (resolves all symlinks) path = os.path.realpath(f.get_location().get_path()) parent = os.path.abspath(os.path.join(path, os.pardir)) @@ -20,7 +20,7 @@ def menu_activate_cb(self, menu, files): self._open_parent_dir(files) def get_file_items(self, window, files): - if any(f.is_directory() or not self.is_symbolic_link(f) for f in files): + if any(not self.is_symbolic_link(f) for f in files): return if len(files) == 1: @@ -38,5 +38,3 @@ def is_symbolic_link(self, f): f_type = f.get_location().query_file_type(Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS) return f_type is Gio.FileType.SYMBOLIC_LINK - def get_background_items(self, window, file): - return tuple() From 4547d4e8719f1972d9eb68a75009c6c703c3069d Mon Sep 17 00:00:00 2001 From: CreamyCookie Date: Tue, 17 Jul 2018 20:58:50 +0200 Subject: [PATCH 5/6] Fixed bug where menu item would appear when no files selected --- examples/open-symlinks-parent-dirs.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/open-symlinks-parent-dirs.py b/examples/open-symlinks-parent-dirs.py index c030e3a..01a849b 100644 --- a/examples/open-symlinks-parent-dirs.py +++ b/examples/open-symlinks-parent-dirs.py @@ -15,15 +15,16 @@ def _open_parent_dir(self, files_or_directories): path = os.path.realpath(f.get_location().get_path()) parent = os.path.abspath(os.path.join(path, os.pardir)) os.system('caja "%s" &' % parent) - + def menu_activate_cb(self, menu, files): self._open_parent_dir(files) def get_file_items(self, window, files): - if any(not self.is_symbolic_link(f) for f in files): + num_files = len(files) + if num_files == 0 or any(not self.is_symbolic_link(f) for f in files): return - - if len(files) == 1: + + if num_files == 1: lbl = "Open Link's Parent Directory" else: lbl = "Open Links' Parent Directories" From 3ce029fb04b03f8823352d23301937d61ceede6a Mon Sep 17 00:00:00 2001 From: CreamyCookie Date: Thu, 19 Jul 2018 12:28:28 +0200 Subject: [PATCH 6/6] Now using suprocess so that quotes are escaped --- examples/open-symlinks-parent-dirs.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/examples/open-symlinks-parent-dirs.py b/examples/open-symlinks-parent-dirs.py index 01a849b..4134fc8 100644 --- a/examples/open-symlinks-parent-dirs.py +++ b/examples/open-symlinks-parent-dirs.py @@ -1,5 +1,6 @@ import os import os.path +import subprocess import urllib from gi.repository import Caja, GObject, Gio @@ -10,11 +11,13 @@ def __init__(self): pass def _open_parent_dir(self, files_or_directories): + args = ['caja'] for f in files_or_directories: # find the real location of the file (resolves all symlinks) path = os.path.realpath(f.get_location().get_path()) parent = os.path.abspath(os.path.join(path, os.pardir)) - os.system('caja "%s" &' % parent) + args.append(parent) + subprocess.Popen(args) def menu_activate_cb(self, menu, files): self._open_parent_dir(files)