mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-31 14:33:54 -04:00
Replace use of __import__ with the much nicer importlib. calibre now *requires* python 2.7
This commit is contained in:
parent
7ba4ddbeb6
commit
bbd493dee7
@ -5,7 +5,7 @@ __appname__ = 'calibre'
|
||||
__version__ = '0.7.50'
|
||||
__author__ = "Kovid Goyal <kovid@kovidgoyal.net>"
|
||||
|
||||
import re
|
||||
import re, importlib
|
||||
_ver = __version__.split('.')
|
||||
_ver = [int(re.search(r'(\d+)', x).group(1)) for x in _ver]
|
||||
numeric_version = tuple(_ver)
|
||||
@ -33,10 +33,10 @@ try:
|
||||
except:
|
||||
preferred_encoding = 'utf-8'
|
||||
|
||||
win32event = __import__('win32event') if iswindows else None
|
||||
winerror = __import__('winerror') if iswindows else None
|
||||
win32api = __import__('win32api') if iswindows else None
|
||||
fcntl = None if iswindows else __import__('fcntl')
|
||||
win32event = importlib.import_module('win32event') if iswindows else None
|
||||
winerror = importlib.import_module('winerror') if iswindows else None
|
||||
win32api = importlib.import_module('win32api') if iswindows else None
|
||||
fcntl = None if iswindows else importlib.import_module('fcntl')
|
||||
|
||||
filesystem_encoding = sys.getfilesystemencoding()
|
||||
if filesystem_encoding is None: filesystem_encoding = 'utf-8'
|
||||
@ -74,7 +74,7 @@ if plugins is None:
|
||||
(['winutil'] if iswindows else []) + \
|
||||
(['usbobserver'] if isosx else []):
|
||||
try:
|
||||
p, err = __import__(plugin), ''
|
||||
p, err = importlib.import_module(plugin), ''
|
||||
except Exception, err:
|
||||
p = None
|
||||
err = str(err)
|
||||
|
@ -2,7 +2,7 @@ from __future__ import with_statement
|
||||
__license__ = 'GPL v3'
|
||||
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||
|
||||
import os, sys, zipfile
|
||||
import os, sys, zipfile, importlib
|
||||
|
||||
from calibre.constants import numeric_version
|
||||
from calibre.ptempfile import PersistentTemporaryFile
|
||||
@ -517,7 +517,7 @@ class InterfaceActionBase(Plugin): # {{{
|
||||
This method must return the actual interface action plugin object.
|
||||
'''
|
||||
mod, cls = self.actual_plugin.split(':')
|
||||
return getattr(__import__(mod, fromlist=['1'], level=0), cls)(gui,
|
||||
return getattr(importlib.import_module(mod), cls)(gui,
|
||||
self.site_customization)
|
||||
|
||||
# }}}
|
||||
@ -575,7 +575,7 @@ class PreferencesPlugin(Plugin): # {{{
|
||||
base, _, wc = self.config_widget.partition(':')
|
||||
if not wc:
|
||||
wc = 'ConfigWidget'
|
||||
base = __import__(base, fromlist=[1])
|
||||
base = importlib.import_module(base)
|
||||
widget = getattr(base, wc)
|
||||
return widget(parent)
|
||||
|
||||
|
@ -124,7 +124,7 @@ class Device(DeviceConfig, DevicePlugin):
|
||||
if not prefix:
|
||||
return 0, 0
|
||||
prefix = prefix[:-1]
|
||||
win32file = __import__('win32file', globals(), locals(), [], -1)
|
||||
import win32file
|
||||
try:
|
||||
sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters = \
|
||||
win32file.GetDiskFreeSpace(prefix)
|
||||
|
@ -34,7 +34,7 @@ License: GPL 2 (http://www.gnu.org/copyleft/gpl.html) or BSD
|
||||
import re, sys, codecs
|
||||
|
||||
from logging import getLogger, StreamHandler, Formatter, \
|
||||
DEBUG, INFO, WARN, ERROR, CRITICAL
|
||||
DEBUG, INFO, WARN, CRITICAL
|
||||
|
||||
|
||||
MESSAGE_THRESHOLD = CRITICAL
|
||||
@ -242,8 +242,6 @@ class Element:
|
||||
|
||||
if bidi:
|
||||
|
||||
orig_bidi = self.bidi
|
||||
|
||||
if not self.bidi or self.isDocumentElement:
|
||||
# Once the bidi is set don't change it (except for doc element)
|
||||
self.bidi = bidi
|
||||
@ -775,7 +773,6 @@ class HtmlPattern (Pattern):
|
||||
|
||||
def handleMatch (self, m, doc):
|
||||
rawhtml = m.group(2)
|
||||
inline = True
|
||||
place_holder = self.stash.store(rawhtml)
|
||||
return doc.createTextNode(place_holder)
|
||||
|
||||
@ -1031,7 +1028,6 @@ class BlockGuru:
|
||||
remainder of the original list"""
|
||||
|
||||
items = []
|
||||
item = -1
|
||||
|
||||
i = 0 # to keep track of where we are
|
||||
|
||||
@ -1849,22 +1845,7 @@ For lower versions of Python use:
|
||||
""" % EXECUTABLE_NAME_FOR_USAGE
|
||||
|
||||
def parse_options():
|
||||
|
||||
try:
|
||||
optparse = __import__("optparse")
|
||||
except:
|
||||
if len(sys.argv) == 2:
|
||||
return {'input': sys.argv[1],
|
||||
'output': None,
|
||||
'message_threshold': CRITICAL,
|
||||
'safe': False,
|
||||
'extensions': [],
|
||||
'encoding': None }
|
||||
|
||||
else:
|
||||
print OPTPARSE_WARNING
|
||||
return None
|
||||
|
||||
import optparse
|
||||
parser = optparse.OptionParser(usage="%prog INPUTFILE [options]")
|
||||
|
||||
parser.add_option("-f", "--file", dest="filename",
|
||||
|
@ -6,7 +6,7 @@ __license__ = 'GPL v3'
|
||||
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||
__docformat__ = 'restructuredtext en'
|
||||
|
||||
import textwrap, codecs
|
||||
import textwrap, codecs, importlib
|
||||
from functools import partial
|
||||
|
||||
from PyQt4.Qt import QWidget, QSpinBox, QDoubleSpinBox, QLineEdit, QTextEdit, \
|
||||
@ -22,8 +22,8 @@ from calibre.customize.ui import plugin_for_input_format
|
||||
def config_widget_for_input_plugin(plugin):
|
||||
name = plugin.name.lower().replace(' ', '_')
|
||||
try:
|
||||
return __import__('calibre.gui2.convert.'+name,
|
||||
fromlist=[1]).PluginWidget
|
||||
return importlib.import_module(
|
||||
'calibre.gui2.convert.'+name).PluginWidget
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
@ -4,7 +4,7 @@ __license__ = 'GPL 3'
|
||||
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
|
||||
__docformat__ = 'restructuredtext en'
|
||||
|
||||
import shutil
|
||||
import shutil, importlib
|
||||
|
||||
from PyQt4.Qt import QString, SIGNAL
|
||||
|
||||
@ -82,8 +82,8 @@ class BulkConfig(Config):
|
||||
output_widget = None
|
||||
name = self.plumber.output_plugin.name.lower().replace(' ', '_')
|
||||
try:
|
||||
output_widget = __import__('calibre.gui2.convert.'+name,
|
||||
fromlist=[1])
|
||||
output_widget = importlib.import_module(
|
||||
'calibre.gui2.convert.'+name)
|
||||
pw = output_widget.PluginWidget
|
||||
pw.ICON = I('back.png')
|
||||
pw.HELP = _('Options specific to the output format.')
|
||||
|
@ -6,7 +6,7 @@ __license__ = 'GPL v3'
|
||||
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||
__docformat__ = 'restructuredtext en'
|
||||
|
||||
import sys, cPickle, shutil
|
||||
import sys, cPickle, shutil, importlib
|
||||
|
||||
from PyQt4.Qt import QString, SIGNAL, QAbstractListModel, Qt, QVariant, QFont
|
||||
|
||||
@ -182,8 +182,8 @@ class Config(ResizableDialog, Ui_Dialog):
|
||||
output_widget = None
|
||||
name = self.plumber.output_plugin.name.lower().replace(' ', '_')
|
||||
try:
|
||||
output_widget = __import__('calibre.gui2.convert.'+name,
|
||||
fromlist=[1])
|
||||
output_widget = importlib.import_module(
|
||||
'calibre.gui2.convert.'+name)
|
||||
pw = output_widget.PluginWidget
|
||||
pw.ICON = I('back.png')
|
||||
pw.HELP = _('Options specific to the output format.')
|
||||
@ -193,8 +193,8 @@ class Config(ResizableDialog, Ui_Dialog):
|
||||
input_widget = None
|
||||
name = self.plumber.input_plugin.name.lower().replace(' ', '_')
|
||||
try:
|
||||
input_widget = __import__('calibre.gui2.convert.'+name,
|
||||
fromlist=[1])
|
||||
input_widget = importlib.import_module(
|
||||
'calibre.gui2.convert.'+name)
|
||||
pw = input_widget.PluginWidget
|
||||
pw.ICON = I('forward.png')
|
||||
pw.HELP = _('Options specific to the input format.')
|
||||
|
@ -6,7 +6,7 @@ __license__ = 'GPL v3'
|
||||
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||
__docformat__ = 'restructuredtext en'
|
||||
|
||||
import os, sys
|
||||
import os, sys, importlib
|
||||
|
||||
from calibre.customize.ui import config
|
||||
from calibre.gui2.dialogs.catalog_ui import Ui_Dialog
|
||||
@ -43,8 +43,7 @@ class Catalog(ResizableDialog, Ui_Dialog):
|
||||
name = plugin.name.lower().replace(' ', '_')
|
||||
if type(plugin) in builtin_plugins:
|
||||
try:
|
||||
catalog_widget = __import__('calibre.gui2.catalog.'+name,
|
||||
fromlist=[1])
|
||||
catalog_widget = importlib.import_module('calibre.gui2.catalog.'+name)
|
||||
pw = catalog_widget.PluginWidget()
|
||||
pw.initialize(name, db)
|
||||
pw.ICON = I('forward.png')
|
||||
@ -75,7 +74,7 @@ class Catalog(ResizableDialog, Ui_Dialog):
|
||||
# Import the dynamic PluginWidget() from .py file provided in plugin.zip
|
||||
try:
|
||||
sys.path.insert(0, plugin.resources_path)
|
||||
catalog_widget = __import__(name, fromlist=[1])
|
||||
catalog_widget = importlib.import_module(name)
|
||||
pw = catalog_widget.PluginWidget()
|
||||
pw.initialize(name)
|
||||
pw.ICON = I('forward.png')
|
||||
|
@ -5,6 +5,8 @@ __license__ = 'GPL v3'
|
||||
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||
__docformat__ = 'restructuredtext en'
|
||||
|
||||
import importlib
|
||||
|
||||
from PyQt4.Qt import QIcon, Qt, QStringListModel, QVariant
|
||||
|
||||
from calibre.gui2.preferences import ConfigWidgetBase, test_widget, AbortCommit
|
||||
@ -104,8 +106,8 @@ class OutputOptions(Base):
|
||||
for plugin in output_format_plugins():
|
||||
name = plugin.name.lower().replace(' ', '_')
|
||||
try:
|
||||
output_widget = __import__('calibre.gui2.convert.'+name,
|
||||
fromlist=[1])
|
||||
output_widget = importlib.import_module(
|
||||
'calibre.gui2.convert.'+name)
|
||||
pw = output_widget.PluginWidget
|
||||
self.conversion_widgets.append(pw)
|
||||
except ImportError:
|
||||
|
@ -3,7 +3,7 @@ __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||
|
||||
''' Post installation script for linux '''
|
||||
|
||||
import sys, os, cPickle, textwrap, stat
|
||||
import sys, os, cPickle, textwrap, stat, importlib
|
||||
from subprocess import check_call
|
||||
|
||||
from calibre import __appname__, prints, guess_type
|
||||
@ -309,7 +309,7 @@ class PostInstall:
|
||||
for src in entry_points['console_scripts']:
|
||||
prog, right = src.split('=')
|
||||
prog = prog.strip()
|
||||
module = __import__(right.split(':')[0].strip(), fromlist=['a'])
|
||||
module = importlib.import_module(right.split(':')[0].strip())
|
||||
parser = getattr(module, 'option_parser', None)
|
||||
if parser is None:
|
||||
continue
|
||||
|
@ -6,7 +6,7 @@ __license__ = 'GPL v3'
|
||||
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||
__docformat__ = 'restructuredtext en'
|
||||
|
||||
import os, cPickle, sys
|
||||
import os, cPickle, sys, importlib
|
||||
from multiprocessing.connection import Client
|
||||
from threading import Thread
|
||||
from Queue import Queue
|
||||
@ -75,7 +75,7 @@ class Progress(Thread):
|
||||
|
||||
def get_func(name):
|
||||
module, func, notification = PARALLEL_FUNCS[name]
|
||||
module = __import__(module, fromlist=[1])
|
||||
module = importlib.import_module(module)
|
||||
func = getattr(module, func)
|
||||
return func, notification
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user