Last active
December 15, 2018 14:52
-
-
Save mwgamera/f786b075d170ab76e0449a0fccca3013 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Open Terminal in chosen local directory from Caja; based on example from: | |
# <https://github.com/mate-desktop/python-caja/blob/master/examples/open-terminal.py> | |
# klg, Nov 2018 | |
import os | |
from gi.repository import Caja, GObject, Gio | |
TERMINAL_SCHEMA = 'org.mate.applications-terminal' | |
TERMINAL_KEY = 'exec' | |
class OpenTerminalExtension(GObject.GObject, Caja.MenuProvider): | |
def __init__(self): | |
self.gsettings = Gio.Settings.new(TERMINAL_SCHEMA) | |
pass | |
def spawn_terminal_at(self, path): | |
term = self.gsettings[TERMINAL_KEY] | |
pid = os.fork() | |
if pid == 0: | |
os.chdir(path) | |
os.execl('/bin/sh', 'sh', '-c', '%s &' % term) | |
assert False | |
os.waitpid(pid, 0) | |
def check_make_menu(self, file): | |
if not file.is_directory(): | |
return | |
path = file.get_location().get_path() | |
if not path: | |
return | |
item = Caja.MenuItem( | |
name='CajaPython::openterminal', | |
icon='Terminal', | |
label='Open Terminal', | |
tip='Open Terminal in %s' % file.get_name()) | |
item.connect('activate', self.menu_activate_cb, file) | |
return item, | |
def menu_activate_cb(self, menu, file): | |
self.spawn_terminal_at(file.get_location().get_path()) | |
def get_file_items(self, window, files): | |
if len(files) != 1: | |
return | |
return self.check_make_menu(files[0]) | |
def get_background_items(self, window, folder): | |
return self.check_make_menu(folder) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment