2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2002-2004 José Matos <jamatos@lyx.org>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 from parser_tools import read_file, write_file, read_version, set_version, \
24 read_format, set_format, chain, lyxformat, get_value
26 # Allow the dummy object to be able to carry related data
30 self.output = sys.stdout
31 self.input = sys.stdin
38 def warning(self, message, debug_level= 1):
39 if debug_level <= self.debug:
40 self.err.write(message + "\n")
42 def error(self, message):
44 self.warning("Quiting.")
48 print """Usage: lyx2lyx [options] [file]
49 Convert old lyx file <file> to newer format, files can be compressed with gzip.
50 If there no file is specified then the standard input is assumed, in this case
51 gziped files are not handled.
53 -h, --help this information
54 -v, --version output version information and exit
55 -l, --list list all available formats
56 -d, --debug level level=0..2 (O_ no debug information, 2_verbose)
58 -e, --err error_file name of the error file or else goes to stderr
59 -f, --from version initial version (optional)
60 -t, --to version final version (optional)
61 -o, --output name name of the output file or else goes to stdout
62 -q, --quiet same as --debug=0"""
65 def parse_options(argv, version, opt):
66 _options = ["help", "version", "list", "debug=", "err=", "from=", "to=", "output=", "quiet"]
68 opts, args = getopt.getopt(argv[1:], "d:e:f:hlo:qt:v", _options)
74 if o in ("-h", "--help"):
77 if o in ("-v", "--version"):
78 print "lyx2lyx, version %s" %(version)
79 print "Copyright (C) 2002-2004 José Matos and Dekel Tsur"
81 if o in ("-d", "--debug"):
83 if o in ("-q", "--quiet"):
85 if o in ("-l", "--list"):
86 # list available formats
88 if o in ("-o", "--output"):
89 opt.output = open(a, "w")
90 if o in ("-f", "--from"):
91 opt.start = lyxformat(a, opt)
92 if o in ("-t", "--to"):
93 opt.end = lyxformat(a, opt)
94 if o in ("-e","--err"):
95 opt.err = open(a, "w")
99 opt.dir = os.path.dirname(os.path.abspath(file))
101 gzip.open(file).readline()
102 opt.output = gzip.GzipFile("","wb",6,opt.output)
103 opt.input = gzip.open(file)
105 opt.input = open(file)
110 # options object, with default values
113 parse_options(argv, version, opt)
115 header, body = [], []
117 read_file(header, body, opt)
119 initial_version = read_version(header)
120 opt.format = read_format(header, opt)
121 opt.language = get_value(header, "\\language", 0)
122 if opt.language == "":
123 opt.language = "english"
125 mode, convertion_chain = chain(opt, initial_version)
126 opt.warning("convertion chain: " + str(convertion_chain), 3)
128 for step in convertion_chain:
129 convert = getattr(__import__("lyx_" + step), mode)
130 convert(header,body, opt)
132 set_version(header, version)
133 set_format(header, opt.format)
134 write_file(header, body, opt)
136 if __name__ == "__main__":