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
|
||||
from functools import partial
|
||||
|
||||
import six
|
||||
import apsw
|
||||
from polyglot.builtins import reraise
|
||||
|
||||
from calibre import isbytestring, force_unicode, prints, as_unicode
|
||||
from calibre.constants import (iswindows, filesystem_encoding,
|
||||
@ -1627,7 +1627,7 @@ class DB(object):
|
||||
except EnvironmentError as err:
|
||||
if err.errno == errno.EEXIST:
|
||||
# Parent directory already exists, re-raise original exception
|
||||
six.reraise(*exc_info)
|
||||
reraise(*exc_info)
|
||||
raise
|
||||
finally:
|
||||
del exc_info
|
||||
|
@ -6,7 +6,8 @@ from __future__ import (unicode_literals, division, absolute_import,
|
||||
import re, sys
|
||||
from collections import defaultdict
|
||||
|
||||
import six
|
||||
from polyglot.builtins import reraise
|
||||
|
||||
from lxml.etree import tostring
|
||||
from lxml.html import (fragment_fromstring, document_fromstring,
|
||||
tostring as htostring)
|
||||
@ -63,7 +64,7 @@ def to_int(x):
|
||||
|
||||
|
||||
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)
|
||||
return text.strip()
|
||||
|
||||
@ -157,7 +158,7 @@ class Document:
|
||||
return cleaned_article
|
||||
except StandardError as e:
|
||||
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):
|
||||
# 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:
|
||||
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
|
||||
|
||||
if append:
|
||||
|
@ -14,7 +14,8 @@ from Queue import Queue, Empty
|
||||
from threading import Thread, Event
|
||||
from multiprocessing.pool import ThreadPool
|
||||
|
||||
import six
|
||||
from polyglot.builtins import reraise
|
||||
|
||||
from PyQt5.Qt import (
|
||||
QImageReader, QFormLayout, QVBoxLayout, QSplitter, QGroupBox, QListWidget,
|
||||
QLineEdit, QSpinBox, QTextEdit, QSize, QListWidgetItem, QIcon, QImage,
|
||||
@ -369,7 +370,7 @@ def create_themeball(report, progress=None, abort=None):
|
||||
return
|
||||
if errors:
|
||||
e = errors[0]
|
||||
six.reraise(*e)
|
||||
reraise(*e)
|
||||
|
||||
if progress is not None:
|
||||
progress(next(num), _('Creating theme file'))
|
||||
|
@ -11,7 +11,7 @@ import sys
|
||||
import time
|
||||
from threading import Thread
|
||||
|
||||
import six
|
||||
from polyglot.builtins import reraise
|
||||
from PyQt5.Qt import QEventLoop
|
||||
|
||||
from calibre import force_unicode
|
||||
@ -315,7 +315,7 @@ def linux_native_dialog(name):
|
||||
t.start()
|
||||
loop.exec_(QEventLoop.ExcludeUserInputEvents)
|
||||
if ret[1] is not None:
|
||||
six.reraise(*ret[1])
|
||||
reraise(*ret[1])
|
||||
return ret[0]
|
||||
except Exception:
|
||||
linux_native_dialog.native_failed = True
|
||||
|
@ -14,7 +14,7 @@ from operator import itemgetter
|
||||
from functools import wraps
|
||||
from future_builtins import map
|
||||
|
||||
import six
|
||||
from polyglot.builtins import reraise
|
||||
|
||||
from calibre import guess_type, force_unicode
|
||||
from calibre.constants import __version__, plugins
|
||||
@ -480,7 +480,7 @@ class HTTPConnection(HTTPRequest):
|
||||
if 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)
|
||||
six.reraise(etype, e, tb)
|
||||
reraise(etype, e, tb)
|
||||
|
||||
data, output = result
|
||||
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 six
|
||||
from polyglot.builtins import reraise
|
||||
|
||||
from calibre.constants import iswindows, plugins
|
||||
|
||||
@ -50,6 +50,8 @@ class FlagConstants(object):
|
||||
for x in 'RANDOM SEQUENTIAL TEXT BINARY'.split():
|
||||
x = 'O_' + x
|
||||
setattr(self, x, getattr(os, x, 0))
|
||||
|
||||
|
||||
fc = FlagConstants()
|
||||
|
||||
|
||||
@ -79,6 +81,7 @@ def flags_from_mode(mode):
|
||||
flags |= (fc.O_BINARY if binary else fc.O_TEXT)
|
||||
return flags
|
||||
|
||||
|
||||
if iswindows:
|
||||
from numbers import Integral
|
||||
import msvcrt
|
||||
@ -122,7 +125,7 @@ if iswindows:
|
||||
}
|
||||
|
||||
def raise_winerror(pywinerr):
|
||||
six.reraise(
|
||||
reraise(
|
||||
WindowsError,
|
||||
WindowsError(pywinerr.winerror,
|
||||
(pywinerr.funcname or '') + b': ' + (pywinerr.strerror or '')),
|
||||
@ -180,7 +183,7 @@ else:
|
||||
return speedup.fdopen(os.open(path, flags), path, mode, buffering)
|
||||
|
||||
def raise_winerror(x):
|
||||
six.reraise(NotImplementedError, None, sys.exc_info()[2])
|
||||
reraise(NotImplementedError, None, sys.exc_info()[2])
|
||||
|
||||
|
||||
def find_tests():
|
||||
|
@ -12,8 +12,8 @@ __all__ = ['dukpy', 'Context', 'undefined', 'JSError', 'to_python']
|
||||
import errno, os, sys, numbers, hashlib, json
|
||||
from functools import partial
|
||||
|
||||
import six
|
||||
import dukpy
|
||||
from polyglot.builtins import reraise
|
||||
|
||||
from calibre.constants import iswindows
|
||||
from calibre.utils.filenames import atomic_rename
|
||||
@ -84,9 +84,11 @@ stream = '''
|
||||
module.exports = {};
|
||||
'''
|
||||
|
||||
|
||||
def sha1sum(x):
|
||||
return hashlib.sha1(x).hexdigest()
|
||||
|
||||
|
||||
def load_file(base_dirs, builtin_modules, name):
|
||||
try:
|
||||
ans = builtin_modules.get(name)
|
||||
@ -97,6 +99,7 @@ def load_file(base_dirs, builtin_modules, name):
|
||||
return [True, ans]
|
||||
if not name.endswith('.js'):
|
||||
name += '.js'
|
||||
|
||||
def do_open(*args):
|
||||
with open(os.path.join(*args), 'rb') as f:
|
||||
return [True, f.read().decode('utf-8')]
|
||||
@ -111,6 +114,7 @@ def load_file(base_dirs, builtin_modules, name):
|
||||
except Exception as e:
|
||||
return [False, str(e)]
|
||||
|
||||
|
||||
def readfile(path, enc='utf-8'):
|
||||
try:
|
||||
with open(path, 'rb') as f:
|
||||
@ -120,6 +124,7 @@ def readfile(path, enc='utf-8'):
|
||||
except EnvironmentError as 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):
|
||||
bdir, bname = os.path.dirname(os.path.abspath(name)), os.path.basename(name)
|
||||
tname = ('_' if iswindows else '.') + bname
|
||||
@ -127,6 +132,7 @@ def atomic_write(name, raw):
|
||||
f.write(raw)
|
||||
atomic_rename(f.name, name)
|
||||
|
||||
|
||||
def writefile(path, data, enc='utf-8'):
|
||||
if enc == undefined:
|
||||
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 [None, None]
|
||||
|
||||
|
||||
class Function(object):
|
||||
|
||||
def __init__(self, func):
|
||||
@ -158,7 +165,8 @@ class Function(object):
|
||||
self.reraise(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):
|
||||
try:
|
||||
@ -179,6 +187,7 @@ def to_python(x):
|
||||
return Function(x)
|
||||
return x
|
||||
|
||||
|
||||
class JSError(Exception):
|
||||
|
||||
def __init__(self, ex):
|
||||
@ -214,8 +223,10 @@ class JSError(Exception):
|
||||
'stack': self.stack or undefined
|
||||
}
|
||||
|
||||
|
||||
contexts = {}
|
||||
|
||||
|
||||
def create_context(base_dirs, *args):
|
||||
data = to_python(args[0]) if args else {}
|
||||
ctx = Context(base_dirs=base_dirs)
|
||||
@ -225,6 +236,7 @@ def create_context(base_dirs, *args):
|
||||
contexts[key] = ctx
|
||||
return key
|
||||
|
||||
|
||||
def run_in_context(code, ctx, options=None):
|
||||
c = contexts[ctx]
|
||||
try:
|
||||
@ -237,6 +249,7 @@ def run_in_context(code, ctx, options=None):
|
||||
return [False, {'message':type('')(e)}]
|
||||
return [True, to_python(ans)]
|
||||
|
||||
|
||||
class Context(object):
|
||||
|
||||
def __init__(self, base_dirs=(), builtin_modules=None):
|
||||
@ -333,7 +346,7 @@ class Context(object):
|
||||
'<init>')
|
||||
|
||||
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):
|
||||
try:
|
||||
@ -347,6 +360,7 @@ class Context(object):
|
||||
except dukpy.JSError as e:
|
||||
self.reraise(e)
|
||||
|
||||
|
||||
def test_build():
|
||||
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