View Single Post
Old 04-12-2009, 10:34 AM   #2
Junior Member
minstrel began at the beginning.
minstrel's Avatar
Posts: 3
Karma: 10
Join Date: Apr 2009
Device: Sony PRS-505
Originally Posted by minstrel View Post
7) Is there a way to debug Calibre plugins, like writing some debugging text to a console when the run() method is called?
A minor update: Reading up some more in an older thread I figured out I can launch "calibredb add [myfile.html]" from the command line, thus enabling nice print debug output from my python plugin.

Thus I nicely was able to verify that my plugin gets called from the importer, and the regular expression works. I even get to see the temporary file name. However, with nicely returning the tempfile's name I still get the original text in my ebook, not the substituted one.

What am I doing wrong?

import os, re
from calibre.customize import FileTypePlugin

class CleanupLitPlugin(FileTypePlugin):

  name                = 'Regular Expression plugin' # Name of the plugin
  description         = 'Apply Regular Expression to input'
  supported_platforms = ['windows', 'osx', 'linux'] # Platforms this plugin will run on
  author              = 'Markus Brenner' # The author of this plugin
  version             = (1, 0, 0)   # The version number of this plugin
  file_types          = set(['html', 'epub']) # The file types that this plugin will be applied to
  on_import           = True
  def run(self, path_to_ebook):
    file = open(path_to_ebook, 'r+b')
    outfile = self.temporary_file(".html")
    for line in file:
      output = re.sub(r'Hello',r'World',line)
      print output

C:\Users\mab\Documents\Calibre-test\HMTL>calibredb add test.html
Loading plugin from C:\Users\mab\AppData\Roaming\calibre\plugins\Regular Express


<p>World, good morning.</p>


Building file list...
        Parsing Calibre-test\HMTL\test.html
Open ebook created in c:\users\mab\appdata\local\temp\calibre_0.5.6_13qosd_create_oebzip
Output saved to c:\users\mab\appdata\local\temp\calibre_0.5.6_svedje_plugin_html
minstrel is offline   Reply With Quote