Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Recipes

Notices

Reply
 
Thread Tools Search this Thread
Old 09-08-2011, 05:21 PM   #1
trixiekindle
Junior Member
trixiekindle began at the beginning.
 
Posts: 1
Karma: 10
Join Date: Sep 2011
Device: kindle
Subclassing, instead of copying, built in recipes

Hi all,

I don't know if this of any use, my apologies if it's been covered elsewhere before.

It's always seemed a bit pesky to me that to change a built-in recipe you have to copy and edit it - then you have to keep an eye out for changes to the original and go through the whole copy/edit procedure again if you want to stay up to date. Really, I'd rather subclass the original recipe to implement changed behaviour.

I'm not a pythonista so I've just lived with it up to now, but today I made a bit of an effort to work it out.

I came up with a short script to load built-in recipes from the zip file into a python module. Apologies for my dog-python.

Code:
-----------------------------------------------------------------------------
RecipeLoader.py
-----------------------------------------------------------------------------
import sys
import imp
import zipfile

def LoadBuiltinRecipe(recipeName,moduleName):
    """ Load a built-in recipe into a python module
        recipeName is the filename of the recipe within builtin_recipes.zip
        moduleName is a unique name for the module
    """

    # presumably there's a way to get the path of this file without hardcoding it
    # this one works on a stock install on linux
    # on a stock windows install it's C:\Program Files\Calibre2\resources\builtin_recipes.zip
    
    with zipfile.ZipFile('/opt/calibre/resources/builtin_recipes.zip') as recipes:

        # I cribbed this, with considerable trimming and some modification, from:
        #       <http://movable-python.googlecode.com/svn/trunk/modules/zip_imp.py>
        
        zf=recipes.open(recipeName)
        mod = imp.new_module(moduleName)
        mod.__file__ = moduleName
        exec zf.read() in mod.__dict__
        sys.modules[moduleName] = mod
        return mod
-----------------------------------------------------------------------------
With that in place you can then subclass the built-in recipe in your own recipe files:

Code:
-----------------------------------------------------------------------------
import sys
sys.path.insert(0,'/the/directory/where/you/put/RecipeLoader.py')

from RecipeLoader import LoadBuiltinRecipe

# subclass the Instapaper recipe and clear the timefmt 
class MyRecipe(LoadBuiltinRecipe('instapaper.recipe','instapaper').AdvancedUserRecipe1299694372):
    timefmt =''
-----------------------------------------------------------------------------
This keeps things, in my mind, much tidier, if the original gets updated then I stay up to date and my recipe is nice and simple - only my customisations are in the file. It's not just initialisation code you can change of course, you can override any method from the original builtin recipe to change its behaviour.

Of course I may have missed a much simpler way to do this

Best wishes
TK
trixiekindle is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing built in recipes via CLI? jbmia Recipes 7 07-22-2011 02:25 PM
Here are my Recipes built for the Sony 650 mufc Recipes 2 01-29-2011 05:24 PM
Question re downloading built-in recipes Greggywocky Calibre 2 10-31-2010 04:01 PM
copying from bookshelf to PC lvd4 Fictionwise eBookwise 33 10-18-2010 06:51 PM
customizing built in recipes jrasmussen Calibre 2 02-15-2010 11:26 PM


All times are GMT -4. The time now is 01:56 PM.


MobileRead.com is a privately owned, operated and funded community.