Use unicode cmdline args consistently

Previously the commandline arguments were processed in an operating system
dependent encoding if running under python 2. Now they are converted to
unicode during the parsing, using the more modern argpase instead of optparse.
The individual conversion methods do no longer need to know anything about
commandline encoding. This fixes a bug similar to #10218 if running under
python 2 which I probably introduced during the python 3 conversion.
This commit is contained in:
Georg Baum 2016-06-16 22:05:56 +02:00
parent bf49ea32c2
commit c6610453e0
4 changed files with 52 additions and 50 deletions

View File

@ -204,10 +204,10 @@ def get_encoding(language, inputencoding, format, cjk_encoding):
class LyX_base: class LyX_base:
"""This class carries all the information of the LyX file.""" """This class carries all the information of the LyX file."""
def __init__(self, end_format = 0, input = "", output = "", error = "", def __init__(self, end_format = 0, input = u'', output = u'', error = u'',
debug = default_debug__, try_hard = 0, cjk_encoding = '', debug = default_debug__, try_hard = 0, cjk_encoding = u'',
final_version = "", systemlyxdir = '', language = "english", final_version = u'', systemlyxdir = u'', language = u'english',
encoding = "auto"): encoding = u'auto'):
"""Arguments: """Arguments:
end_format: final format that the file should be converted. (integer) end_format: final format that the file should be converted. (integer)
@ -459,7 +459,7 @@ class LyX_base:
# Since we do not know the encoding yet we need to read the input as # Since we do not know the encoding yet we need to read the input as
# bytes in binary mode, and convert later to unicode. # bytes in binary mode, and convert later to unicode.
if input and input != '-': if input and input != u'-':
self.dir = os.path.dirname(os.path.abspath(input)) self.dir = os.path.dirname(os.path.abspath(input))
try: try:
gzip.open(input).readline() gzip.open(input).readline()
@ -469,7 +469,7 @@ class LyX_base:
self.input = open(input, 'rb') self.input = open(input, 'rb')
self.compressed = False self.compressed = False
else: else:
self.dir = '' self.dir = u''
self.input = os.fdopen(sys.stdin.fileno(), 'rb') self.input = os.fdopen(sys.stdin.fileno(), 'rb')
self.compressed = False self.compressed = False
@ -817,9 +817,9 @@ class LyX_base:
class File(LyX_base): class File(LyX_base):
" This class reads existing LyX files." " This class reads existing LyX files."
def __init__(self, end_format = 0, input = "", output = "", error = "", def __init__(self, end_format = 0, input = u'', output = u'', error = u'',
debug = default_debug__, try_hard = 0, cjk_encoding = '', debug = default_debug__, try_hard = 0, cjk_encoding = u'',
final_version = '', systemlyxdir = ''): final_version = u'', systemlyxdir = u''):
LyX_base.__init__(self, end_format, input, output, error, LyX_base.__init__(self, end_format, input, output, error,
debug, try_hard, cjk_encoding, final_version, debug, try_hard, cjk_encoding, final_version,
systemlyxdir) systemlyxdir)

View File

@ -19,58 +19,66 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
" Program used to convert between different versions of the lyx file format." " Program used to convert between different versions of the lyx file format."
import optparse import argparse
import sys import sys
import LyX import LyX
# Provide support for both python 2 and 3
PY2 = sys.version_info[0] == 2
if PY2:
# argparse returns strings in the commandline encoding, we need to convert.
# sys.getdefaultencoding() would not always be correct, see
# http://legacy.python.org/dev/peps/pep-0383/
def cmd_arg(arg):
return arg.decode(sys.getfilesystemencoding())
else:
cmd_arg = str
# End of code to support for both python 2 and 3
def main(): def main():
args = {} args = {}
args["usage"] = "usage: %prog [options] [file]" args["usage"] = "%(prog)s [options] [file]"
args["version"] = """lyx2lyx, version %s
Copyright (C) 2011 The LyX Team, José Matos and Dekel Tsur""" % LyX.version__
args["description"] = """Convert old lyx file <file> to newer format, args["description"] = """Convert old lyx file <file> to newer format,
files can be compressed with gzip. If there no file is specified then files can be compressed with gzip. If there no file is specified then
the standard input is assumed, in this case gziped files are not the standard input is assumed, in this case gziped files are not
handled.""" handled."""
parser = optparse.OptionParser(**args) parser = argparse.ArgumentParser(**args)
parser.set_defaults(debug=LyX.default_debug__, cjk_encoding = '') parser.set_defaults(debug=LyX.default_debug__, cjk_encoding = '')
parser.add_option("-d", "--debug", type="int", parser.add_argument("-d", "--debug", type=int, dest="debug",
help="level=0..2 (O_ quiet, 10_verbose) default: 2") help="level=0..2 (O_ quiet, 10_verbose) default: 2")
parser.add_option("-q", "--quiet", parser.add_argument("-q", "--quiet",
action="store_const", const=0, dest="debug") action="store_const", const=0, dest="debug")
parser.add_option("-v", "--verbose", parser.add_argument("-v", "--verbose",
action="store_const", const=1, dest="debug") action="store_const", const=1, dest="debug")
parser.add_option("--noisy", parser.add_argument("--noisy",
action="store_const", const=10, dest="debug") action="store_const", const=10, dest="debug")
parser.add_option("-c", "--encoding", dest="cjk_encoding", parser.add_argument("-c", "--encoding", type=cmd_arg, dest="cjk_encoding",
help="files in format 413 and lower are read and" help="files in format 413 and lower are read and"
" written in the format of CJK-LyX." " written in the format of CJK-LyX."
"If encoding is not given or 'auto' the encoding" "If encoding is not given or 'auto' the encoding"
"is determined from the locale.") "is determined from the locale.")
parser.add_option("-e", "--err", dest="error", parser.add_argument("-e", "--err", type=cmd_arg, dest="error",
help= "file name of the error file else goes to stderr") help= "file name of the error file else goes to stderr")
parser.add_option("-o", "--output", parser.add_argument("-o", "--output", type=cmd_arg, dest="output",
help= "name of the output file else goes to stdout") help= "name of the output file else goes to stdout")
parser.add_option("-t", "--to", dest= "end_format", parser.add_argument("-t", "--to", type=cmd_arg, dest= "end_format",
help= "destination file format, default (latest)") help= "destination file format, default (latest)")
parser.add_option("-V", "--final_version", dest= "final_version", parser.add_argument("-V", "--final_version", type=cmd_arg, dest= "final_version",
help= "destination version, default (latest)") help= "destination version, default (latest)")
parser.add_option("-l", "--list", action="store_true", parser.add_argument("-l", "--list", action="store_true",
help = "list all available formats and supported versions") help = "list all available formats and supported versions")
parser.add_option("-n", "--try-hard", action="store_true", parser.add_argument("-n", "--try-hard", action="store_true",
help = "try hard (ignore any convertion errors)") help = "try hard (ignore any convertion errors)")
parser.add_option("-s", "--systemlyxdir", dest= "systemlyxdir", parser.add_argument("-s", "--systemlyxdir", type=cmd_arg, dest= "systemlyxdir",
help= "LyX system directory for conversion from version 489 or older") help= "LyX system directory for conversion from version 489 or older")
parser.add_argument('--version', action='version', version="""lyx2lyx, version %s
Copyright (C) 2011 The LyX Team, José Matos and Dekel Tsur""" % LyX.version__)
parser.add_argument("input", nargs='?', type=cmd_arg, default=None)
(options, args) = parser.parse_args() options = parser.parse_args()
if args:
options.input = args[0]
else:
options.input = None
if options.list: if options.list:
sys.stderr.write(LyX.format_info()) sys.stderr.write(LyX.format_info())

