1 # -*- coding: utf-8 -*-
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
9 # Full author contact details are available in file CREDITS
11 # This is the main file for the user preferences conversion system.
12 # There are two subsidiary files:
13 # prefs2prefs_lfuns.py
14 # prefs2prefs_prefs.py
15 # The former is used to convert bind and ui files; the latter, to convert
16 # the preferences file.
18 # I've organized it this way because, in many ways, converting bind and ui
19 # files lfuns) and converting the preferences file are the same task. It's
20 # very line-by-line, unlike lyx2lyx and layout2layout, where changes can be
21 # more "global". So we read the file, line by line, and give a bunch of
22 # converter functions a chance to see if they want to modify that line.
24 # The converter functions are all in the subsidiary files. They take a line
25 # as argument and return a list: (Bool, NewLine), where the Bool says if
26 # we've modified anything and the NewLine is the new line, if so, which will
27 # be used to replace the old line.
29 # The format of the existing files is format 0, as of 2.0.alpha6. We'll
30 # introduce new format numbers as we proceed, just as with layout2layout.
31 # These will be different for the bind and ui files and for the preferences
34 import os, re, string, sys
35 from getopt import getopt
37 ###########################################################
38 # Utility functions, borrowed from layout2layout.py
41 " Remove byte order mark."
42 if line[0:3] == "\357\273\277":
49 " Read input file and strip lineendings."
50 lines = source.read().splitlines() or ['']
51 lines[0] = trim_bom(lines[0])
55 def write(output, lines):
56 " Write output file with native lineendings."
57 output.write(os.linesep.join(lines) + os.linesep)
60 # for use by find_format_lines
61 re_comment = re.compile(r'^#')
62 re_empty = re.compile(r'^\s*$')
64 def find_format_line(lines):
66 Returns (bool, int), where int is number of the line the `Format'
67 specification is on, or else the number of the first non-blank,
68 non-comment line. The bool tells whether we found a format line.
70 for i in range(len(lines)):
72 if re_comment.search(l) or re_empty.search(l):
74 m = re_format.search(l)
77 # we're done when we have hit a non-comment, non-empty line
82 # for use by get_format
83 re_format = re.compile(r'^Format\s+(\d+)\s*$')
85 def get_format(lines):
86 " Gets format of current file and replaces the format line with a new one "
87 (found, format_line) = find_format_line(lines)
90 line = lines[format_line]
91 m = re_format.search(line)
93 sys.stderr.write("Couldn't match format line!\n" + line + "\n")
95 return int(m.group(1))
98 def update_format(lines):
99 " Writes new format line "
100 (found, format_line) = find_format_line(lines)
102 lines[format_line:format_line] = ("Format 1", "")
105 line = lines[format_line]
106 m = re_format.search(line)
108 sys.stderr.write("Couldn't match format line!\n" + line + "\n")
110 format = int(m.group(1))
111 lines[format_line] = "Format " + str(format + 1)
115 sys.stderr.write("\n%s\n" % (msg))
119 ###########################################################
122 print "%s [-l] [-p] infile outfile" % sys.argv[0]
123 print "or: %s [-l] [-p] <infile >outfile" % sys.argv[0]
124 print " -l: convert LFUNs (bind and ui files)"
125 print " -p: convert preferences"
126 print "Note that exactly one of -l and -p is required."
131 (options, args) = getopt(sys.argv[1:], "lp")
134 abort("Unrecognized option")
142 source = open(args[0], 'rb')
143 output = open(args[1], 'wb')
147 abort("Either zero or two arguments must be given.")
151 for (opt, param) in options:
153 from prefs2prefs_lfuns import conversions
155 from prefs2prefs_prefs import conversions
159 abort("Neither -l nor -p given.")
160 elif len(options) > 1:
162 abort("Only one of -l or -p should be given.")
164 current_format = len(conversions)
166 format = get_format(lines)
168 while format < current_format:
169 target_format, convert = conversions[format]
172 # make sure the conversion list is sequential
173 if int(old_format) + 1 != target_format:
174 abort("Something is wrong with the conversion chain.")
177 for i in range(len(lines)):
178 (update, newline) = c(lines[i])
183 format = get_format(lines)
186 if int(old_format) + 1 != int(format):
187 abort("Failed to convert to new format!")
199 if __name__ == "__main__":