Skip to content

Commit

Permalink
Merge pull request #13 from brightics/feature-BRTCDEV-21640-MD
Browse files Browse the repository at this point in the history
[21640-MD]python script 파라미터 공유 버그 수정
  • Loading branch information
gyu77hs authored and sds-github committed Sep 2, 2021
2 parents 0026276 + 8ae8338 commit eb9cf1e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 31 deletions.
@@ -1,19 +1,19 @@
/*
Copyright 2019 Samsung SDS
Licensed 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.
*/

/*
Copyright 2019 Samsung SDS
Licensed 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 com.samsung.sds.brightics.agent.context.python;

import com.google.gson.JsonArray;
Expand All @@ -26,6 +26,7 @@
import com.samsung.sds.brightics.common.core.exception.BrighticsCoreException;
import com.samsung.sds.brightics.common.data.DataStatus;
import com.samsung.sds.brightics.common.network.proto.ContextType;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
Expand All @@ -38,7 +39,7 @@
public class PythonScriptBuilder {

private static final String SCRIPT_DELIM = "\n";

private static final String PYTHON_INDENT = " "; // 4 spaces
private StringJoiner script = new StringJoiner(SCRIPT_DELIM);
private TaskMessageWrapper message;

Expand All @@ -50,6 +51,24 @@ public String script() {
return script.toString();
}

/**
* Wrapping code with function
* Call only in type {@code PythonScriptType.UDF} and {@code PythonScriptType.PythonScript}
* @return python code
*/
public String wrapScriptByFunc() {
String uniqFuncName = String.format("brtc_py_wrapper_%d", this.hashCode()).replace("-", "_");
String prefixScript = String.format("def %s():%s", uniqFuncName, SCRIPT_DELIM);
String postfixScript = String.format("%s%s()", SCRIPT_DELIM, uniqFuncName);
StringJoiner wrappedScript = new StringJoiner(SCRIPT_DELIM, prefixScript, postfixScript);

String[] scriptCodes = script.toString().split(SCRIPT_DELIM);
for (int i = 0; i < scriptCodes.length; i++)
wrappedScript.add(PYTHON_INDENT + parseTabIndent(scriptCodes[i]));

return wrappedScript.toString();
}

PythonScriptBuilder addScript() {
script.add(getScript((message.params.internalData)));
return this;
Expand Down Expand Up @@ -91,18 +110,18 @@ PythonScriptBuilder addInputsScript() {

return this;
}

PythonScriptBuilder addInputsUDFScript() {
DataAttribute inputs = message.getInputs();

for (Entry<String, Collection<String>> entry : inputs.entrySetAsIterableValue()) {
script.add(entry.getValue().stream().
map(this::makeDataLoadScript).
collect(Collectors.joining(", ", String.format("%s = ", entry.getKey()), " ")));
}

return this;
}

PythonScriptBuilder addInputsUDFScript() {
DataAttribute inputs = message.getInputs();

for (Entry<String, Collection<String>> entry : inputs.entrySetAsIterableValue()) {
script.add(entry.getValue().stream().
map(this::makeDataLoadScript).
collect(Collectors.joining(", ", String.format("%s = ", entry.getKey()), " ")));
}

return this;
}

PythonScriptBuilder addOutDataScript() {
String[] outDatas = message.getOutData();
Expand Down Expand Up @@ -335,4 +354,25 @@ private String makeVariablePythonString(String type, String value) {
return String.format("%s", StringUtils.trim(value));
}
}

/**
* Tab 으로 Indentation 한 코드를 Space으로 변경하기 위한 메소드
* @param line a line of code
* @return Space Indent로 변환된 문자열 또는 원본 문자열
*/
private String parseTabIndent(String line) {
String rst = line;
int cnt = 0;

while (rst.length() > 0 && rst.charAt(0) == '\t') {
cnt++;
rst = rst.substring(1);
}

for (int i = cnt; i > 0; i--) {
rst = PYTHON_INDENT + rst;
}

return rst;
}
}
Expand Up @@ -52,7 +52,7 @@ public String getSource(TaskMessageWrapper message) {
addScript().
addPutScriptResultScript().
addWriteOutputsScript().
script();
wrapScriptByFunc();
}
},
UDF {
Expand All @@ -64,7 +64,7 @@ public String getSource(TaskMessageWrapper message) {
addScript().
addPutOutputsScript().
addWriteOutputsScript().
script();
wrapScriptByFunc();
}
},
Function {
Expand Down

0 comments on commit eb9cf1e

Please sign in to comment.