X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=lib%2Fscripts%2Flyxpreview_tools.py;h=7783fe2b7ddde61529d3d5307642ebb828887870;hb=e4065cc1b1394c61108735611e0ebf02b12e6bbe;hp=de2e5c5762cc98942560bf483206a4ac4d45e4af;hpb=cabc7c4be191f4700f79bd6ca9109697ae074b58;p=lyx.git diff --git a/lib/scripts/lyxpreview_tools.py b/lib/scripts/lyxpreview_tools.py index de2e5c5762..7783fe2b7d 100644 --- a/lib/scripts/lyxpreview_tools.py +++ b/lib/scripts/lyxpreview_tools.py @@ -10,12 +10,12 @@ # Paul A. Rubin, rubin@msu.edu. # A repository of the following functions, used by the lyxpreview2xyz scripts. -# copyfileobj, error, find_exe, find_exe_or_terminate, make_texcolor, mkstemp, +# copyfileobj, error, find_exe, find_exe_or_terminate, make_texcolor, # progress, run_command, run_latex, warning # Requires python 2.4 or later (subprocess module). -import os, re, string, subprocess, sys, tempfile +import os, re, subprocess, sys, tempfile # Control the output to stdout @@ -72,18 +72,18 @@ def error(message): def make_texcolor(hexcolor, graphics): # Test that the input string contains 6 hexadecimal chars. - hexcolor_re = re.compile("^[0-9a-fA-F]{6}$") + hexcolor_re = re.compile(b"^[0-9a-fA-F]{6}$") if not hexcolor_re.match(hexcolor): error("Cannot convert color '%s'" % hexcolor) - red = float(string.atoi(hexcolor[0:2], 16)) / 255.0 - green = float(string.atoi(hexcolor[2:4], 16)) / 255.0 - blue = float(string.atoi(hexcolor[4:6], 16)) / 255.0 + red = float(int(hexcolor[0:2], 16)) / 255.0 + green = float(int(hexcolor[2:4], 16)) / 255.0 + blue = float(int(hexcolor[4:6], 16)) / 255.0 if graphics: - return "%f,%f,%f" % (red, green, blue) + return b"%f,%f,%f" % (red, green, blue) else: - return "rgb %f %f %f" % (red, green, blue) + return b"rgb %f %f %f" % (red, green, blue) def find_exe(candidates): @@ -110,7 +110,7 @@ def find_exe(candidates): def find_exe_or_terminate(candidates): exe = find_exe(candidates) if exe == None: - error("Unable to find executable from '%s'" % string.join(candidates)) + error("Unable to find executable from '%s'" % " ".join(candidates)) return exe @@ -168,7 +168,7 @@ def run_command_win32(cmd): if hr != winerror.ERROR_IO_PENDING: data = data + buffer - except pywintypes.error, e: + except pywintypes.error as e: if e.args[0] != winerror.ERROR_BROKEN_PIPE: error = 1 break @@ -196,16 +196,6 @@ def run_command(cmd, stderr2stdout = True): return run_command_popen(cmd, stderr2stdout) -def get_version_info(): - version_re = re.compile("([0-9])\.([0-9])") - - match = version_re.match(sys.version) - if match == None: - error("Unable to extract version info from 'sys.version'") - - return string.atoi(match.group(1)), string.atoi(match.group(2)) - - def copyfileobj(fsrc, fdst, rewind=0, length=16*1024): """copy data from file-like object fsrc to file-like object fdst""" if rewind: @@ -219,69 +209,26 @@ def copyfileobj(fsrc, fdst, rewind=0, length=16*1024): fdst.write(buf) -class TempFile: - """clone of tempfile.TemporaryFile to use with python < 2.0.""" - # Cache the unlinker so we don't get spurious errors at shutdown - # when the module-level "os" is None'd out. Note that this must - # be referenced as self.unlink, because the name TempFile - # may also get None'd out before __del__ is called. - unlink = os.unlink - - def __init__(self): - self.filename = tempfile.mktemp() - self.file = open(self.filename,"w+b") - self.close_called = 0 - - def close(self): - if not self.close_called: - self.close_called = 1 - self.file.close() - self.unlink(self.filename) - - def __del__(self): - self.close() - - def read(self, size = -1): - return self.file.read(size) - - def write(self, line): - return self.file.write(line) - - def seek(self, offset): - return self.file.seek(offset) - - def flush(self): - return self.file.flush() - - -def mkstemp(): - """create a secure temporary file and return its object-like file""" - major, minor = get_version_info() - - if major >= 2 and minor >= 0: - return tempfile.TemporaryFile() - else: - return TempFile() - def write_metrics_info(metrics_info, metrics_file): metrics = open(metrics_file, 'w') for metric in metrics_info: metrics.write("Snippet %s %f\n" % metric) metrics.close() + # Reads a .tex files and create an identical file but only with # pages whose index is in pages_to_keep def filter_pages(source_path, destination_path, pages_to_keep): - def_re = re.compile(r"(\\newcommandx|\\renewcommandx|\\global\\long\\def)(\\[a-zA-Z]+)(.+)") - source_file = open(source_path, "r") - destination_file = open(destination_path, "w") + def_re = re.compile(b"(\\\\newcommandx|\\\\renewcommandx|\\\\global\\\\long\\\\def)(\\[a-zA-Z]+)(.+)") + source_file = open(source_path, "rb") + destination_file = open(destination_path, "wb") page_index = 0 skip_page = False macros = [] for line in source_file: # We found a new page - if line.startswith("\\begin{preview}"): + if line.startswith(b"\\begin{preview}"): page_index += 1 # If the page index isn't in pages_to_keep we don't copy it skip_page = page_index not in pages_to_keep @@ -293,12 +240,12 @@ def filter_pages(source_path, destination_path, pages_to_keep): macroname = match.group(2) if not macroname in macros: macros.append(macroname) - if definecmd == "\\renewcommandx": - line = line.replace(definecmd, "\\newcommandx") + if definecmd == b"\\renewcommandx": + line = line.replace(definecmd, b"\\newcommandx") destination_file.write(line) # End of a page, we reset the skip_page bool - if line.startswith("\\end{preview}"): + if line.startswith(b"\\end{preview}"): skip_page = False destination_file.close() @@ -366,9 +313,9 @@ def run_tex(tex, tex_file): def string_in_file(string, infile): if not os.path.isfile(infile): return False - f = open(infile, 'r') + f = open(infile, 'rb') for line in f.readlines(): - if string in line: + if string.encode() in line: f.close() return True f.close() @@ -378,15 +325,15 @@ def string_in_file(string, infile): # Returns a list of indexes of pages giving errors extracted from the latex log def check_latex_log(log_file): - error_re = re.compile("^! ") - snippet_re = re.compile("^Preview: Snippet ") - data_re = re.compile("([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)") + error_re = re.compile(b"^! ") + snippet_re = re.compile(b"^Preview: Snippet ") + data_re = re.compile(b"([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)") found_error = False error_pages = [] try: - for line in open(log_file, 'r').readlines(): + for line in open(log_file, 'rb').readlines(): if not found_error: match = error_re.match(line) if match != None: @@ -406,6 +353,6 @@ def check_latex_log(log_file): except: warning('check_latex_log: Unable to open "%s"' % log_file) - warning(`sys.exc_type` + ',' + `sys.exc_value`) + warning(repr(sys.exc_info()[0]) + ',' + repr(sys.exc_info()[1])) return error_pages