Register Guidelines E-Books Search Today's Posts Mark Forums Read

Go Back   MobileRead Forums > E-Book Software > Sigil > Plugins

Notices

Reply
 
Thread Tools Search this Thread
Old 04-13-2020, 06:33 AM   #1
carmenchu
Groupie
carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.
 
Posts: 183
Karma: 266070
Join Date: Dec 2010
Device: Win7,Win10,Lubuntu,smartphone
Stumped, help!: bk.getmetadataxml() + BeautifulSoup

I am working on the attached plug-in (works, but only reports: under construction), to customize the metadata in the opf file.
Trouble is, when pass it to soup, as
Spoiler:
Code:
from sigil_bs4 import BeautifulSoup
from sigil_bs4.builder._lxml import LXMLTreeBuilderForXML # From xmlsouptest
.........
metadata = bk.getmetadataxml()
    ##### Uncomment either for the metada_soup to work
    # print(bk.getmetadataxml()) 
    # print(metadata)
    ############
    metadata_soup = BeautifulSoup(bk.getmetadataxml(), 'lxml') # 'lxml', 'xml'
    print('soup= ',metadata_soup, '\n')

what I get in the plug-in window is (1):
Spoiler:
Code:
Status: success
soup= 
fiction science fiction 52c38ef6-669b-488c-8307-71d9cc6d3ff5 1950-04-15T05:00:00+00:00 Stilson, Charles Billings en Polaris and the Goddess Glorian Distributed Proofreaders Canada 2020-04-06 Stilson, Charles Billings should be list Stilson, Charles Billings should be whole tag old author: dc:creator ['Stilson', ' Charles Billings'] authorSort Charles Billings Stilson authorName [( Charles Billings Stilson, ' Charles Billings Stilson'), ( Charles Billings Stilson, 'file:asStilson, Charles Billings')] Done

but uncommenting either of the commented-out print() instructions, as:
Spoiler:
Code:
metadata = bk.getmetadataxml()
    ##### Uncomment either for the metada_soup to work
    # print(bk.getmetadataxml()) 
    print(metadata)
    ############
    metadata_soup = BeautifulSoup(bk.getmetadataxml(), 'lxml') # 'lxml', 'xml'
    print('soup= ',metadata_soup, '\n')

gives instead (2):
Spoiler:
Code:
Status: success

<metadata xmlns:calibre="http://calibre.kovidgoyal.net/2009/metadata" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:opf="http://www.idpf.org/2007/opf" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<dc:subject>fiction</dc:subject>
<dc:subject>science fiction</dc:subject>
<dc:identifier id="uuid_id" opf:scheme="uuid">52c38ef6-669b-488c-8307-71d9cc6d3ff5</dc:identifier>
<dc:date>1950-04-15T05:00:00+00:00</dc:date>
<dc:creator opf:file-as="Unknown" opf:role="aut">Stilson, Charles Billings</dc:creator>
<dc:language>en</dc:language>
<meta name="cover" content="cover"/>
<meta name="calibre:timestamp" content="2019-04-03T00:35:53.586000+00:00"/>
<dc:title>Polaris and the Goddess Glorian</dc:title>
<dc:publisher>Distributed Proofreaders Canada</dc:publisher>
<meta name="Sigil version" content="1.2.0"/>
<dc:date xmlns:opf="http://www.idpf.org/2007/opf" opf:event="modification">2020-04-06</dc:date>
</metadata>

soup=  <html><body><metadata xmlns:calibre="http://calibre.kovidgoyal.net/2009/metadata" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:opf="http://www.idpf.org/2007/opf" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<dc:subject>fiction</dc:subject>
<dc:subject>science fiction</dc:subject>
<dc:identifier id="uuid_id" opf:scheme="uuid">52c38ef6-669b-488c-8307-71d9cc6d3ff5</dc:identifier>
<dc:date>1950-04-15T05:00:00+00:00</dc:date>
<dc:creator opf:file-as="Unknown" opf:role="aut">Stilson, Charles Billings</dc:creator>
<dc:language>en</dc:language>
<meta content="cover" name="cover"/>
<meta content="2019-04-03T00:35:53.586000+00:00" name="calibre:timestamp"/>
<dc:title>Polaris and the Goddess Glorian</dc:title>
<dc:publisher>Distributed Proofreaders Canada</dc:publisher>
<meta content="1.2.0" name="Sigil version"/>
<dc:date opf:event="modification" xmlns:opf="http://www.idpf.org/2007/opf">2020-04-06</dc:date>
</metadata>
</body></html> 

<dc:creator opf:file-as="Unknown" opf:role="aut">Stilson, Charles Billings</dc:creator> should be list

<dc:creator opf:file-as="Unknown" opf:role="aut">Stilson, Charles Billings</dc:creator> should be whole tag

old author:  dc:creator 

['Stilson', ' Charles Billings']  authorSort
 Charles Billings Stilson  authorName
[(<dc:creator opf:file-as="Stilson, Charles Billings" opf:role="aut"> Charles Billings Stilson</dc:creator>, ' Charles Billings Stilson'), (<dc:creator opf:file-as="Stilson, Charles Billings" opf:role="aut"> Charles Billings Stilson</dc:creator>, 'file:asStilson, Charles Billings')]

