#!/usr/bin/env python # Copyright 2009 (C) Pierre Duquesne # Licensed under the BSD Revised License. import gtk import pyexiv2 import sys import os.path USAGE = "Usage: peanut FILE1 [FILE2 ...]" def quit(*args): sys.exit(0) def show_error(text="", title=""): d = gtk.MessageDialog(buttons=gtk.BUTTONS_CLOSE) d.connect('delete-event', quit) d.set_title(title) d.set_markup(str(text)) d.run() d.destroy() def make_thumbnail_widget(thumbdata): pbloader = gtk.gdk.PixbufLoader() pbloader.write(thumbdata) pixbuf = pbloader.get_pixbuf() pbloader.close() imgwidget = gtk.Image() imgwidget.set_from_pixbuf(pixbuf) return imgwidget class Editor(object): def __init__(self, text="", title="", thumbdata=None): self.title = title self.text = text self.thumbdata = thumbdata self.dialog = None self.buffer = None def create_dialog(self): textview = gtk.TextView() textview.set_wrap_mode(gtk.WRAP_WORD) buffer = textview.get_buffer() buffer.set_text(self.text) scrolled = gtk.ScrolledWindow() scrolled.set_shadow_type(gtk.SHADOW_ETCHED_IN) scrolled.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) scrolled.add(textview) dialog = gtk.Dialog(buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_SAVE, gtk.RESPONSE_OK)) dialog.connect('delete-event', quit) if self.thumbdata: thumbnail = make_thumbnail_widget(self.thumbdata) else: thumbnail = gtk.Label("No thumbnail") dialog.vbox.pack_start(thumbnail, expand=False, fill=False) dialog.vbox.pack_start(scrolled, expand=True, fill=True) dialog.set_title(self.title) dialog.set_default_size(400, 400) dialog.show_all() self.dialog = dialog self.buffer = buffer def get_new_comment(self): self.create_dialog() response = self.dialog.run() if response == gtk.RESPONSE_OK: ret = self.buffer.get_text(*self.buffer.get_bounds()) else: ret = None self.dialog.destroy() return ret def edit(filename): image = pyexiv2.Image(filename) image.readMetadata() comment = image.getComment() try: ttype, tdata = image.getThumbnailData() except: tdata = None editor = Editor(text=comment, title=os.path.basename(filename), thumbdata=tdata) newcomment = editor.get_new_comment() if newcomment != None: image.setComment(newcomment) image.writeMetadata() if __name__ == "__main__": if '-h' in sys.argv[1:] or '--help' in sys.argv[1:]: print USAGE sys.exit(0) filenames = sys.argv[1:] if len(filenames) == 0: msg = 'Error: you must provide at least one filename.\n\n' + USAGE print msg show_error(msg, title="Error") sys.exit(1) for f in filenames: try: edit(f) except Exception, e: show_error(str(e), title="Error")