# -*- coding: utf-8 -*-

"""
This program receive a xml file name as input parameter.
The xml file is parsed to extract a binary pdf document named as the second parameter.
"""

import sys
import xml.etree.ElementTree as ET
import base64


inputfile = sys.argv[1]
outputfile = sys.argv[2]

try:
    itree = ET.parse(inputfile)
    root = itree.getroot()
    body = root.find('{http://schemas.xmlsoap.org/soap/envelope/}Body')
    output = body.find('{http://cooperazione.sian.it/schema/wsmrga/}MVVSiRPVOutput')
    pdf = output.find('{http://cooperazione.sian.it/schema/wsmrga/}filePdf')
    content = base64.b64decode(pdf.text.encode('ascii'))
    with open(outputfile, 'w') as ofile:
        ofile.write(content)
    sys.exit(0)

except IOError as e:
    print("%s %s" % (e.strerror, e.filename))
    sys.exit(1)


