View Single Post
Old 08-09-2019, 06:59 AM   #22
Vroni
Banned
Vroni knows the difference between 'who' and 'whom'Vroni knows the difference between 'who' and 'whom'Vroni knows the difference between 'who' and 'whom'Vroni knows the difference between 'who' and 'whom'Vroni knows the difference between 'who' and 'whom'Vroni knows the difference between 'who' and 'whom'Vroni knows the difference between 'who' and 'whom'Vroni knows the difference between 'who' and 'whom'Vroni knows the difference between 'who' and 'whom'Vroni knows the difference between 'who' and 'whom'Vroni knows the difference between 'who' and 'whom'
 
Posts: 168
Karma: 10010
Join Date: Oct 2018
Device: Tolino/PRS 650/Tablet
Well it might be the colon in dc:language which confuses your version diap, as this is not a tag but a tag with a namepace. Its just not found, thats why it adds a new one. Always.

This is my coding now:

Code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from sigil_bs4 import BeautifulSoup
from sigil_bs4.builder._lxml import LXMLTreeBuilderForXML


def run(bk):
#    xmlbuilder = LXMLTreeBuilderForXML(parser=None)
    metadata = bk.getmetadataxml()
    print('...')
    print(metadata)
#    metadata_soup = BeautifulSoup(metadata, features=None, from_encoding="utf-8", builder=xmlbuilder)
    metadata_soup = BeautifulSoup(bk.getmetadataxml(), 'lxml')
    print('...')
    print(metadata_soup)
    print('...')    
    dc_language = metadata_soup.find({"dc:language"})
    print(dc_language)
   
    if dc_language is None:
        print('...')  
        print('Creating new element')
        dc_language = metadata_soup.new_tag('dc:language')
        metadata_soup.metadata.append(dc_language)
    dc_language.string = 'en-US'
    new_metadata = metadata_soup.decodexml(indent_level=0, formatter='minimal', indent_chars="  ")[40:]
    print('...')
    print(new_metadata)
    
    bk.setmetadataxml(new_metadata)
    print('Done')
    return 0

def main():
    print('I reached main when I should not have\n')
    return -1

if __name__ == "__main__":
    sys.exit(main())
If i use doitsos version i get this one:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<html>
<body>
 <metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf"><dc:identifier id="BookId" opf:scheme="UUID">urn:uuid:7967fadc-d511-42ee-aad1-a472e662546a</dc:identifier>
 <dc:title>[Title here]</dc:title>
 <dc:language>en-US</dc:language></metadata>
</body>
</html>
Vroni is offline   Reply With Quote