|12-08-2012, 03:22 PM||#1|
Join Date: Oct 2012
Device: Onyx boox A60
Help with plugin dev - get_resources() works funny
First let me say I'm rather new developer in python so I may not know all the tricks and probably this is the reason I write this post.
Ok. So my problem is like that.
I'm trying to write some plugin for calibre that embeds font in the epub. (in attachment)
I know that:
1. there already are such plugins,
2. There is a feature in the calibre that does that, but nevertheless I wanted to experiment.
Basically what I wanted to do was to have font file inside the plugin apart of python code.
So my plugin (zip file) has content like that:
Now, my intent was to copy font file from the plugin into the epub and modify css file in the process.
I've found some plugin that did pretty much the same apart from fact that fonts were external to the plugin file.
I want them to be embedded into plugin because I use linux and windows interchangeably and those external paths
were always incomaptibile with one of the system. Embedding them will make no trouble for calibre to correctly know their location.
What is more, I do not want to install them system wide
I've read that I needed to access such resource from within the plugin. The correct way to do that was to use
Now the funny part begins.
According to the calibre documentation (http://manual.calibre-ebook.com/crea...lugin-zip-file)
I need to pass a resource_name or list_of_resource_names to the function and in return I should get a byte_object or dictionary_of_bytes_objects
Ok, I've created some debug version of plugin to see if this works:
from calibre.customize import FileTypePlugin class DebugPlugin(FileTypePlugin): name = 'Debug plugin' # Name of the plugin description = "Debug plugin" supported_platforms = [ 'windows', 'linux' ] # Platforms this plugin will run on author = 'Author' # The author of this plugin version = (1, 0, 2) # The version number of this plugin file_types = set(['epub']) on_postprocess = True priority = 100 def run(self, path_to_ebook): print( "Debug_plugin: run..., path to ebook (arg) '" + str( path_to_ebook ) + "'" ) fontFamilyName = "Font" fontRegularName = fontFamilyName + '.otf' res = get_resources( fontRegularName ) if res is None: print( "Debug_plugin: Could not get resource '" + fontRegularName + "'") else: print( "Debug_plugin: get_resources for '" + fontRegularName + "' returned: class: " + str( res.__class__ ) + ", name: " + str( res.__class__.__name__ ) + ", len:" + str( len( res ) ) ) return path_to_ebook
Debug_plugin: run..., path to ebook (arg) 'C:\TEMP\calibre_0.9.9_tmp_nbujxt\3aktb6.epub' Debug_plugin: get_resources for 'Font.otf' returned: class: <type 'str'>, name: str, len:48976
encoding page. I expected 'pure' bytes not string.
So i decided to get to know how this function works. I've downloaded Calibre source code from (sourceforge.net/projects/calibre/files/0.9.9/calibre-0.9.9.tar.xz) and I found that function
get_resources is defined in ...\calibre-0.9.9\calibre\src\calibre\customize\zipplugin.py
From what I can tell get_resources() really should return bytes object or dictionary (at least according to Python 2.7.3 ZipFile.read() documentation)
but, what surprised me, get_resources() accept two parameters , while in calibre manual it says that only one is needed.
I've checked that and indeed when I gave two arguments to the function:
get_resources( fontRegularName, 'foobar' )
TypeError: get_resources() takes exactly 2 arguments (3 given)
1. How is it possible that get_resources() accepts exactly 2 arguments, but I can only provide one (second), while first one is 'hidden' ? Could anyone could direct me to the site where such python feature is described.
( arguments are not default, function is not a class mentod, so it is not the case where first 'self' arg is passed implicitely)
2. Why does get_resources() return string object instead of bytes. Is there some decorator function involved ? Where is this in the calibre code ?
If someone could point me to it I would be very gratefull.
Please forgive me for long and spam-like post. I know I need to learn a lot about python but at least for now I don't know how to solve the issue.
Ok, I've looked into python documentation (2.7.4) and it says:
Last edited by GaryJonston; 12-08-2012 at 07:04 PM.
|12-08-2012, 11:48 PM||#2|
Creator of calibre
Join Date: Oct 2006
Location: Mumbai, India
In python 2.x a str is a bytes object. A string is a unicode object.
|12-09-2012, 05:23 AM||#3|
Join Date: Oct 2012
Device: Onyx boox A60
So this would probably mean I could use function ZipFile.writestr() to copy files from one zip file (plugin) to another (epub)
But if i try to do this in result I got a epub that cannot be opened. The zip file is 'invalid'. Not sure why it happenes.
I still don't know how the 'hidden argument' to a function thing is achieved in python.
|Thread Tools||Search this Thread|
|Thread||Thread Starter||Forum||Replies||Last Post|
|Modify ePub plugin dev thread||kiwidude||Development||346||09-02-2013 06:14 PM|
|Help with plugin dev.||Buzzy||Development||2||10-26-2012 04:22 PM|
|Einfacher Feature-Request: /dev/ptmx und /dev/ttyUSB2 modes||Seneca||PocketBook||2||12-05-2011 05:41 PM|
|Another plugin dev question||DiapDealer||Plugins||2||12-11-2010 02:46 PM|
|Epub plugin dev||DiapDealer||Plugins||15||11-12-2010 10:36 AM|