mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Allow file type plugins to specify that they should be run for all file types
This commit is contained in:
parent
8f4f573e50
commit
2bb5f7af79
@ -320,6 +320,7 @@ class FileTypePlugin(Plugin): # {{{
|
|||||||
'''
|
'''
|
||||||
|
|
||||||
#: Set of file types for which this plugin should be run.
|
#: Set of file types for which this plugin should be run.
|
||||||
|
#: Use '*' for all file types.
|
||||||
#: For example: ``{'lit', 'mobi', 'prc'}``
|
#: For example: ``{'lit', 'mobi', 'prc'}``
|
||||||
file_types = set()
|
file_types = set()
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
|||||||
|
|
||||||
import os, shutil, traceback, functools, sys
|
import os, shutil, traceback, functools, sys
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
from itertools import chain
|
||||||
|
|
||||||
from calibre.customize import (CatalogPlugin, FileTypePlugin, PluginNotFound,
|
from calibre.customize import (CatalogPlugin, FileTypePlugin, PluginNotFound,
|
||||||
MetadataReaderPlugin, MetadataWriterPlugin,
|
MetadataReaderPlugin, MetadataWriterPlugin,
|
||||||
@ -122,11 +123,7 @@ _on_postadd = []
|
|||||||
|
|
||||||
|
|
||||||
def reread_filetype_plugins():
|
def reread_filetype_plugins():
|
||||||
global _on_import
|
global _on_import, _on_postimport, _on_preprocess, _on_postprocess, _on_postadd
|
||||||
global _on_postimport
|
|
||||||
global _on_preprocess
|
|
||||||
global _on_postprocess
|
|
||||||
global _on_postadd
|
|
||||||
_on_import = defaultdict(list)
|
_on_import = defaultdict(list)
|
||||||
_on_postimport = defaultdict(list)
|
_on_postimport = defaultdict(list)
|
||||||
_on_preprocess = defaultdict(list)
|
_on_preprocess = defaultdict(list)
|
||||||
@ -147,16 +144,21 @@ def reread_filetype_plugins():
|
|||||||
_on_postprocess[ft].append(plugin)
|
_on_postprocess[ft].append(plugin)
|
||||||
|
|
||||||
|
|
||||||
|
def plugins_for_ft(ft, occasion):
|
||||||
|
op = {
|
||||||
|
'import':_on_import, 'preprocess':_on_preprocess, 'postprocess':_on_postprocess, 'postimport':_on_postimport,
|
||||||
|
}[occasion]
|
||||||
|
for p in chain(op.get(ft, ()), op.get('*', ())):
|
||||||
|
if not is_disabled(p):
|
||||||
|
yield p
|
||||||
|
|
||||||
|
|
||||||
def _run_filetype_plugins(path_to_file, ft=None, occasion='preprocess'):
|
def _run_filetype_plugins(path_to_file, ft=None, occasion='preprocess'):
|
||||||
occasion_plugins = {'import':_on_import, 'preprocess':_on_preprocess,
|
|
||||||
'postprocess':_on_postprocess}[occasion]
|
|
||||||
customization = config['plugin_customization']
|
customization = config['plugin_customization']
|
||||||
if ft is None:
|
if ft is None:
|
||||||
ft = os.path.splitext(path_to_file)[-1].lower().replace('.', '')
|
ft = os.path.splitext(path_to_file)[-1].lower().replace('.', '')
|
||||||
nfp = path_to_file
|
nfp = path_to_file
|
||||||
for plugin in occasion_plugins.get(ft, []):
|
for plugin in plugins_for_ft(ft, occasion):
|
||||||
if is_disabled(plugin):
|
|
||||||
continue
|
|
||||||
plugin.site_customization = customization.get(plugin.name, '')
|
plugin.site_customization = customization.get(plugin.name, '')
|
||||||
oo, oe = sys.stdout, sys.stderr # Some file type plugins out there override the output streams with buggy implementations
|
oo, oe = sys.stdout, sys.stderr # Some file type plugins out there override the output streams with buggy implementations
|
||||||
with plugin:
|
with plugin:
|
||||||
@ -177,20 +179,15 @@ def _run_filetype_plugins(path_to_file, ft=None, occasion='preprocess'):
|
|||||||
return nfp
|
return nfp
|
||||||
|
|
||||||
|
|
||||||
run_plugins_on_import = functools.partial(_run_filetype_plugins,
|
run_plugins_on_import = functools.partial(_run_filetype_plugins, occasion='import')
|
||||||
occasion='import')
|
run_plugins_on_preprocess = functools.partial(_run_filetype_plugins, occasion='preprocess')
|
||||||
run_plugins_on_preprocess = functools.partial(_run_filetype_plugins,
|
run_plugins_on_postprocess = functools.partial(_run_filetype_plugins, occasion='postprocess')
|
||||||
occasion='preprocess')
|
|
||||||
run_plugins_on_postprocess = functools.partial(_run_filetype_plugins,
|
|
||||||
occasion='postprocess')
|
|
||||||
|
|
||||||
|
|
||||||
def run_plugins_on_postimport(db, book_id, fmt):
|
def run_plugins_on_postimport(db, book_id, fmt):
|
||||||
customization = config['plugin_customization']
|
customization = config['plugin_customization']
|
||||||
fmt = fmt.lower()
|
fmt = fmt.lower()
|
||||||
for plugin in _on_postimport.get(fmt, []):
|
for plugin in plugins_for_ft(fmt, 'postimport'):
|
||||||
if is_disabled(plugin):
|
|
||||||
continue
|
|
||||||
plugin.site_customization = customization.get(plugin.name, '')
|
plugin.site_customization = customization.get(plugin.name, '')
|
||||||
with plugin:
|
with plugin:
|
||||||
try:
|
try:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user