A go at creating a prefs2prefs framework. There's a main file, prefs2prefs.py, and two subsidiary files,
prefs2prefs_lfuns.py and prefs2prefs_prefs.py. I've organized it this way because, in many ways, these are
the same task. It's very line-by-line, unlike lyx2lyx and layout2layout, where things 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. (Only the lfun one has anything in it now.) 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.
The format of the existing files is format 0, and we'll introduce new format numbers as we proceed,
just as with layout2layout. So the conversion from format 0 to format 1 will be huge; others will
generally be simple.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@35426 a592a061-630c-0410-9148-cb99ea01b6c8
2010-09-17 14:44:38 +00:00
|
|
|
# file prefs2prefs.py
|
|
|
|
# This file is part of LyX, the document processor.
|
|
|
|
# Licence details can be found in the file COPYING.
|
|
|
|
|
2020-11-01 23:08:18 +00:00
|
|
|
# author Richard Kimberly Heck
|
A go at creating a prefs2prefs framework. There's a main file, prefs2prefs.py, and two subsidiary files,
prefs2prefs_lfuns.py and prefs2prefs_prefs.py. I've organized it this way because, in many ways, these are
the same task. It's very line-by-line, unlike lyx2lyx and layout2layout, where things 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. (Only the lfun one has anything in it now.) 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.
The format of the existing files is format 0, and we'll introduce new format numbers as we proceed,
just as with layout2layout. So the conversion from format 0 to format 1 will be huge; others will
generally be simple.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@35426 a592a061-630c-0410-9148-cb99ea01b6c8
2010-09-17 14:44:38 +00:00
|
|
|
|
|
|
|
# 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:
|
2021-02-15 16:06:55 +00:00
|
|
|
# prefs2prefs_lfuns.py
|
|
|
|
# prefs2prefs_prefs.py
|
A go at creating a prefs2prefs framework. There's a main file, prefs2prefs.py, and two subsidiary files,
prefs2prefs_lfuns.py and prefs2prefs_prefs.py. I've organized it this way because, in many ways, these are
the same task. It's very line-by-line, unlike lyx2lyx and layout2layout, where things 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. (Only the lfun one has anything in it now.) 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.
The format of the existing files is format 0, and we'll introduce new format numbers as we proceed,
just as with layout2layout. So the conversion from format 0 to format 1 will be huge; others will
generally be simple.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@35426 a592a061-630c-0410-9148-cb99ea01b6c8
2010-09-17 14:44:38 +00:00
|
|
|
# The former is used to convert bind and ui files; the latter, to convert
|
2020-11-01 23:08:18 +00:00
|
|
|
# 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.
|
A go at creating a prefs2prefs framework. There's a main file, prefs2prefs.py, and two subsidiary files,
prefs2prefs_lfuns.py and prefs2prefs_prefs.py. I've organized it this way because, in many ways, these are
the same task. It's very line-by-line, unlike lyx2lyx and layout2layout, where things 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. (Only the lfun one has anything in it now.) 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.
The format of the existing files is format 0, and we'll introduce new format numbers as we proceed,
just as with layout2layout. So the conversion from format 0 to format 1 will be huge; others will
generally be simple.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@35426 a592a061-630c-0410-9148-cb99ea01b6c8
2010-09-17 14:44:38 +00:00
|
|
|
|
|
|
|
import os, re, string, sys
|
|
|
|
from getopt import getopt
|
2017-04-08 18:58:16 +00:00
|
|
|
import io
|
A go at creating a prefs2prefs framework. There's a main file, prefs2prefs.py, and two subsidiary files,
prefs2prefs_lfuns.py and prefs2prefs_prefs.py. I've organized it this way because, in many ways, these are
the same task. It's very line-by-line, unlike lyx2lyx and layout2layout, where things 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. (Only the lfun one has anything in it now.) 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.
The format of the existing files is format 0, and we'll introduce new format numbers as we proceed,
just as with layout2layout. So the conversion from format 0 to format 1 will be huge; others will
generally be simple.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@35426 a592a061-630c-0410-9148-cb99ea01b6c8
2010-09-17 14:44:38 +00:00
|
|
|
|
|
|
|
###########################################################
|
|
|
|
# Utility functions, borrowed from layout2layout.py
|
|
|
|
|
|
|
|
def trim_bom(line):
|
2021-02-15 16:06:55 +00:00
|
|
|
" Remove byte order mark."
|
2024-06-10 10:29:56 +00:00
|
|
|
if line[0:3] == "\357\273\277":
|
2021-02-15 16:06:55 +00:00
|
|
|
return line[3:]
|
|
|
|
else:
|
|
|
|
return line
|
A go at creating a prefs2prefs framework. There's a main file, prefs2prefs.py, and two subsidiary files,
prefs2prefs_lfuns.py and prefs2prefs_prefs.py. I've organized it this way because, in many ways, these are
the same task. It's very line-by-line, unlike lyx2lyx and layout2layout, where things 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. (Only the lfun one has anything in it now.) 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.
The format of the existing files is format 0, and we'll introduce new format numbers as we proceed,
just as with layout2layout. So the conversion from format 0 to format 1 will be huge; others will
generally be simple.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@35426 a592a061-630c-0410-9148-cb99ea01b6c8
2010-09-17 14:44:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def read(source):
|
2021-02-15 16:06:55 +00:00
|
|
|
" Read input file and strip lineendings."
|
|
|
|
lines = source.read().splitlines() or ['']
|
|
|
|
lines[0] = trim_bom(lines[0])
|
|
|
|
return lines
|
A go at creating a prefs2prefs framework. There's a main file, prefs2prefs.py, and two subsidiary files,
prefs2prefs_lfuns.py and prefs2prefs_prefs.py. I've organized it this way because, in many ways, these are
the same task. It's very line-by-line, unlike lyx2lyx and layout2layout, where things 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. (Only the lfun one has anything in it now.) 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.
The format of the existing files is format 0, and we'll introduce new format numbers as we proceed,
just as with layout2layout. So the conversion from format 0 to format 1 will be huge; others will
generally be simple.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@35426 a592a061-630c-0410-9148-cb99ea01b6c8
2010-09-17 14:44:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def write(output, lines):
|
2021-02-15 16:06:55 +00:00
|
|
|
" Write output file with native lineendings."
|
|
|
|
output.write(os.linesep.join(lines) + os.linesep)
|
A go at creating a prefs2prefs framework. There's a main file, prefs2prefs.py, and two subsidiary files,
prefs2prefs_lfuns.py and prefs2prefs_prefs.py. I've organized it this way because, in many ways, these are
the same task. It's very line-by-line, unlike lyx2lyx and layout2layout, where things 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. (Only the lfun one has anything in it now.) 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.
The format of the existing files is format 0, and we'll introduce new format numbers as we proceed,
just as with layout2layout. So the conversion from format 0 to format 1 will be huge; others will
generally be simple.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@35426 a592a061-630c-0410-9148-cb99ea01b6c8
2010-09-17 14:44:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
# for use by find_format_lines
|
|
|
|
re_comment = re.compile(r'^#')
|
|
|
|
re_empty = re.compile(r'^\s*$')
|
|
|
|
|
|
|
|
def find_format_line(lines):
|
2021-02-15 16:06:55 +00:00
|
|
|
'''
|
|
|
|
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)):
|
|
|
|
l = lines[i]
|
|
|
|
if re_comment.search(l) or re_empty.search(l):
|
|
|
|
continue
|
|
|
|
m = re_format.search(l)
|
|
|
|
if m:
|
|
|
|
return (True, i)
|
|
|
|
# we're done when we have hit a non-comment, non-empty line
|
|
|
|
break
|
|
|
|
return (False, i)
|
A go at creating a prefs2prefs framework. There's a main file, prefs2prefs.py, and two subsidiary files,
prefs2prefs_lfuns.py and prefs2prefs_prefs.py. I've organized it this way because, in many ways, these are
the same task. It's very line-by-line, unlike lyx2lyx and layout2layout, where things 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. (Only the lfun one has anything in it now.) 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.
The format of the existing files is format 0, and we'll introduce new format numbers as we proceed,
just as with layout2layout. So the conversion from format 0 to format 1 will be huge; others will
generally be simple.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@35426 a592a061-630c-0410-9148-cb99ea01b6c8
2010-09-17 14:44:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
# for use by get_format
|
|
|
|
re_format = re.compile(r'^Format\s+(\d+)\s*$')
|
|
|
|
|
|
|
|
def get_format(lines):
|
2021-02-15 16:06:55 +00:00
|
|
|
" Gets format of current file and replaces the format line with a new one "
|
|
|
|
(found, format_line) = find_format_line(lines)
|
|
|
|
if not found:
|
|
|
|
return 0
|
|
|
|
line = lines[format_line]
|
|
|
|
m = re_format.search(line)
|
|
|
|
if not m:
|
|
|
|
sys.stderr.write("Couldn't match format line!\n" + line + "\n")
|
|
|
|
sys.exit(1)
|
|
|
|
return int(m.group(1))
|
A go at creating a prefs2prefs framework. There's a main file, prefs2prefs.py, and two subsidiary files,
prefs2prefs_lfuns.py and prefs2prefs_prefs.py. I've organized it this way because, in many ways, these are
the same task. It's very line-by-line, unlike lyx2lyx and layout2layout, where things 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. (Only the lfun one has anything in it now.) 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.
The format of the existing files is format 0, and we'll introduce new format numbers as we proceed,
just as with layout2layout. So the conversion from format 0 to format 1 will be huge; others will
generally be simple.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@35426 a592a061-630c-0410-9148-cb99ea01b6c8
2010-09-17 14:44:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def update_format(lines):
|
2021-02-15 16:06:55 +00:00
|
|
|
" Writes new format line "
|
|
|
|
(found, format_line) = find_format_line(lines)
|
|
|
|
if not found:
|
|
|
|
lines[format_line:format_line] = ("Format 1", "")
|
|
|
|
return
|
A go at creating a prefs2prefs framework. There's a main file, prefs2prefs.py, and two subsidiary files,
prefs2prefs_lfuns.py and prefs2prefs_prefs.py. I've organized it this way because, in many ways, these are
the same task. It's very line-by-line, unlike lyx2lyx and layout2layout, where things 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. (Only the lfun one has anything in it now.) 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.
The format of the existing files is format 0, and we'll introduce new format numbers as we proceed,
just as with layout2layout. So the conversion from format 0 to format 1 will be huge; others will
generally be simple.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@35426 a592a061-630c-0410-9148-cb99ea01b6c8
2010-09-17 14:44:38 +00:00
|
|
|
|
2021-02-15 16:06:55 +00:00
|
|
|
line = lines[format_line]
|
|
|
|
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[format_line] = "Format " + str(format + 1)
|
A go at creating a prefs2prefs framework. There's a main file, prefs2prefs.py, and two subsidiary files,
prefs2prefs_lfuns.py and prefs2prefs_prefs.py. I've organized it this way because, in many ways, these are
the same task. It's very line-by-line, unlike lyx2lyx and layout2layout, where things 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. (Only the lfun one has anything in it now.) 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.
The format of the existing files is format 0, and we'll introduce new format numbers as we proceed,
just as with layout2layout. So the conversion from format 0 to format 1 will be huge; others will
generally be simple.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@35426 a592a061-630c-0410-9148-cb99ea01b6c8
2010-09-17 14:44:38 +00:00
|
|
|
|
|
|
|
|
2015-11-27 03:40:39 +00:00
|
|
|
def abort(msg):
|
2021-02-15 16:06:55 +00:00
|
|
|
sys.stderr.write("\n%s\n" % (msg))
|
|
|
|
sys.exit(10)
|
2015-11-27 03:40:39 +00:00
|
|
|
|
A go at creating a prefs2prefs framework. There's a main file, prefs2prefs.py, and two subsidiary files,
prefs2prefs_lfuns.py and prefs2prefs_prefs.py. I've organized it this way because, in many ways, these are
the same task. It's very line-by-line, unlike lyx2lyx and layout2layout, where things 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. (Only the lfun one has anything in it now.) 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.
The format of the existing files is format 0, and we'll introduce new format numbers as we proceed,
just as with layout2layout. So the conversion from format 0 to format 1 will be huge; others will
generally be simple.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@35426 a592a061-630c-0410-9148-cb99ea01b6c8
2010-09-17 14:44:38 +00:00
|
|
|
#
|
|
|
|
###########################################################
|
|
|
|
|
|
|
|
def usage():
|
2021-02-15 16:06:55 +00:00
|
|
|
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.")
|
A go at creating a prefs2prefs framework. There's a main file, prefs2prefs.py, and two subsidiary files,
prefs2prefs_lfuns.py and prefs2prefs_prefs.py. I've organized it this way because, in many ways, these are
the same task. It's very line-by-line, unlike lyx2lyx and layout2layout, where things 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. (Only the lfun one has anything in it now.) 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.
The format of the existing files is format 0, and we'll introduce new format numbers as we proceed,
just as with layout2layout. So the conversion from format 0 to format 1 will be huge; others will
generally be simple.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@35426 a592a061-630c-0410-9148-cb99ea01b6c8
2010-09-17 14:44:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main(argv):
|
2021-02-15 16:06:55 +00:00
|
|
|
try:
|
|
|
|
(options, args) = getopt(sys.argv[1:], "lp")
|
|
|
|
except:
|
|
|
|
usage()
|
|
|
|
abort("Unrecognized option")
|
|
|
|
|
|
|
|
opened_files = False
|
|
|
|
# Open files
|
|
|
|
if len(args) == 0:
|
|
|
|
source = sys.stdin
|
|
|
|
output = sys.stdout
|
|
|
|
elif len(args) == 2:
|
2024-06-10 10:29:56 +00:00
|
|
|
source = open(args[0], encoding='utf_8', errors='surrogateescape')
|
|
|
|
output = open(args[1], 'w', encoding='utf_8', newline='\n')
|
2021-02-15 16:06:55 +00:00
|
|
|
opened_files = True
|
|
|
|
else:
|
|
|
|
usage()
|
|
|
|
abort("Either zero or two arguments must be given.")
|
|
|
|
|
|
|
|
conversions = False
|
|
|
|
|
|
|
|
for (opt, param) in options:
|
|
|
|
if opt == "-l":
|
|
|
|
from prefs2prefs_lfuns import conversions
|
|
|
|
elif opt == "-p":
|
|
|
|
from prefs2prefs_prefs import conversions
|
|
|
|
|
|
|
|
if not conversions:
|
|
|
|
usage()
|
|
|
|
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:
|
|
|
|
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:
|
|
|
|
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):
|
|
|
|
abort("Failed to convert to new format!")
|
|
|
|
|
|
|
|
write(output, lines)
|
|
|
|
|
|
|
|
# Close files
|
|
|
|
if opened_files:
|
|
|
|
source.close()
|
|
|
|
output.close()
|
|
|
|
|
|
|
|
return 0
|
A go at creating a prefs2prefs framework. There's a main file, prefs2prefs.py, and two subsidiary files,
prefs2prefs_lfuns.py and prefs2prefs_prefs.py. I've organized it this way because, in many ways, these are
the same task. It's very line-by-line, unlike lyx2lyx and layout2layout, where things 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. (Only the lfun one has anything in it now.) 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.
The format of the existing files is format 0, and we'll introduce new format numbers as we proceed,
just as with layout2layout. So the conversion from format 0 to format 1 will be huge; others will
generally be simple.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@35426 a592a061-630c-0410-9148-cb99ea01b6c8
2010-09-17 14:44:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-02-15 16:06:55 +00:00
|
|
|
main(sys.argv)
|