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

kezhenxu94 pushed a commit to branch python/coding/directive
in repository https://gitbox.apache.org/repos/asf/skywalking-eyes.git

commit a9d0fa74c15a1fed1963eeae82eff987b55ec306
Author: kezhenxu94 <[email protected]>
AuthorDate: Wed Sep 8 15:05:19 2021 +0800

    Honor Python's coding directive
---
 assets/languages.yaml       |  4 ++--
 assets/styles.yaml          |  7 +++++++
 pkg/comments/config_test.go |  2 +-
 pkg/header/fix_test.go      | 34 +++++++++++++++++++++++++++++++++-
 4 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/assets/languages.yaml b/assets/languages.yaml
index 44222a4..00e710b 100644
--- a/assets/languages.yaml
+++ b/assets/languages.yaml
@@ -1097,7 +1097,7 @@ Cython:
   codemirror_mode: python
   codemirror_mime_type: text/x-cython
   language_id: 79
-  comment_style_id: Hashtag
+  comment_style_id: PythonStyle
 D:
   type: programming
   color: "#ba595e"
@@ -4441,7 +4441,7 @@ Python:
     - python3
     - rusthon
   language_id: 303
-  comment_style_id: Hashtag
+  comment_style_id: PythonStyle
 Python console:
   type: programming
   group: Python
diff --git a/assets/styles.yaml b/assets/styles.yaml
index 6d971f2..5a784c8 100644
--- a/assets/styles.yaml
+++ b/assets/styles.yaml
@@ -102,3 +102,10 @@
   start: "'"
   middle: "'"
   end: "'"
+
+- id: PythonStyle
+  # (interpreter binary and encoding comment) | (only interpreter binary) | 
(only encoding comment)
+  after: '(?m)(^*#!.+$\n^[ \t\f]*#.*?coding[:=][ 
\t]*([-_.a-zA-Z0-9]+).*$)|(^*#!.+$)|(^[ \t\f]*#.*?coding[:=][ 
\t]*([-_.a-zA-Z0-9]+).*$)'
+  start: '#'
+  middle: '#'
+  end: '#'
diff --git a/pkg/comments/config_test.go b/pkg/comments/config_test.go
index ae3367f..b967fd5 100644
--- a/pkg/comments/config_test.go
+++ b/pkg/comments/config_test.go
@@ -52,7 +52,7 @@ func TestCommentStyle(t *testing.T) {
                commentStyleID string
        }{
                {filename: "Test.java", commentStyleID: "SlashAsterisk"},
-               {filename: "Test.py", commentStyleID: "Hashtag"},
+               {filename: "Test.py", commentStyleID: "PythonStyle"},
        }
        for _, test := range tests {
                t.Run(test.filename, func(t *testing.T) {
diff --git a/pkg/header/fix_test.go b/pkg/header/fix_test.go
index e8dce75..d98f36e 100644
--- a/pkg/header/fix_test.go
+++ b/pkg/header/fix_test.go
@@ -89,7 +89,7 @@ func TestRewriteContent(t *testing.T) {
 print_string "hello worlds!\n";;
 `},
                {
-                       name:  "Python with Shebang",
+                       name:  "Python with interpreter binary",
                        style: comments.FileCommentStyle("test.py"),
                        content: `#!/usr/bin/env python3
 if __name__ == '__main__':
@@ -104,6 +104,38 @@ if __name__ == '__main__':
     print('Hello World')
 `},
                {
+                       name:  "Python with interpreter binary and encoding",
+                       style: comments.FileCommentStyle("test.py"),
+                       content: `#!/usr/bin/env python3
+# -*- coding: latin-1 -*-
+if __name__ == '__main__':
+    print('Hello World')
+`,
+                       licenseHeader: getLicenseHeader("test.py", t.Error),
+                       expectedContent: `#!/usr/bin/env python3
+# -*- coding: latin-1 -*-
+# Apache License 2.0
+#   http://www.apache.org/licenses/LICENSE-2.0
+# Apache License 2.0
+if __name__ == '__main__':
+    print('Hello World')
+`},
+               {
+                       name:  "Python with encoding",
+                       style: comments.FileCommentStyle("test.py"),
+                       content: `# -*- coding: latin-1 -*-
+if __name__ == '__main__':
+    print('Hello World')
+`,
+                       licenseHeader: getLicenseHeader("test.py", t.Error),
+                       expectedContent: `# -*- coding: latin-1 -*-
+# Apache License 2.0
+#   http://www.apache.org/licenses/LICENSE-2.0
+# Apache License 2.0
+if __name__ == '__main__':
+    print('Hello World')
+`},
+               {
                        name:  "Python",
                        style: comments.FileCommentStyle("test.py"),
                        content: `if __name__ == '__main__':

Reply via email to