mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Replace the six module with the polyglot module
This commit is contained in:
parent
73a0b1ca2a
commit
b1435f9b44
@ -11,8 +11,8 @@ __docformat__ = 'restructuredtext en'
|
|||||||
import os, shutil, uuid, json, glob, time, hashlib, errno, sys
|
import os, shutil, uuid, json, glob, time, hashlib, errno, sys
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
import six
|
|
||||||
import apsw
|
import apsw
|
||||||
|
from polyglot.builtins import reraise
|
||||||
|
|
||||||
from calibre import isbytestring, force_unicode, prints, as_unicode
|
from calibre import isbytestring, force_unicode, prints, as_unicode
|
||||||
from calibre.constants import (iswindows, filesystem_encoding,
|
from calibre.constants import (iswindows, filesystem_encoding,
|
||||||
@ -1627,7 +1627,7 @@ class DB(object):
|
|||||||
except EnvironmentError as err:
|
except EnvironmentError as err:
|
||||||
if err.errno == errno.EEXIST:
|
if err.errno == errno.EEXIST:
|
||||||
# Parent directory already exists, re-raise original exception
|
# Parent directory already exists, re-raise original exception
|
||||||
six.reraise(*exc_info)
|
reraise(*exc_info)
|
||||||
raise
|
raise
|
||||||
finally:
|
finally:
|
||||||
del exc_info
|
del exc_info
|
||||||
|
@ -6,7 +6,8 @@ from __future__ import (unicode_literals, division, absolute_import,
|
|||||||
import re, sys
|
import re, sys
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
import six
|
from polyglot.builtins import reraise
|
||||||
|
|
||||||
from lxml.etree import tostring
|
from lxml.etree import tostring
|
||||||
from lxml.html import (fragment_fromstring, document_fromstring,
|
from lxml.html import (fragment_fromstring, document_fromstring,
|
||||||
tostring as htostring)
|
tostring as htostring)
|
||||||
@ -63,7 +64,7 @@ def to_int(x):
|
|||||||
|
|
||||||
|
|
||||||
def clean(text):
|
def clean(text):
|
||||||
text = re.sub('\s*\n\s*', '\n', text)
|
text = re.sub('\\s*\n\\s*', '\n', text)
|
||||||
text = re.sub('[ \t]{2,}', ' ', text)
|
text = re.sub('[ \t]{2,}', ' ', text)
|
||||||
return text.strip()
|
return text.strip()
|
||||||
|
|
||||||
@ -157,7 +158,7 @@ class Document:
|
|||||||
return cleaned_article
|
return cleaned_article
|
||||||
except StandardError as e:
|
except StandardError as e:
|
||||||
self.log.exception('error getting summary: ')
|
self.log.exception('error getting summary: ')
|
||||||
six.reraise(Unparseable, Unparseable(str(e)), sys.exc_info()[2])
|
reraise(Unparseable, Unparseable(str(e)), sys.exc_info()[2])
|
||||||
|
|
||||||
def get_article(self, candidates, best_candidate):
|
def get_article(self, candidates, best_candidate):
|
||||||
# Now that we have the top candidate, look through its siblings for content that might also be related.
|
# Now that we have the top candidate, look through its siblings for content that might also be related.
|
||||||
@ -184,7 +185,7 @@ class Document:
|
|||||||
|
|
||||||
if node_length > 80 and link_density < 0.25:
|
if node_length > 80 and link_density < 0.25:
|
||||||
append = True
|
append = True
|
||||||
elif node_length < 80 and link_density == 0 and re.search('\.( |$)', node_content):
|
elif node_length < 80 and link_density == 0 and re.search(r'\.( |$)', node_content):
|
||||||
append = True
|
append = True
|
||||||
|
|
||||||
if append:
|
if append:
|
||||||
|
@ -14,7 +14,8 @@ from Queue import Queue, Empty
|
|||||||
from threading import Thread, Event
|
from threading import Thread, Event
|
||||||
from multiprocessing.pool import ThreadPool
|
from multiprocessing.pool import ThreadPool
|
||||||
|
|
||||||
import six
|
from polyglot.builtins import reraise
|
||||||
|
|
||||||
from PyQt5.Qt import (
|
from PyQt5.Qt import (
|
||||||
QImageReader, QFormLayout, QVBoxLayout, QSplitter, QGroupBox, QListWidget,
|
QImageReader, QFormLayout, QVBoxLayout, QSplitter, QGroupBox, QListWidget,
|
||||||
QLineEdit, QSpinBox, QTextEdit, QSize, QListWidgetItem, QIcon, QImage,
|
QLineEdit, QSpinBox, QTextEdit, QSize, QListWidgetItem, QIcon, QImage,
|
||||||
@ -369,7 +370,7 @@ def create_themeball(report, progress=None, abort=None):
|
|||||||
return
|
return
|
||||||
if errors:
|
if errors:
|
||||||
e = errors[0]
|
e = errors[0]
|
||||||
six.reraise(*e)
|
reraise(*e)
|
||||||
|
|
||||||
if progress is not None:
|
if progress is not None:
|
||||||
progress(next(num), _('Creating theme file'))
|
progress(next(num), _('Creating theme file'))
|
||||||
|
@ -11,7 +11,7 @@ import sys
|
|||||||
import time
|
import time
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
|
||||||
import six
|
from polyglot.builtins import reraise
|
||||||
from PyQt5.Qt import QEventLoop
|
from PyQt5.Qt import QEventLoop
|
||||||
|
|
||||||
from calibre import force_unicode
|
from calibre import force_unicode
|
||||||
@ -315,7 +315,7 @@ def linux_native_dialog(name):
|
|||||||
t.start()
|
t.start()
|
||||||
loop.exec_(QEventLoop.ExcludeUserInputEvents)
|
loop.exec_(QEventLoop.ExcludeUserInputEvents)
|
||||||
if ret[1] is not None:
|
if ret[1] is not None:
|
||||||
six.reraise(*ret[1])
|
reraise(*ret[1])
|
||||||
return ret[0]
|
return ret[0]
|
||||||
except Exception:
|
except Exception:
|
||||||
linux_native_dialog.native_failed = True
|
linux_native_dialog.native_failed = True
|
||||||
|
@ -14,7 +14,7 @@ from operator import itemgetter
|
|||||||
from functools import wraps
|
from functools import wraps
|
||||||
from future_builtins import map
|
from future_builtins import map
|
||||||
|
|
||||||
import six
|
from polyglot.builtins import reraise
|
||||||
|
|
||||||
from calibre import guess_type, force_unicode
|
from calibre import guess_type, force_unicode
|
||||||
from calibre.constants import __version__, plugins
|
from calibre.constants import __version__, plugins
|
||||||
@ -480,7 +480,7 @@ class HTTPConnection(HTTPRequest):
|
|||||||
if e.log:
|
if e.log:
|
||||||
self.log.warn(e.log)
|
self.log.warn(e.log)
|
||||||
return self.simple_response(e.http_code, msg=e.message or '', close_after_response=e.close_connection, extra_headers=eh)
|
return self.simple_response(e.http_code, msg=e.message or '', close_after_response=e.close_connection, extra_headers=eh)
|
||||||
six.reraise(etype, e, tb)
|
reraise(etype, e, tb)
|
||||||
|
|
||||||
data, output = result
|
data, output = result
|
||||||
output = self.finalize_output(output, data, self.method is HTTP1)
|
output = self.finalize_output(output, data, self.method is HTTP1)
|
||||||
|
@ -8,7 +8,7 @@ __copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
|
|||||||
|
|
||||||
import os, sys
|
import os, sys
|
||||||
|
|
||||||
import six
|
from polyglot.builtins import reraise
|
||||||
|
|
||||||
from calibre.constants import iswindows, plugins
|
from calibre.constants import iswindows, plugins
|
||||||
|
|
||||||
@ -50,6 +50,8 @@ class FlagConstants(object):
|
|||||||
for x in 'RANDOM SEQUENTIAL TEXT BINARY'.split():
|
for x in 'RANDOM SEQUENTIAL TEXT BINARY'.split():
|
||||||
x = 'O_' + x
|
x = 'O_' + x
|
||||||
setattr(self, x, getattr(os, x, 0))
|
setattr(self, x, getattr(os, x, 0))
|
||||||
|
|
||||||
|
|
||||||
fc = FlagConstants()
|
fc = FlagConstants()
|
||||||
|
|
||||||
|
|
||||||
@ -79,6 +81,7 @@ def flags_from_mode(mode):
|
|||||||
flags |= (fc.O_BINARY if binary else fc.O_TEXT)
|
flags |= (fc.O_BINARY if binary else fc.O_TEXT)
|
||||||
return flags
|
return flags
|
||||||
|
|
||||||
|
|
||||||
if iswindows:
|
if iswindows:
|
||||||
from numbers import Integral
|
from numbers import Integral
|
||||||
import msvcrt
|
import msvcrt
|
||||||
@ -122,7 +125,7 @@ if iswindows:
|
|||||||
}
|
}
|
||||||
|
|
||||||
def raise_winerror(pywinerr):
|
def raise_winerror(pywinerr):
|
||||||
six.reraise(
|
reraise(
|
||||||
WindowsError,
|
WindowsError,
|
||||||
WindowsError(pywinerr.winerror,
|
WindowsError(pywinerr.winerror,
|
||||||
(pywinerr.funcname or '') + b': ' + (pywinerr.strerror or '')),
|
(pywinerr.funcname or '') + b': ' + (pywinerr.strerror or '')),
|
||||||
@ -180,7 +183,7 @@ else:
|
|||||||
return speedup.fdopen(os.open(path, flags), path, mode, buffering)
|
return speedup.fdopen(os.open(path, flags), path, mode, buffering)
|
||||||
|
|
||||||
def raise_winerror(x):
|
def raise_winerror(x):
|
||||||
six.reraise(NotImplementedError, None, sys.exc_info()[2])
|
reraise(NotImplementedError, None, sys.exc_info()[2])
|
||||||
|
|
||||||
|
|
||||||
def find_tests():
|
def find_tests():
|
||||||
|
@ -12,8 +12,8 @@ __all__ = ['dukpy', 'Context', 'undefined', 'JSError', 'to_python']
|
|||||||
import errno, os, sys, numbers, hashlib, json
|
import errno, os, sys, numbers, hashlib, json
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
import six
|
|
||||||
import dukpy
|
import dukpy
|
||||||
|
from polyglot.builtins import reraise
|
||||||
|
|
||||||
from calibre.constants import iswindows
|
from calibre.constants import iswindows
|
||||||
from calibre.utils.filenames import atomic_rename
|
from calibre.utils.filenames import atomic_rename
|
||||||
@ -84,9 +84,11 @@ stream = '''
|
|||||||
module.exports = {};
|
module.exports = {};
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
def sha1sum(x):
|
def sha1sum(x):
|
||||||
return hashlib.sha1(x).hexdigest()
|
return hashlib.sha1(x).hexdigest()
|
||||||
|
|
||||||
|
|
||||||
def load_file(base_dirs, builtin_modules, name):
|
def load_file(base_dirs, builtin_modules, name):
|
||||||
try:
|
try:
|
||||||
ans = builtin_modules.get(name)
|
ans = builtin_modules.get(name)
|
||||||
@ -97,6 +99,7 @@ def load_file(base_dirs, builtin_modules, name):
|
|||||||
return [True, ans]
|
return [True, ans]
|
||||||
if not name.endswith('.js'):
|
if not name.endswith('.js'):
|
||||||
name += '.js'
|
name += '.js'
|
||||||
|
|
||||||
def do_open(*args):
|
def do_open(*args):
|
||||||
with open(os.path.join(*args), 'rb') as f:
|
with open(os.path.join(*args), 'rb') as f:
|
||||||
return [True, f.read().decode('utf-8')]
|
return [True, f.read().decode('utf-8')]
|
||||||
@ -111,6 +114,7 @@ def load_file(base_dirs, builtin_modules, name):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return [False, str(e)]
|
return [False, str(e)]
|
||||||
|
|
||||||
|
|
||||||
def readfile(path, enc='utf-8'):
|
def readfile(path, enc='utf-8'):
|
||||||
try:
|
try:
|
||||||
with open(path, 'rb') as f:
|
with open(path, 'rb') as f:
|
||||||
@ -120,6 +124,7 @@ def readfile(path, enc='utf-8'):
|
|||||||
except EnvironmentError as e:
|
except EnvironmentError as e:
|
||||||
return [None, errno.errorcode[e.errno], 'Failed to read from file: %s with error: %s' % (path, e.message or e)]
|
return [None, errno.errorcode[e.errno], 'Failed to read from file: %s with error: %s' % (path, e.message or e)]
|
||||||
|
|
||||||
|
|
||||||
def atomic_write(name, raw):
|
def atomic_write(name, raw):
|
||||||
bdir, bname = os.path.dirname(os.path.abspath(name)), os.path.basename(name)
|
bdir, bname = os.path.dirname(os.path.abspath(name)), os.path.basename(name)
|
||||||
tname = ('_' if iswindows else '.') + bname
|
tname = ('_' if iswindows else '.') + bname
|
||||||
@ -127,6 +132,7 @@ def atomic_write(name, raw):
|
|||||||
f.write(raw)
|
f.write(raw)
|
||||||
atomic_rename(f.name, name)
|
atomic_rename(f.name, name)
|
||||||
|
|
||||||
|
|
||||||
def writefile(path, data, enc='utf-8'):
|
def writefile(path, data, enc='utf-8'):
|
||||||
if enc == undefined:
|
if enc == undefined:
|
||||||
enc = 'utf-8'
|
enc = 'utf-8'
|
||||||
@ -140,6 +146,7 @@ def writefile(path, data, enc='utf-8'):
|
|||||||
return [errno.errorcode[e.errno], 'Failed to write to file: %s with error: %s' % (path, e.message or e)]
|
return [errno.errorcode[e.errno], 'Failed to write to file: %s with error: %s' % (path, e.message or e)]
|
||||||
return [None, None]
|
return [None, None]
|
||||||
|
|
||||||
|
|
||||||
class Function(object):
|
class Function(object):
|
||||||
|
|
||||||
def __init__(self, func):
|
def __init__(self, func):
|
||||||
@ -158,7 +165,8 @@ class Function(object):
|
|||||||
self.reraise(e)
|
self.reraise(e)
|
||||||
|
|
||||||
def reraise(self, e):
|
def reraise(self, e):
|
||||||
six.reraise(JSError, JSError(e), sys.exc_info()[2])
|
reraise(JSError, JSError(e), sys.exc_info()[2])
|
||||||
|
|
||||||
|
|
||||||
def to_python(x):
|
def to_python(x):
|
||||||
try:
|
try:
|
||||||
@ -179,6 +187,7 @@ def to_python(x):
|
|||||||
return Function(x)
|
return Function(x)
|
||||||
return x
|
return x
|
||||||
|
|
||||||
|
|
||||||
class JSError(Exception):
|
class JSError(Exception):
|
||||||
|
|
||||||
def __init__(self, ex):
|
def __init__(self, ex):
|
||||||
@ -214,8 +223,10 @@ class JSError(Exception):
|
|||||||
'stack': self.stack or undefined
|
'stack': self.stack or undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
contexts = {}
|
contexts = {}
|
||||||
|
|
||||||
|
|
||||||
def create_context(base_dirs, *args):
|
def create_context(base_dirs, *args):
|
||||||
data = to_python(args[0]) if args else {}
|
data = to_python(args[0]) if args else {}
|
||||||
ctx = Context(base_dirs=base_dirs)
|
ctx = Context(base_dirs=base_dirs)
|
||||||
@ -225,6 +236,7 @@ def create_context(base_dirs, *args):
|
|||||||
contexts[key] = ctx
|
contexts[key] = ctx
|
||||||
return key
|
return key
|
||||||
|
|
||||||
|
|
||||||
def run_in_context(code, ctx, options=None):
|
def run_in_context(code, ctx, options=None):
|
||||||
c = contexts[ctx]
|
c = contexts[ctx]
|
||||||
try:
|
try:
|
||||||
@ -237,6 +249,7 @@ def run_in_context(code, ctx, options=None):
|
|||||||
return [False, {'message':type('')(e)}]
|
return [False, {'message':type('')(e)}]
|
||||||
return [True, to_python(ans)]
|
return [True, to_python(ans)]
|
||||||
|
|
||||||
|
|
||||||
class Context(object):
|
class Context(object):
|
||||||
|
|
||||||
def __init__(self, base_dirs=(), builtin_modules=None):
|
def __init__(self, base_dirs=(), builtin_modules=None):
|
||||||
@ -333,7 +346,7 @@ class Context(object):
|
|||||||
'<init>')
|
'<init>')
|
||||||
|
|
||||||
def reraise(self, e):
|
def reraise(self, e):
|
||||||
six.reraise(JSError, JSError(e), sys.exc_info()[2])
|
reraise(JSError, JSError(e), sys.exc_info()[2])
|
||||||
|
|
||||||
def eval(self, code='', fname='<eval>', noreturn=False):
|
def eval(self, code='', fname='<eval>', noreturn=False):
|
||||||
try:
|
try:
|
||||||
@ -347,6 +360,7 @@ class Context(object):
|
|||||||
except dukpy.JSError as e:
|
except dukpy.JSError as e:
|
||||||
self.reraise(e)
|
self.reraise(e)
|
||||||
|
|
||||||
|
|
||||||
def test_build():
|
def test_build():
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
0
src/polyglot/__init__.py
Normal file
0
src/polyglot/__init__.py
Normal file
28
src/polyglot/builtins.py
Normal file
28
src/polyglot/builtins.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#!/usr/bin/env python2
|
||||||
|
# vim:fileencoding=utf-8
|
||||||
|
# License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
|
||||||
|
|
||||||
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
|
is_py3 = sys.version_info.major >= 3
|
||||||
|
|
||||||
|
if is_py3:
|
||||||
|
def reraise(tp, value, tb=None):
|
||||||
|
try:
|
||||||
|
if value is None:
|
||||||
|
value = tp()
|
||||||
|
if value.__traceback__ is not tb:
|
||||||
|
raise value.with_traceback(tb)
|
||||||
|
raise value
|
||||||
|
finally:
|
||||||
|
value = None
|
||||||
|
tb = None
|
||||||
|
else:
|
||||||
|
exec("""def reraise(tp, value, tb=None):
|
||||||
|
try:
|
||||||
|
raise tp, value, tb
|
||||||
|
finally:
|
||||||
|
tb = None
|
||||||
|
""")
|
Loading…
x
Reference in New Issue
Block a user