mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-06-23 15:30:45 -04:00
Move the meta path finders in constants since it depends on winutil in particular
This commit is contained in:
parent
6690187e0a
commit
d57c4d369b
@ -138,9 +138,8 @@ def _get_cache_dir():
|
||||
return ans
|
||||
|
||||
if iswindows:
|
||||
w = plugins['winutil'][0]
|
||||
try:
|
||||
candidate = os.path.join(w.special_folder_path(w.CSIDL_LOCAL_APPDATA), '%s-cache'%__appname__)
|
||||
candidate = os.path.join(winutil.special_folder_path(winutil.CSIDL_LOCAL_APPDATA), '%s-cache'%__appname__)
|
||||
except ValueError:
|
||||
return confcache
|
||||
elif ismacos:
|
||||
@ -174,6 +173,125 @@ plugins_loc = sys.extensions_location
|
||||
|
||||
# plugins {{{
|
||||
|
||||
from importlib.machinery import ModuleSpec, EXTENSION_SUFFIXES, ExtensionFileLoader
|
||||
from importlib.util import find_spec
|
||||
from importlib import import_module
|
||||
|
||||
|
||||
class DeVendorLoader:
|
||||
|
||||
def __init__(self, aliased_name):
|
||||
self.aliased_module = import_module(aliased_name)
|
||||
try:
|
||||
self.path = self.aliased_module.__loader__.path
|
||||
except Exception:
|
||||
self.path = aliased_name
|
||||
|
||||
def create_module(self, spec):
|
||||
return self.aliased_module
|
||||
|
||||
def exec_module(self, module):
|
||||
return module
|
||||
|
||||
def __repr__(self):
|
||||
return repr(self.path)
|
||||
|
||||
|
||||
class DeVendor:
|
||||
|
||||
def find_spec(self, fullname, path=None, target=None):
|
||||
if fullname == 'calibre.web.feeds.feedparser':
|
||||
return find_spec('feedparser')
|
||||
if fullname.startswith('calibre.ebooks.markdown'):
|
||||
return ModuleSpec(fullname, DeVendorLoader(fullname[len('calibre.ebooks.'):]))
|
||||
|
||||
|
||||
class ExtensionsPackageLoader:
|
||||
|
||||
def __init__(self, calibre_extensions):
|
||||
self.calibre_extensions = calibre_extensions
|
||||
|
||||
def is_package(self, fullname=None):
|
||||
return True
|
||||
|
||||
def get_resource_reader(self, fullname=None):
|
||||
return self
|
||||
|
||||
def get_source(self, fullname=None):
|
||||
return ''
|
||||
|
||||
def contents(self):
|
||||
return iter(self.calibre_extensions)
|
||||
|
||||
def create_module(self, spec):
|
||||
pass
|
||||
|
||||
def exec_module(self, spec):
|
||||
pass
|
||||
|
||||
|
||||
class ExtensionsImporter:
|
||||
|
||||
def __init__(self):
|
||||
extensions = (
|
||||
'pictureflow',
|
||||
'lzx',
|
||||
'msdes',
|
||||
'podofo',
|
||||
'cPalmdoc',
|
||||
'progress_indicator',
|
||||
'icu',
|
||||
'speedup',
|
||||
'html_as_json',
|
||||
'unicode_names',
|
||||
'html_syntax_highlighter',
|
||||
'hyphen',
|
||||
'freetype',
|
||||
'imageops',
|
||||
'hunspell',
|
||||
'_patiencediff_c',
|
||||
'bzzdec',
|
||||
'matcher',
|
||||
'tokenizer',
|
||||
'certgen',
|
||||
)
|
||||
if iswindows:
|
||||
extra = ('winutil', 'wpd', 'winfonts')
|
||||
elif ismacos:
|
||||
extra = ('usbobserver', 'cocoa')
|
||||
elif isfreebsd or ishaiku or islinux or ismacos:
|
||||
extra = ('libusb', 'libmtp')
|
||||
else:
|
||||
extra = ()
|
||||
self.calibre_extensions = frozenset(extensions + extra)
|
||||
|
||||
def find_spec(self, fullname, path=None, target=None):
|
||||
if not fullname.startswith('calibre_extensions'):
|
||||
return
|
||||
parts = fullname.split('.')
|
||||
if parts[0] != 'calibre_extensions':
|
||||
return
|
||||
if len(parts) > 2:
|
||||
return
|
||||
is_package = len(parts) == 1
|
||||
extension_name = None if is_package else parts[1]
|
||||
path = os.path.join(plugins_loc, '__init__.py')
|
||||
if extension_name:
|
||||
if extension_name not in self.calibre_extensions:
|
||||
return
|
||||
for suffix in EXTENSION_SUFFIXES:
|
||||
path = os.path.join(plugins_loc, extension_name + suffix)
|
||||
if os.path.exists(path):
|
||||
break
|
||||
else:
|
||||
return
|
||||
return ModuleSpec(fullname, ExtensionFileLoader(fullname, path), is_package=is_package, origin=path)
|
||||
return ModuleSpec(fullname, ExtensionsPackageLoader(self.calibre_extensions), is_package=is_package, origin=path)
|
||||
|
||||
|
||||
sys.meta_path.insert(0, DeVendor())
|
||||
sys.meta_path.append(ExtensionsImporter())
|
||||
|
||||
|
||||
class Plugins(collections.Mapping):
|
||||
|
||||
@ -218,10 +336,9 @@ cconfd = getenv('CALIBRE_CONFIG_DIRECTORY')
|
||||
if cconfd is not None:
|
||||
config_dir = os.path.abspath(cconfd)
|
||||
elif iswindows:
|
||||
if plugins['winutil'][0] is None:
|
||||
raise Exception(plugins['winutil'][1])
|
||||
from calibre_extensions import winutil
|
||||
try:
|
||||
config_dir = plugins['winutil'][0].special_folder_path(plugins['winutil'][0].CSIDL_APPDATA)
|
||||
config_dir = winutil.special_folder_path(plugins['winutil'][0].CSIDL_APPDATA)
|
||||
except ValueError:
|
||||
config_dir = None
|
||||
if not config_dir or not os.access(config_dir, os.W_OK|os.X_OK):
|
||||
@ -293,25 +410,21 @@ def get_windows_username():
|
||||
Note that usernames on windows are case insensitive, the case of the value
|
||||
returned depends on what the user typed into the login box at login time.
|
||||
'''
|
||||
username = plugins['winutil'][0].username
|
||||
return username()
|
||||
return winutil.username()
|
||||
|
||||
|
||||
def get_windows_temp_path():
|
||||
temp_path = plugins['winutil'][0].temp_path
|
||||
return temp_path()
|
||||
return winutil.temp_path()
|
||||
|
||||
|
||||
def get_windows_user_locale_name():
|
||||
locale_name = plugins['winutil'][0].locale_name
|
||||
return locale_name()
|
||||
return winutil.locale_name()
|
||||
|
||||
|
||||
def get_windows_number_formats():
|
||||
ans = getattr(get_windows_number_formats, 'ans', None)
|
||||
if ans is None:
|
||||
localeconv = plugins['winutil'][0].localeconv
|
||||
d = localeconv()
|
||||
d = winutil.localeconv()
|
||||
thousands_sep, decimal_point = d['thousands_sep'], d['decimal_point']
|
||||
ans = get_windows_number_formats.ans = thousands_sep, decimal_point
|
||||
return ans
|
||||
|
@ -49,121 +49,6 @@ def get_debug_executable():
|
||||
|
||||
if not _run_once:
|
||||
_run_once = True
|
||||
from importlib.machinery import ModuleSpec, EXTENSION_SUFFIXES, ExtensionFileLoader
|
||||
from importlib.util import find_spec
|
||||
from importlib import import_module
|
||||
|
||||
class DeVendorLoader:
|
||||
|
||||
def __init__(self, aliased_name):
|
||||
self.aliased_module = import_module(aliased_name)
|
||||
try:
|
||||
self.path = self.aliased_module.__loader__.path
|
||||
except Exception:
|
||||
self.path = aliased_name
|
||||
|
||||
def create_module(self, spec):
|
||||
return self.aliased_module
|
||||
|
||||
def exec_module(self, module):
|
||||
return module
|
||||
|
||||
def __repr__(self):
|
||||
return repr(self.path)
|
||||
|
||||
class DeVendor:
|
||||
|
||||
def find_spec(self, fullname, path=None, target=None):
|
||||
if fullname == 'calibre.web.feeds.feedparser':
|
||||
return find_spec('feedparser')
|
||||
if fullname.startswith('calibre.ebooks.markdown'):
|
||||
return ModuleSpec(fullname, DeVendorLoader(fullname[len('calibre.ebooks.'):]))
|
||||
|
||||
sys.meta_path.insert(0, DeVendor())
|
||||
|
||||
class ExtensionsPackageLoader:
|
||||
|
||||
def __init__(self, calibre_extensions):
|
||||
self.calibre_extensions = calibre_extensions
|
||||
|
||||
def is_package(self, fullname=None):
|
||||
return True
|
||||
|
||||
def get_resource_reader(self, fullname=None):
|
||||
return self
|
||||
|
||||
def get_source(self, fullname=None):
|
||||
return ''
|
||||
|
||||
def contents(self):
|
||||
return iter(self.calibre_extensions)
|
||||
|
||||
def create_module(self, spec):
|
||||
pass
|
||||
|
||||
def exec_module(self, spec):
|
||||
pass
|
||||
|
||||
class ExtensionsImporter:
|
||||
|
||||
def __init__(self):
|
||||
extensions = (
|
||||
'pictureflow',
|
||||
'lzx',
|
||||
'msdes',
|
||||
'podofo',
|
||||
'cPalmdoc',
|
||||
'progress_indicator',
|
||||
'icu',
|
||||
'speedup',
|
||||
'html_as_json',
|
||||
'unicode_names',
|
||||
'html_syntax_highlighter',
|
||||
'hyphen',
|
||||
'freetype',
|
||||
'imageops',
|
||||
'hunspell',
|
||||
'_patiencediff_c',
|
||||
'bzzdec',
|
||||
'matcher',
|
||||
'tokenizer',
|
||||
'certgen',
|
||||
)
|
||||
if iswindows:
|
||||
extra = ('winutil', 'wpd', 'winfonts')
|
||||
elif ismacos:
|
||||
extra = ('usbobserver', 'cocoa')
|
||||
elif isfreebsd or ishaiku or islinux or ismacos:
|
||||
extra = ('libusb', 'libmtp')
|
||||
else:
|
||||
extra = ()
|
||||
self.calibre_extensions = frozenset(extensions + extra)
|
||||
|
||||
def find_spec(self, fullname, path=None, target=None):
|
||||
if not fullname.startswith('calibre_extensions'):
|
||||
return
|
||||
parts = fullname.split('.')
|
||||
if parts[0] != 'calibre_extensions':
|
||||
return
|
||||
if len(parts) > 2:
|
||||
return
|
||||
is_package = len(parts) == 1
|
||||
extension_name = None if is_package else parts[1]
|
||||
path = os.path.join(plugins_loc, '__init__.py')
|
||||
if extension_name:
|
||||
if extension_name not in self.calibre_extensions:
|
||||
return
|
||||
for suffix in EXTENSION_SUFFIXES:
|
||||
path = os.path.join(plugins_loc, extension_name + suffix)
|
||||
if os.path.exists(path):
|
||||
break
|
||||
else:
|
||||
return
|
||||
return ModuleSpec(fullname, ExtensionFileLoader(fullname, path), is_package=is_package, origin=path)
|
||||
return ModuleSpec(fullname, ExtensionsPackageLoader(self.calibre_extensions), is_package=is_package, origin=path)
|
||||
|
||||
sys.meta_path.append(ExtensionsImporter())
|
||||
|
||||
# Ensure that all temp files/dirs are created under a calibre tmp dir
|
||||
from calibre.ptempfile import base_dir
|
||||
try:
|
||||
|
Loading…
x
Reference in New Issue
Block a user