|
| 1 | +package pylint |
| 2 | + |
| 3 | +import ( |
| 4 | + "codacy/cli-v2/tools/types" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 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 | +} |
| 22 | + |
| 23 | +// writePylintRCHeader writes the common header sections to the RC content |
| 24 | +func writePylintRCHeader(rcContent *strings.Builder) { |
| 25 | + rcContent.WriteString("[MASTER]\n") |
| 26 | + rcContent.WriteString("ignore=CVS\n") |
| 27 | + rcContent.WriteString("persistent=yes\n") |
| 28 | + rcContent.WriteString("load-plugins=\n\n") |
| 29 | + rcContent.WriteString("[MESSAGES CONTROL]\n") |
| 30 | + rcContent.WriteString("disable=all\n") |
| 31 | +} |
| 32 | + |
| 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, ","))) |
| 37 | + } |
| 38 | + rcContent.WriteString("\n") |
| 39 | +} |
| 40 | + |
| 41 | +// writeParametersBySection writes the parameters grouped by section to the RC content |
| 42 | +func writeParametersBySection(rcContent *strings.Builder, groupedParams map[string][]types.PylintPatternParameterConfiguration) { |
| 43 | + for sectionName, params := range groupedParams { |
| 44 | + rcContent.WriteString(fmt.Sprintf("[%s]\n", sectionName)) |
| 45 | + for _, param := range params { |
| 46 | + rcContent.WriteString(fmt.Sprintf("%s=%s\n", param.Name, param.Value)) |
| 47 | + } |
| 48 | + rcContent.WriteString("\n") |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// groupParametersByPatterns groups parameters from patterns into sections |
| 53 | +func groupParametersByPatterns(patterns []types.PatternConfiguration) map[string][]types.PylintPatternParameterConfiguration { |
| 54 | + groupedParams := make(map[string][]types.PylintPatternParameterConfiguration) |
| 55 | + |
| 56 | + for _, pattern := range patterns { |
| 57 | + patternID := extractPatternId(pattern.InternalId) |
| 58 | + params := pattern.Parameters |
| 59 | + |
| 60 | + // If no parameters, check defaults |
| 61 | + if len(params) == 0 { |
| 62 | + params = PatternDefaultParameters[patternID] |
| 63 | + } |
| 64 | + |
| 65 | + for _, param := range params { |
| 66 | + sectionName := GetParameterSection(param.Name) |
| 67 | + if sectionName == nil { |
| 68 | + log.Printf("Parameter %s has no section name", param.Name) |
| 69 | + continue |
| 70 | + } |
| 71 | + |
| 72 | + groupedParams[*sectionName] = append(groupedParams[*sectionName], types.PylintPatternParameterConfiguration{ |
| 73 | + Name: param.Name, |
| 74 | + Value: param.Value, |
| 75 | + SectionName: sectionName, |
| 76 | + }) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return groupedParams |
| 81 | +} |
| 82 | + |
| 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 | + |
| 132 | +// extractPatternId returns the part of the pattern ID after the underscore |
| 133 | +// For example: "PyLintPython3_C0301" -> "C0301" |
| 134 | +func extractPatternId(fullID string) string { |
| 135 | + parts := strings.Split(fullID, "_") |
| 136 | + if len(parts) > 1 { |
| 137 | + return parts[1] |
| 138 | + } |
| 139 | + return fullID |
| 140 | +} |
0 commit comments