From 02ff98d8dcf3223f27f39186b2ccc21b238bf478 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 1 Apr 2019 14:58:41 +0530 Subject: [PATCH] py3: Port DeVendor --- src/calibre/startup.py | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/calibre/startup.py b/src/calibre/startup.py index a6911b9f29..02ef35deee 100644 --- a/src/calibre/startup.py +++ b/src/calibre/startup.py @@ -17,15 +17,16 @@ builtins.__dict__['_'] = lambda s: s # immediately translated to the environment language builtins.__dict__['__'] = lambda s: s -from calibre.constants import iswindows, preferred_encoding, plugins, isosx, islinux, isfrozen, DEBUG, isfreebsd +from calibre.constants import iswindows, preferred_encoding, plugins, isosx, islinux, isfrozen, DEBUG, isfreebsd, ispy3 _run_once = False winutil = winutilerror = None if not _run_once: _run_once = True + from importlib import import_module - if not isfrozen: + if not isfrozen and not ispy3: # Prevent PyQt4 from being loaded class PyQt4Ban(object): @@ -40,15 +41,28 @@ if not _run_once: class DeVendor(object): - def find_module(self, fullname, path=None): - if fullname == 'calibre.web.feeds.feedparser' or fullname.startswith('calibre.ebooks.markdown'): - return self + if ispy3: - def load_module(self, fullname): - from importlib import import_module - if fullname == 'calibre.web.feeds.feedparser': - return import_module('feedparser') - return import_module(fullname[len('calibre.ebooks.'):]) + def find_spec(self, fullname, path, target=None): + spec = None + if fullname == 'calibre.web.feeds.feedparser': + m = import_module('feedparser') + spec = m.__spec__ + elif fullname.startswith('calibre.ebooks.markdown'): + m = import_module(fullname[len('calibre.ebooks.'):]) + spec = m.__spec__ + return spec + + else: + + def find_module(self, fullname, path=None): + if fullname == 'calibre.web.feeds.feedparser' or fullname.startswith('calibre.ebooks.markdown'): + return self + + def load_module(self, fullname): + if fullname == 'calibre.web.feeds.feedparser': + return import_module('feedparser') + return import_module(fullname[len('calibre.ebooks.'):]) sys.meta_path.insert(0, DeVendor())