#!/usr/bin/env python # -*- coding: utf-8 -*- """This module adds a menu item to the nautilus right-click menu which allows to compare the selected files/folder using Meld (Diff and merge tool) just through the right-clicking""" # meld-compare.py version 1.0.1 # # Copyright 2009-2010 Giuseppe Penone # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. import pygtk pygtk.require('2.0') import gtk import nautilus, gconf, urllib, os, sys, subprocess, re NAUTILUS_MENU_LABEL = 'Meld Compare' NAUTILUS_MENU_HELP = 'Compare the selected Files/Folders using Meld (Diff and merge tool)' def error_dialog(message, dialog_title = "Error..."): """The extension's error dialog""" dialog = gtk.MessageDialog(flags=gtk.DIALOG_MODAL, type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK, message_format=message) dialog.set_title(dialog_title) dialog.run() dialog.destroy() class MeldCompare(nautilus.MenuProvider): """Implements the 'Meld Compare' extension to the nautilus right-click menu""" def __init__(self): """Creates a GConfClient (Gnome Configuration Client)""" self.client = gconf.client_get_default() def run(self, menu, element_1, element_2): """Runs the Meld Comparison of selected files/folders""" try: subprocess.call("meld %s %s &" % (element_1, element_2), shell=True) except: error_dialog("Exception Error:\n%s" % sys.exc_info()) def get_file_items(self, window, sel_items): """Adds the 'Add To Audacious Playlist' menu item to the Nautilus right-click menu, connects its 'activate' signal to the 'run' method passing the list of selected Audio items""" if len(sel_items) != 2: return element_1 = urllib.unquote(sel_items[0].get_uri()[7:]) element_2 = urllib.unquote(sel_items[1].get_uri()[7:]) if os.path.isfile(element_1): if not os.path.isfile(element_2): return element_1 = re.escape(element_1) filetype = subprocess.Popen("file -i %s" % element_1, shell=True, stdout=subprocess.PIPE).communicate()[0] if "text" not in filetype: return element_2 = re.escape(element_2) filetype = subprocess.Popen("file -i %s" % element_2, shell=True, stdout=subprocess.PIPE).communicate()[0] if "text" not in filetype: return elif os.path.isdir(element_1): if not os.path.isdir(element_2): return element_1 = re.escape(element_1) element_2 = re.escape(element_2) else: return item = nautilus.MenuItem('NautilusPython::meld', NAUTILUS_MENU_LABEL, NAUTILUS_MENU_HELP) item.set_property('icon', 'meld') item.connect('activate', self.run, element_1, element_2) return item,