The new Version introduced a breaking change to the MenuProvider interface. This change keeps compatibility with the lower version. See: <https://gnome.pages.gitlab.gnome.org/nautilus-python/nautilus-python-migrating-to-4.html>
48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
import locale
|
|
import os
|
|
import subprocess
|
|
from gi.repository import Nautilus, GObject
|
|
|
|
TRANSLATIONS = {
|
|
"de": ("In Kitty öffnen", "Den Ordner in Kitty öffnen"),
|
|
"en": ("Open in Kitty", "Opens the current folder in Kitty"),
|
|
}
|
|
|
|
|
|
class KittyNautilusExtension(GObject.GObject, Nautilus.MenuProvider):
|
|
|
|
def __init__(self):
|
|
# Derive lang from the system locale
|
|
default_locale = locale.getdefaultlocale()[0].split("_")[0]
|
|
lang = default_locale.split("_")[0]
|
|
# Define a save fallback, if translation for the lang does not exist
|
|
if lang not in TRANSLATIONS:
|
|
lang = "en"
|
|
|
|
self.intl = TRANSLATIONS[lang]
|
|
|
|
def launch_kitty(self, menu: Nautilus.MenuItem, path):
|
|
subprocess.Popen(["kitty", "-d", path], shell=False)
|
|
|
|
def make_item(self, name, path) -> Nautilus.MenuItem:
|
|
item = Nautilus.MenuItem(
|
|
name=name,
|
|
label=self.intl[0],
|
|
tip=self.intl[1],
|
|
icon="terminal",
|
|
)
|
|
item.connect("activate", self.launch_kitty, path)
|
|
return item
|
|
|
|
def get_file_items(self, *args) -> [Nautilus.MenuItem]:
|
|
files = args[-1]
|
|
if len(files) == 1:
|
|
path = files[0].get_location().get_path()
|
|
if os.path.isdir(path):
|
|
return [self.make_item("NautilusFileOpenKitty", path)]
|
|
|
|
def get_background_items(self, *args) -> [Nautilus.MenuItem]:
|
|
folder = args[-1]
|
|
path = folder.get_location().get_path()
|
|
return [self.make_item("NautilusBackgroundOpenKitty", path)]
|