]> git.lyx.org Git - features.git/blob - lib/lyx2lyx/lyx2lyx
fix handling of gzipped files, move default warning level to 1
[features.git] / lib / lyx2lyx / lyx2lyx
1 #! /usr/bin/env python
2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2002-2004 José Matos <jamatos@lyx.org>
4 #
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.
9 #
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.
14 #
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.
18
19 import getopt
20 import gzip
21 import sys
22 import os.path
23 from parser_tools import read_file, write_file, read_version, set_version, \
24      read_format, set_format, chain, lyxformat, get_value
25
26 # Allow the dummy object to be able to carry related data
27 # like a C struct
28 class struct:
29     def __init__(self):
30         self.output = sys.stdout
31         self.input = sys.stdin
32         self.err = sys.stderr
33         self.debug = 1
34         self.dir = ""
35         self.start = None
36         self.end = None
37
38     def warning(self, message, debug_level= 1):
39         if debug_level <= self.debug:
40             self.err.write(message + "\n")
41
42     def error(self, message):
43         self.warning(message)
44         self.warning("Quiting.")
45         sys.exit(1)
46
47 def usage():
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.
52 Options:
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)
57                                 default: level=1
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"""
63
64
65 def parse_options(argv, version, opt):
66     _options =  ["help", "version", "list", "debug=", "err=", "from=", "to=", "output=", "quiet"]
67     try:
68        opts, args = getopt.getopt(argv[1:], "d:e:f:hlo:qt:v", _options)
69     except getopt.error:
70         usage()
71         sys.exit(2)
72
73     for o, a in opts:
74         if o in ("-h", "--help"):
75             usage()
76             sys.exit()
77         if o in ("-v", "--version"):
78             print "lyx2lyx, version %s" %(version)
79             print "Copyright (C) 2002-2004 José Matos and Dekel Tsur"
80             sys.exit()
81         if o in ("-d", "--debug"):
82             opt.debug = int(a)
83         if o in ("-q", "--quiet"):
84             opt.debug = 0
85         if o in ("-l", "--list"):
86             # list available formats
87             sys.exit()
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")
96
97     if args:
98         file = args[0]
99         opt.dir = os.path.dirname(os.path.abspath(file))
100         try:
101             gzip.open(file).readline()
102             opt.output = gzip.GzipFile("","wb",6,opt.output)
103             opt.input = gzip.open(file)
104         except:
105             opt.input = open(file)
106
107 def main(argv):
108     version = "1.4.0cvs"
109
110     # options object, with default values
111     opt = struct()
112     
113     parse_options(argv, version, opt)
114
115     header, body = [], []
116
117     read_file(header, body, opt)
118
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"
124
125     mode, convertion_chain = chain(opt, initial_version)
126     opt.warning("convertion chain: " + str(convertion_chain), 3)
127
128     for step in convertion_chain:
129         convert = getattr(__import__("lyx_" + step), mode)
130         convert(header,body, opt)
131
132     set_version(header, version)
133     set_format(header, opt.format)
134     write_file(header, body, opt)
135
136 if __name__ == "__main__":
137     main(sys.argv)