Commit 04997906 authored by Mark Canterbury's avatar Mark Canterbury
Browse files

Stashing changes

parent 46537170
Loading
Loading
Loading
Loading
Loading
+43 −38
Original line number Diff line number Diff line
@@ -2,31 +2,30 @@ import json
import logging
import os
from pathlib import Path
import sys

from xmlschema import *

from translate import *

import jsonschema

logging.basicConfig(level=logging.INFO)


def build_schema_locations(paths):
    schema_locations = []
    for schemaFile in paths:
        try:
            xs = XMLSchema(schemaFile, validation='skip')
            schema_locations.append((xs.target_namespace, str(Path(schemaFile).resolve())))
            xs = XMLSchema(schemaFile, validation="skip")
            schema_locations.append(
                (xs.target_namespace, str(Path(schemaFile).resolve()))
            )
            print(" [ {0}  ->  {1} ]".format(xs.target_namespace, schemaFile))
        except XMLSchemaParseError as ex:
            print(" [ {0} failed to parse:  {1} ]".format(schemaFile, ex))
    return schema_locations



if __name__ == "__main__":
    schema_paths = [ "103120/schema/ts_103120_Authorisation.xsd",
    schema_paths = [
        "103120/schema/ts_103120_Authorisation.xsd",
        "103120/schema/ts_103120_Common.xsd",
        "103120/schema/ts_103120_Core.xsd",
        "103120/schema/ts_103120_Delivery.xsd",
@@ -34,47 +33,53 @@ if __name__ == "__main__":
        "103120/schema/ts_103120_Notification.xsd",
        "103120/schema/ts_103120_Task.xsd",
        "103280/TS_103_280.xsd",
                    "testing/deps/xmldsig/xmldsig-core-schema.xsd"]
        "testing/deps/xmldsig/xmldsig-core-schema.xsd",
    ]

    schema_locations = build_schema_locations(schema_paths)

    ns_to_id_map = {
        schema[0]: "TS_103_120_" + schema[0].split('/')[-1].lower() + ".json" 
        for schema in schema_locations if '03120' in schema[0] 
        schema[0]: "TS_103_120_" + schema[0].split("/")[-1].lower() + ".json"
        for schema in schema_locations
        if "03120" in schema[0]
    } | {
        'http://uri.etsi.org/03280/common/2017/07' : 'etsi103280.json',
        'http://www.w3.org/2000/09/xmldsig#' : 'xmldsig.json',
        "http://uri.etsi.org/03280/common/2017/07": "etsi103280.json",
        "http://www.w3.org/2000/09/xmldsig#": "xmldsig.json",
    }

    output_path = Path('103120/json_schema')
    output_path = Path("103120/json_schema")
    if not output_path.exists():
        os.mkdir(str(output_path))

    json_schemas = {}
    for schema_tuple in schema_locations:
        if 'xmldsig' in (schema_tuple[1]):
        if "xmldsig" in (schema_tuple[1]):
            continue
        js = translate_schema(schema_tuple[1], ns_to_id_map, schema_locations)
        if ns_to_id_map[schema_tuple[0]] == 'core.json':
            js['$defs']['HI1Message']['properties'].pop('Signature')
        if ns_to_id_map[schema_tuple[0]] == "core.json":
            js["$defs"]["HI1Message"]["properties"].pop("Signature")
        js_path = output_path / ns_to_id_map[schema_tuple[0]]

        if "Core" in schema_tuple[1]:
            js["$defs"]['OneOfHI1Object'] = {
                'oneOf' : [
                    {'$ref' : 'TS_103_120_authorisation.json#/$defs/AuthorisationObject'},
                    {'$ref' : 'TS_103_120_task.json#/$defs/LITaskObject'},
                    {'$ref' : 'TS_103_120_task.json#/$defs/LDTaskObject'},
                    {'$ref' : 'TS_103_120_document.json#/$defs/DocumentObject'},
                    {'$ref' : 'TS_103_120_notification.json#/$defs/NotificationObject'},
            js["$defs"]["OneOfHI1Object"] = {
                "oneOf": [
                    {
                        "$ref": "TS_103_120_authorisation.json#/$defs/AuthorisationObject"
                    },
                    {"$ref": "TS_103_120_task.json#/$defs/LITaskObject"},
                    {"$ref": "TS_103_120_task.json#/$defs/LDTaskObject"},
                    {"$ref": "TS_103_120_document.json#/$defs/DocumentObject"},
                    {"$ref": "TS_103_120_notification.json#/$defs/NotificationObject"},
                ]
            }

        json_string = json.dumps(js, indent=2)

        if "Core" in schema_tuple[1]:
            json_string = json_string.replace('"$ref": "#/$defs/HI1Object"', '"$ref": "#/$defs/OneOfHI1Object"')
            json_string = json_string.replace(
                '"$ref": "#/$defs/HI1Object"', '"$ref": "#/$defs/OneOfHI1Object"'
            )

        if "TS_103_120" in str(js_path):
            with open(str(js_path), 'w') as f:
            with open(str(js_path), "w") as f:
                f.write(json_string)