Register Guidelines E-Books Today's Posts Search

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

Notices

Reply
 
Thread Tools Search this Thread
Old 05-05-2025, 09:57 PM   #1
moriabbey
Junior Member
moriabbey began at the beginning.
 
Posts: 1
Karma: 10
Join Date: May 2025
Device: iPad Mini
automatically changing metadata field

I'm looking to make a plugin that seems fairly simple, but I can't figure out how to go about it. I want it to, every time a specific custom metadata field is changed, change the "Title sort" metadata field to be identical to that custom metadata field. Reading and changing metadata fields seems straightforward enough from the API documentation, but I don't see how I would get it to trigger on a metadata field being changed. I have some very basic Python experience, enough to fumble my way through the API docs, but zero experience with calibre plugins or any kind of real development. Is something like this at all possible?
moriabbey is offline   Reply With Quote
Old 05-05-2025, 10:54 PM   #2
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 45,250
Karma: 27110894
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
You use the add_listener() function of the db api to add a callback that is called on various events including metadata change. But be careful not to do much work in the allback as this would tank performance and also dont change metadata in the callback itself as that would lead to infinite recursion. Instead use a different thread or if its a GUI plugin an qt signal to do your changes asynchronously.
kovidgoyal is online now   Reply With Quote
Advert
Old 05-06-2025, 12:17 PM   #3
thiago.eec
Wizard
thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.
 
Posts: 1,198
Karma: 1355861
Join Date: Dec 2016
Location: Goiânia - Brazil
Device: iPad, Kindle Paperwhite, Kindle Oasis
Quote:
Originally Posted by moriabbey View Post
I'm looking to make a plugin that seems fairly simple, but I can't figure out how to go about it. I want it to, every time a specific custom metadata field is changed, change the "Title sort" metadata field to be identical to that custom metadata field. Reading and changing metadata fields seems straightforward enough from the API documentation, but I don't see how I would get it to trigger on a metadata field being changed. I have some very basic Python experience, enough to fumble my way through the API docs, but zero experience with calibre plugins or any kind of real development. Is something like this at all possible?
Here is how I did in my GUI plugin (Reading Goal):

In the __init__ method of your main class, set up the listener:
Code:
self.gui.add_db_listener(self.metadata_changed_event)
Then, create the 'metadata_changed_event' method:
Code:
    def metadata_changed_event(self, db, event_type, event_data):    
        if event_type is EventType.metadata_changed:
            if event_data[0] == self.prefs['your_column_name']:
                self.ids = event_data[1]
        return
event_data[0] is the name of the column changed, so you compare it with your custom column. In my case, it was saved in the json prefs. But you could hardcode it:
Code:
 if event_data[0] == 'your_column_name':
Now, you can perform the operations you want, using 'self.ids'. This a list of ids that have their metadata changed. Like @Kovid said, this must be done outside of the ' metadata_changed_event' method.

Last edited by thiago.eec; 05-06-2025 at 02:47 PM.
thiago.eec is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatically load 'template' into Comments field? CloudReader Library Management 12 12-19-2023 08:37 PM
How do I stop calibre from changing metadata automatically? pkax Calibre 13 10-30-2022 11:13 AM
Calibre fills automatically title and author metadata in books without any metadata? thosedays Library Management 5 10-28-2016 03:33 PM
How to Stop Calibre from Automatically Changing OPF metadata Sneddles Conversion 10 07-19-2014 06:57 PM
Automatically populating metadata "tags" field from directory structure battaglia01 Calibre 1 12-13-2012 09:23 PM


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


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