aboutsummaryrefslogtreecommitdiff
path: root/bin/odfrecode
blob: 401f46d6b0184d48c9054fa1f571e8e18830960b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/python
# vim:encoding=utf-8:fileencoding=utf-8
#
# simple national to 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 <agx@sigxcpu.org>
# (c) 2008 Torsten Werner <twerner@debian.org>
#
#   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

def main(argv):
    parser = OptionParser()
    parser.add_option("--recoder", dest="recoder",
                      help="use recoder RECODER", metavar="recoder")

    encoding = sys.getfilesystemencoding()

    (options, args) = parser.parse_args()

    if len(args) != 1:
       print >>sys.stderr, "Need a file to convert."
       return 1
    else:
        filename = os.path.abspath(args[0])

    if not options.recoder:
       print >>sys.stderr, "Missing recoder."
       return 1
    else:
        try:
            converter = odfrecode.get_recoder(options.recoder)
        except KeyError:
            print >>sys.stderr, "No recoder for '%s' found." % options.recoder
            print >>sys.stderr, "Available recoders: %s." % odfrecode.recoders.recoders.keys()
            return 1

    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)
    os.unlink(filename)
    shutil.copy(dstname, filename)
    os.unlink(dstname)

    print filename

if __name__ == "__main__":
   main(sys.argv)

# vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: