#!/usr/bin/python # vim:encoding=utf-8:fileencoding=utf-8 # # call the apropriate recoders for the unicode open document format converter # # Migrating from M* W*rd to ODF can be a pain if documents use characters from # fonts that only support some non unicode encodings like armscii. Remap these # to the appriopriate unicode codepoints. Any corrections are very welcome. # # (c) 2007,2008,2009 Guido Günther # (c) 2008 Torsten Werner # # 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import sys, os, shutil import odfrecode from optparse import OptionParser import gtk import gnomevfs fontmap = { "EastRoman": "DejaVu Sans" } class ConvError(Exception): pass class SuccessDialog(gtk.Dialog): def __init__(self, filename, backup): self.uri = "file://%s" % filename gtk.Dialog.__init__(self, buttons=(gtk.STOCK_OK, gtk.RESPONSE_ACCEPT)) label = gtk.Label("succesfully converted to Unicode.") uri = gtk.LinkButton(self.uri, label=os.path.basename(filename)) uri.connect('clicked', self.show_uri) uri.set_tooltip_text("Click to open '%s'" % filename) stock = gtk.image_new_from_stock(gtk.STOCK_DIALOG_INFO, gtk.ICON_SIZE_DIALOG) hbox = gtk.HBox(False, 3) hbox.pack_start(stock, True, True, 0) hbox.add(uri) hbox.add(label) self.vbox.add(hbox) self.vbox.add(gtk.Label("A backup copy was saved as '%s'." % os.path.basename(backup))) self.show_all() def show_uri(self, dummy): gnomevfs.url_show(self.uri) self.destroy() def main(argv): try: encoding = sys.getfilesystemencoding() parser = OptionParser() parser.add_option("--recoder", dest="recoder", help="use recoder RECODER", metavar="recoder") (options, args) = parser.parse_args() if len(args) != 1: raise ConvError, "Need a file to convert." else: filename = os.path.abspath(args[0]) try: fileinfo = gnomevfs.get_file_info(filename, gnomevfs.FILE_INFO_GET_MIME_TYPE) except gnomevfs.NotFoundError: raise ConvError, "'%s' nicht gefunden" % filename if fileinfo.mime_type not in [ "application/vnd.oasis.opendocument.text", "application/vnd.oasis.opendocument.text-template" ]: raise ConvError, "'%s' is no OpenDocument Text, but '%s'." % (filename, fileinfo.mime_type) if not options.recoder: # FIXME: list available recoders raise ConvError, "Missing converter.\nSupported converters are: '%s'" % \ ", ".join(odfrecode.get_recoders().keys()) else: try: converter = odfrecode.get_recoder(options.recoder) except KeyError: raise ConvError, "No recoder for '%s' found." % options.recoder prefix, postfix = filename.decode(encoding).rsplit(u'.', 1) backup = u"%s.%s.%s" % (prefix, options.recoder, postfix) try: os.unlink(backup) except OSError: pass # file doesn't exist shutil.copy(filename, backup) dstname = odfrecode.to_utf8(filename, converter, fontmap) os.unlink(filename) shutil.copy(dstname, filename) os.unlink(dstname) dialog = SuccessDialog(filename, backup) except ConvError, error: dialog = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK, message_format="Error during conversion") dialog.format_secondary_text(str(error)) dialog.set_property("title", "ODF Charset Converter") dialog.run() dialog.destroy() if __name__ == "__main__": main(sys.argv) # vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: