Make the original path to file available to FileType plugins

This commit is contained in:
Kovid Goyal 2016-12-05 07:45:25 +05:30
parent cc0d25c123
commit 6f99bc3cea
2 changed files with 16 additions and 2 deletions

View File

@ -349,7 +349,9 @@ class FileTypePlugin(Plugin): # {{{
modified ebook. If no modifications are needed, it should modified ebook. If no modifications are needed, it should
return the path to the original ebook. If an error is encountered return the path to the original ebook. If an error is encountered
it should raise an Exception. The default implementation it should raise an Exception. The default implementation
simply return the path to the original ebook. simply return the path to the original ebook. Note that the path to
the original file (before any file type plugins are run, is available as
self.original_path_to_file).
The modified ebook file should be created with the The modified ebook file should be created with the
:meth:`temporary_file` method. :meth:`temporary_file` method.
@ -813,4 +815,3 @@ class LibraryClosedPlugin(Plugin): # {{{
raise NotImplementedError('LibraryClosedPlugin ' raise NotImplementedError('LibraryClosedPlugin '
'run method must be overridden in subclass') 'run method must be overridden in subclass')
# }}} # }}}

View File

@ -39,6 +39,7 @@ def _config():
return ConfigProxy(c) return ConfigProxy(c)
config = _config() config = _config()
@ -97,6 +98,7 @@ def restore_plugin_state_to_default(plugin_or_name):
ep.remove(x) ep.remove(x)
config['enabled_plugins'] = ep config['enabled_plugins'] = ep
default_disabled_plugins = set([ default_disabled_plugins = set([
'Overdrive', 'Douban Books', 'OZON.ru', 'Edelweiss', 'Google Images', 'Big Book Search', 'Overdrive', 'Douban Books', 'OZON.ru', 'Edelweiss', 'Google Images', 'Big Book Search',
]) ])
@ -111,6 +113,7 @@ def is_disabled(plugin):
# File type plugins {{{ # File type plugins {{{
_on_import = {} _on_import = {}
_on_postimport = {} _on_postimport = {}
_on_preprocess = {} _on_preprocess = {}
@ -158,6 +161,7 @@ def _run_filetype_plugins(path_to_file, ft=None, occasion='preprocess'):
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:
try: try:
plugin.original_path_to_file = path_to_file
nfp = plugin.run(path_to_file) nfp = plugin.run(path_to_file)
if not nfp: if not nfp:
nfp = path_to_file nfp = path_to_file
@ -171,6 +175,7 @@ def _run_filetype_plugins(path_to_file, ft=None, occasion='preprocess'):
nfp = path_to_file nfp = path_to_file
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, run_plugins_on_preprocess = functools.partial(_run_filetype_plugins,
@ -317,6 +322,8 @@ def available_stores():
# }}} # }}}
# Metadata read/write {{{ # Metadata read/write {{{
_metadata_readers = {} _metadata_readers = {}
_metadata_writers = {} _metadata_writers = {}
@ -362,6 +369,7 @@ class QuickMetadata(object):
def __exit__(self, *args): def __exit__(self, *args):
self.quick = False self.quick = False
quick_metadata = QuickMetadata() quick_metadata = QuickMetadata()
@ -376,6 +384,7 @@ class ApplyNullMetadata(object):
def __exit__(self, *args): def __exit__(self, *args):
self.apply_null = False self.apply_null = False
apply_null_metadata = ApplyNullMetadata() apply_null_metadata = ApplyNullMetadata()
@ -390,6 +399,7 @@ class ForceIdentifiers(object):
def __exit__(self, *args): def __exit__(self, *args):
self.force_identifiers = False self.force_identifiers = False
force_identifiers = ForceIdentifiers() force_identifiers = ForceIdentifiers()
@ -629,6 +639,7 @@ def all_edit_book_tool_plugins():
# Initialize plugins {{{ # Initialize plugins {{{
_initialized_plugins = [] _initialized_plugins = []
@ -694,6 +705,7 @@ def initialize_plugins(perf=False):
reread_filetype_plugins() reread_filetype_plugins()
reread_metadata_plugins() reread_metadata_plugins()
initialize_plugins() initialize_plugins()
@ -799,6 +811,7 @@ def main(args=sys.argv):
return 0 return 0
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(main()) sys.exit(main())
# }}} # }}}