You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@linkis.apache.org by pe...@apache.org on 2022/09/22 13:48:23 UTC

[incubator-linkis] branch dev-1.3.1 updated: Dev 1.3.1 fix python code parser (#3478)

This is an automated email from the ASF dual-hosted git repository.

peacewong pushed a commit to branch dev-1.3.1
in repository https://gitbox.apache.org/repos/asf/incubator-linkis.git


The following commit(s) were added to refs/heads/dev-1.3.1 by this push:
     new 2577568e2 Dev 1.3.1 fix python code parser (#3478)
2577568e2 is described below

commit 2577568e2c45420249fb600e6a0bac3895b61a97
Author: lvjianhui <90...@qq.com>
AuthorDate: Thu Sep 22 21:48:16 2022 +0800

    Dev 1.3.1 fix python code parser (#3478)
    
    * fix PythonCodeParser statementBuffer error
    
    fix PythonCodeParser statementBuffer error
    
    Co-Authored-By: lvjianhui <25...@users.noreply.github.com>
    
    * fix PythonCodeParser statementBuffer error
    
    * 1. fix PythonCodeParser statementBuffer error
    2. Code formatted by mvn spotless:apply
    
    * 1. update except result
    
    Co-authored-by: lvjianhui <25...@users.noreply.github.com>
---
 .../governance/common/paser/CodeParser.scala       | 12 ++++++++++-
 .../common/paser/PythonCodeParseTest.scala         | 24 ++++++++++++++++++++++
 2 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/linkis-computation-governance/linkis-computation-governance-common/src/main/scala/org/apache/linkis/governance/common/paser/CodeParser.scala b/linkis-computation-governance/linkis-computation-governance-common/src/main/scala/org/apache/linkis/governance/common/paser/CodeParser.scala
index cc7bf0526..92623ad7c 100644
--- a/linkis-computation-governance/linkis-computation-governance-common/src/main/scala/org/apache/linkis/governance/common/paser/CodeParser.scala
+++ b/linkis-computation-governance/linkis-computation-governance-common/src/main/scala/org/apache/linkis/governance/common/paser/CodeParser.scala
@@ -159,6 +159,13 @@ class PythonCodeParser extends SingleCodeParser with Logging {
         statementBuffer.append(l)
         recordBrackets(bracketStack, l.trim)
       case l if notDoc && l.startsWith("@") =>
+        if (
+            statementBuffer.nonEmpty && bracketStack.isEmpty && StringUtils
+              .isNotBlank(statementBuffer.last) && !statementBuffer.last.startsWith("@")
+        ) {
+          codeBuffer.append(statementBuffer.mkString("\n"))
+          statementBuffer.clear()
+        }
         statementBuffer.append(l)
         recordBrackets(bracketStack, l.trim)
       case l if notDoc && l.startsWith("else") => // LOG.info("I am else")
@@ -168,7 +175,10 @@ class PythonCodeParser extends SingleCodeParser with Logging {
         statementBuffer.append(l)
         recordBrackets(bracketStack, l.trim)
       case l if notDoc && StringUtils.isNotBlank(l) =>
-        if (statementBuffer.nonEmpty && bracketStack.isEmpty) {
+        if (
+            statementBuffer.nonEmpty && bracketStack.isEmpty && StringUtils
+              .isNotBlank(statementBuffer.last) && !statementBuffer.last.startsWith("@")
+        ) {
           codeBuffer.append(statementBuffer.mkString("\n"))
           statementBuffer.clear()
         }
diff --git a/linkis-computation-governance/linkis-computation-governance-common/src/test/scala/org/apache/linkis/governance/common/paser/PythonCodeParseTest.scala b/linkis-computation-governance/linkis-computation-governance-common/src/test/scala/org/apache/linkis/governance/common/paser/PythonCodeParseTest.scala
index 3b050ff5d..29611d717 100644
--- a/linkis-computation-governance/linkis-computation-governance-common/src/test/scala/org/apache/linkis/governance/common/paser/PythonCodeParseTest.scala
+++ b/linkis-computation-governance/linkis-computation-governance-common/src/test/scala/org/apache/linkis/governance/common/paser/PythonCodeParseTest.scala
@@ -54,4 +54,28 @@ class PythonCodeParseTest {
     assertTrue(strings.length == 8)
   }
 
+  @Test
+  @DisplayName("testParsePythonWithDecorator")
+  def testParsePythonWithDecorator(): Unit = {
+    val parser = new PythonCodeParser
+    val pythonCode: String =
+      """
+        |import time
+        |def timmer(func):
+        |    def wrapper(*args, **kwargs):
+        |        start = time.time()
+        |        func(*args, **kwargs)
+        |        stop = time.time()
+        |        print('foo运行时长:', stop - start)
+        |    return wrapper
+        |@timmer
+        |def foo(x, y, z):
+        |    time.sleep(3)
+        |    print(f'x={x}, y={y}, z={z}')
+        |foo(1,2,3)
+        """.stripMargin
+    val strings = parser.parsePythonCode(pythonCode)
+    assertTrue(strings.length == 4)
+  }
+
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@linkis.apache.org
For additional commands, e-mail: commits-help@linkis.apache.org