Search in sources :

Example 1 with PythonPackage

use of com.palantir.conjure.python.poet.PythonPackage in project conjure-python by palantir.

the class ClientGenerator method generateClient.

public PythonSnippet generateClient(ServiceDefinition serviceDef) {
    ImportTypeVisitor importTypeVisitor = new ImportTypeVisitor(serviceDef.getServiceName(), implTypeNameProcessor, implPackageNameProcessor);
    ImmutableSet.Builder<Type> referencedTypesBuilder = ImmutableSet.builder();
    List<PythonEndpointDefinition> endpoints = serviceDef.getEndpoints().stream().map(endpointDef -> generateEndpoint(endpointDef, referencedTypesBuilder)).collect(Collectors.toList());
    List<PythonImport> imports = referencedTypesBuilder.build().stream().flatMap(entry -> entry.accept(importTypeVisitor).stream()).collect(Collectors.toList());
    return PythonService.builder().pythonPackage(PythonPackage.of(implPackageNameProcessor.process(serviceDef.getServiceName().getPackage()))).className(implTypeNameProcessor.process(serviceDef.getServiceName())).definitionPackage(PythonPackage.of(definitionPackageNameProcessor.process(serviceDef.getServiceName().getPackage()))).definitionName(definitionTypeNameProcessor.process(serviceDef.getServiceName())).addAllImports(PythonService.CONJURE_IMPORTS).addAllImports(imports).docs(serviceDef.getDocs()).addAllEndpointDefinitions(endpoints).build();
}
Also used : ImportTypeVisitor(com.palantir.conjure.python.types.ImportTypeVisitor) PythonPackage(com.palantir.conjure.python.poet.PythonPackage) ImmutableSet(com.google.common.collect.ImmutableSet) TypeVisitor(com.palantir.conjure.visitor.TypeVisitor) PythonSnippet(com.palantir.conjure.python.poet.PythonSnippet) Type(com.palantir.conjure.spec.Type) ImportTypeVisitor(com.palantir.conjure.python.types.ImportTypeVisitor) PythonEndpointDefinition(com.palantir.conjure.python.poet.PythonEndpointDefinition) PythonEndpointParam(com.palantir.conjure.python.poet.PythonEndpointDefinition.PythonEndpointParam) ServiceDefinition(com.palantir.conjure.spec.ServiceDefinition) Collectors(java.util.stream.Collectors) ParameterTypeVisitor(com.palantir.conjure.visitor.ParameterTypeVisitor) PythonService(com.palantir.conjure.python.poet.PythonService) List(java.util.List) PythonTypeNameVisitor(com.palantir.conjure.python.types.PythonTypeNameVisitor) MyPyTypeNameVisitor(com.palantir.conjure.python.types.MyPyTypeNameVisitor) PythonImport(com.palantir.conjure.python.poet.PythonImport) PackageNameProcessor(com.palantir.conjure.python.processors.packagename.PackageNameProcessor) CaseConverter(com.palantir.conjure.CaseConverter) DealiasingTypeVisitor(com.palantir.conjure.visitor.DealiasingTypeVisitor) TypeNameProcessor(com.palantir.conjure.python.processors.typename.TypeNameProcessor) PrimitiveType(com.palantir.conjure.spec.PrimitiveType) EndpointDefinition(com.palantir.conjure.spec.EndpointDefinition) Type(com.palantir.conjure.spec.Type) PrimitiveType(com.palantir.conjure.spec.PrimitiveType) ImmutableSet(com.google.common.collect.ImmutableSet) PythonImport(com.palantir.conjure.python.poet.PythonImport) PythonEndpointDefinition(com.palantir.conjure.python.poet.PythonEndpointDefinition)

Example 2 with PythonPackage

use of com.palantir.conjure.python.poet.PythonPackage in project conjure-python by palantir.

the class ConjurePythonGenerator method generate.

