Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre

Notices

Reply
 
Thread Tools Search this Thread
Old 11-02-2010, 07:55 AM   #1
nsomlai
Junior Member
nsomlai began at the beginning.
 
Posts: 4
Karma: 10
Join Date: Nov 2010
Device: Kindle 3
Write custom column from Python

Hello,

Sorry for a noob question but I have one-day experience with Python.

I'm working on a metadata downloader plugin and I would like to store a source URL for the metadata. I have created a new database field called url (multiline, non-taggable text) and want my script to fill it in. This is what I tried:

mi = Metadata(title)
... fill in other stuff
mi.url = sourceurl

This doesn't give any error but also leaves the field empty.

Any help would be appreciated.
nsomlai is offline   Reply With Quote
Old 11-02-2010, 08:41 AM   #2
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,443
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
All custom attributes begin with '#'. which means that you cannot use attribute notation to set them. Use mi['#url'] = ..., where '#url' is the sort key, instead.
chaley is offline   Reply With Quote
Advert
Old 11-02-2010, 09:10 AM   #3
nsomlai
Junior Member
nsomlai began at the beginning.
 
Posts: 4
Karma: 10
Join Date: Nov 2010
Device: Kindle 3
Thanks for the tip, but I couldn't make it work.

coverurl = root.xpath(xpath_cover)
if len(coverurl) > 0:
mi['#molyurl'] = coverurl[0]

I'm getting this error:
'Metadata' object does not support item assignment.
nsomlai is offline   Reply With Quote
Old 11-02-2010, 09:15 AM   #4
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,443
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Sorry. Been a while since I messed with the code.

Try
mi.set(field key, field value)
e.g.
mi.set('#foo', 'this is what foo is')

If 'foo' is a series-type field, then you need to supply a third parameter that is the series index.

The python prototype is 'def set(self, field, val, extra=None):'
chaley is offline   Reply With Quote
Old 11-02-2010, 09:37 AM   #5
nsomlai
Junior Member
nsomlai began at the beginning.
 
Posts: 4
Karma: 10
Join Date: Nov 2010
Device: Kindle 3
Thanks, but still no joy. This did not cause any error, but doesn't fill the field either.

Looking at the code of metadata, my suspicion is that it only accepts fields it already knows about:
elif field in _data['user_metadata'].iterkeys():
... store the value

If I'm not wrong, this field is initialized with an empty map. I tried

mi = Metadata(title, authors=[], other={'#molyurl':'x'})

but this results in some other strange error, 'dict' object has no title or something similar.
nsomlai is offline   Reply With Quote
Advert
Old 11-02-2010, 10:27 AM   #6
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,443
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
A metadata instance will contain custom field information only if someone has put it there. Because of that, I think you are facing at least two problems.

1) If you are creating a Metadata instance yourself, then it will have no knowledge of custom fields. The only way I know to get a complete Metadata instance for a given db/custom column set is to use library.database2.get_metadata.

2) Assigning a metadata field in the mi structure has no effect on anything. Something later must take that data and put it into the database. I have no idea where that happens, or how it happens. That said, it is highly unlikely to work unless the code calls the set_metadata method in database2.

My guess is that the plugin doesn't know about the database (doesn't have an instance of an open db), in which case you will not be able to see custom field information. I am uninterested in plugins, so I can't give you any more information than that.

FWIW: the 'other' argument to the constructor is expected to be a Metadata instance, not some random dict.
chaley is offline   Reply With Quote
Old 11-02-2010, 10:44 AM   #7
nsomlai
Junior Member
nsomlai began at the beginning.
 
Posts: 4
Karma: 10
Join Date: Nov 2010
Device: Kindle 3
Thanks, I'll then just put it in the Comment field with some special mark.
nsomlai is offline   Reply With Quote
Old 11-02-2010, 11:12 AM   #8
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,443
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by nsomlai View Post
Thanks, I'll then just put it in the Comment field with some special mark.
If you do that, then you can later use search and replace to move the information to a custom field. For example, if you put the text in a comment as follows
Code:
###myurl###:the url text:###/myurl###
then the following regexp search/replace should do what you want:
source: comments
search for: ###myurl###.*?):###/myurl###
replace with:\1
Destination: #myurl

A similar one would remove the tagged text from comments.
chaley is offline   Reply With Quote
Reply

Tags
custom metadata access


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom column read ? pchrist7 Calibre 2 10-04-2010 02:52 AM
custom column i need a little help shinken Calibre 3 09-15-2010 03:41 AM
Custom Column Problem MSJim Calibre 7 09-08-2010 05:07 PM
custom column not searchable SkyDream Calibre 5 07-26-2010 05:38 AM
Calibre custom news feed and python help. harrynewman Calibre 4 10-08-2009 09:26 AM


All times are GMT -4. The time now is 11:14 AM.


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