-
Notifications
You must be signed in to change notification settings - Fork 123
/
go.py
30 lines (23 loc) 路 913 Bytes
/
go.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""Parse package dependencies from go.mod files."""
import re
from typing import List
from readmeai.core.parsers import BaseFileParser
class GoModParser(BaseFileParser):
"""Parser for go.mod files."""
def __init__(self) -> None:
"""Initializes the handler with given configuration."""
super().__init__()
def parse(self, content: str) -> List[str]:
"""Parse the content of a go.mod file."""
try:
lines = content.split("\n")
pattern = r"^\s*([\w\.\-_/]+)\s+v[\w\.\-_/]+"
regex = re.compile(pattern)
package_names = (
regex.match(line.strip()).group(1).split("/")[-1]
for line in lines
if regex.match(line.strip())
)
return list(package_names)
except Exception as exc:
return self.handle_parsing_error(f"go.mod: {str(exc)}")