Skip to content

Commit

Permalink
improved readme, added .gitattributes files, fixed a bug in function …
Browse files Browse the repository at this point in the history
…line validator for python
  • Loading branch information
ismailbayram committed Aug 1, 2023
1 parent ffce9e8 commit 08f9b56
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
internal/browser/pyproject/** linguist-vendored
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
25 changes: 16 additions & 9 deletions internal/browser/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}

Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions internal/browser/python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 08f9b56

Please sign in to comment.