diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..63433ca --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +internal/browser/pyproject/** linguist-vendored diff --git a/README.md b/README.md index 6b02d3a..648e5a0 100644 --- a/README.md +++ b/README.md @@ -16,14 +16,14 @@ like in the `.github/workflows/codequality.yml`. # Installation ## Install with Go ```bash -go install github.com/ismailbayram/bigpicture@1.0.0 +go install github.com/ismailbayram/bigpicture@1.1.0 ``` ## Install on Linux ```bash curl -L https://github.com/ismailbayram/bigpicture/releases/download/1.1.0/bigpicture-amd64-linux -o /usr/local/bin/bigpicture chmod +x /usr/local/bin/bigpicture ``` -## Install on Mac +## Install on Macos ```bash curl -L https://github.com/ismailbayram/bigpicture/releases/download/1.1.0/bigpicture-amd64-darwin -o /usr/local/bin/bigpicture chmod +x /usr/local/bin/bigpicture diff --git a/go.mod b/go.mod index e1cf0e4..dab58cc 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,8 @@ require ( ) require ( + github.com/DataDog/go-python3 v0.0.0-20211102160307-40adc605f1fe // indirect + github.com/alecthomas/participle/v2 v2.0.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/go-playground/locales v0.14.1 // indirect diff --git a/go.sum b/go.sum index 5ed2482..932dadc 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,7 @@ +github.com/DataDog/go-python3 v0.0.0-20211102160307-40adc605f1fe h1:bNMi0HArOQY4899TKLi4RP7g9BZ2kwLOiVpoJHWxyFs= +github.com/DataDog/go-python3 v0.0.0-20211102160307-40adc605f1fe/go.mod h1:7ctnOCLiUlwKO9GvAjusUF68edSbiHqC18gVPQF0ojA= +github.com/alecthomas/participle/v2 v2.0.0 h1:Fgrq+MbuSsJwIkw3fEj9h75vDP0Er5JzepJ0/HNHv0g= +github.com/alecthomas/participle/v2 v2.0.0/go.mod h1:rAKZdJldHu8084ojcWevWAL8KmEU+AT+Olodb+WoN2Y= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/internal/browser/python.go b/internal/browser/python.go index fac06dd..35d6c0a 100644 --- a/internal/browser/python.go +++ b/internal/browser/python.go @@ -95,7 +95,7 @@ func (b *PythonBrowser) parseFile(path string, parentNode *graph.Node) *graph.No for name, functionBody := range functionsInfo { functions = append(functions, graph.Function{ Name: name, - LineCount: len(strings.Split(functionBody, "\n")) - 2, + LineCount: len(strings.Split(functionBody, "\n")) - 1, }) } @@ -135,33 +135,40 @@ func findImports(pythonCode string) []string { return imports } -func findFunctions(pythonCode string) map[string]string { +func findFunctions(fileContent string) map[string]string { functions := make(map[string]string) - lines := strings.Split(pythonCode, "\n") + lines := strings.Split(fileContent, "\n") functionRegex := regexp.MustCompile(`^\s*def\s+(\w+)\s*\((.*?)\):`) var currentFunctionName string - var currentFunctionContent strings.Builder + var currentFunctionContent string for _, line := range lines { if matches := functionRegex.FindStringSubmatch(line); len(matches) > 1 { if currentFunctionName != "" { - functions[currentFunctionName] = currentFunctionContent.String() - currentFunctionContent.Reset() + functions[currentFunctionName] = strings.TrimSpace(currentFunctionContent) + currentFunctionContent = "" } currentFunctionName = matches[1] } + if currentFunctionName != "" && (strings.Contains(line, "@") || strings.Contains(line, "class ")) { + functions[currentFunctionName] = strings.TrimSpace(currentFunctionContent) + currentFunctionContent = "" + currentFunctionName = "" + continue + } + if currentFunctionName != "" { - currentFunctionContent.WriteString(line) - currentFunctionContent.WriteString("\n") + currentFunctionContent += line + currentFunctionContent += "\n" } } if currentFunctionName != "" { - functions[currentFunctionName] = currentFunctionContent.String() + functions[currentFunctionName] = strings.TrimSpace(currentFunctionContent) } return functions diff --git a/internal/browser/python_test.go b/internal/browser/python_test.go index 9657757..dd0795b 100644 --- a/internal/browser/python_test.go +++ b/internal/browser/python_test.go @@ -76,6 +76,24 @@ func TestPythonBrowser_ParseFile(t *testing.T) { assert.Equal(t, "/baskets/exceptions.py", node.ImportRaw[6]) assert.Equal(t, "/cars/exceptions.py", node.ImportRaw[7]) assert.Equal(t, "/cars", node.ImportRaw[8]) + + assert.Equal(t, 143, node.LineCount) + + assert.Equal(t, 8, len(node.Functions)) + funcs := map[string]int{ + "get_or_create_basket": 16, + "apply_discounts": 15, + "_check_basket_items": 17, + "add_basket_item": 22, + "clean_discounts": 7, + "clean_basket": 7, + "delete_basket_item": 7, + "complete_basket": 19, + } + for _, f := range node.Functions { + assert.Equal(t, funcs[f.Name], f.LineCount, f.Name) + } + } func TestPythonBrowser_Browse(t *testing.T) {