Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

comicrack: fix generated xml #7

Merged
merged 3 commits into from
Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bdnex/conf/ComicInfo.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ComicInfo" nillable="true" type="ComicInfo" />
<xs:complexType name="ComicInfo">
<xs:sequence>
<xs:all>
<xs:element minOccurs="0" maxOccurs="1" default="" name="Title" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" default="" name="Series" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" default="" name="Number" type="xs:string" />
Expand Down Expand Up @@ -41,7 +41,7 @@
<xs:element minOccurs="0" maxOccurs="1" default="Unknown" name="AgeRating" type="AgeRating" />
<xs:element minOccurs="0" maxOccurs="1" name="Pages" type="ArrayOfComicPageInfo" />
<xs:element minOccurs="0" maxOccurs="1" name="CommunityRating" type="Rating" />
</xs:sequence>
</xs:all>
</xs:complexType>
<xs:simpleType name="YesNo">
<xs:restriction base="xs:string">
Expand Down
3 changes: 2 additions & 1 deletion bdnex/conf/bdgest_mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"Editeur": "Publisher",
"Format": "Format",
"Language": "LanguageISO",
"Scénario": "Letterer",
"Lettrage": "Letterer",
"Scénario": "Writer",
"Titre": "Title",
"Tome": "Volume",
"author": "Writer",
Expand Down
18 changes: 6 additions & 12 deletions bdnex/lib/comicrack.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import rarfile
import zipfile
import logging
import json
import xmlschema

from pkg_resources import resource_filename

Expand All @@ -22,20 +24,12 @@ def __init__(self, input_filename=None, comic_info=None):
def comicInfo_xml_create(self):
self.logger.info("Create ComicInfo.xml")

mytree = ET.parse(COMICINFO_TEMPLATE)
myroot = mytree.getroot()

for element in myroot[1][0]:
metadata_key = element.attrib['name']
if metadata_key in self.comic_info.keys():
if element.attrib["type"] == "xs:string":
element.attrib['default'] = str(self.comic_info[metadata_key])
elif element.attrib["type"] == "xs:int":
element.attrib['default'] = str(self.comic_info[metadata_key])

tmpdir = tempfile.mkdtemp()
comic_info_fp = os.path.join(tmpdir, 'ComicInfo.xml')
mytree.write(comic_info_fp, encoding='UTF-8', xml_declaration=True)

data = json.dumps(self.comic_info)
tmp_xml = xmlschema.from_json(data, preserve_root=True, schema=schema)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

schema is not defined, I think you meant to have something such as

with open(COMICINFO_TEMPLATE, 'r') as xsd_template:
     schema = xsd_template.read()

     tmp_xml = xmlschema.from_json(data, preserve_root=True, schema=schema)
     ET.ElementTree(tmp_xml).write(comic_info_fp, encoding='UTF-8', xml_declaration=True)

However, even with this, this code change is not working unfortunately. Currently, all the values stored in self.comic_info (coming from the json file) are set as strings and converted into str or integer in the deleted code above, note that L34 should have been int(self.comic_info[metadata_key]) instead of str(self.comic_info[metadata_key]).

So now with this code change, the data coming from the json file is expected to be of the right type but it now breaks against the xsd schema validation, see for example with the number of pages "120":

xmlschema.validators.exceptions.XMLSchemaEncodeError: failed validating '120' with XsdAtomicBuiltin(name='xs:int'):

Reason: '120' is not an instance of <class 'int'>

So maybe some refactoring could be done in https://github.com/lbesnard/bdnex/blob/main/bdnex/conf/bdgest_mapping.json to add the type of each key

such as:

{
    "Couleurs": {"value": "Colorist",
                        "type": "str"},
    "Couverture":  {"value": "CoverArtist",
                        "type": "str"},
    "Planches":  {"value": "PageCount",
                         "type": "int"}
}

and modify the code accordingly in https://github.com/lbesnard/bdnex/blob/main/bdnex/lib/bdgest.py#L428

thoughts?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just pushed a fix regarding the schema i've forgot... thx for catching it!!
Doesn't the pagecount integer problem haven't been fixed here:
#5 ?

ET.ElementTree(tmp_xml).write(comic_info_fp, encoding='UTF-8', xml_declaration=True)

return comic_info_fp

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def _read(fn):
'tenacity',
'opencv-contrib-python-headless',
'pandas',
'xmlschema',
],

extras_require={
Expand Down