View Single Post
Old 04-22-2023, 12:42 AM   #10
Geremia
Addict
Geremia rocks like Gibraltar!Geremia rocks like Gibraltar!Geremia rocks like Gibraltar!Geremia rocks like Gibraltar!Geremia rocks like Gibraltar!Geremia rocks like Gibraltar!Geremia rocks like Gibraltar!Geremia rocks like Gibraltar!Geremia rocks like Gibraltar!Geremia rocks like Gibraltar!Geremia rocks like Gibraltar!
 
Posts: 258
Karma: 100000
Join Date: Oct 2012
Device: Calibre
How's this?
Quote:
If you need to subclass a conversion input plugin instead of a file type plugin, the process is similar, but there are a few key differences.
  1. Create a new folder in your Calibre plugins directory (usually located in the Calibre configuration folder) and name it something like “myinputplugin”.
  2. Inside the “myinputplugin” folder, create a new file named “__init__.py” (note the double underscores before and after “init”). This file will contain the code for your plugin.
  3. In the “__init__.py” file, you can import the conversion input plugin you want to subclass like this:
    Code:
    from calibre.customize.conversion import InputFormatPlugin
    from calibre_plugins.pdf_input import PDFInput
    In this example, we’re importing the “InputFormatPlugin” class from the “calibre.customize.conversion” module, as well as the built-in PDF input plugin from the “calibre_plugins.pdf_input” module.
  4. You can then define a new class that inherits from the input plugin you want to subclass, and override any methods you need to modify. For example, to modify the way the PDF input plugin handles metadata, you could define a new “get_metadata” method like this:
    Code:
    class MyPDFInput(PDFInput):
        def get_metadata(self, stream, filename=None, title=None, author=None, description=None):
            # Call the original get_metadata method from the PDFInput class
            metadata = super().get_metadata(stream, filename, title, author, description)
            
            # Modify the description field
            metadata['description'] = "My custom description"
            
            # Return the modified metadata
            return metadata
    Note that in this example, we’re creating a new class called “MyPDFInput” that inherits from the “PDFInput” class.
  5. Finally, you can register your plugin by creating a new file named “plugin.py” (again, in the “myinputplugin” folder) and adding the following code:
    Code:
    from calibre.customize import InputFormatPlugin
    
    class MyPDFPlugin(InputFormatPlugin):
        name = 'My PDF Plugin'
        description = 'My custom PDF plugin'
        supported_extensions = set(['pdf'])
        input_plugin = 'myinputplugin.MyPDFInput'
    
    # Register the plugin
    InputFormatPlugin.register_plugin('pdf', MyPDFPlugin)
    This code defines a new InputFormatPlugin that registers your custom PDFInput subclass under the name “My PDF Plugin”. The “supported_extensions” set specifies which file extensions your plugin can handle (in this case, just PDF files), and the “input_plugin” parameter specifies the name of your custom PDFInput subclass.
And that’s it! After restarting Calibre, your plugin should be available to use for any PDF files you convert in the conversion dialog.
Geremia is offline   Reply With Quote