@@ -7,115 +7,128 @@ import (
77 "strings"
88)
99
10- // GeneratePylintRC generates a pylintrc file content with the specified patterns enabled
11- func GeneratePylintRC (config types.ToolConfiguration ) string {
12- var rcContent strings.Builder
10+ // getDefaultParametersForPatterns returns a map of pattern IDs to their default parameters
11+ func getDefaultParametersForPatterns (patternIDs []string ) map [string ][]types.ParameterConfiguration {
12+ defaultParams := make (map [string ][]types.ParameterConfiguration )
13+
14+ for _ , patternID := range patternIDs {
15+ if params , exists := PatternDefaultParameters [patternID ]; exists {
16+ defaultParams [patternID ] = params
17+ }
18+ }
19+
20+ return defaultParams
21+ }
1322
14- // Write header
23+ // writePylintRCHeader writes the common header sections to the RC content
24+ func writePylintRCHeader (rcContent * strings.Builder ) {
1525 rcContent .WriteString ("[MASTER]\n " )
1626 rcContent .WriteString ("ignore=CVS\n " )
1727 rcContent .WriteString ("persistent=yes\n " )
1828 rcContent .WriteString ("load-plugins=\n \n " )
19-
20- // Disable all patterns by default
2129 rcContent .WriteString ("[MESSAGES CONTROL]\n " )
2230 rcContent .WriteString ("disable=all\n " )
31+ }
2332
24- // Collect all enabled pattern IDs and their parameters
25- var enabledPatternsIds []string
26- patternParams := make (map [string ]map [string ]string )
27-
28- // Only process patterns if the tool is enabled
29- if config .IsEnabled {
30- for _ , pattern := range config .Patterns {
31- patternID := extractPatternId (pattern .InternalId )
32- enabledPatternsIds = append (enabledPatternsIds , patternID )
33-
34- // Store parameters for this pattern
35- params := make (map [string ]string )
36- for _ , param := range pattern .Parameters {
37- params [param .Name ] = param .Value
38- }
39- patternParams [patternID ] = params
40- }
41- }
42-
43- // Write all enabled patterns in a single line
44- if len (enabledPatternsIds ) > 0 {
45- rcContent .WriteString (fmt .Sprintf ("enable=%s\n " , strings .Join (enabledPatternsIds , "," )))
33+ // writeEnabledPatterns writes the enabled patterns section to the RC content
34+ func writeEnabledPatterns (rcContent * strings.Builder , patternIDs []string ) {
35+ if len (patternIDs ) > 0 {
36+ rcContent .WriteString (fmt .Sprintf ("enable=%s\n " , strings .Join (patternIDs , "," )))
4637 }
4738 rcContent .WriteString ("\n " )
39+ }
4840
49- // Group parameters by section
50- groupedParams := groupParametersBySection (config )
51-
52- // Write parameters for each section
41+ // writeParametersBySection writes the parameters grouped by section to the RC content
42+ func writeParametersBySection (rcContent * strings.Builder , groupedParams map [string ][]types.PylintPatternParameterConfiguration ) {
5343 for sectionName , params := range groupedParams {
5444 rcContent .WriteString (fmt .Sprintf ("[%s]\n " , sectionName ))
5545 for _ , param := range params {
56- value := param .Value
57- if value == "" {
58- // If no value is set, use default from PatternDefaultParameters
59- if defaultParams , ok := PatternDefaultParameters [extractPatternId (param .Name )]; ok {
60- for _ , defaultParam := range defaultParams {
61- if defaultParam .Name == param .Name {
62- value = defaultParam .Value
63- break
64- }
65- }
66- }
67- }
68- rcContent .WriteString (fmt .Sprintf ("%s=%s\n " , param .Name , value ))
46+ rcContent .WriteString (fmt .Sprintf ("%s=%s\n " , param .Name , param .Value ))
6947 }
7048 rcContent .WriteString ("\n " )
7149 }
72-
73- return rcContent .String ()
7450}
7551
76- // groupParametersBySection groups parameters by their section name
77- func groupParametersBySection (config types.ToolConfiguration ) map [string ][]types.PylintPatternParameterConfiguration {
78- // Initialize the result map
52+ // groupParametersByPatterns groups parameters from patterns into sections
53+ func groupParametersByPatterns (patterns []types.PatternConfiguration ) map [string ][]types.PylintPatternParameterConfiguration {
7954 groupedParams := make (map [string ][]types.PylintPatternParameterConfiguration )
8055
81- // Iterate through each pattern
82- for _ , pattern := range config .Patterns {
83- // Get parameters to process - either from the pattern or from defaults
84- if len (pattern .Parameters ) == 0 {
85- // If no parameters, check defaults
86- patternID := extractPatternId (pattern .InternalId )
87- pattern .Parameters = PatternDefaultParameters [patternID ]
88- }
56+ for _ , pattern := range patterns {
57+ patternID := extractPatternId (pattern .InternalId )
58+ params := pattern .Parameters
8959
90- // Convert pattern parameters to PylintPatternParameterConfiguration
91- parameters := make ([]types.PylintPatternParameterConfiguration , len (pattern .Parameters ))
92- for i , param := range pattern .Parameters {
93- parameters [i ] = types.PylintPatternParameterConfiguration {
94- Name : param .Name ,
95- Value : param .Value ,
96- SectionName : GetParameterSection (param .Name ),
97- }
60+ // If no parameters, check defaults
61+ if len (params ) == 0 {
62+ params = PatternDefaultParameters [patternID ]
9863 }
9964
100- // Process all parameters (either from pattern or defaults)
101- for _ , param := range parameters {
102- // Skip parameters without a section name
103- if param .SectionName == nil {
65+ for _ , param := range params {
66+ sectionName := GetParameterSection (param .Name )
67+ if sectionName == nil {
10468 log .Printf ("Parameter %s has no section name" , param .Name )
10569 continue
10670 }
10771
108- // Get the section name
109- sectionName := * param .SectionName
110-
111- // Add the parameter to the appropriate section
112- groupedParams [ sectionName ] = append ( groupedParams [ sectionName ], param )
72+ groupedParams [ * sectionName ] = append ( groupedParams [ * sectionName ], types. PylintPatternParameterConfiguration {
73+ Name : param .Name ,
74+ Value : param . Value ,
75+ SectionName : sectionName ,
76+ } )
11377 }
11478 }
11579
11680 return groupedParams
11781}
11882
83+ func GeneratePylintRCDefault () string {
84+ var rcContent strings.Builder
85+
86+ writePylintRCHeader (& rcContent )
87+ writeEnabledPatterns (& rcContent , DefaultPatterns )
88+
89+ // Get default parameters for enabled patterns
90+ defaultParams := getDefaultParametersForPatterns (DefaultPatterns )
91+
92+ // Convert default parameters to pattern configurations
93+ var patterns []types.PatternConfiguration
94+ for patternID , params := range defaultParams {
95+ patterns = append (patterns , types.PatternConfiguration {
96+ InternalId : "PyLintPython3_" + patternID ,
97+ Parameters : params ,
98+ })
99+ }
100+
101+ // Group and write parameters
102+ groupedParams := groupParametersByPatterns (patterns )
103+ writeParametersBySection (& rcContent , groupedParams )
104+
105+ return rcContent .String ()
106+ }
107+
108+ // GeneratePylintRC generates a pylintrc file content with the specified patterns enabled
109+ func GeneratePylintRC (config types.ToolConfiguration ) string {
110+ var rcContent strings.Builder
111+
112+ writePylintRCHeader (& rcContent )
113+
114+ // Collect enabled pattern IDs
115+ var enabledPatternsIds []string
116+ if config .IsEnabled {
117+ for _ , pattern := range config .Patterns {
118+ patternID := extractPatternId (pattern .InternalId )
119+ enabledPatternsIds = append (enabledPatternsIds , patternID )
120+ }
121+ }
122+
123+ writeEnabledPatterns (& rcContent , enabledPatternsIds )
124+
125+ // Group and write parameters
126+ groupedParams := groupParametersByPatterns (config .Patterns )
127+ writeParametersBySection (& rcContent , groupedParams )
128+
129+ return rcContent .String ()
130+ }
131+
119132// extractPatternId returns the part of the pattern ID after the underscore
120133// For example: "PyLintPython3_C0301" -> "C0301"
121134func extractPatternId (fullID string ) string {
0 commit comments