Done

- It drove me batty, to spot *when* the 'type (2)' report, with tags, would become 'type (1)'--not a single tag in sight--while I hadn't done anything that looked significant (comment/uncomment a print() instruction shouldn't be only cosmetic?)
- Please, am I missing something? Some format instruction to be added in situation (1)?
The internal works (navigation & assignments) seem right, but the soup doesn't look adequate for dropping back into the opf file...

Greatly appreciate any help: (stumped)**3!
Attached Files
File Type: zip CustomizeOPF_V0_0.zip (3.8 KB, 197 views)
carmenchu is offline   Reply With Quote
Old 04-13-2020, 09:29 AM   #2
DiapDealer
Grand Sorcerer
DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.
 
DiapDealer's Avatar
 
Posts: 27,463
Karma: 192992430
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
Pretty good discussion here on parsing/serializing the metadata fragment as pure xml with sigil_bs4.Beautiful soup.

And as mentioned in that discussion, /src/Resource_Files/python3lib/xmlprocessor.py in the Sigil source code (or Program Files\python3lib\xmlprocessor.py in a Windows Sigil installation) has examples on how to parse pure xml with bs4.
DiapDealer is offline   Reply With Quote
Advert
Old 04-13-2020, 09:51 AM   #3
DiapDealer
Grand Sorcerer
DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.
 
DiapDealer's Avatar
 
Posts: 27,463
Karma: 192992430
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
As to why you're getting different output to the plugin runner window with what appears to be cosmetic-only changes: debug-printing to the plugin runner window is tricky at best. If your plugin is working as expected; trust that. Otherwise if you need to use extensive debug prints, it's almost always best to find a way to test your code outside of Sigil.

All output/results of a Sigil plugin are delivered back to Sigil's plugin window as an xml file. That xml file is then parsed and displayed via the plugin runner window. Depending on when, in the process, output with tags is printed, that output can actually get rendered by Qt instead displayed in its "raw" state.

Last edited by DiapDealer; 04-13-2020 at 10:20 AM.
DiapDealer is offline   Reply With Quote
Old 04-13-2020, 12:31 PM   #4
carmenchu
Groupie
carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.
 
Posts: 183
Karma: 266070
Join Date: Dec 2010
Device: Win7,Win10,Lubuntu,smartphone
Quote:
Originally Posted by DiapDealer View Post
As to why you're getting different output to the plugin runner window with what appears to be cosmetic-only changes: debug-printing to the plugin runner window is tricky at best. If your plugin is working as expected; trust that. Otherwise if you need to use extensive debug prints, it's almost always best to find a way to test your code outside of Sigil.

All output/results of a Sigil plugin are delivered back to Sigil's plugin window as an xml file. That xml file is then parsed and displayed via the plugin runner window. Depending on when, in the process, output with tags is printed, that output can actually get rendered by Qt instead displayed in its "raw" state.
Thanks! As a matter of fact, after
Spoiler:
Code:
from sigil_bs4 import BeautifulSoup
# from sigil_bs4.builder._lxml import LXMLTreeBuilderForXML 
...
    new_metadata = metadata_soup.decodexml(indent_level=0, formatter='minimal', indent_chars="  ")
    print(new_metadata)
    
    bk.setmetadataxml(new_metadata)
    print('\nDone')

my code prints normal. Looks like BeautifulSoup must dump back all the structure into place for thinks to behave as expected--one learns.

BTW, can one define preferences as either
Code:
prefs['mine'] = {source1:'value1',source2:'value2',...}
prefs['mine'] = [('source1','value1'),('source2','value2'),...]
or must it be a single string/list to be processed after loading?
carmenchu is offline   Reply With Quote
Old 04-13-2020, 12:51 PM   #5
DiapDealer
Grand Sorcerer
DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.
 
DiapDealer's Avatar
 
Posts: 27,463
Karma: 192992430
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
You can use whatever you like for the preferences. Prefs are just a dictionary serialized to a json file. You can create your preferences as a dictionary of dictionaries, or as a dictionary of lists, or as a dictionary of lists of tuples if you like. It's just up to you to make sure the structure is properly populated before you save the preferences. Just like it's up to you parse the structure after retrieving the stored preferences.

I tend to use a dictionary of simple dictionaries, myself. Mostly for common grouping purposes. I've not really encountered a situation where complicating it much further than that would be an advantage. Your mileage may vary, though.
DiapDealer is offline   Reply With Quote
Advert
Old 04-13-2020, 12:58 PM   #6
carmenchu
Groupie
carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.
 
Posts: 183
Karma: 266070
Join Date: Dec 2010
Device: Win7,Win10,Lubuntu,smartphone
Thanks again!
carmenchu is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Stumped dustyp Library Management 1 04-03-2020 06:14 PM
BeautifulSoup on calibre thiago.eec Development 5 01-15-2019 08:24 AM
Private recipe repeatedly fails with BeautifulSoup find (calibre 3.6) hiperlink Recipes 6 08-06-2017 07:56 AM
I'm Stumped.... 93terp Amazon Kindle 19 08-04-2016 04:27 PM
Stumped Grauheim ePub 5 10-21-2009 12:32 PM


All times are GMT -4. The time now is 09:13 AM.


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