private List<PythonFile> generate(ConjureDefinition conjureDefinition) {
    DealiasingTypeVisitor dealiasingTypeVisitor = new DealiasingTypeVisitor(conjureDefinition.getTypes().stream().collect(Collectors.toMap(type -> type.accept(TypeDefinitionVisitor.TYPE_NAME), Function.identity())));
    PackageNameProcessor implPackageNameProcessor = new ConstantPackageNameProcessor(config.pythonicPackageName().orElse(""));
    List<PackageNameProcessor> packageNameProcessors = new ArrayList<>();
    packageNameProcessors.add(new TwoComponentStrippingPackageNameProcessor());
    packageNameProcessors.add(FlatteningPackageNameProcessor.INSTANCE);
    packageNameProcessors.addAll(config.pythonicPackageName().map(pythonPackageName -> ImmutableSet.of(new PrefixingPackageNameProcessor(pythonPackageName))).orElseGet(ImmutableSet::of));
    PackageNameProcessor definitionPackageNameProcessor = new CompoundPackageNameProcessor(packageNameProcessors);
    TypeNameProcessor implTypeNameProcessor = new PackagePrependingTypeNameProcessor(new CompoundPackageNameProcessor(ImmutableList.of(new TwoComponentStrippingPackageNameProcessor(), FlatteningPackageNameProcessor.INSTANCE)));
    TypeNameProcessor definitionTypeNameProcessor = NameOnlyTypeNameProcessor.INSTANCE;
    List<PythonFile> pythonFiles = new ArrayList<>();
    pythonFiles.add(getImplPythonFile(conjureDefinition, dealiasingTypeVisitor, implPackageNameProcessor, implTypeNameProcessor, definitionPackageNameProcessor, definitionTypeNameProcessor));
    List<PythonFile> initFiles = getInitFiles(conjureDefinition, implTypeNameProcessor, definitionPackageNameProcessor, definitionTypeNameProcessor);
    pythonFiles.addAll(initFiles);
    PythonPackage rootPackage = PythonPackage.of(buildPackageNameProcessor().process("."));
    pythonFiles.add(getRootInit(initFiles.stream().map(PythonFile::pythonPackage).collect(Collectors.toSet()), rootPackage));
    return pythonFiles;
}
Also used : ConstantPackageNameProcessor(com.palantir.conjure.python.processors.packagename.ConstantPackageNameProcessor) FlatteningPackageNameProcessor(com.palantir.conjure.python.processors.packagename.FlatteningPackageNameProcessor) PackageNameProcessor(com.palantir.conjure.python.processors.packagename.PackageNameProcessor) TwoComponentStrippingPackageNameProcessor(com.palantir.conjure.python.processors.packagename.TwoComponentStrippingPackageNameProcessor) PrefixingPackageNameProcessor(com.palantir.conjure.python.processors.packagename.PrefixingPackageNameProcessor) CompoundPackageNameProcessor(com.palantir.conjure.python.processors.packagename.CompoundPackageNameProcessor) PythonPackage(com.palantir.conjure.python.poet.PythonPackage) ArrayList(java.util.ArrayList) DealiasingTypeVisitor(com.palantir.conjure.visitor.DealiasingTypeVisitor) TwoComponentStrippingPackageNameProcessor(com.palantir.conjure.python.processors.packagename.TwoComponentStrippingPackageNameProcessor) PythonFile(com.palantir.conjure.python.poet.PythonFile) ConstantPackageNameProcessor(com.palantir.conjure.python.processors.packagename.ConstantPackageNameProcessor) PrefixingPackageNameProcessor(com.palantir.conjure.python.processors.packagename.PrefixingPackageNameProcessor) PackagePrependingTypeNameProcessor(com.palantir.conjure.python.processors.typename.PackagePrependingTypeNameProcessor) NameOnlyTypeNameProcessor(com.palantir.conjure.python.processors.typename.NameOnlyTypeNameProcessor) TypeNameProcessor(com.palantir.conjure.python.processors.typename.TypeNameProcessor) PackagePrependingTypeNameProcessor(com.palantir.conjure.python.processors.typename.PackagePrependingTypeNameProcessor) CompoundPackageNameProcessor(com.palantir.conjure.python.processors.packagename.CompoundPackageNameProcessor)

Example 3 with PythonPackage

use of com.palantir.conjure.python.poet.PythonPackage in project conjure-python by palantir.

the class ConjurePythonGenerator method write.

public void write(ConjureDefinition conjureDefinition, PythonFileWriter writer) {
    generate(conjureDefinition).forEach(writer::writePythonFile);
    PythonPackage rootPackage = PythonPackage.of(buildPackageNameProcessor().process(""));
    if (!config.generateRawSource()) {
        writer.writePythonFile(buildPythonSetupFile(rootPackage));
        writer.writePythonFile(buildPyTypedFile());
    }
    if (config.shouldWriteCondaRecipe()) {
        writer.writePythonFile(buildCondaMetaYamlFile(rootPackage));
    }
}
Also used : PythonPackage(com.palantir.conjure.python.poet.PythonPackage)

Example 4 with PythonPackage

use of com.palantir.conjure.python.poet.PythonPackage in project conjure-python by palantir.

the class ConjurePythonGenerator method getInitFiles.

private List<PythonFile> getInitFiles(ConjureDefinition conjureDefinition, TypeNameProcessor implTypeNameProcessor, PackageNameProcessor definitionPackageNameProcessor, TypeNameProcessor definitionTypeNameProcessor) {
    String moduleSpecifier = ".._impl";
    DefinitionImportTypeDefinitionVisitor definitionImportTypeDefinitionVisitor = new DefinitionImportTypeDefinitionVisitor(moduleSpecifier, implTypeNameProcessor, definitionTypeNameProcessor);
    List<PythonSnippet> snippets = new ArrayList<>();
    snippets.addAll(conjureDefinition.getTypes().stream().map(typeDefinition -> {
        TypeName typeName = typeDefinition.accept(TypeDefinitionVisitor.TYPE_NAME);
        return EmptySnippet.builder().pythonPackage(PythonPackage.of(definitionPackageNameProcessor.process(typeName.getPackage()))).addAllImports(typeDefinition.accept(definitionImportTypeDefinitionVisitor)).build();
    }).collect(Collectors.toList()));
    snippets.addAll(conjureDefinition.getServices().stream().map(serviceDefinition -> EmptySnippet.builder().pythonPackage(PythonPackage.of(definitionPackageNameProcessor.process(serviceDefinition.getServiceName().getPackage()))).addImports(PythonImport.of(moduleSpecifier, NamedImport.of(implTypeNameProcessor.process(serviceDefinition.getServiceName()), definitionTypeNameProcessor.process(serviceDefinition.getServiceName())))).build()).collect(Collectors.toList()));
    Map<PythonPackage, List<PythonSnippet>> snippetsByPackage = snippets.stream().collect(Collectors.groupingBy(PythonSnippet::pythonPackage));
    return KeyedStream.stream(snippetsByPackage).map((pythonPackage, pythonSnippets) -> PythonFile.builder().pythonPackage(pythonPackage).fileName(INIT_PY).contents(pythonSnippets).build()).values().collect(Collectors.toList());
}
Also used : PythonPackage(com.palantir.conjure.python.poet.PythonPackage) TypeName(com.palantir.conjure.spec.TypeName) PythonSnippet(com.palantir.conjure.python.poet.PythonSnippet) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) DefinitionImportTypeDefinitionVisitor(com.palantir.conjure.python.types.DefinitionImportTypeDefinitionVisitor)

Example 5 with PythonPackage

use of com.palantir.conjure.python.poet.PythonPackage in project conjure-python by palantir.

the class ConjurePythonGenerator method getImplPythonFile.

private PythonFile getImplPythonFile(ConjureDefinition conjureDefinition, DealiasingTypeVisitor dealiasingTypeVisitor, PackageNameProcessor implPackageNameProcessor, TypeNameProcessor implTypeNameProcessor, PackageNameProcessor definitionPackageNameProcessor, TypeNameProcessor definitionTypeNameProcessor) {
    PythonTypeGenerator beanGenerator = new PythonTypeGenerator(implPackageNameProcessor, implTypeNameProcessor, definitionPackageNameProcessor, definitionTypeNameProcessor, dealiasingTypeVisitor);
    ClientGenerator clientGenerator = new ClientGenerator(implPackageNameProcessor, implTypeNameProcessor, definitionPackageNameProcessor, definitionTypeNameProcessor, dealiasingTypeVisitor);
    List<PythonSnippet> snippets = new ArrayList<>();
    snippets.addAll(conjureDefinition.getTypes().stream().map(beanGenerator::generateType).collect(Collectors.toList()));
    snippets.addAll(conjureDefinition.getServices().stream().map(clientGenerator::generateClient).collect(Collectors.toList()));
    Map<PythonPackage, List<PythonSnippet>> snippetsByPackage = snippets.stream().collect(Collectors.groupingBy(PythonSnippet::pythonPackage));
    PythonPackage rootPackage = PythonPackage.of(implPackageNameProcessor.process(""));
    List<PythonFile> pythonFiles = KeyedStream.stream(snippetsByPackage).map((_pythonPackage, pythonSnippets) -> PythonFile.builder().pythonPackage(rootPackage).fileName(IMPL_PY).contents(pythonSnippets).build()).values().collect(Collectors.toList());
    return Iterables.getOnlyElement(pythonFiles);
}
Also used : PythonPackage(com.palantir.conjure.python.poet.PythonPackage) ClientGenerator(com.palantir.conjure.python.client.ClientGenerator) PythonSnippet(com.palantir.conjure.python.poet.PythonSnippet) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) PythonTypeGenerator(com.palantir.conjure.python.types.PythonTypeGenerator) PythonFile(com.palantir.conjure.python.poet.PythonFile)

Aggregations

PythonPackage (com.palantir.conjure.python.poet.PythonPackage)5 PythonSnippet (com.palantir.conjure.python.poet.PythonSnippet)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ImmutableList (com.google.common.collect.ImmutableList)2 PythonFile (com.palantir.conjure.python.poet.PythonFile)2 PackageNameProcessor (com.palantir.conjure.python.processors.packagename.PackageNameProcessor)2 TypeNameProcessor (com.palantir.conjure.python.processors.typename.TypeNameProcessor)2 DealiasingTypeVisitor (com.palantir.conjure.visitor.DealiasingTypeVisitor)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 CaseConverter (com.palantir.conjure.CaseConverter)1 ClientGenerator (com.palantir.conjure.python.client.ClientGenerator)1 PythonEndpointDefinition (com.palantir.conjure.python.poet.PythonEndpointDefinition)1 PythonEndpointParam (com.palantir.conjure.python.poet.PythonEndpointDefinition.PythonEndpointParam)1 PythonImport (com.palantir.conjure.python.poet.PythonImport)1 PythonService (com.palantir.conjure.python.poet.PythonService)1 CompoundPackageNameProcessor (com.palantir.conjure.python.processors.packagename.CompoundPackageNameProcessor)1 ConstantPackageNameProcessor (com.palantir.conjure.python.processors.packagename.ConstantPackageNameProcessor)1 FlatteningPackageNameProcessor (com.palantir.conjure.python.processors.packagename.FlatteningPackageNameProcessor)1 PrefixingPackageNameProcessor (com.palantir.conjure.python.processors.packagename.PrefixingPackageNameProcessor)1