Verify spoiler.xml against Cockatrice's XSD file (#105)

Verifies spoiler.xml against Cockatrice's XSD file

verify_xml currently takes an xml file and XSD as a string

Prints a pass/fail above XML dump

* Re-order xml writing to pass XSD

* Improved XSD verification

Now prints error

Now handles malformed XML or XSD
This commit is contained in:
tritoch 2017-07-06 21:37:11 -05:00 committed by GitHub
parent 3ee6aa3842
commit 04e6a1892f
2 changed files with 46 additions and 14 deletions

33
main.py
View File

@ -9,6 +9,8 @@ import json
import io
import sys
import verify_files
import requests
from lxml import etree
presets = {
"isfullspoil": False, # when full spoil comes around, we only want to use WOTC images
@ -85,6 +87,32 @@ def save_xml(xmlstring, outfile):
xmlfile.write(xmlstring.encode('utf-8'))
def verify_xml(file, schema):
try:
schema_doc = etree.fromstring(schema)
except Exception as e:
print "XSD for " + file + " is invalid"
print schema
print e
return False
xml_schema = etree.XMLSchema(schema_doc)
try:
xml_doc = etree.parse(file)
except Exception as e:
print "XML file " + file + " is invalid"
print e
return False
try:
xml_schema.assert_(xml_doc)
except:
xsd_errors = xml_schema.error_log
print "Errors validating XML file " + file + " against XSD:"
for error in xsd_errors:
print error
return False
return True
if __name__ == '__main__':
parseargs()
AllSets = spoilers.get_allsets() # get AllSets from mtgjson
@ -138,6 +166,11 @@ if __name__ == '__main__':
save_setjson(combinedjson, 'spoiler')
spoilers.write_combined_xml(combinedjson, setinfos)
save_xml(spoilers.pretty_xml('out/spoiler.xml'), 'out/spoiler.xml')
cockatrice_xsd = requests.get('https://raw.githubusercontent.com/Cockatrice/Cockatrice/master/doc/cards.xsd').text
if verify_xml('out/spoiler.xml', cockatrice_xsd): # check if our XML passes Cockatrice's XSD
print 'spoiler.xml passes Cockatrice XSD verification'
else:
print 'spoiler.xml fails Cockatrice XSD verification'
errorlog = spoilers.remove_corrected_errors(errorlog, card_corrections)
save_errorlog(errorlog)
save_allsets(AllSets)

View File

@ -578,9 +578,6 @@ def write_combined_xml(mtgjson, setinfos):
cardsxml.write("<name>" + name.encode('utf-8') + "</name>\n")
cardsxml.write(
'<set rarity="' + card['rarity'] + '" picURL="' + card["url"] + '">' + setcode + '</set>\n')
cardsxml.write(
"<manacost>" + manacost.encode('utf-8') + "</manacost>\n")
cardsxml.write("<cmc>" + cardcmc + "</cmc>\n")
if card.has_key('colors'):
colorTranslate = {
"White": "W",
@ -592,22 +589,24 @@ def write_combined_xml(mtgjson, setinfos):
for color in card['colors']:
cardsxml.write(
'<color>' + colorTranslate[color] + '</color>\n')
if name + ' enters the battlefield tapped' in text:
cardsxml.write("<cipt>1</cipt>\n")
cardsxml.write("<type>" + cardtype.encode('utf-8') + "</type>\n")
if pt:
cardsxml.write("<pt>" + pt + "</pt>\n")
if card.has_key('loyalty'):
cardsxml.write(
"<loyalty>" + str(card['loyalty']) + "</loyalty>\n")
cardsxml.write("<tablerow>" + tablerow + "</tablerow>\n")
cardsxml.write("<text>" + text.encode('utf-8') + "</text>\n")
if related:
# for relatedname in related:
cardsxml.write(
"<related>" + related.encode('utf-8') + "</related>\n")
related = ''
cardsxml.write(
"<manacost>" + manacost.encode('utf-8') + "</manacost>\n")
cardsxml.write("<cmc>" + cardcmc + "</cmc>\n")
cardsxml.write("<type>" + cardtype.encode('utf-8') + "</type>\n")
if pt:
cardsxml.write("<pt>" + pt + "</pt>\n")
cardsxml.write("<tablerow>" + tablerow + "</tablerow>\n")
cardsxml.write("<text>" + text.encode('utf-8') + "</text>\n")
if name + ' enters the battlefield tapped' in text:
cardsxml.write("<cipt>1</cipt>\n")
if card.has_key('loyalty'):
cardsxml.write(
"<loyalty>" + str(card['loyalty']) + "</loyalty>\n")
cardsxml.write("</card>\n")
cardsxml.write("</cards>\n</cockatrice_carddatabase>")