summaryrefslogtreecommitdiff
path: root/codegen/codegen.py
blob: d8b2fe4b15cdf421dfb9286acd76f476c095477d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""Generate code to parse ISO 20022 messages"""

import xsd
import codeset

ktBase = """/*
 * This file is part of LibEuFin.
 * Copyright (C) 2024 Taler Systems S.A.

 * LibEuFin is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation; either version 3, or
 * (at your option) any later version.

 * LibEuFin is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
 * Public License for more details.

 * You should have received a copy of the GNU Affero General Public
 * License along with LibEuFin; see the file COPYING.  If not, see
 * <http://www.gnu.org/licenses/>
 */

// THIS FILE IS GENERATED, DO NOT EDIT

package tech.libeufin.ebics.iso20022

"""

codesets = codeset.extract()

schemas = ["pain.001.001.11", "pain.002.001.13", "camt.054.001.08", "camt.054.001.04"]
# Parse each schema
parsed = [xsd.parse(name) for name in schemas]

# Replace codeset with appropriate types
usedCodeset = {}
def replaceCodeset(ty: xsd.Type) -> xsd.Type:
    if isinstance(ty, xsd.Primitive):
        set = codesets.get(ty.name, None)
        if set is not None:
            usedCodeset[ty.name] = set
            return xsd.Enumeration(ty.name, None)
    return ty
parsed = [{name:replaceCodeset(ty) for name, ty in found.items()} for found in parsed]

# Find commons types and check similarly named types are equals
types = {}
common = {}
for found in parsed:
    for ty in found.values():
        prev = types.get(ty.name, None)
        if prev is not None:
            if prev == ty:
                common[ty.name] = ty
            else:
                print(f"Schema clash: {prev} != {ty}")
        else:
            types[ty.name] = ty


# Gen code for used code sets
with open("ebics/src/main/kotlin/iso20022/codesets.kt", "w") as f:
    f.write(ktBase + codeset.codegen(list(usedCodeset.values())))

# Keep only types unique to each schema
unique = [
    [value for name, value in types.items() if name not in common] for types in parsed
]
with open("ebics/src/main/kotlin/iso20022/common.kt", "w") as f:
    f.write(ktBase + xsd.codegen(list(common.values()), types))
for schema, inner in zip(schemas, unique):
    name = schema.replace(".", "_")
    with open(f"ebics/src/main/kotlin/iso20022/{name}.kt", "w") as f:
        f.write(ktBase + xsd.codegen(inner, types))