mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Read the search replace patterns file differently
Might fix patterns not being recognized on windows
This commit is contained in:
parent
495bece5e8
commit
1d945ced65
@ -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)
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user