Remove more uses of the plugins dict

This commit is contained in:
Kovid Goyal 2020-10-18 09:45:44 +05:30
parent 830b1a94e6
commit 8504ed08e8
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
9 changed files with 24 additions and 43 deletions

View File

@ -8,7 +8,7 @@ __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
import os, tempfile, shutil, errno, time, atexit
from threading import Thread
from calibre.constants import ismacos, plugins
from calibre.constants import ismacos
from calibre.ptempfile import remove_dir
from calibre.utils.filenames import remove_dir_if_empty
from calibre.utils.recycle_bin import delete_tree, delete_file
@ -35,7 +35,8 @@ class DeleteService(Thread):
Thread.__init__(self)
self.requests = Queue()
if ismacos:
plugins['cocoa'][0].enable_cocoa_multithreading()
from calibre_extensions.cocoa import enable_cocoa_multithreading
enable_cocoa_multithreading()
def shutdown(self, timeout=20):
self.requests.put(None)

View File

@ -18,7 +18,7 @@ from PyQt5.Qt import (
)
from calibre import as_unicode
from calibre.constants import isportable, iswindows, plugins
from calibre.constants import isportable, iswindows
from calibre.gui2 import (
choose_files, choose_save_file, config, error_dialog, gprefs, info_dialog,
open_url, warning_dialog
@ -44,6 +44,8 @@ except ImportError:
if iswindows and not isportable:
from calibre_extensions import winutil
def get_exe():
exe_base = os.path.abspath(os.path.dirname(sys.executable))
exe = os.path.join(exe_base, 'calibre.exe')
@ -52,7 +54,6 @@ if iswindows and not isportable:
return exe
def startup_shortcut_path():
winutil = plugins['winutil'][0]
startup_path = winutil.special_folder_path(winutil.CSIDL_STARTUP)
return os.path.join(startup_path, "calibre.lnk")
@ -63,12 +64,12 @@ if iswindows and not isportable:
for arg in args:
quoted_args.append('"{}"'.format(arg))
quoted_args = ' '.join(quoted_args)
plugins['winutil'][0].manage_shortcut(shortcut_path, target, description, quoted_args)
winutil.manage_shortcut(shortcut_path, target, description, quoted_args)
def shortcut_exists_at(shortcut_path, target):
if not os.access(shortcut_path, os.R_OK):
return False
name = plugins['winutil'][0].manage_shortcut(shortcut_path, None, None, None)
name = winutil.manage_shortcut(shortcut_path, None, None, None)
if name is None:
return False
return os.path.normcase(os.path.abspath(name)) == os.path.normcase(os.path.abspath(target))

View File

@ -19,8 +19,9 @@ from functools import partial
from calibre.ebooks.metadata import title_sort, author_to_author_sort
from calibre.utils.date import parse_date, isoformat, local_tz, UNDEFINED_DATE
from calibre import isbytestring, force_unicode
from calibre.constants import iswindows, DEBUG, plugins, plugins_loc
from calibre.constants import iswindows, DEBUG, plugins_loc
from calibre.utils.icu import sort_key
from calibre_extensions import speedup as _c_speedup
from calibre import prints
from polyglot.builtins import cmp, native_string_type, unicode_type
from polyglot import reprlib
@ -30,8 +31,6 @@ from dateutil.tz import tzoffset
global_lock = RLock()
_c_speedup = plugins['speedup'][0]
def _c_convert_timestamp(val):
if not val:

View File

@ -15,7 +15,7 @@ import sys
import time
from threading import Lock, Thread
from calibre.constants import islinux, ismacos, iswindows, plugins
from calibre.constants import islinux, ismacos, iswindows
from calibre.srv.http_response import create_http_handler
from calibre.srv.loop import ServerLoop
from calibre.srv.opts import Options
@ -102,6 +102,7 @@ if islinux:
elif iswindows:
from calibre.srv.utils import HandleInterrupt
from calibre_extensions import winutil
class TreeWatcher(Thread):
@ -111,7 +112,6 @@ elif iswindows:
self.path_to_watch = path_to_watch
def run(self):
winutil = plugins['winutil'][0]
dir_handle = winutil.create_file(
self.path_to_watch,
winutil.FILE_LIST_DIRECTORY,

View File

@ -19,7 +19,7 @@ from css_parser.css import CSSRule
from lxml.etree import Comment
from calibre import detect_ncpus, force_unicode, prepare_string_for_xml
from calibre.constants import iswindows, plugins
from calibre.constants import iswindows
from calibre.customize.ui import plugin_for_input_format
from calibre.ebooks import parse_css_length
from calibre.ebooks.css_transform_rules import StyleDeclaration
@ -52,11 +52,11 @@ from polyglot.binary import (
)
from polyglot.builtins import as_bytes, iteritems, map, unicode_type
from polyglot.urllib import quote, urlparse
from calibre_extensions import speedup
RENDER_VERSION = 1
BLANK_JPEG = b'\xff\xd8\xff\xdb\x00C\x00\x03\x02\x02\x02\x02\x02\x03\x02\x02\x02\x03\x03\x03\x03\x04\x06\x04\x04\x04\x04\x04\x08\x06\x06\x05\x06\t\x08\n\n\t\x08\t\t\n\x0c\x0f\x0c\n\x0b\x0e\x0b\t\t\r\x11\r\x0e\x0f\x10\x10\x11\x10\n\x0c\x12\x13\x12\x10\x13\x0f\x10\x10\x10\xff\xc9\x00\x0b\x08\x00\x01\x00\x01\x01\x01\x11\x00\xff\xcc\x00\x06\x00\x10\x10\x05\xff\xda\x00\x08\x01\x01\x00\x00?\x00\xd2\xcf \xff\xd9' # noqa
speedup = plugins['speedup'][0]
def XPath(expr):
@ -776,17 +776,11 @@ def ensure_body(root):
def html_as_json(root):
from calibre_extensions.html_as_json import serialize
ns, name = split_name(root.tag)
if ns not in (None, XHTML_NS):
raise ValueError('HTML tag must be in empty or XHTML namespace')
ensure_body(root)
pl, err = plugins['html_as_json']
if err:
raise SystemExit('Failed to load html_as_json plugin with error: {}'.format(err))
try:
serialize = pl.serialize
except AttributeError:
raise SystemExit('You are running calibre from source, you need to also update the main calibre installation to version >=4.3')
for child in tuple(root.iterchildren('*')):
if child.tag.partition('}')[-1] not in ('head', 'body'):
root.remove(child)

View File

@ -6,13 +6,13 @@
import os
import socket
import weakref
from calibre_extensions.speedup import utf8_decode, websocket_mask as fast_mask
from collections import deque
from hashlib import sha1
from struct import error as struct_error, pack, unpack_from
from threading import Lock
from calibre import as_unicode
from calibre.constants import plugins
from calibre.srv.http_response import HTTPConnection, create_http_handler
from calibre.srv.loop import (
RDWR, READ, WRITE, Connection, HandleInterrupt, ServerLoop
@ -20,16 +20,10 @@ from calibre.srv.loop import (
from calibre.srv.utils import DESIRED_SEND_BUFFER_SIZE
from calibre.utils.speedups import ReadOnlyFileBuffer
from polyglot import http_client
from polyglot.builtins import unicode_type
from polyglot.binary import as_base64_unicode
from polyglot.builtins import unicode_type
from polyglot.queue import Empty, Queue
speedup, err = plugins['speedup']
if not speedup:
raise RuntimeError('Failed to load speedup module with error: ' + err)
fast_mask, utf8_decode = speedup.websocket_mask, speedup.utf8_decode
del speedup, err
HANDSHAKE_STR = (
"HTTP/1.1 101 Switching Protocols\r\n"
"Upgrade: WebSocket\r\n"

View File

@ -6,12 +6,8 @@ __license__ = 'GPL v3'
__copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
import socket
from calibre.constants import plugins
from polyglot.builtins import unicode_type
certgen, err = plugins['certgen']
if err:
raise ImportError('Failed to load the certgen module with error: %s' % err)
from calibre_extensions import certgen
def create_key_pair(size=2048):

View File

@ -13,7 +13,7 @@ from contextlib import suppress, closing
from calibre import force_unicode, isbytestring, prints, sanitize_file_name
from calibre.constants import (
filesystem_encoding, iswindows, plugins, preferred_encoding, ismacos
filesystem_encoding, iswindows, preferred_encoding, ismacos
)
from calibre.utils.localization import get_udc
from polyglot.builtins import iteritems, itervalues, unicode_type, range
@ -222,7 +222,7 @@ def case_preserving_open_file(path, mode='wb', mkdir_mode=0o777):
def windows_get_fileid(path):
''' The fileid uniquely identifies actual file contents (it is the same for
all hardlinks to a file). Similar to inode number on linux. '''
get_file_id = plugins['winutil'][0].get_file_id
from calibre_extensions.winutil import get_file_id
if isbytestring(path):
path = path.decode(filesystem_encoding)
with suppress(OSError):
@ -456,8 +456,9 @@ def nlinks_file(path):
if iswindows:
from calibre_extensions.winutil import move_file
def rename_file(a, b):
move_file = plugins['winutil'][0].move_file
if isinstance(a, bytes):
a = os.fsdecode(a)
if isinstance(b, bytes):

View File

@ -7,7 +7,6 @@ import os
import regex
from calibre.constants import plugins
from calibre.utils.hyphenation.dictionaries import (
dictionary_name_for_locale, path_to_dictionary
)
@ -15,19 +14,14 @@ from polyglot.builtins import unicode_type
from polyglot.functools import lru_cache
REGEX_FLAGS = regex.VERSION1 | regex.WORD | regex.FULLCASE | regex.UNICODE
hyphen = None
@lru_cache()
def dictionary_for_locale(locale):
global hyphen
name = dictionary_name_for_locale(locale)
if name is not None:
from calibre_extensions import hyphen
path = path_to_dictionary(name)
if hyphen is None:
hyphen, hyphen_err = plugins['hyphen']
if hyphen_err:
raise RuntimeError('Failed to load the hyphen plugin with error: {}'.format(hyphen_err))
fd = os.open(path, getattr(os, 'O_BINARY', 0) | os.O_RDONLY)
return hyphen.load_dictionary(fd)
@ -41,6 +35,7 @@ def add_soft_hyphens(word, dictionary, hyphen_char='\u00ad'):
if len(q) < 4:
return word
lq = q.lower() # the hyphen library needs lowercase words to work
from calibre_extensions import hyphen
try:
ans = hyphen.simple_hyphenate(dictionary, lq)
except ValueError: