Skip to content

Commit

Permalink
camel-python language extension
Browse files Browse the repository at this point in the history
  • Loading branch information
kulagaIA committed Aug 13, 2024
1 parent 55b06cf commit 271a54e
Show file tree
Hide file tree
Showing 19 changed files with 849 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@
*** xref:reference/extensions/jackson-protobuf.adoc[Protobuf Jackson]
*** xref:reference/extensions/pubnub.adoc[PubNub]
*** xref:reference/extensions/pulsar.adoc[Pulsar]
*** xref:reference/extensions/python.adoc[Python]
*** xref:reference/extensions/qdrant.adoc[Qdrant]
*** xref:reference/extensions/quartz.adoc[Quartz]
*** xref:reference/extensions/quickfix.adoc[QuickFix]
Expand Down
46 changes: 46 additions & 0 deletions docs/modules/ROOT/pages/reference/extensions/python.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Do not edit directly!
// This file was generated by camel-quarkus-maven-plugin:update-extension-doc-page
[id="extensions-python"]
= python
:page-aliases: extensions/python.adoc
:linkattrs:
:cq-artifact-id: camel-quarkus-python
:cq-native-supported: true
:cq-status: Stable
:cq-status-deprecation: Stable
:cq-description: Evaluate a Python script
:cq-deprecated: false
:cq-jvm-since: 1.0.0
:cq-native-since: 3.2.0

ifeval::[{doc-show-badges} == true]
[.badges]
[.badge-key]##JVM since##[.badge-supported]##1.0.0## [.badge-key]##Native since##[.badge-supported]##3.2.0##
endif::[]

Evaluate a python script

[id="extensions-python-whats-inside"]
== What's inside

* xref:{cq-camel-components}:languages:python-language.adoc[python language]

Please refer to the above link for usage and configuration details.

[id="extensions-python-maven-coordinates"]
== Maven coordinates

https://{link-quarkus-code-generator}/?extension-search=camel-quarkus-python[Create a new project with this extension on {link-quarkus-code-generator}, window="_blank"]

Or add the coordinates to your existing project:

[source,xml]
----
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-python</artifactId>
</dependency>
----
ifeval::[{doc-show-user-guide-link} == true]
Check the xref:user-guide/index.adoc[User guide] for more information about writing Camel Quarkus applications.
endif::[]
1 change: 1 addition & 0 deletions extensions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
<module>platform-http</module>
<module>protobuf</module>
<module>pubnub</module>
<module>python</module>
<module>qdrant</module>
<module>quartz</module>
<module>qute</module>
Expand Down
67 changes: 67 additions & 0 deletions extensions/python/deployment/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-python-parent</artifactId>
<version>3.14.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>camel-quarkus-python-deployment</artifactId>
<name>Camel Quarkus :: Python :: Deployment</name>

<dependencies>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-core-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-python</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-support-language-deployment</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-processor</artifactId>
<version>${quarkus.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.component.python.deployment;

import io.quarkus.builder.item.MultiBuildItem;
import org.python.core.PyCode;

public final class PythonCompiledExpressionBuildItem extends MultiBuildItem {

private final String sourceCode;

private final PyCode compiledCode;

public PythonCompiledExpressionBuildItem(String sourceCode, PyCode compiledCode) {
this.sourceCode = sourceCode;
this.compiledCode = compiledCode;
}

public String getSourceCode() {
return sourceCode;
}

public PyCode getCompiledCode() {
return compiledCode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.component.python.deployment;

import java.util.List;

import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.pkg.steps.NativeOrNativeSourcesBuild;
import io.quarkus.runtime.RuntimeValue;
import org.apache.camel.ExpressionIllegalSyntaxException;
import org.apache.camel.language.python.PythonLanguage;
import org.apache.camel.quarkus.component.python.PythonExpressionRecorder;
import org.apache.camel.quarkus.core.deployment.spi.CamelBeanBuildItem;
import org.apache.camel.quarkus.support.language.deployment.ExpressionBuildItem;
import org.apache.camel.quarkus.support.language.deployment.ExpressionExtractionResultBuildItem;
import org.apache.camel.quarkus.support.language.deployment.ScriptBuildItem;
import org.python.core.PyCode;
import org.python.util.PythonInterpreter;

class PythonProcessor {

private static final String FEATURE = "camel-python";

@BuildStep
FeatureBuildItem feature() {
return new FeatureBuildItem(FEATURE);
}

@BuildStep(onlyIf = NativeOrNativeSourcesBuild.class)
void compileExpressions(ExpressionExtractionResultBuildItem result,
List<ExpressionBuildItem> expressions,
List<ScriptBuildItem> scripts,
BuildProducer<PythonCompiledExpressionBuildItem> producer) {
if (result.isSuccess()) {
List<ExpressionBuildItem> pythonExpressions = expressions.stream()
.filter(exp -> "python".equals(exp.getLanguage())).toList();
List<ScriptBuildItem> pythonScripts = scripts.stream()
.filter(exp -> "python".equals(exp.getLanguage())).toList();
if (pythonExpressions.isEmpty() && pythonScripts.isEmpty()) {
return;
}

try (PythonInterpreter compiler = new PythonInterpreter()) {
for (ExpressionBuildItem pythonExpression : pythonExpressions) {
producer.produce(createPythonExpressionBuildItem(compiler, pythonExpression.getExpression()));
}
for (ScriptBuildItem pythonScript : pythonScripts) {
producer.produce(createPythonExpressionBuildItem(compiler, pythonScript.getLoadedContent()));
}
}
}
}

@Record(ExecutionTime.STATIC_INIT)
@BuildStep(onlyIf = NativeOrNativeSourcesBuild.class)
CamelBeanBuildItem configureLanguage(
PythonExpressionRecorder recorder,
ExpressionExtractionResultBuildItem result,
List<PythonCompiledExpressionBuildItem> sources) {

if (result.isSuccess() && !sources.isEmpty()) {
RuntimeValue<PythonLanguage.Builder> builder = recorder.languageBuilder();
for (PythonCompiledExpressionBuildItem source : sources) {
recorder.addScript(
builder,
source.getSourceCode(),
source.getCompiledCode());
}
final RuntimeValue<PythonLanguage> language = recorder.languageNewInstance(builder);
return new CamelBeanBuildItem("python", PythonLanguage.class.getName(), language);
}
return null;
}

private PythonCompiledExpressionBuildItem createPythonExpressionBuildItem(PythonInterpreter compiler, String expression) {
PyCode compiledExpression;
try {
compiledExpression = compiler.compile(expression);
} catch (Exception e) {
throw new ExpressionIllegalSyntaxException(expression, e);
}
return new PythonCompiledExpressionBuildItem(expression, compiledExpression);
}
}
39 changes: 39 additions & 0 deletions extensions/python/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-extensions</artifactId>
<version>3.14.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>camel-quarkus-python-parent</artifactId>
<name>Camel Quarkus :: Python</name>
<packaging>pom</packaging>

<modules>
<module>deployment</module>
<module>runtime</module>
</modules>
</project>
Loading

0 comments on commit 271a54e

Please sign in to comment.