#!/usr/bin/python # (c) Eugene M. Minkovskii; readablertf.py 07 Dec 2003 # emin(at)mccme(point)ru # This utility writen for python 2.3 and later ## GPL {{{1 # 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 1, 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., 675 Mass Ave, Cambridge, MA 02139, USA. ## }}} ## Change Log {{{ # 0.1 -- a little bit faster work. ## }}} import re, os, os.path, sys from optparse import OptionParser __version__ = (0, 1) parser = OptionParser( usage="""[python] %%prog [options] files and/or directories Make RTF files readable. If directories specified, prepare all RTF files in that directory. For example: to prepare all files in current directory use: %%prog %s """%os.curdir) parser.add_option("-e", "--rtf-encoding", action="store", dest="encoding", default="cp1251", help="Internal RTF encoding") parser.add_option("-r", "--recursive", action="store_true", dest="recursive", default=False, help="Search RTF recursively in subdirectories") parser.add_option("-n", "--no-backup", action="store_false", dest="backup", default=True, help="Don't create backup copy") parser.add_option("-q", "--quiet", action="store_true", dest="quiet", default=False, help="Keep silence") parser.add_option("-v", "--version", action="version", help="Print version number and exit. " "This version number should be included in all bug reports") parser.version = "%%prog %i.%i"%__version__ (options, args) = parser.parse_args() if not args: parser.print_help() sys.exit(1) def prepare(filename): f = file(filename, "rb+") contents = f.read() f.seek(0) if options.backup: file(filename+".bak", "wb").write(contents) # convert hex codes into letters contents = re.sub( r"\\'([0-9a-fA-F]{2})", lambda x: chr(int(x.group(1), 16)), contents).decode(options.encoding) # join splited words contents = re.sub( ur"(?u)\b(\w+)(\r?\n|\r)(\w+[]).,!:;'\"]?)", ur"\1\3\2", contents) # write result f.write(contents.encode(options.encoding)) f.truncate() f.close() counter = 0 for i in filter(os.path.isfile, args): prepare(i) counter += 1 if not options.quiet: print i for i in filter(os.path.isdir, args): if options.recursive: for root, dirs, files in os.walk(i): files = filter(lambda x: re.search(r"(?i)\.rtf$",x), files) for j in files: prepare(root+os.sep+j) counter += 1 if not options.quiet: print root+os.sep+j else: files = filter(os.path.isfile, os.listdir(i)) files = filter(lambda x: re.search(r"(?i)\.rtf$",x), files) for j in files: prepare(i+os.sep+j) counter += 1 if not options.quiet: print i+os.sep+j if not options.quiet: print "="*72 print "%i file%s prepared"%(counter, {True:"s",False:""}[counter>1]) sys.exit(0)