Add data in user_metadata dictionary
Hello all.
I'm writing plugin.
It should add to book record some data in user_metadata structure (defined in library metadata) on book adding.
Code is following:
============
import os
from calibre.customize import FileTypePlugin
class AddField(FileTypePlugin):
....name = 'Add field on start'
....description = ''
....supported_platforms = ['windows', 'osx', 'linux']
....author = ''
....version = (1, 0, 0)
....file_types = set(['pdf','djvu'])
....on_import = True
....minimum_calibre_version = (0, 7, 53)
....def run(self, path_to_ebook):
........from calibre.ebooks.metadata.meta import get_metadata, set_metadata
........file = open(path_to_ebook, 'r+b')
........ext = os.path.splitext(path_to_ebook)[-1][1:].lower()
........mi = get_metadata(file, ext)
........field_value = 'This is Field'
........print "[1] Field is:", mi.get_user_metadata("#field",False)
........field = {"#value#": field_value, 'datatype': 'text'}
........mi.set_user_metadata('#field', field)
........mi.title = "This is Title"
........set_metadata(file, mi, ext)
........mi = get_metadata(file, ext)
........print "[2] Field is:", mi.get_user_metadata("#field",False)
........return path_to_ebook
=============
In debug mode we have the following
=============
[PASS]
calibre 0.8.28
Windows-7-6.1.7600
Windows
('Windows', '7', '6.1.7600')
Python 2.7.1
Windows: ('7', '6.1.7600', '', 'Multiprocessor Free')
Starting up...
[PASS]
Started up in 6.05700016022 with 436 books
Worker Launch took: 1.30799984932
Job: 1 Read metadata (0 of 1) finished
Read metadata (0 of 1)
[1] Field is: None
[2] Field is: None
Added This is Title to db in: 1.71900010109 seconds
=============
1) "[1] Field is: None" is proper. But why the second is "None" too? How to make calibre to set field in desirable way?
2) Is it possible to add and to fill new fields in user_metadata, which not exist?
|