diff options
author | Antoine A <> | 2024-01-26 03:51:27 +0100 |
---|---|---|
committer | Antoine A <> | 2024-01-28 12:37:11 +0100 |
commit | f78ce3e453371fe74bc2cc3d6bcd1abd58aeed56 (patch) | |
tree | 2cd0f80029a19c91b2a86ea7e91675d7141f2056 /codegen/codegen.py | |
parent | 3975f316c5c1b2010a3dd0047225091f0919a660 (diff) | |
download | libeufin-f78ce3e453371fe74bc2cc3d6bcd1abd58aeed56.tar.gz libeufin-f78ce3e453371fe74bc2cc3d6bcd1abd58aeed56.tar.bz2 libeufin-f78ce3e453371fe74bc2cc3d6bcd1abd58aeed56.zip |
Generate ISO20022 types and parsercodegen
Diffstat (limited to 'codegen/codegen.py')
-rw-r--r-- | codegen/codegen.py | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/codegen/codegen.py b/codegen/codegen.py new file mode 100644 index 00000000..d8b2fe4b --- /dev/null +++ b/codegen/codegen.py @@ -0,0 +1,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)) |