feat: [cmd] add python analysis api support

上级 f5f1b2ea
from ddd.shared.domain_model import DomainModel
class Blog(object):
def __init__(self, id, title, content):
self.id = id
self.title = title
self.content = content
@classmethod
def from_dict(cls, adict):
blog = Blog(
id=adict['id'],
title=adict['title'],
content=adict['content'],
)
return blog
def to_dict(self):
return {
'id': self.id,
'title': self.title,
'content': self.content,
}
def __eq__(self, other):
return self.to_dict() == other.to_dict()
DomainModel.register(Blog)
...@@ -2,12 +2,15 @@ package cmd ...@@ -2,12 +2,15 @@ package cmd
import ( import (
"encoding/json" "encoding/json"
"fmt"
"github.com/phodal/coca/cmd/cmd_util" "github.com/phodal/coca/cmd/cmd_util"
"github.com/phodal/coca/pkg/adapter/cocafile" "github.com/phodal/coca/pkg/adapter/cocafile"
"github.com/phodal/coca/pkg/application/analysis" "github.com/phodal/coca/pkg/application/analysis"
"github.com/phodal/coca/pkg/application/pyapp"
"github.com/phodal/coca/pkg/domain/core_domain" "github.com/phodal/coca/pkg/domain/core_domain"
"github.com/phodal/coca/pkg/infrastructure/ast/cocago" "github.com/phodal/coca/pkg/infrastructure/ast/cocago"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"io/ioutil"
) )
type AnalysisCmdConfig struct { type AnalysisCmdConfig struct {
...@@ -25,14 +28,40 @@ var analysisCmd = &cobra.Command{ ...@@ -25,14 +28,40 @@ var analysisCmd = &cobra.Command{
Short: "analysis code", Short: "analysis code",
Long: ``, Long: ``,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if analysisCmdConfig.Lang == "go" { switch analysisCmdConfig.Lang {
case "go":
analysisGo() analysisGo()
} else { case "py":
case "python":
analysisPython()
default:
analysisJava() analysisJava()
} }
}, },
} }
func analysisPython() {
importPath := analysisCmdConfig.Path
var results []core_domain.CodeFile
files := cocafile.GetFilesWithFilter(importPath, cocafile.PythonFileFilter)
for _, file := range files {
fmt.Fprintf(output, "Process Python file: %s\n", file)
app := new(pyapp.PythonApiApp)
content, _ := ioutil.ReadFile(file)
result := app.Analysis(string(content), "")
results = append(results, result)
}
var ds []core_domain.CodeDataStruct
for _, result := range results {
ds = append(ds, result.DataStructures...)
}
cModel, _ := json.MarshalIndent(ds, "", "\t")
cmd_util.WriteToCocaFile("pydeps.json", string(cModel))
}
func analysisGo() { func analysisGo() {
importPath := analysisCmdConfig.Path importPath := analysisCmdConfig.Path
......
...@@ -15,3 +15,14 @@ func Test_Analysis_Go(t *testing.T) { ...@@ -15,3 +15,14 @@ func Test_Analysis_Go(t *testing.T) {
}} }}
RunTestCmd(t, analysis) RunTestCmd(t, analysis)
} }
func Test_Analysis_Python(t *testing.T) {
path := "../_fixtures/grammar/python"
analysis := []testcase.CmdTestCase{{
Name: "analysis",
Cmd: "analysis -f -l py -p " + path,
Golden: "testdata/analysis_python.txt",
}}
RunTestCmd(t, analysis)
}
...@@ -18,6 +18,10 @@ var TypeScriptFileFilter = func(path string) bool { ...@@ -18,6 +18,10 @@ var TypeScriptFileFilter = func(path string) bool {
return strings.HasSuffix(path, ".ts") return strings.HasSuffix(path, ".ts")
} }
var PythonFileFilter = func(path string) bool {
return strings.HasSuffix(path, ".py")
}
var GoFileFilter = func(path string) bool { var GoFileFilter = func(path string) bool {
return strings.HasSuffix(path, ".go") return strings.HasSuffix(path, ".go")
} }
......
...@@ -21,7 +21,7 @@ func ProcessTsString(code string) *parser.PythonParser { ...@@ -21,7 +21,7 @@ func ProcessTsString(code string) *parser.PythonParser {
type PythonApiApp struct { type PythonApiApp struct {
} }
func (j *PythonApiApp) Analysis(code string, fileName string) *core_domain.CodeFile { func (j *PythonApiApp) Analysis(code string, fileName string) core_domain.CodeFile {
scriptParser := ProcessTsString(code) scriptParser := ProcessTsString(code)
context := scriptParser.Root() context := scriptParser.Root()
......
...@@ -56,10 +56,6 @@ func TestCocagoParser_ProcessFile(t *testing.T) { ...@@ -56,10 +56,6 @@ func TestCocagoParser_ProcessFile(t *testing.T) {
"hello_world", "hello_world",
"hello_world", "hello_world",
}, },
{
"basic_interface",
"basic_interface",
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
...@@ -75,6 +71,16 @@ func getFilePath(name string) string { ...@@ -75,6 +71,16 @@ func getFilePath(name string) string {
return "testdata/node_infos/" + name return "testdata/node_infos/" + name
} }
// todo: may fill in para
func Test_basic_interface(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
filePath := getFilePath("basic_interface")
results := testParser.ProcessFile(filePath + ".code")
g.Expect(cocatest.JSONFileBytesEqual(results, filePath+".json")).To(Equal(true))
}
// todo: support it // todo: support it
func Test_NestedMethod(t *testing.T) { func Test_NestedMethod(t *testing.T) {
t.Parallel() t.Parallel()
......
...@@ -167,6 +167,6 @@ func BuildArgList(context *parser.ArglistContext) []core_domain.AnnotationKeyVal ...@@ -167,6 +167,6 @@ func BuildArgList(context *parser.ArglistContext) []core_domain.AnnotationKeyVal
return arguments return arguments
} }
func (s *PythonIdentListener) GetCodeFileInfo() *core_domain.CodeFile { func (s *PythonIdentListener) GetCodeFileInfo() core_domain.CodeFile {
return currentCodeFile return *currentCodeFile
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册