All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.regnosys.rosetta.generator.python.PythonCodeGenerator Maven / Gradle / Ivy

There is a newer version: 11.25.1
Show newest version
package com.regnosys.rosetta.generator.python;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.inject.Inject;
import com.regnosys.rosetta.generator.external.AbstractExternalGenerator;
import com.regnosys.rosetta.rosetta.RosettaEnumeration;
import com.regnosys.rosetta.rosetta.RosettaMetaType;
import com.regnosys.rosetta.rosetta.RosettaModel;
import com.regnosys.rosetta.rosetta.simple.Data;
import com.regnosys.rosetta.rosetta.simple.Function;
import com.regnosys.rosetta.generator.python.enums.PythonEnumGenerator;
import com.regnosys.rosetta.generator.python.object.PythonModelObjectGenerator;
import com.regnosys.rosetta.generator.python.func.PythonFunctionGenerator;
import com.regnosys.rosetta.generator.python.util.PythonModelGeneratorUtil;
import com.regnosys.rosetta.generator.python.util.Util;

public class PythonCodeGenerator extends AbstractExternalGenerator {
    private static final Logger LOGGER = LoggerFactory.getLogger(PythonCodeGenerator.class);

    @Inject PythonModelObjectGenerator pojoGenerator;
    @Inject PythonFunctionGenerator funcGenerator;
    @Inject PythonEnumGenerator enumGenerator;

    private List subfolders;
    private AtomicReference previousNamespace;
    private static String namespace;
    public PythonCodeGenerator() {
        super("Python");
    }

    @Override
    public Map beforeAllGenerate(ResourceSet set,
            Collection models, String version) {
        subfolders        = new ArrayList();
        previousNamespace = new AtomicReference<>("");
        namespace         = null;
        return Collections.emptyMap();
    }

    @Override
    public Map generate(Resource resource, 
                                                        RosettaModel model,
                                                        String version) {
        String cleanVersion = cleanVersion(version);

        Map result = new HashMap<>();

        List rosettaClasses = model.getElements().stream().filter(e -> e instanceof Data)
                .map(Data.class::cast).collect(Collectors.toList());

        List metaTypes =
                model.getElements().stream().filter(RosettaMetaType.class::isInstance)
                        .map(RosettaMetaType.class::cast).collect(Collectors.toList());

        List rosettaEnums =
                model.getElements().stream().filter(RosettaEnumeration.class::isInstance)
                        .map(RosettaEnumeration.class::cast).collect(Collectors.toList());

        List rosettaFunctions =
                model.getElements().stream().filter(t -> Function.class.isInstance(t))
                        .map(Function.class::cast).collect(Collectors.toList());

        if (rosettaClasses.size() > 0 ||
                metaTypes.size () > 0 ||
                rosettaEnums.size() > 0 ||
                rosettaFunctions.size() > 0) {
            if (!subfolders.contains(model.getName())) {
                subfolders.add(model.getName());
            }
            if (rosettaFunctions.size()> 0 && !subfolders.contains(model.getName() + ".functions")) {
                subfolders.add(model.getName() + ".functions");
            }
        }

        if (!model.getName().equals(previousNamespace.get())) {
            previousNamespace.set(model.getName());
            LOGGER.debug("processing module: {}", model.getName());
        }
        result.putAll(pojoGenerator.generate(rosettaClasses, metaTypes, cleanVersion));
        result.putAll(enumGenerator.generate(rosettaEnums, cleanVersion));
        result.putAll(funcGenerator.generate(rosettaFunctions, cleanVersion));

        return result;
    }

    private String cleanVersion(String version) {
        String cleanVersion = "0.0.0";
        if (version != null && !version.equals("${project.version}")) {
            String[] versionParts = version.split("\\.");
            if (versionParts.length > 2) {
                String thirdPart = versionParts[2].replaceAll("[^\\d]", "");
                cleanVersion = versionParts[0] + "." + versionParts[1] + "." + thirdPart;
            }
        }
        return cleanVersion;
    }

    @Override
    public Map afterAllGenerate(ResourceSet set,
                                                                Collection models, 
                                                                String version) {
        String cleanVersion = cleanVersion(version);
        Map result = new HashMap<>();

        List workspaces = getWorkspaces(subfolders);
        result.putAll(generateWorkspaces(workspaces, cleanVersion));
        result.putAll(generateInits(subfolders));
        if (namespace == null) {
            Iterator iterator = models.iterator();
            if (iterator.hasNext()) {
                namespace = Util.getNamespace(iterator.next());
            }
        }
        if (namespace != null) {
            result.put("pyproject.toml", PythonModelGeneratorUtil.createPYProjectTomlFile(namespace, cleanVersion));
        }
        return result;
    }

    private ArrayList getWorkspaces(List subfolders) {
        ArrayList firstElements = new ArrayList<>();

        for (String subfolder : subfolders) {
            String[] parts = subfolder.split("\\.");
            if (parts.length > 0) {
                if (!firstElements.contains(parts[0]))
                    firstElements.add(parts[0]);
            }
        }

        return firstElements;
    }

    private Map generateWorkspaces(List workspaces, String version) {
        Map result = new HashMap<>();

        for (String workspace : workspaces) {
            result.put(PythonModelGeneratorUtil.toPyFileName(workspace, "__init__"), PythonModelGeneratorUtil.createTopLevelInitFile(version));
            result.put(PythonModelGeneratorUtil.toPyFileName(workspace, "version"), PythonModelGeneratorUtil.createVersionFile(version));
            result.put(PythonModelGeneratorUtil.toFileName(workspace, "py.typed"), "");
        }

        return result;
    }

    public Map generateInits(List subfolders) {
        Map result = new HashMap<>();

        for (String subfolder : subfolders) {
            String[] parts = subfolder.split("\\.");
            for (int i = 1; i < parts.length; i++) {
                StringBuilder keyBuilder = new StringBuilder(parts[0]);
                for (int j = 1; j <= i; j++) {
                    keyBuilder.append(".").append(parts[j]);
                }
                String key = PythonModelGeneratorUtil.toPyFileName(keyBuilder.toString(), "__init__");
                result.putIfAbsent(key, " ");
            }
        }

        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy