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