Created
August 11, 2017 16:17
-
-
Save khoa-io/06882f67f6e2a205402df4706b737808 to your computer and use it in GitHub Desktop.
Open Folder in Atom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: UTF-8 -*- | |
# This example is contributed by Hoàng Văn Khoa | |
# Example modified for Atom | |
# Based on https://github.com/gnunn1/tilix/blob/master/data/nautilus/open-tilix.py | |
# Installation: | |
# mkdir -p ~/.local/share/nautilus-python/extensions | |
# cp open-atom.py ~/.local/share/nautilus-python/extensions | |
from gettext import gettext, textdomain | |
from subprocess import PIPE, call | |
from urllib import unquote | |
from urlparse import urlparse | |
from gi import require_version | |
require_version('Gtk', '3.0') | |
require_version('Nautilus', '3.0') | |
from gi.repository import Gio, GObject, Gtk, Nautilus | |
TERMINAL = "atom" | |
GSETTINGS_OPEN_TERMINAL = "nautilus-open" | |
REMOTE_URI_SCHEME = ['ftp', 'sftp'] | |
textdomain("atom") | |
_ = gettext | |
def open_terminal_in_file(filename): | |
if filename: | |
call('{0} -w "{1}" &'.format(TERMINAL, filename), shell=True) | |
else: | |
call("{0} &".format(TERMINAL), shell=True) | |
class OpenAtomShortcutProvider(GObject.GObject, | |
Nautilus.LocationWidgetProvider): | |
def __init__(self): | |
source = Gio.SettingsSchemaSource.get_default() | |
self._window = None | |
self._uri = None | |
def _create_accel_group(self): | |
self._accel_group = Gtk.AccelGroup() | |
shortcut = self._gsettings.get_string(GSETTINGS_OPEN_TERMINAL) | |
key, mod = Gtk.accelerator_parse(shortcut) | |
self._accel_group.connect(key, mod, Gtk.AccelFlags.VISIBLE, | |
self._open_terminal) | |
def _bind_shortcut(self, gsettings, key): | |
if key == GSETTINGS_OPEN_TERMINAL: | |
self._accel_group.disconnect(self._open_terminal) | |
self._create_accel_group() | |
def _open_terminal(self, *args): | |
filename = unquote(self._uri[7:]) | |
open_terminal_in_file(filename) | |
def get_widget(self, uri, window): | |
self._uri = uri | |
if self._window: | |
self._window.remove_accel_group(self._accel_group) | |
if self._gsettings: | |
window.add_accel_group(self._accel_group) | |
self._window = window | |
return None | |
class OpenAtomExtension(GObject.GObject, Nautilus.MenuProvider): | |
def _open_terminal(self, file_): | |
if file_.get_uri_scheme() in REMOTE_URI_SCHEME: | |
result = urlparse(file_.get_uri()) | |
if result.username: | |
value = 'ssh -t {0}@{1}'.format(result.username, | |
result.hostname) | |
else: | |
value = 'ssh -t {0}'.format(result.hostname) | |
if result.port: | |
value = "{0} -p {1}".format(value, result.port) | |
if file_.is_directory(): | |
value = '{0} cd "{1}" ; $SHELL'.format(value, result.path) | |
call('{0} -e "{1}" &'.format(TERMINAL, value), shell=True) | |
else: | |
filename = Gio.File.new_for_uri(file_.get_uri()).get_path() | |
open_terminal_in_file(filename) | |
def _menu_activate_cb(self, menu, file_): | |
self._open_terminal(file_) | |
def _menu_background_activate_cb(self, menu, file_): | |
self._open_terminal(file_) | |
def get_file_items(self, window, files): | |
if len(files) != 1: | |
return | |
items = [] | |
file_ = files[0] | |
print("Handling file: ", file_.get_uri()) | |
print("file scheme: ", file_.get_uri_scheme()) | |
if file_.is_directory(): | |
if file_.get_uri_scheme() in REMOTE_URI_SCHEME: | |
uri = file_.get_uri().decode('utf-8') | |
item = Nautilus.MenuItem(name='NautilusPython::open_remote_item', | |
label=_(u'Open Remote Atom'), | |
tip=_(u'Open Remote Atom ' | |
'In').format(uri)) | |
item.connect('activate', self._menu_activate_cb, file_) | |
items.append(item) | |
filename = file_.get_name().decode('utf-8') | |
item = Nautilus.MenuItem(name='NautilusPython::open_file_item', | |
label=_(u'Open With Atom'), | |
tip=_(u'Open {} With Atom').format(filename)) | |
item.connect('activate', self._menu_activate_cb, file_) | |
items.append(item) | |
return items | |
def get_background_items(self, window, file_): | |
items = [] | |
if file_.get_uri_scheme() in REMOTE_URI_SCHEME: | |
item = Nautilus.MenuItem(name='NautilusPython::open_bg_remote_item', | |
label=_(u'Open Remote Atom Here'), | |
tip=_(u'Open Remote Atom In This Directory')) | |
item.connect('activate', self._menu_activate_cb, file_) | |
items.append(item) | |
item = Nautilus.MenuItem(name='NautilusPython::open_bg_file_item', | |
label=_(u'Open With Atom'), | |
tip=_(u'Open This Directory With Atom')) | |
item.connect('activate', self._menu_background_activate_cb, file_) | |
items.append(item) | |
return items |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment