Skip to content
Snippets Groups Projects
trans.py 1.23 KiB
from flask import Flask, render_template

import utils as utils
import os
from lxml import etree
from prettierfier import prettify_xml

config = utils.config

from pprint import pprint

def pretty_c14n(_xml):
    xml = _xml
    err = False
    try: 
        xml = etree.canonicalize(xml, with_comments=True, strip_text=True)
        # Bad side-effect of "prettify_xml": adds XML declaration again
        xml = prettify_xml(xml, indent=2, debug=False)
        xml = etree.canonicalize(xml, with_comments=True)
    except Exception as error:
        xml = str(error)
        err = True
        
    return err, xml

def cert_to_xml(cert_doc): 
    dcc_template = config['main_xml_template']
    ###pprint(config)
    version = utils.get_version()
    if not version:
        version = '???'   
    s = { \
      'name':[{'content':[config['name']],'lang':'en'}], \
      'release':version \
    }
    cert_doc['DCC']['administrativeData']['dccSoftware'].append(s)
    try: 
        xml = render_template(dcc_template, doc=cert_doc['DCC'], utils=utils)
        err, xml = pretty_c14n(xml)
        if err:
            xml = '<error>' + xml + '</error>\n' 
    except Exception as error:
        xml = '<error>' + str(error) + '</error>\n'
        
    return xml