Read the search replace patterns file differently

Might fix patterns not being recognized on windows
This commit is contained in:
Kovid Goyal 2017-06-12 20:58:12 +05:30
parent 495bece5e8
commit 1d945ced65
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -331,31 +331,29 @@ def abspath(x):
def read_sr_patterns(path, log=None):
import json, re, codecs
import json, re
pats = []
with codecs.open(path, 'r', 'utf-8') as f:
pat = None
for line in f.readlines():
if line.endswith(u'\n'):
line = line[:-1]
if pat is None:
if not line.strip():
continue
try:
re.compile(line)
except:
msg = u'Invalid regular expression: %r from file: %r'%(
line, path)
if log is not None:
log.error(msg)
raise SystemExit(1)
else:
raise ValueError(msg)
pat = line
else:
pats.append((pat, line))
pat = None
with open(path, 'rb') as f:
lines = f.read().decode('utf-8').splitlines()
pat = None
for line in lines:
if pat is None:
if not line.strip():
continue
try:
re.compile(line)
except:
msg = u'Invalid regular expression: %r from file: %r'%(
line, path)
if log is not None:
log.error(msg)
raise SystemExit(1)
else:
raise ValueError(msg)
pat = line
else:
pats.append((pat, line))
pat = None
return json.dumps(pats)