]> git.lyx.org Git - lyx.git/blobdiff - lib/scripts/prefs2prefs.py
whitespace.
[lyx.git] / lib / scripts / prefs2prefs.py
index bdbc7e327a90646dced7c0a0c79c2ce7b6bab5ae..132e21a52d959223fad57d810d9a8b05a9f421af 100644 (file)
@@ -1,4 +1,3 @@
-#! /usr/bin/env python
 # -*- coding: utf-8 -*-
 
 # file prefs2prefs.py
@@ -48,7 +47,7 @@ def trim_bom(line):
 
 def read(source):
        " Read input file and strip lineendings."
-       lines = source.read().splitlines()
+       lines = source.read().splitlines() or ['']
        lines[0] = trim_bom(lines[0])
        return lines
 
@@ -100,17 +99,21 @@ def update_format(lines):
        " Writes new format line "
        (found, format_line) = find_format_line(lines)
        if not found:
-               lines.insert(format_line, "Format 1")
+               lines[format_line:format_line] = ("Format 1", "")
                return
 
        line = lines[format_line]
-       m = re_format.search(l)
+       m = re_format.search(line)
        if not m:
                sys.stderr.write("Couldn't match format line!\n" + line + "\n")
                sys.exit(1)
        format = int(m.group(1))
-       lines[i] = "Format " + str(format + 1)
+       lines[format_line] = "Format " + str(format + 1)
+
 
+def abort(msg):
+       sys.stderr.write("\n%s\n" % (msg))
+       sys.exit(10)
 
 #
 ###########################################################
@@ -120,7 +123,7 @@ def usage():
        print "or: %s [-l] [-p] <infile >outfile" % sys.argv[0]
        print "  -l: convert LFUNs (bind and ui files)"
        print "  -p: convert preferences"
-       print "Note that one of -l and -p is required."
+       print "Note that exactly one of -l and -p is required."
 
 
 def main(argv):
@@ -128,8 +131,7 @@ def main(argv):
                (options, args) = getopt(sys.argv[1:], "lp")
        except:
                usage()
-               print "\nUnrecognized option"
-               sys.exit(1)
+               abort("Unrecognized option")
 
        opened_files = False
        # Open files
@@ -142,40 +144,47 @@ def main(argv):
                opened_files = True
        else:
                usage()
-               print "\nEither zero or two arguments must be given."
-               sys.exit(1)
+               abort("Either zero or two arguments must be given.")
 
        conversions = False
 
        for (opt, param) in options:
                if opt == "-l":
-                       from prefs2prefs_lfuns import conversions, current_format
+                       from prefs2prefs_lfuns import conversions
                elif opt == "-p":
-                       from prefs2prefs_prefs import conversions, current_format
+                       from prefs2prefs_prefs import conversions
        
        if not conversions:
                usage()
-               print "\nNeither -l nor -p given"
-               sys.exit(1)
+               abort("Neither -l nor -p given.")
+       elif len(options) > 1:
+               usage()
+               abort("Only one of -l or -p should be given.")
 
+       current_format = len(conversions)
        lines = read(source)
        format = get_format(lines)
 
        while format < current_format:
-               for c in conversions[format]:
+               target_format, convert = conversions[format]
+               old_format = format
+
+               # make sure the conversion list is sequential
+               if int(old_format) + 1 != target_format:
+                       abort("Something is wrong with the conversion chain.")
+
+               for c in convert:
                        for i in range(len(lines)):
                                (update, newline) = c(lines[i])
                                if update:
                                        lines[i] = newline
 
                update_format(lines)
+               format = get_format(lines)
 
                # sanity check
-               old_format = format
-               format = get_format(lines)
                if int(old_format) + 1 != int(format):
-                       sys.stderr.write("Failed to convert to new format!\n")
-                       sys.exit(1)
+                       abort("Failed to convert to new format!")
 
        write(output, lines)