View File

@ -1893,8 +1893,7 @@ def convert_graphics(document):
return return
i = i + 1 i = i + 1
filename = document.body[j].split()[1] filename = document.body[j].split()[1]
absname = os.path.normpath(os.path.join(document.dir, filename)) if document.dir == u'' and not os.path.isabs(filename):
if document.input == stdin and not os.path.isabs(filename):
# We don't know the directory and cannot check the document. # We don't know the directory and cannot check the document.
# We could use a heuristic and take the current directory, # We could use a heuristic and take the current directory,
# and we could try to find out if documentname has an extension, # and we could try to find out if documentname has an extension,
@ -1905,6 +1904,7 @@ def convert_graphics(document):
You may need to correct the document manually or run You may need to correct the document manually or run
lyx2lyx again with the .lyx document as commandline argument.""" % filename) lyx2lyx again with the .lyx document as commandline argument.""" % filename)
continue continue
absname = os.path.normpath(os.path.join(document.dir, filename))
# This needs to be the same algorithm as in pre 233 insetgraphics # This needs to be the same algorithm as in pre 233 insetgraphics
if access(absname, F_OK): if access(absname, F_OK):
continue continue

View File

@ -39,10 +39,6 @@ from parser_tools import find_token, find_token_backwards, find_re, \
find_end_of_inset, find_end_of_layout, find_nonempty_line, \ find_end_of_inset, find_end_of_layout, find_nonempty_line, \
get_containing_layout, get_value, check_token get_containing_layout, get_value, check_token
# Provide support for both python 2 and 3
PY2 = sys.version_info[0] == 2
# End of code to support for both python 2 and 3
#################################################################### ####################################################################
# Private helper functions # Private helper functions
@ -1135,11 +1131,11 @@ def convert_origin(document):
if i == -1: if i == -1:
document.warning("Malformed LyX document: No \\textclass!!") document.warning("Malformed LyX document: No \\textclass!!")
return return
if document.dir == "": if document.dir == u'':
origin = "stdin" origin = u'stdin'
else: else:
relpath = '' relpath = u''
if document.systemlyxdir and document.systemlyxdir != '': if document.systemlyxdir and document.systemlyxdir != u'':
try: try:
if os.path.isabs(document.dir): if os.path.isabs(document.dir):
absdir = os.path.normpath(document.dir) absdir = os.path.normpath(document.dir)
@ -1150,16 +1146,14 @@ def convert_origin(document):
else: else:
abssys = os.path.normpath(os.path.abspath(document.systemlyxdir)) abssys = os.path.normpath(os.path.abspath(document.systemlyxdir))
relpath = os.path.relpath(absdir, abssys) relpath = os.path.relpath(absdir, abssys)
if relpath.find('..') == 0: if relpath.find(u'..') == 0:
relpath = '' relpath = u''
except: except:
relpath = '' relpath = u''
if relpath == '': if relpath == u'':
origin = document.dir.replace('\\', '/') + '/' origin = document.dir.replace(u'\\', u'/') + u'/'
else: else:
origin = os.path.join("/systemlyxdir", relpath).replace('\\', '/') + '/' origin = os.path.join(u"/systemlyxdir", relpath).replace(u'\\', u'/') + u'/'
if os.name != 'nt' and PY2:
origin = unicode(origin, sys.getfilesystemencoding())
document.header[i:i] = ["\\origin " + origin] document.header[i:i] = ["\\origin " + origin]