From 3a68b57aaef64fa68814eefaacc35562465481e9 Mon Sep 17 00:00:00 2001 From: Kevin Hendricks Date: Tue, 20 Dec 2016 09:27:42 -0500 Subject: [PATCH] Make file offset calculations more robust to different failure modes --- src/FlightCrew-plugin/plugin.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/FlightCrew-plugin/plugin.py b/src/FlightCrew-plugin/plugin.py index 4d5080f7..2f3fe5cf 100644 --- a/src/FlightCrew-plugin/plugin.py +++ b/src/FlightCrew-plugin/plugin.py @@ -80,6 +80,8 @@ } def generate_line_offsets(s): + if s is None: + return None offlst = [0] i = s.find('\n', 0) while i >= 0: @@ -174,15 +176,23 @@ def run(bk): # generate file offsets once per file if bk.launcher_version() >= 20160802 and filepath != lastfilepath: text = None - with open(os.path.join(ebook_root, filepath), 'rb') as f: - text = f.read() - text = text.decode('utf-8', errors='replace') + afile = os.path.join(ebook_root, filepath) + if os.path.isfile(afile): + with open(afile, 'rb') as f: + text = f.read() + text = text.decode('utf-8', errors='replace') offlst = generate_line_offsets(text) lastfilepath = filepath # generate column offset - if bk.launcher_version() >= 20160802 and lineno and colno: - coffset = charoffset(int(lineno), int(colno), offlst) + if bk.launcher_version() >= 20160802: + if lineno is not None and colno is not None and offlst is not None: + try: + nlineno = int(lineno) + ncolno = int(colno) + coffset = charoffset(nlineno, ncolno, offlst) + except ValueError: + coffset = None msg = resid + ' : ' + msg + " near column " + colno # since will be passed as attribute make sure to handle any embedded quotes