12-14-2015, 09:38 AM | #1 |
Digital Amanuensis
Posts: 727
Karma: 1446357
Join Date: Dec 2011
Location: Turin, Italy
Device: Several eReaders and tablets
|
Suggestion: Plugin API exposing basic metadata
The plugin developer might want/need to access a couple of basic ebook metadata, like dc:identifier or dc:language.
At the moment, she must call bk.get_opf() and parse the resulting blob. Although the above requires half a dozen lines of code (if you know lxml and xpath), I am quite sure that this a common situation to the extent that a future version of the Plugin APIs might want to include a call, say get_meta(), to get, say, a Python dict() with the metadata from the current OPF. |
12-14-2015, 09:59 AM | #2 |
Banned
Posts: 272
Karma: 1224588
Join Date: Sep 2014
Device: Sony PRS 650
|
Thats easy: write a class offering high level methods and hide the basic stuff in protected methods. Publish it here so that everyone can access the data more easily.
|
12-14-2015, 10:30 AM | #3 |
Ex-Helpdesk Junkie
Posts: 19,421
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
|
Perhaps a sticky containing useful code fragments, like this calibre recipes sticky here: https://www.mobileread.com/forums/showthread.php?t=70263
The builtin plugin API should be stable and not change in every version just because someone thinks of a new interesting thing you can add to it. And this allows people to share complicated stuff as well, things that stand less chance of being built into Sigil. Also means people don't need to wait for a new version of Sigil in order to publish a plugin... |
12-14-2015, 10:37 AM | #4 |
Grand Sorcerer
Posts: 28,040
Karma: 199464182
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
The bookcontainer (Edit-type plugin) object--usually represented "bk" in a plugin--has get|setmetadataxml() methods that can retrieve and set the xml portion of the opf file that deals wirh metadata. There's no need to parse the entire opf to get/set metadata.
bk.getmetadataxml() parse metadata meta = rebuilt metadata xml fragment bk.setmetadataxml(meta) Last edited by DiapDealer; 12-14-2015 at 10:44 AM. |
12-14-2015, 10:42 AM | #5 |
Digital Amanuensis
Posts: 727
Karma: 1446357
Join Date: Dec 2011
Location: Turin, Italy
Device: Several eReaders and tablets
|
Alright, but you still need to parse the resulting XML string to extract the value.
Anyway, just my two cents. I am currently using: Code:
def get_meta(self, meta, default=""): value = default try: import lxml.etree as etree opf = self.bk.get_opf().encode("utf-8") root = etree.fromstring(opf) query = "//dc:%s" % (meta) namespaces = {"dc": "http://purl.org/dc/elements/1.1/"} dc_elem = root.xpath(query, namespaces=namespaces) if len(dc_elem) > 0: value = dc_elem[0].text except: pass return value |
12-14-2015, 10:51 AM | #6 | ||
Digital Amanuensis
Posts: 727
Karma: 1446357
Join Date: Dec 2011
Location: Turin, Italy
Device: Several eReaders and tablets
|
Quote:
Quote:
Anyway, if there is consensus my proposal is unwarranted, no problem, feel free to copy the code above (or modify it with bk.getmetadataxml()), in case you need to extract the value of a metadatum. |
||
12-14-2015, 11:28 AM | #7 |
Sigil Developer
Posts: 8,158
Karma: 5450818
Join Date: Nov 2009
Device: many
|
There is an example in the testme plugin that does a quick serial parse of the metadata using QuickParser. So no need to use lxml since it need not exist for Python 2.7 plugins. Calling it parses the metadata into text, tag, attribute and contents fields that are simple python data types. But yes you still need to parse it somehow.
The reason behind that is that epub3 metadata refines makes relationships among those entries and across to manifest entries as well. As any interface would need to identify and build up any refines done and map them to the primary entries. So you will need to parse all of the metadata just to get one entry and be sure of having all of its refines. Thanks for the code snippet. I may modify it and add it to the plugin developer thread to keep it with the gui pieces and things. |
12-14-2015, 11:32 AM | #8 |
Ex-Helpdesk Junkie
Posts: 19,421
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
|
I still think a sticky for sharing useful code is a good idea regardless.
And I am also concerned with keeping backwards compatibility. Sure, people *should* always upgrade to the latest version of Sigil, but I think plugins should still endeavor to work with any version of Sigil (that supports plugins). I am not necessarily knocking the idea of extending the plugin launcher. |
12-14-2015, 11:42 AM | #9 | |
Sigil Developer
Posts: 8,158
Karma: 5450818
Join Date: Nov 2009
Device: many
|
Quote:
https://www.mobileread.com/forums/sho...d.php?t=251452 It does have a few nice links at the front to useful code snippets and docs, but has become a more q&a thing. A new thread is probably needed. I will add it to my ever growing to-do list! KevinH |
|
12-14-2015, 12:32 PM | #10 | |
Ex-Helpdesk Junkie
Posts: 19,421
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
|
Quote:
But at least further questions can be addressed in the q&a thread, and code snippets could be posted to some sort of new "reusable code snippets" thread. |
|
12-14-2015, 12:42 PM | #11 |
Banned
Posts: 272
Karma: 1224588
Join Date: Sep 2014
Device: Sony PRS 650
|
Do you have lxml duplicated in each plugin? PIL? strings? No, you havent, you are importing them. So your argument isn't an argument.
|
12-14-2015, 03:05 PM | #12 | |
Ex-Helpdesk Junkie
Posts: 19,421
Karma: 85397180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
|
Quote:
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Regarding using metadata objects in identify method of metadata download plugin api | aprekates | Development | 1 | 07-06-2014 04:35 AM |
calibre's new plugin API | kovidgoyal | Plugins | 26 | 05-07-2011 03:43 PM |
Small Change to Metadata Source plugin API | kovidgoyal | Development | 2 | 04-25-2011 03:55 PM |
Metadata scraper plugin api | kiwidude | Development | 5 | 03-06-2011 12:58 PM |
Ubook plugin api | Dopedangel | Reading and Management | 0 | 08-25-2007 07:54 AM |