]> git.lyx.org Git - lyx.git/blobdiff - lib/scripts/prefs2prefs.py
Introduce new InsetLayout tag InheritFont
[lyx.git] / lib / scripts / prefs2prefs.py
index 3026d061637e0acb4f45fa9b804e59f452d5568b..00d619de211d800978076b99583262a97c177b00 100644 (file)
@@ -1,49 +1,37 @@
-#! /usr/bin/env python
 # -*- coding: utf-8 -*-
 
 # file prefs2prefs.py
 # This file is part of LyX, the document processor.
 # Licence details can be found in the file COPYING.
 
-# author Richard Heck
+# author Richard Kimberly Heck
 
 # Full author contact details are available in file CREDITS
 
 # This is the main file for the user preferences conversion system.
 # There are two subsidiary files:
-#      prefs2prefs_lfuns.py
-#      prefs2prefs_prefs.py
+#    prefs2prefs_lfuns.py
+#    prefs2prefs_prefs.py
 # The former is used to convert bind and ui files; the latter, to convert
-# the preferences file.
-#
-# I've organized it this way because, in many ways, converting bind and ui 
-# files  lfuns) and converting the preferences file are the same task. It's 
-# very line-by-line, unlike lyx2lyx and layout2layout, where changes can be 
-# more "global". So we read the file, line by line, and give a bunch of 
-# converter functions a chance to see if they want to modify that line.
-
-# The converter functions are all in the subsidiary files. They take a line 
-# as  argument and return a list: (Bool, NewLine), where the Bool says if 
-# we've modified anything and the NewLine is the new line, if so, which will
-# be used to replace the old line.
-
-# The format of the existing files is format 0, as of 2.0.alpha6. We'll 
-# introduce new format numbers as we proceed, just as with layout2layout.
-# These will be different for the bind and ui files and for the preferences
-# file. 
+# the preferences file. The converter functions are all in the subsidiary 
+# files. 
+# 
+# The format of the existing files was format 0, as of 2.0.alpha6.
 
+from __future__ import print_function
 import os, re, string, sys
 from getopt import getopt
+import io
 
 ###########################################################
 # Utility functions, borrowed from layout2layout.py
 
 def trim_bom(line):
        " Remove byte order mark."
-       if line[0:3] == "\357\273\277":
+       if line[0:3] == u"\357\273\277":
                return line[3:]
        else:
-               return  line
+               return line
 
 
 def read(source):
@@ -64,8 +52,8 @@ re_empty   = re.compile(r'^\s*$')
 
 def find_format_line(lines):
        '''
-       Returns (bool, int), where int is number of the line the `Format' 
-       specification is on, or else the number of the first non-blank, 
+       Returns (bool, int), where int is number of the line the `Format'
+       specification is on, or else the number of the first non-blank,
        non-comment line. The bool tells whether we found a format line.
        '''
        for i in range(len(lines)):
@@ -112,15 +100,19 @@ def update_format(lines):
        lines[format_line] = "Format " + str(format + 1)
 
 
+def abort(msg):
+       sys.stderr.write("\n%s\n" % (msg))
+       sys.exit(10)
+
 #
 ###########################################################
 
 def usage():
-       print "%s [-l] [-p] infile outfile" % sys.argv[0]
-       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 exactly one of -l and -p is required."
+       print ("%s [-l] [-p] infile outfile" % sys.argv[0])
+       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 exactly one of -l and -p is required.")
 
 
 def main(argv):
@@ -128,8 +120,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
@@ -137,13 +128,12 @@ def main(argv):
                source = sys.stdin
                output = sys.stdout
        elif len(args) == 2:
-               source = open(args[0], 'rb')
-               output = open(args[1], 'wb')
+               source = io.open(args[0], 'r', encoding='utf_8', errors='surrogateescape')
+               output = io.open(args[1], 'w', encoding='utf_8', newline='\n')
                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
 
@@ -152,15 +142,13 @@ def main(argv):
                        from prefs2prefs_lfuns import conversions
                elif opt == "-p":
                        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()
-               print "\nOnly one of -l or -p should be given."
-               sys.exit(1)
+               abort("Only one of -l or -p should be given.")
 
        current_format = len(conversions)
        lines = read(source)
@@ -172,22 +160,25 @@ def main(argv):
 
                # make sure the conversion list is sequential
                if int(old_format) + 1 != target_format:
-                       sys.stderr.write("Something is wrong with the conversion chain.\n")
-                       sys.exit(1)
+                       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
+                       try:
+                               # first see if the routine will accept a list of lines
+                               c(lines)
+                       except:
+                               # if not, it wants individual lines
+                               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
                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)
 
@@ -200,4 +191,4 @@ def main(argv):
 
 
 if __name__ == "__main__":
-    main(sys.argv)
+       main(sys.argv)